global: big vec refactor + lazy

This commit is contained in:
nym21
2025-05-05 12:47:52 +02:00
parent 320c708e10
commit 9ba3c2b7c5
51 changed files with 1608 additions and 1097 deletions

View File

@@ -4,6 +4,7 @@ use std::{
mem,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
use arc_swap::{ArcSwap, Guard};
@@ -11,8 +12,8 @@ use memmap2::Mmap;
use rayon::prelude::*;
use crate::{
BaseVecIterator, DynamicVec, Error, GenericVec, Result, StoredIndex, StoredType, UnsafeSlice,
Value, Version,
AnyCollectableVec, AnyIterableVec, AnyVec, BaseVecIterator, BoxedVecIterator, CollectableVec,
Error, GenericStoredVec, Result, StoredIndex, StoredType, UnsafeSlice, Value, Version,
};
#[derive(Debug)]
@@ -79,19 +80,16 @@ where
}
}
impl<I, T> DynamicVec for RawVec<I, T>
impl<I, T> GenericStoredVec<I, T> for RawVec<I, T>
where
I: StoredIndex,
T: StoredType,
{
type I = I;
type T = T;
#[inline]
fn read_(&self, index: usize, mmap: &Mmap) -> Result<Option<T>> {
let index = index * Self::SIZE_OF_T;
let slice = &mmap[index..(index + Self::SIZE_OF_T)];
Self::T::try_read_from_bytes(slice)
T::try_read_from_bytes(slice)
.map(|v| Some(v))
.map_err(Error::from)
}
@@ -119,28 +117,6 @@ where
fn path(&self) -> &Path {
self.pathbuf.as_path()
}
}
impl<I, T> GenericVec<I, T> for RawVec<I, T>
where
I: StoredIndex,
T: StoredType,
{
fn collect_range(&self, from: Option<usize>, to: Option<usize>) -> Result<Vec<T>> {
let stored_len = self.stored_len();
let from = from.unwrap_or_default();
let to = to.map_or(stored_len, |i| i.min(stored_len));
if from >= stored_len || from >= to {
return Ok(vec![]);
}
Ok(self
.iter_at_(from)
.take(to - from)
.map(|(_, v)| v.into_inner())
.collect::<Vec<_>>())
}
fn flush(&mut self) -> Result<()> {
let pushed_len = self.pushed_len();
@@ -188,10 +164,41 @@ where
Ok(())
}
// #[inline]
// fn version(&self) -> Version {
// self.version
// }
}
impl<I, T> AnyVec for RawVec<I, T>
where
I: StoredIndex,
T: StoredType,
{
#[inline]
fn version(&self) -> Version {
self.version
}
#[inline]
fn name(&self) -> String {
self.name_()
}
#[inline]
fn len(&self) -> usize {
self.len_()
}
#[inline]
fn modified_time(&self) -> Result<Duration> {
self.modified_time_()
}
#[inline]
fn index_type_to_string(&self) -> &str {
I::to_string()
}
}
impl<I, T> Clone for RawVec<I, T> {
@@ -270,3 +277,30 @@ where
}
}
}
impl<I, T> AnyIterableVec<I, T> for RawVec<I, T>
where
I: StoredIndex,
T: StoredType,
{
fn boxed_iter<'a>(&'a self) -> BoxedVecIterator<'a, I, T>
where
T: 'a,
{
Box::new(self.into_iter())
}
}
impl<I, T> AnyCollectableVec for RawVec<I, T>
where
I: StoredIndex,
T: StoredType,
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}
}