mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-27 07:14:26 +01:00
Fix the facet string levels codecs
This commit is contained in:
parent
8c86348119
commit
5676b204dd
@ -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, ()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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)?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user