Introduce the highest_iter private method on the FacetStringIter struct

This commit is contained in:
Kerollmops 2021-08-17 10:41:11 +02:00
parent 64df159057
commit 1c604de158
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -299,7 +299,15 @@ impl<'t> FacetStringIter<'t> {
field_id: FieldId, field_id: FieldId,
documents_ids: RoaringBitmap, documents_ids: RoaringBitmap,
) -> heed::Result<FacetStringIter<'t>> { ) -> heed::Result<FacetStringIter<'t>> {
FacetStringIter::new(rtxn, index, field_id, documents_ids, true) let db = index.facet_id_string_docids.remap_types::<ByteSlice, ByteSlice>();
let highest_iter = Self::highest_iter(rtxn, index, db, field_id)?;
Ok(FacetStringIter {
rtxn,
db,
field_id,
level_iters: vec![(documents_ids, highest_iter)],
must_reduce: true,
})
} }
pub fn new_non_reducing( pub fn new_non_reducing(
@ -307,43 +315,15 @@ impl<'t> FacetStringIter<'t> {
index: &'t Index, index: &'t Index,
field_id: FieldId, field_id: FieldId,
documents_ids: RoaringBitmap, documents_ids: RoaringBitmap,
) -> heed::Result<FacetStringIter<'t>> {
FacetStringIter::new(rtxn, index, field_id, documents_ids, false)
}
fn new(
rtxn: &'t heed::RoTxn,
index: &'t Index,
field_id: FieldId,
documents_ids: RoaringBitmap,
must_reduce: bool,
) -> heed::Result<FacetStringIter<'t>> { ) -> heed::Result<FacetStringIter<'t>> {
let db = index.facet_id_string_docids.remap_types::<ByteSlice, ByteSlice>(); let db = index.facet_id_string_docids.remap_types::<ByteSlice, ByteSlice>();
let highest_level = Self::highest_level(rtxn, db, field_id)?.unwrap_or(0); let highest_iter = Self::highest_iter(rtxn, index, db, field_id)?;
let highest_iter = match NonZeroU8::new(highest_level) {
Some(highest_level) => Left(FacetStringGroupRange::new(
rtxn,
index.facet_id_string_docids,
field_id,
highest_level,
Unbounded,
Unbounded,
)?),
None => Right(FacetStringLevelZeroRange::new(
rtxn,
index.facet_id_string_docids,
field_id,
Unbounded,
Unbounded,
)?),
};
Ok(FacetStringIter { Ok(FacetStringIter {
rtxn, rtxn,
db, db,
field_id, field_id,
level_iters: vec![(documents_ids, highest_iter)], level_iters: vec![(documents_ids, highest_iter)],
must_reduce, must_reduce: false,
}) })
} }
@ -359,6 +339,34 @@ impl<'t> FacetStringIter<'t> {
.transpose()? .transpose()?
.map(|(key_bytes, _)| key_bytes[2])) // the level is the third bit .map(|(key_bytes, _)| key_bytes[2])) // the level is the third bit
} }
fn highest_iter<X, Y>(
rtxn: &'t heed::RoTxn,
index: &'t Index,
db: Database<X, Y>,
field_id: FieldId,
) -> heed::Result<Either<FacetStringGroupRange<'t>, FacetStringLevelZeroRange<'t>>> {
let highest_level = Self::highest_level(rtxn, db, field_id)?.unwrap_or(0);
match NonZeroU8::new(highest_level) {
Some(highest_level) => FacetStringGroupRange::new(
rtxn,
index.facet_id_string_docids,
field_id,
highest_level,
Unbounded,
Unbounded,
)
.map(Left),
None => FacetStringLevelZeroRange::new(
rtxn,
index.facet_id_string_docids,
field_id,
Unbounded,
Unbounded,
)
.map(Right),
}
}
} }
impl<'t> Iterator for FacetStringIter<'t> { impl<'t> Iterator for FacetStringIter<'t> {