mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-28 03:08:10 -07:00
vec: compression part 2 and done
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
mod error;
|
||||
mod value;
|
||||
mod values;
|
||||
|
||||
pub use error::*;
|
||||
pub use value::*;
|
||||
pub use values::*;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
use std::ops::Range;
|
||||
|
||||
use memmap2::Mmap;
|
||||
use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes};
|
||||
|
||||
use crate::MAX_PAGE_SIZE;
|
||||
|
||||
use super::Result;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Values<T> {
|
||||
Owned(Box<[T]>),
|
||||
Ref(Box<Mmap>),
|
||||
}
|
||||
|
||||
impl<T> Values<T> {
|
||||
const PER_PAGE: usize = MAX_PAGE_SIZE / Self::SIZE_OF_T;
|
||||
const SIZE_OF_T: usize = size_of::<T>();
|
||||
|
||||
pub fn get(&self, index: usize) -> Result<Option<&T>>
|
||||
where
|
||||
T: TryFromBytes + IntoBytes + Immutable + KnownLayout,
|
||||
{
|
||||
let index = Self::index_to_decoded_index(index);
|
||||
|
||||
Ok(match self {
|
||||
Self::Owned(a) => a.get(index),
|
||||
Self::Ref(m) => {
|
||||
let range = Self::index_to_byte_range(index);
|
||||
let source = &m[range];
|
||||
Some(T::try_ref_from_bytes(source)?)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_arr(&self) -> &[T] {
|
||||
match self {
|
||||
Self::Owned(a) => a,
|
||||
Self::Ref(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_mmap(&self) -> &Mmap {
|
||||
match self {
|
||||
Self::Owned(_) => unreachable!(),
|
||||
Self::Ref(m) => m,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn index_to_byte_range(index: usize) -> Range<usize> {
|
||||
let index = Self::index_to_byte_index(index) as usize;
|
||||
index..(index + Self::SIZE_OF_T)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn index_to_byte_index(index: usize) -> u64 {
|
||||
(index * Self::SIZE_OF_T) as u64
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn index_to_decoded_index(index: usize) -> usize {
|
||||
index % Self::PER_PAGE
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<Box<[T]>> for Values<T> {
|
||||
fn from(value: Box<[T]>) -> Self {
|
||||
Self::Owned(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<Mmap> for Values<T> {
|
||||
fn from(value: Mmap) -> Self {
|
||||
Self::Ref(Box::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Values<T> {
|
||||
fn default() -> Self {
|
||||
Self::Owned(vec![].into_boxed_slice())
|
||||
}
|
||||
}
|
||||
+562
-370
File diff suppressed because it is too large
Load Diff
@@ -1,20 +0,0 @@
|
||||
use std::{fs::File, sync::OnceLock};
|
||||
|
||||
use super::CompressedPagesMetadata;
|
||||
|
||||
type CompressedPage<T> = Option<(usize, Box<[T]>)>;
|
||||
|
||||
pub enum Back<T> {
|
||||
Raw {
|
||||
raw_pages: Vec<OnceLock<Box<memmap2::Mmap>>>,
|
||||
raw_page: memmap2::Mmap,
|
||||
file: File,
|
||||
file_position: u64,
|
||||
buf: Vec<u8>,
|
||||
},
|
||||
Compressed {
|
||||
decoded_pages: Option<Vec<OnceLock<Box<[T]>>>>,
|
||||
decoded_page: CompressedPage<T>,
|
||||
pages: CompressedPagesMetadata,
|
||||
},
|
||||
}
|
||||
@@ -1,15 +1,13 @@
|
||||
mod back;
|
||||
mod compressed;
|
||||
mod compressed_page_meta;
|
||||
mod compressed_pages_meta;
|
||||
mod length;
|
||||
mod page;
|
||||
mod pages;
|
||||
mod unsafe_slice;
|
||||
mod version;
|
||||
|
||||
pub use back::*;
|
||||
pub use compressed::*;
|
||||
pub use compressed_page_meta::*;
|
||||
pub use compressed_pages_meta::*;
|
||||
pub use length::*;
|
||||
pub use page::*;
|
||||
pub use pages::*;
|
||||
pub use unsafe_slice::*;
|
||||
pub use version::*;
|
||||
|
||||
@@ -9,7 +9,11 @@ pub trait AnyStorableVec: Send + Sync {
|
||||
fn index_type_to_string(&self) -> &str;
|
||||
fn len(&self) -> usize;
|
||||
fn is_empty(&self) -> bool;
|
||||
fn collect_range_values(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<serde_json::Value>>;
|
||||
fn collect_range_values(
|
||||
&self,
|
||||
from: Option<i64>,
|
||||
to: Option<i64>,
|
||||
) -> Result<Vec<serde_json::Value>>;
|
||||
fn flush(&mut self) -> io::Result<()>;
|
||||
}
|
||||
|
||||
@@ -38,7 +42,11 @@ where
|
||||
self.flush()
|
||||
}
|
||||
|
||||
fn collect_range_values(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<serde_json::Value>> {
|
||||
fn collect_range_values(
|
||||
&self,
|
||||
from: Option<i64>,
|
||||
to: Option<i64>,
|
||||
) -> Result<Vec<serde_json::Value>> {
|
||||
Ok(self
|
||||
.collect_range(from, to)?
|
||||
.into_iter()
|
||||
|
||||
Reference in New Issue
Block a user