Fix the facet string levels codecs

This commit is contained in:
Clément Renault 2021-07-04 18:09:53 +02:00 committed by Kerollmops
parent 8c86348119
commit 5676b204dd
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 48 additions and 2 deletions

View File

@ -16,7 +16,7 @@ where
type DItem = (Option<(&'a str, &'a str)>, C::DItem); type DItem = (Option<(&'a str, &'a str)>, C::DItem);
fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> { fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> {
let (contains_bounds, tail_bytes) = bytes.split_first()?; let (contains_bounds, bytes) = bytes.split_first()?;
if *contains_bounds != 0 { if *contains_bounds != 0 {
let (left_len, bytes) = try_split_at(bytes, 2)?; let (left_len, bytes) = try_split_at(bytes, 2)?;
@ -33,7 +33,7 @@ where
C::bytes_decode(bytes).map(|item| (Some((left, right)), item)) C::bytes_decode(bytes).map(|item| (Some((left, right)), item))
} else { } else {
C::bytes_decode(tail_bytes).map(|item| (None, item)) C::bytes_decode(bytes).map(|item| (None, item))
} }
} }
} }
@ -49,11 +49,21 @@ where
match bounds { match bounds {
Some((left, right)) => { Some((left, right)) => {
bytes.push(u8::max_value());
if left.is_empty() || right.is_empty() {
return None;
}
let left_len: u16 = left.len().try_into().ok()?; let left_len: u16 = left.len().try_into().ok()?;
let right_len: u16 = right.len().try_into().ok()?; let right_len: u16 = right.len().try_into().ok()?;
bytes.extend_from_slice(&left_len.to_be_bytes()); bytes.extend_from_slice(&left_len.to_be_bytes());
bytes.extend_from_slice(&right_len.to_be_bytes()); bytes.extend_from_slice(&right_len.to_be_bytes());
bytes.extend_from_slice(left.as_bytes());
bytes.extend_from_slice(right.as_bytes());
let value_bytes = C::bytes_encode(&value)?; let value_bytes = C::bytes_encode(&value)?;
bytes.extend_from_slice(&value_bytes[..]); bytes.extend_from_slice(&value_bytes[..]);
@ -78,3 +88,35 @@ fn try_split_at(slice: &[u8], mid: usize) -> Option<(&[u8], &[u8])> {
None None
} }
} }
#[cfg(test)]
mod tests {
use heed::types::Unit;
use heed::{BytesDecode, BytesEncode};
use roaring::RoaringBitmap;
use super::*;
use crate::CboRoaringBitmapCodec;
#[test]
fn deserialize_roaring_bitmaps() {
let bounds = Some(("abc", "def"));
let docids: RoaringBitmap = (0..100).chain(3500..4398).collect();
let key = (bounds, docids.clone());
let bytes =
FacetStringZeroBoundsValueCodec::<CboRoaringBitmapCodec>::bytes_encode(&key).unwrap();
let (out_bounds, out_docids) =
FacetStringZeroBoundsValueCodec::<CboRoaringBitmapCodec>::bytes_decode(&bytes).unwrap();
assert_eq!((out_bounds, out_docids), (bounds, docids));
}
#[test]
fn deserialize_unit() {
let bounds = Some(("abc", "def"));
let key = (bounds, ());
let bytes = FacetStringZeroBoundsValueCodec::<Unit>::bytes_encode(&key).unwrap();
let (out_bounds, out_unit) =
FacetStringZeroBoundsValueCodec::<Unit>::bytes_decode(&bytes).unwrap();
assert_eq!((out_bounds, out_unit), (bounds, ()));
}
}

View File

@ -286,6 +286,10 @@ impl<'s, A: AsRef<[u8]>> Store<'s, A> {
value: String, value: String,
id: DocumentId, id: DocumentId,
) -> Result<()> { ) -> Result<()> {
if value.is_empty() {
return Ok(());
}
let sorter = &mut self.field_id_docid_facet_strings_sorter; let sorter = &mut self.field_id_docid_facet_strings_sorter;
Self::write_field_id_docid_facet_string_value(sorter, field_id, id, &value)?; Self::write_field_id_docid_facet_string_value(sorter, field_id, id, &value)?;