2021-10-05 17:18:42 +08:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::mem::size_of;
|
|
|
|
use std::str;
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
use heed::BoxedError;
|
|
|
|
|
2021-10-05 17:18:42 +08:00
|
|
|
pub struct StrBEU32Codec;
|
|
|
|
|
|
|
|
impl<'a> heed::BytesDecode<'a> for StrBEU32Codec {
|
|
|
|
type DItem = (&'a str, u32);
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
|
2021-10-05 17:18:42 +08:00
|
|
|
let footer_len = size_of::<u32>();
|
|
|
|
|
|
|
|
if bytes.len() < footer_len {
|
2023-11-23 01:21:19 +08:00
|
|
|
panic!() // TODO Do not panic
|
2021-10-05 17:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let (word, bytes) = bytes.split_at(bytes.len() - footer_len);
|
2023-11-23 01:21:19 +08:00
|
|
|
let word = str::from_utf8(word)?;
|
|
|
|
let pos = bytes.try_into().map(u32::from_be_bytes)?;
|
2021-10-05 17:18:42 +08:00
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
Ok((word, pos))
|
2021-10-05 17:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> heed::BytesEncode<'a> for StrBEU32Codec {
|
|
|
|
type EItem = (&'a str, u32);
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_encode((word, pos): &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
|
2021-10-05 17:18:42 +08: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-23 01:21:19 +08:00
|
|
|
Ok(Cow::Owned(bytes))
|
2021-10-05 17:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-23 16:22:01 +08:00
|
|
|
|
|
|
|
pub struct StrBEU16Codec;
|
|
|
|
|
|
|
|
impl<'a> heed::BytesDecode<'a> for StrBEU16Codec {
|
|
|
|
type DItem = (&'a str, u16);
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
|
2023-03-23 16:22:01 +08:00
|
|
|
let footer_len = size_of::<u16>();
|
|
|
|
|
2023-04-24 15:59:30 +08:00
|
|
|
if bytes.len() < footer_len + 1 {
|
2023-11-23 01:21:19 +08:00
|
|
|
panic!() // TODO do not panic
|
2023-03-23 16:22:01 +08:00
|
|
|
}
|
|
|
|
|
2023-04-24 15:59:30 +08:00
|
|
|
let (word_plus_nul_byte, bytes) = bytes.split_at(bytes.len() - footer_len);
|
2023-11-23 01:21:19 +08:00
|
|
|
let (_, word) = word_plus_nul_byte.split_last().unwrap();
|
|
|
|
let word = str::from_utf8(word).ok().unwrap();
|
|
|
|
let pos = bytes.try_into().map(u16::from_be_bytes).ok().unwrap();
|
2023-03-23 16:22:01 +08:00
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
Ok((word, pos))
|
2023-03-23 16:22:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> heed::BytesEncode<'a> for StrBEU16Codec {
|
|
|
|
type EItem = (&'a str, u16);
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
fn bytes_encode((word, pos): &Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
|
2023-03-23 16:22:01 +08:00
|
|
|
let pos = pos.to_be_bytes();
|
|
|
|
|
2023-04-24 15:59:30 +08:00
|
|
|
let mut bytes = Vec::with_capacity(word.len() + 1 + pos.len());
|
2023-03-23 16:22:01 +08:00
|
|
|
bytes.extend_from_slice(word.as_bytes());
|
2023-04-24 15:59:30 +08:00
|
|
|
bytes.push(0);
|
2023-03-23 16:22:01 +08:00
|
|
|
bytes.extend_from_slice(&pos[..]);
|
|
|
|
|
2023-11-23 01:21:19 +08:00
|
|
|
Ok(Cow::Owned(bytes))
|
2023-03-23 16:22:01 +08:00
|
|
|
}
|
|
|
|
}
|