use crate::core::index::index_reader::Identity;
use crate::core::index::index_reader_context::{IndexReaderContext, IndexReaderContextBase};
use crate::core::index::leaf_reader::LeafReader;
use crate::core::util::error::lucene_error::Result;
use std::fmt;
pub struct LeafReaderContext<LR> {
pub ord: usize,
pub doc_base: usize,
reader: LR,
base: IndexReaderContextBase,
pub(crate) top_parent: TopParentMeta,
}
#[derive(Clone, Default)]
pub struct TopParentMeta {
pub(crate) leaves_num: usize,
pub(crate) max_doc: i32,
pub(crate) id: Identity,
}
impl<LR> LeafReaderContext<LR> {
pub(crate) fn new(
reader: LR,
ord: i32,
doc_base: usize,
leaf_ord: usize,
leaf_doc_base: usize,
parent: TopParentMeta,
) -> Self {
Self {
ord: leaf_ord,
doc_base: leaf_doc_base,
reader,
base: IndexReaderContextBase::new(true, ord, doc_base),
top_parent: parent,
}
}
pub(crate) fn from_top_lr(reader: LR) -> Result<Self>
where
LR: LeafReader,
{
let max_doc = reader.max_doc()?;
let base = IndexReaderContextBase::new(true, 0, 0);
let top_parent = TopParentMeta {
leaves_num: 1,
max_doc,
id: base.id().clone(),
};
Ok(Self {
ord: 0,
doc_base: 0,
reader,
base,
top_parent,
})
}
}
impl<LR> IndexReaderContext for LeafReaderContext<LR>
where
LR: LeafReader,
{
type IndexReader = LR;
fn reader(&self) -> &Self::IndexReader {
&self.reader
}
type LeafReader = LR;
fn leaves(&self) -> Result<&[LeafReaderContext<Self::LeafReader>]> {
Ok(std::slice::from_ref(self))
}
fn base(&self) -> &IndexReaderContextBase {
&self.base
}
}
impl<LR> LeafReaderContext<LR> {
pub fn top_parent(&self) -> &TopParentMeta {
&self.top_parent
}
}
impl<LR> fmt::Display for LeafReaderContext<LR>
where
LR: LeafReader,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"LeafReaderContext({} docBase={} ord={})",
self.reader, self.doc_base, self.ord
)
}
}