Make the FacetStringIter work in both, ascending and descending orders

This commit is contained in:
Kerollmops 2021-08-17 11:07:34 +02:00
parent 22ebd2658f
commit 110bf6b778
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4

View File

@ -421,7 +421,7 @@ pub struct FacetStringIter<'t> {
rtxn: &'t heed::RoTxn<'t>, rtxn: &'t heed::RoTxn<'t>,
db: Database<ByteSlice, ByteSlice>, db: Database<ByteSlice, ByteSlice>,
field_id: FieldId, field_id: FieldId,
level_iters: Vec<(RoaringBitmap, EitherStringRange<'t>)>, level_iters: Vec<(RoaringBitmap, Either<EitherStringRange<'t>, EitherStringRevRange<'t>>)>,
must_reduce: bool, must_reduce: bool,
} }
@ -438,7 +438,24 @@ impl<'t> FacetStringIter<'t> {
rtxn, rtxn,
db, db,
field_id, field_id,
level_iters: vec![(documents_ids, highest_iter)], level_iters: vec![(documents_ids, Left(highest_iter))],
must_reduce: true,
})
}
pub fn new_reverse_reducing(
rtxn: &'t heed::RoTxn,
index: &'t Index,
field_id: FieldId,
documents_ids: RoaringBitmap,
) -> heed::Result<FacetStringIter<'t>> {
let db = index.facet_id_string_docids.remap_types::<ByteSlice, ByteSlice>();
let highest_reverse_iter = Self::highest_reverse_iter(rtxn, index, db, field_id)?;
Ok(FacetStringIter {
rtxn,
db,
field_id,
level_iters: vec![(documents_ids, Right(highest_reverse_iter))],
must_reduce: true, must_reduce: true,
}) })
} }
@ -455,7 +472,7 @@ impl<'t> FacetStringIter<'t> {
rtxn, rtxn,
db, db,
field_id, field_id,
level_iters: vec![(documents_ids, highest_iter)], level_iters: vec![(documents_ids, Left(highest_iter))],
must_reduce: false, must_reduce: false,
}) })
} }
@ -536,6 +553,21 @@ impl<'t> Iterator for FacetStringIter<'t> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
'outer: loop { 'outer: loop {
let (documents_ids, last) = self.level_iters.last_mut()?; let (documents_ids, last) = self.level_iters.last_mut()?;
let is_ascending = last.is_left();
// We remap the different iterator types to make
// the algorithm less complex to understand.
let last = match last {
Left(ascending) => match ascending {
Left(last) => Left(Left(last)),
Right(last) => Right(Left(last)),
},
Right(descending) => match descending {
Left(last) => Left(Right(last)),
Right(last) => Right(Right(last)),
},
};
match last { match last {
Left(last) => { Left(last) => {
for result in last { for result in last {
@ -547,24 +579,50 @@ impl<'t> Iterator for FacetStringIter<'t> {
*documents_ids -= &docids; *documents_ids -= &docids;
} }
let result = match string_bounds { let result = if is_ascending {
Some((left, right)) => FacetStringLevelZeroRange::new( match string_bounds {
self.rtxn, Some((left, right)) => {
self.db, FacetStringLevelZeroRevRange::new(
self.field_id, self.rtxn,
Included(left), self.db,
Included(right), self.field_id,
) Included(left),
.map(Right), Included(right),
None => FacetStringGroupRange::new( )
self.rtxn, .map(Right)
self.db, }
self.field_id, None => FacetStringGroupRevRange::new(
NonZeroU8::new(level.get() - 1).unwrap(), self.rtxn,
Included(left), self.db,
Included(right), self.field_id,
) NonZeroU8::new(level.get() - 1).unwrap(),
.map(Left), Included(left),
Included(right),
)
.map(Left),
}
.map(Right)
} else {
match string_bounds {
Some((left, right)) => FacetStringLevelZeroRange::new(
self.rtxn,
self.db,
self.field_id,
Included(left),
Included(right),
)
.map(Right),
None => FacetStringGroupRange::new(
self.rtxn,
self.db,
self.field_id,
NonZeroU8::new(level.get() - 1).unwrap(),
Included(left),
Included(right),
)
.map(Left),
}
.map(Left)
}; };
match result { match result {