use crate::core::util::bkd::heap_point_write::HeapPointValue;
use crate::core::util::bkd::point_reader::PointReader;
use crate::core::util::close::Closeable;
use crate::core::util::error::lucene_error::{LuceneError, Result};
pub struct HeapPointReader {
points: Option<HeapPointValue>,
cur_read: Option<usize>,
end: usize,
bytes_per_doc: usize,
}
impl HeapPointReader {
pub fn new(
get_slice: Option<HeapPointValue>,
start: usize,
end: usize,
bytes_per_doc: usize,
) -> Self {
HeapPointReader {
points: get_slice,
cur_read: start.checked_sub(1),
end,
bytes_per_doc,
}
}
pub fn remove_points(&mut self) -> Option<HeapPointValue> {
self.points.take()
}
}
impl Closeable for HeapPointReader {}
impl PointReader for HeapPointReader {
fn next(&mut self) -> Result<bool> {
self.cur_read = match self.cur_read {
None => Some(0),
Some(i) => i.checked_add(1),
};
Ok(matches!(self.cur_read, Some(i) if i < self.end))
}
type Value<'a> = &'a HeapPointValue;
fn point_value(&mut self) -> Result<Self::Value<'_>> {
let cur_read = match self.cur_read {
Some(i) => i,
None => return Err(LuceneError::illegal_state("Iterator not yet advanced")),
};
match self.points {
None => Err(LuceneError::illegal_state("No points available")),
Some(ref mut points) => {
points.set_offset(self.bytes_per_doc * cur_read);
Ok(points)
},
}
}
}