mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 10:37:41 +08:00
rewrite roaring codec without byteorder.
This commit is contained in:
parent
1373637da1
commit
984dc7c1ed
@ -1,7 +1,7 @@
|
||||
use std::borrow::Cow;
|
||||
use std::convert::TryInto;
|
||||
use std::mem::size_of;
|
||||
|
||||
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
|
||||
use roaring::RoaringBitmap;
|
||||
|
||||
pub struct BoRoaringBitmapCodec;
|
||||
@ -11,14 +11,12 @@ impl heed::BytesDecode<'_> for BoRoaringBitmapCodec {
|
||||
|
||||
fn bytes_decode(bytes: &[u8]) -> Option<Self::DItem> {
|
||||
let mut bitmap = RoaringBitmap::new();
|
||||
let num_u32 = bytes.len() / size_of::<u32>();
|
||||
for i in 0..num_u32 {
|
||||
let start = i * size_of::<u32>();
|
||||
let end = (i + 1) * size_of::<u32>();
|
||||
let mut bytes = bytes.get(start..end)?;
|
||||
let integer = bytes.read_u32::<NativeEndian>().ok()?;
|
||||
bitmap.insert(integer);
|
||||
|
||||
for chunk in bytes.chunks(size_of::<u32>()) {
|
||||
let bytes = chunk.try_into().ok()?;
|
||||
bitmap.push(u32::from_ne_bytes(bytes));
|
||||
}
|
||||
|
||||
Some(bitmap)
|
||||
}
|
||||
}
|
||||
@ -27,10 +25,12 @@ impl heed::BytesEncode<'_> for BoRoaringBitmapCodec {
|
||||
type EItem = RoaringBitmap;
|
||||
|
||||
fn bytes_encode(item: &Self::EItem) -> Option<Cow<[u8]>> {
|
||||
let mut bytes = Vec::with_capacity(item.len() as usize * 4);
|
||||
for integer in item.iter() {
|
||||
bytes.write_u32::<NativeEndian>(integer).ok()?;
|
||||
}
|
||||
Some(Cow::Owned(bytes))
|
||||
let mut out = Vec::with_capacity(item.len() as usize * size_of::<u32>());
|
||||
|
||||
item.iter()
|
||||
.map(|i| i.to_ne_bytes())
|
||||
.for_each(|bytes| out.extend_from_slice(&bytes));
|
||||
|
||||
Some(Cow::Owned(out))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user