use crate::core::index::composite_reader::CompositeReader;
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::index::leaf_reader_context::{LeafReaderContext, TopParentMeta};
use crate::core::util::TryIntoInt;
use crate::core::util::error::lucene_error::Result;
pub struct CompositeReaderContext<CR>
where
CR: CompositeReader,
{
leaves: Box<[LeafReaderContext<CR::LeafReader>]>,
reader: CR,
base: IndexReaderContextBase,
}
pub(crate) fn create<CR>(reader: CR) -> Result<CompositeReaderContext<CR>>
where
CR: CompositeReader,
{
let base = IndexReaderContextBase::new(true, 0, 0);
let mut builder = Builder::new();
reader.visit_leaves(&mut |leaf_reader| builder.add_leaf(leaf_reader, base.id()))?;
let max_doc = builder.max_doc;
let leaves = builder.leaves;
let mut ctx = CompositeReaderContext {
leaves: leaves.into_boxed_slice(),
reader,
base,
};
let leaves_num = ctx.leaves.len();
ctx.leaves.iter_mut().for_each(|leaf| {
leaf.top_parent.leaves_num = leaves_num;
leaf.top_parent.max_doc = max_doc;
});
Ok(ctx)
}
impl<CR> IndexReaderContext for CompositeReaderContext<CR>
where
CR: CompositeReader,
{
type IndexReader = CR;
fn reader(&self) -> &Self::IndexReader {
&self.reader
}
type LeafReader = CR::LeafReader;
fn leaves(&self) -> Result<&[LeafReaderContext<Self::LeafReader>]> {
Ok(self.leaves.as_ref())
}
fn base(&self) -> &IndexReaderContextBase {
&self.base
}
}
struct Builder<LR> {
pub(crate) leaves: Vec<LeafReaderContext<LR>>,
pub(crate) leaf_doc_base: usize,
pub(crate) max_doc: i32,
}
impl<LR> Builder<LR> {
fn new() -> Self {
Self {
leaves: Vec::new(),
leaf_doc_base: 0,
max_doc: 0,
}
}
}
impl<LR> Builder<LR>
where
LR: LeafReader + Clone,
{
fn add_leaf(&mut self, reader: &LR, parent_id: &Identity) -> Result<()> {
let leaves_size = self.leaves.len();
let atomic = LeafReaderContext::new(
reader.clone(),
0,
self.leaf_doc_base,
leaves_size,
self.leaf_doc_base,
TopParentMeta {
leaves_num: 0,
max_doc: 0,
id: parent_id.clone(),
},
);
self.leaves.push(atomic);
let max_doc = reader.max_doc()?;
self.leaf_doc_base += max_doc.try_convert()?;
self.max_doc += max_doc;
Ok(())
}
}