2021-07-17 12:50:01 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::{marker, str};
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
use crate::error::SerializationError;
|
|
|
|
use crate::heed_codec::RoaringBitmapCodec;
|
|
|
|
use crate::{try_split_array_at, try_split_at, Result};
|
2021-09-02 15:17:52 +02:00
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
pub type FacetStringLevelZeroValueCodec = StringValueCodec<RoaringBitmapCodec>;
|
2021-07-17 12:50:01 +02:00
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
/// A codec that encodes a string in front of a value.
|
2021-07-17 12:50:01 +02:00
|
|
|
///
|
|
|
|
/// The usecase is for the facet string levels algorithm where we must know the
|
|
|
|
/// original string of a normalized facet value, the original values are stored
|
|
|
|
/// in the value to not break the lexicographical ordering of the LMDB keys.
|
2021-08-16 13:36:30 +02:00
|
|
|
pub struct StringValueCodec<C>(marker::PhantomData<C>);
|
2021-07-17 12:50:01 +02:00
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
impl<'a, C> heed::BytesDecode<'a> for StringValueCodec<C>
|
2021-07-17 12:50:01 +02:00
|
|
|
where
|
|
|
|
C: heed::BytesDecode<'a>,
|
|
|
|
{
|
|
|
|
type DItem = (&'a str, C::DItem);
|
|
|
|
|
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> {
|
2021-08-16 13:36:30 +02:00
|
|
|
let (string, bytes) = decode_prefix_string(bytes)?;
|
2021-07-17 12:50:01 +02:00
|
|
|
C::bytes_decode(bytes).map(|item| (string, item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
impl<'a, C> heed::BytesEncode<'a> for StringValueCodec<C>
|
2021-07-17 12:50:01 +02:00
|
|
|
where
|
|
|
|
C: heed::BytesEncode<'a>,
|
|
|
|
{
|
|
|
|
type EItem = (&'a str, C::EItem);
|
|
|
|
|
|
|
|
fn bytes_encode((string, value): &'a Self::EItem) -> Option<Cow<[u8]>> {
|
|
|
|
let value_bytes = C::bytes_encode(&value)?;
|
|
|
|
|
|
|
|
let mut bytes = Vec::with_capacity(2 + string.len() + value_bytes.len());
|
2021-08-16 13:36:30 +02:00
|
|
|
encode_prefix_string(string, &mut bytes).ok()?;
|
2021-07-17 12:50:01 +02:00
|
|
|
bytes.extend_from_slice(&value_bytes[..]);
|
|
|
|
|
|
|
|
Some(Cow::Owned(bytes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
pub fn decode_prefix_string(value: &[u8]) -> Option<(&str, &[u8])> {
|
|
|
|
let (original_length_bytes, bytes) = try_split_array_at(value)?;
|
|
|
|
let original_length = u16::from_be_bytes(original_length_bytes) as usize;
|
|
|
|
let (string, bytes) = try_split_at(bytes, original_length)?;
|
|
|
|
let string = str::from_utf8(string).ok()?;
|
|
|
|
Some((string, bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn encode_prefix_string(string: &str, buffer: &mut Vec<u8>) -> Result<()> {
|
|
|
|
let string_len: u16 =
|
|
|
|
string.len().try_into().map_err(|_| SerializationError::InvalidNumberSerialization)?;
|
|
|
|
buffer.extend_from_slice(&string_len.to_be_bytes());
|
|
|
|
buffer.extend_from_slice(string.as_bytes());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-17 12:50:01 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use heed::types::Unit;
|
|
|
|
use heed::{BytesDecode, BytesEncode};
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_roaring_bitmaps() {
|
|
|
|
let string = "abc";
|
|
|
|
let docids: RoaringBitmap = (0..100).chain(3500..4398).collect();
|
|
|
|
let key = (string, docids.clone());
|
2021-08-16 13:36:30 +02:00
|
|
|
let bytes = StringValueCodec::<RoaringBitmapCodec>::bytes_encode(&key).unwrap();
|
2021-07-17 12:50:01 +02:00
|
|
|
let (out_string, out_docids) =
|
2021-08-16 13:36:30 +02:00
|
|
|
StringValueCodec::<RoaringBitmapCodec>::bytes_decode(&bytes).unwrap();
|
2021-07-17 12:50:01 +02:00
|
|
|
assert_eq!((out_string, out_docids), (string, docids));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_unit() {
|
|
|
|
let string = "def";
|
|
|
|
let key = (string, ());
|
2021-08-16 13:36:30 +02:00
|
|
|
let bytes = StringValueCodec::<Unit>::bytes_encode(&key).unwrap();
|
|
|
|
let (out_string, out_unit) = StringValueCodec::<Unit>::bytes_decode(&bytes).unwrap();
|
2021-07-17 12:50:01 +02:00
|
|
|
assert_eq!((out_string, out_unit), (string, ()));
|
|
|
|
}
|
|
|
|
}
|