rewrite roaring codec without byteorder.

This commit is contained in:
Marin Postma 2021-04-27 18:42:08 +02:00
parent 1373637da1
commit 984dc7c1ed
No known key found for this signature in database
GPG Key ID: D5241F0C0C865F30

View File

@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryInto;
use std::mem::size_of; use std::mem::size_of;
use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt};
use roaring::RoaringBitmap; use roaring::RoaringBitmap;
pub struct BoRoaringBitmapCodec; pub struct BoRoaringBitmapCodec;
@ -11,14 +11,12 @@ impl heed::BytesDecode<'_> for BoRoaringBitmapCodec {
fn bytes_decode(bytes: &[u8]) -> Option<Self::DItem> { fn bytes_decode(bytes: &[u8]) -> Option<Self::DItem> {
let mut bitmap = RoaringBitmap::new(); let mut bitmap = RoaringBitmap::new();
let num_u32 = bytes.len() / size_of::<u32>();
for i in 0..num_u32 { for chunk in bytes.chunks(size_of::<u32>()) {
let start = i * size_of::<u32>(); let bytes = chunk.try_into().ok()?;
let end = (i + 1) * size_of::<u32>(); bitmap.push(u32::from_ne_bytes(bytes));
let mut bytes = bytes.get(start..end)?;
let integer = bytes.read_u32::<NativeEndian>().ok()?;
bitmap.insert(integer);
} }
Some(bitmap) Some(bitmap)
} }
} }
@ -27,10 +25,12 @@ impl heed::BytesEncode<'_> for BoRoaringBitmapCodec {
type EItem = RoaringBitmap; type EItem = RoaringBitmap;
fn bytes_encode(item: &Self::EItem) -> Option<Cow<[u8]>> { fn bytes_encode(item: &Self::EItem) -> Option<Cow<[u8]>> {
let mut bytes = Vec::with_capacity(item.len() as usize * 4); let mut out = Vec::with_capacity(item.len() as usize * size_of::<u32>());
for integer in item.iter() {
bytes.write_u32::<NativeEndian>(integer).ok()?; item.iter()
} .map(|i| i.to_ne_bytes())
Some(Cow::Owned(bytes)) .for_each(|bytes| out.extend_from_slice(&bytes));
Some(Cow::Owned(out))
} }
} }