2021-10-05 11:18:42 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::mem::size_of;
|
|
|
|
use std::str;
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
use heed::BoxedError;
|
|
|
|
|
2023-11-28 10:11:17 +01:00
|
|
|
use super::SliceTooShortError;
|
|
|
|
|
2021-10-05 11:18:42 +02:00
|
|
|
pub struct StrBEU32Codec;
|
|
|
|
|
|
|
|
impl<'a> heed::BytesDecode<'a> for StrBEU32Codec {
|
|
|
|
type DItem = (&'a str, u32);
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
|
2021-10-05 11:18:42 +02:00
|
|
|
let footer_len = size_of::<u32>();
|
|
|
|
|
|
|
|
if bytes.len() < footer_len {
|
2023-11-28 10:11:17 +01:00
|
|
|
return Err(SliceTooShortError.into());
|
2021-10-05 11:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let (word, bytes) = bytes.split_at(bytes.len() - footer_len);
|
2023-11-22 18:21:19 +01:00
|
|
|
let word = str::from_utf8(word)?;
|
|
|
|
let pos = bytes.try_into().map(u32::from_be_bytes)?;
|
2021-10-05 11:18:42 +02:00
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
Ok((word, pos))
|
2021-10-05 11:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> heed::BytesEncode<'a> for StrBEU32Codec {
|
|
|
|
type EItem = (&'a str, u32);
|
|
|
|
|
2024-07-09 17:25:39 +02:00
|
|
|
fn bytes_encode((word, pos): &Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError> {
|
2021-10-05 11:18:42 +02:00
|
|
|
let pos = pos.to_be_bytes();
|
|
|
|
|
|
|
|
let mut bytes = Vec::with_capacity(word.len() + pos.len());
|
|
|
|
bytes.extend_from_slice(word.as_bytes());
|
|
|
|
bytes.extend_from_slice(&pos[..]);
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
Ok(Cow::Owned(bytes))
|
2021-10-05 11:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-23 09:22:01 +01:00
|
|
|
|
|
|
|
pub struct StrBEU16Codec;
|
|
|
|
|
|
|
|
impl<'a> heed::BytesDecode<'a> for StrBEU16Codec {
|
|
|
|
type DItem = (&'a str, u16);
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
|
2023-03-23 09:22:01 +01:00
|
|
|
let footer_len = size_of::<u16>();
|
|
|
|
|
2023-04-24 09:59:30 +02:00
|
|
|
if bytes.len() < footer_len + 1 {
|
2023-11-28 10:11:17 +01:00
|
|
|
return Err(SliceTooShortError.into());
|
2023-03-23 09:22:01 +01:00
|
|
|
}
|
|
|
|
|
2023-04-24 09:59:30 +02:00
|
|
|
let (word_plus_nul_byte, bytes) = bytes.split_at(bytes.len() - footer_len);
|
2023-11-28 14:26:31 +01:00
|
|
|
// unwrap: we just checked the footer + 1 above.
|
2023-11-22 18:21:19 +01:00
|
|
|
let (_, word) = word_plus_nul_byte.split_last().unwrap();
|
2023-11-23 14:47:56 +01:00
|
|
|
let word = str::from_utf8(word)?;
|
|
|
|
let pos = bytes.try_into().map(u16::from_be_bytes)?;
|
2023-03-23 09:22:01 +01:00
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
Ok((word, pos))
|
2023-03-23 09:22:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> heed::BytesEncode<'a> for StrBEU16Codec {
|
|
|
|
type EItem = (&'a str, u16);
|
|
|
|
|
2024-07-09 17:25:39 +02:00
|
|
|
fn bytes_encode((word, pos): &Self::EItem) -> Result<Cow<'a, [u8]>, BoxedError> {
|
2023-03-23 09:22:01 +01:00
|
|
|
let pos = pos.to_be_bytes();
|
|
|
|
|
2023-04-24 09:59:30 +02:00
|
|
|
let mut bytes = Vec::with_capacity(word.len() + 1 + pos.len());
|
2023-03-23 09:22:01 +01:00
|
|
|
bytes.extend_from_slice(word.as_bytes());
|
2023-04-24 09:59:30 +02:00
|
|
|
bytes.push(0);
|
2023-03-23 09:22:01 +01:00
|
|
|
bytes.extend_from_slice(&pos[..]);
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
Ok(Cow::Owned(bytes))
|
2023-03-23 09:22:01 +01:00
|
|
|
}
|
|
|
|
}
|