server: mcp + global: refactor

This commit is contained in:
nym21
2025-06-21 12:43:14 +02:00
parent c9e0f9d985
commit c3ae3cb768
92 changed files with 13601 additions and 12554 deletions
+2 -2
View File
@@ -6,13 +6,13 @@ use super::{BoxedVecIterator, StoredIndex, StoredType};
pub trait AnyVec: Send + Sync {
fn version(&self) -> Version;
fn name(&self) -> String;
fn name(&self) -> &str;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn modified_time(&self) -> Result<Duration>;
fn index_type_to_string(&self) -> String;
fn index_type_to_string(&self) -> &'static str;
fn value_type_to_size_of(&self) -> usize;
}
+2 -12
View File
@@ -66,7 +66,7 @@ where
self.mut_pushed().push(value)
}
fn path(&self) -> &Path;
fn path(&self) -> PathBuf;
// ---
@@ -141,7 +141,7 @@ where
#[inline]
fn path_vec(&self) -> PathBuf {
Self::path_vec_(self.path())
Self::path_vec_(&self.path())
}
#[inline]
fn path_vec_(path: &Path) -> PathBuf {
@@ -158,16 +158,6 @@ where
path.join("compressed")
}
#[inline]
fn name_(&self) -> String {
self.path()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_owned()
}
fn modified_time_(&self) -> Result<Duration> {
Ok(self
.path_vec()
+6 -24
View File
@@ -1,10 +1,6 @@
use std::{
fmt::Debug,
ops::Add,
path::{Path, PathBuf},
};
use std::{fmt::Debug, ops::Add};
use brk_core::{Error, Result};
use brk_core::{Error, Printable, Result};
pub trait StoredIndex
where
@@ -20,18 +16,12 @@ where
+ From<usize>
+ Add<usize, Output = Self>
+ Send
+ Sync,
+ Sync
+ Printable,
{
fn unwrap_to_usize(self) -> usize;
fn to_usize(self) -> Result<usize>;
fn to_string() -> String;
fn decremented(self) -> Option<Self>;
fn to_folder_name(value_name: &str) -> String {
format!("{}_to_{value_name}", Self::to_string().to_lowercase())
}
fn path(path: &Path, value_name: &str) -> PathBuf {
path.join(Self::to_folder_name(value_name))
}
}
impl<I> StoredIndex for I
@@ -48,7 +38,8 @@ where
+ From<usize>
+ Add<usize, Output = Self>
+ Send
+ Sync,
+ Sync
+ Printable,
{
#[inline]
fn unwrap_to_usize(self) -> usize {
@@ -60,15 +51,6 @@ where
self.try_into().map_err(|_| Error::FailedKeyTryIntoUsize)
}
#[inline]
fn to_string() -> String {
std::any::type_name::<I>()
.split("::")
.last()
.unwrap()
.to_lowercase()
}
#[inline]
fn decremented(self) -> Option<Self> {
self.unwrap_to_usize().checked_sub(1).map(Self::from)
+5 -5
View File
@@ -1,6 +1,6 @@
use std::{iter::Skip, path::Path};
use std::iter::Skip;
use brk_core::Value;
use brk_core::{Printable, Value};
use super::{StoredIndex, StoredType};
@@ -20,7 +20,7 @@ pub trait BaseVecIterator: Iterator {
fn len(&self) -> usize;
fn path(&self) -> &Path;
fn name(&self) -> &str;
fn is_empty(&self) -> bool {
self.len() == 0
@@ -62,7 +62,7 @@ pub trait VecIterator<'a>: BaseVecIterator<Item = (Self::I, Value<'a, Self::T>)>
fn unwrap_get_inner_(&mut self, i: usize) -> Self::T {
self.get_(i)
.unwrap_or_else(|| {
dbg!(self.path(), i, self.len());
dbg!(self.name(), i, self.len());
panic!("unwrap_get_inner_")
})
.into_inner()
@@ -86,7 +86,7 @@ pub trait VecIterator<'a>: BaseVecIterator<Item = (Self::I, Value<'a, Self::T>)>
self.next().map(|(i, v)| (i, Value::Owned(v.into_inner())))
}
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
Self::I::to_string()
}
}
+30 -23
View File
@@ -1,7 +1,7 @@
use std::{
fs::{self, File},
mem,
path::Path,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
@@ -41,39 +41,46 @@ where
pub const CACHE_LENGTH: usize = MAX_CACHE_SIZE / Self::PAGE_SIZE;
/// Same as import but will reset the folder under certain errors, so be careful !
pub fn forced_import(path: &Path, mut version: Version) -> Result<Self> {
pub fn forced_import(path: &Path, name: &str, mut version: Version) -> Result<Self> {
version = version + VERSION;
let res = Self::import(path, version);
let res = Self::import(path, name, version);
match res {
Err(Error::WrongEndian)
| Err(Error::DifferentVersion { .. })
| Err(Error::DifferentCompressionMode) => {
fs::remove_dir_all(path)?;
Self::import(path, version)
Self::import(path, name, version)
}
_ => res,
}
}
pub fn import(path: &Path, version: Version) -> Result<Self> {
fs::create_dir_all(path)?;
pub fn import(path: &Path, name: &str, version: Version) -> Result<Self> {
let pages_meta = {
let path = path.join(name).join(I::to_string());
let vec_exists = fs::exists(Self::path_vec_(path)).is_ok_and(|b| b);
let compressed_path = Self::path_compressed_(path);
let compressed_exists = fs::exists(&compressed_path).is_ok_and(|b| b);
let vec_exists = fs::exists(Self::path_vec_(&path)).is_ok_and(|b| b);
let compressed_path = Self::path_compressed_(&path);
let compressed_exists = fs::exists(&compressed_path).is_ok_and(|b| b);
if vec_exists && !compressed_exists {
return Err(Error::DifferentCompressionMode);
}
if vec_exists && !compressed_exists {
return Err(Error::DifferentCompressionMode);
}
if !vec_exists && !compressed_exists {
File::create(&compressed_path)?;
}
if !vec_exists && !compressed_exists {
fs::create_dir_all(&path)?;
File::create(&compressed_path)?;
}
Arc::new(ArcSwap::new(Arc::new(CompressedPagesMetadata::read(
&path,
)?)))
};
Ok(Self {
inner: RawVec::import(path, version)?,
pages_meta: Arc::new(ArcSwap::new(Arc::new(CompressedPagesMetadata::read(path)?))),
inner: RawVec::import(path, name, version)?,
pages_meta,
})
}
@@ -199,7 +206,7 @@ where
}
#[inline]
fn path(&self) -> &Path {
fn path(&self) -> PathBuf {
self.inner.path()
}
@@ -351,8 +358,8 @@ where
}
#[inline]
fn name(&self) -> String {
self.name_()
fn name(&self) -> &str {
self.inner.name()
}
#[inline]
@@ -366,7 +373,7 @@ where
}
#[inline]
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
@@ -421,8 +428,8 @@ where
}
#[inline]
fn path(&self) -> &Path {
self.vec.path()
fn name(&self) -> &str {
self.vec.name()
}
}
+19 -21
View File
@@ -84,7 +84,7 @@ where
pub fn forced_import_or_init_from_1(
mode: Computation,
path: &Path,
value_name: &str,
name: &str,
version: Version,
format: Format,
source: BoxedAnyIterableVec<S1I, S1T>,
@@ -92,12 +92,12 @@ where
) -> Result<Self> {
Ok(match mode {
Computation::Eager => Self::Eager {
vec: EagerVec::forced_import(path, value_name, version, format)?,
vec: EagerVec::forced_import(path, name, version, format)?,
deps: Dependencies::From1(source, compute),
},
Computation::Lazy => {
let _ = fs::remove_dir_all(I::path(path, value_name));
Self::LazyFrom1(LazyVecFrom1::init(value_name, version, source, compute))
let _ = fs::remove_dir_all(path.join(name).join(I::to_string()));
Self::LazyFrom1(LazyVecFrom1::init(name, version, source, compute))
}
})
}
@@ -106,7 +106,7 @@ where
pub fn forced_import_or_init_from_2(
mode: Computation,
path: &Path,
value_name: &str,
name: &str,
version: Version,
format: Format,
source1: BoxedAnyIterableVec<S1I, S1T>,
@@ -115,14 +115,12 @@ where
) -> Result<Self> {
Ok(match mode {
Computation::Eager => Self::Eager {
vec: EagerVec::forced_import(path, value_name, version, format)?,
vec: EagerVec::forced_import(path, name, version, format)?,
deps: Dependencies::From2((source1, source2), compute),
},
Computation::Lazy => {
let _ = fs::remove_dir_all(I::path(path, value_name));
Self::LazyFrom2(LazyVecFrom2::init(
value_name, version, source1, source2, compute,
))
let _ = fs::remove_dir_all(path.join(name).join(I::to_string()));
Self::LazyFrom2(LazyVecFrom2::init(name, version, source1, source2, compute))
}
})
}
@@ -131,7 +129,7 @@ where
pub fn forced_import_or_init_from_3(
mode: Computation,
path: &Path,
value_name: &str,
name: &str,
version: Version,
format: Format,
source1: BoxedAnyIterableVec<S1I, S1T>,
@@ -141,13 +139,13 @@ where
) -> Result<Self> {
Ok(match mode {
Computation::Eager => Self::Eager {
vec: EagerVec::forced_import(path, value_name, version, format)?,
vec: EagerVec::forced_import(path, name, version, format)?,
deps: Dependencies::From3((source1, source2, source3), compute),
},
Computation::Lazy => {
let _ = fs::remove_dir_all(I::path(path, value_name));
let _ = fs::remove_dir_all(path.join(name).join(I::to_string()));
Self::LazyFrom3(LazyVecFrom3::init(
value_name, version, source1, source2, source3, compute,
name, version, source1, source2, source3, compute,
))
}
})
@@ -225,7 +223,7 @@ where
}
}
fn name(&self) -> String {
fn name(&self) -> &str {
match self {
ComputedVec::Eager { vec, .. } => vec.name(),
ComputedVec::LazyFrom1(v) => v.name(),
@@ -234,7 +232,7 @@ where
}
}
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
@@ -324,12 +322,12 @@ where
}
#[inline]
fn path(&self) -> &Path {
fn name(&self) -> &str {
match self {
Self::Eager(i) => i.path(),
Self::LazyFrom1(i) => i.path(),
Self::LazyFrom2(i) => i.path(),
Self::LazyFrom3(i) => i.path(),
Self::Eager(i) => i.name(),
Self::LazyFrom1(i) => i.name(),
Self::LazyFrom2(i) => i.name(),
Self::LazyFrom3(i) => i.name(),
}
}
}
+8 -4
View File
@@ -106,7 +106,7 @@ where
Ok(())
}
pub fn path(&self) -> &Path {
pub fn path(&self) -> PathBuf {
self.inner.path()
}
@@ -136,7 +136,11 @@ where
self.computed_version.store(Arc::new(Some(version)));
if self.is_empty() {
info!("Computing {}...", self.name())
info!(
"Computing {}_to_{}...",
self.index_type_to_string(),
self.name()
)
}
Ok(())
@@ -1306,7 +1310,7 @@ where
}
#[inline]
fn name(&self) -> String {
fn name(&self) -> &str {
self.inner.name()
}
@@ -1321,7 +1325,7 @@ where
}
#[inline]
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
+4 -4
View File
@@ -35,7 +35,7 @@ where
let inner = StoredVec::forced_import(path, value_name, version, format)?;
Ok(Self {
height: Height::try_from(Self::path_height_(inner.path()).as_path()).ok(),
height: Height::try_from(Self::path_height_(&inner.path()).as_path()).ok(),
inner,
})
}
@@ -91,7 +91,7 @@ where
Height::try_from(self.path_height().as_path())
}
fn path_height(&self) -> PathBuf {
Self::path_height_(self.inner.path())
Self::path_height_(&self.inner.path())
}
fn path_height_(path: &Path) -> PathBuf {
path.join("height")
@@ -109,7 +109,7 @@ where
}
#[inline]
fn name(&self) -> String {
fn name(&self) -> &str {
self.inner.name()
}
@@ -124,7 +124,7 @@ where
}
#[inline]
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
+8 -8
View File
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, path::Path};
use std::marker::PhantomData;
use brk_core::{Result, Value, Version};
@@ -27,7 +27,7 @@ where
S1T: StoredType,
{
pub fn init(
value_name: &str,
name: &str,
version: Version,
source: BoxedAnyIterableVec<S1I, S1T>,
compute: ComputeFrom1<I, T, S1I, S1T>,
@@ -37,7 +37,7 @@ where
}
Self {
name: I::to_folder_name(value_name),
name: name.to_string(),
version,
source,
compute,
@@ -96,8 +96,8 @@ where
}
#[inline]
fn path(&self) -> &Path {
self.source.path()
fn name(&self) -> &str {
self.source.name()
}
}
@@ -131,11 +131,11 @@ where
self.version()
}
fn name(&self) -> String {
self.name.clone()
fn name(&self) -> &str {
self.name.as_str()
}
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
+8 -8
View File
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, path::Path};
use std::marker::PhantomData;
use brk_core::{Result, Value, Version};
@@ -33,7 +33,7 @@ where
S2T: StoredType,
{
pub fn init(
value_name: &str,
name: &str,
version: Version,
source1: BoxedAnyIterableVec<S1I, S1T>,
source2: BoxedAnyIterableVec<S2I, S2T>,
@@ -52,7 +52,7 @@ where
}
Self {
name: I::to_folder_name(value_name),
name: name.to_string(),
version,
source1,
source2,
@@ -126,8 +126,8 @@ where
}
#[inline]
fn path(&self) -> &Path {
self.source1.path()
fn name(&self) -> &str {
self.source1.name()
}
}
@@ -166,11 +166,11 @@ where
self.version()
}
fn name(&self) -> String {
self.name.clone()
fn name(&self) -> &str {
self.name.as_str()
}
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
+8 -8
View File
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, path::Path};
use std::marker::PhantomData;
use brk_core::{Result, Value, Version};
@@ -37,7 +37,7 @@ where
S3T: StoredType,
{
pub fn init(
value_name: &str,
name: &str,
version: Version,
source1: BoxedAnyIterableVec<S1I, S1T>,
source2: BoxedAnyIterableVec<S2I, S2T>,
@@ -58,7 +58,7 @@ where
}
Self {
name: I::to_folder_name(value_name),
name: name.to_string(),
version,
source1,
source2,
@@ -149,8 +149,8 @@ where
}
#[inline]
fn path(&self) -> &Path {
self.source1.path()
fn name(&self) -> &str {
self.source1.name()
}
}
@@ -195,11 +195,11 @@ where
self.version()
}
fn name(&self) -> String {
self.name.clone()
fn name(&self) -> &str {
self.name.as_str()
}
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
+30 -21
View File
@@ -20,7 +20,8 @@ use crate::{
#[derive(Debug)]
pub struct RawVec<I, T> {
version: Version,
pathbuf: PathBuf,
parent: PathBuf,
name: String,
// Consider Arc<ArcSwap<Option<Mmap>>> for dataraces when reorg ?
mmap: Arc<ArcSwap<Mmap>>,
pushed: Vec<T>,
@@ -33,33 +34,40 @@ where
T: StoredType,
{
/// Same as import but will reset the folder under certain errors, so be careful !
pub fn forced_import(path: &Path, version: Version) -> Result<Self> {
let res = Self::import(path, version);
pub fn forced_import(path: &Path, name: &str, version: Version) -> Result<Self> {
let res = Self::import(path, name, version);
match res {
Err(Error::WrongEndian) | Err(Error::DifferentVersion { .. }) => {
fs::remove_dir_all(path)?;
Self::import(path, version)
Self::import(path, name, version)
}
_ => res,
}
}
pub fn import(path: &Path, version: Version) -> Result<Self> {
fs::create_dir_all(path)?;
pub fn import(path: &Path, name: &str, version: Version) -> Result<Self> {
let (version, mmap) = {
let path = path.join(name).join(I::to_string());
let version_path = Self::path_version_(path);
fs::create_dir_all(&path)?;
if !version.validate(version_path.as_ref())? {
version.write(version_path.as_ref())?;
}
let version_path = Self::path_version_(&path);
let file = Self::open_file_(Self::path_vec_(path).as_path())?;
let mmap = Arc::new(ArcSwap::new(Self::new_mmap(file)?));
if !version.validate(version_path.as_ref())? {
version.write(version_path.as_ref())?;
}
let file = Self::open_file_(Self::path_vec_(&path).as_path())?;
let mmap = Arc::new(ArcSwap::new(Self::new_mmap(file)?));
(version, mmap)
};
Ok(Self {
mmap,
version,
pathbuf: path.to_owned(),
name: name.to_string(),
parent: path.to_owned(),
pushed: vec![],
phantom: PhantomData,
})
@@ -121,8 +129,8 @@ where
}
#[inline]
fn path(&self) -> &Path {
self.pathbuf.as_path()
fn path(&self) -> PathBuf {
self.parent.join(self.name()).join(I::to_string())
}
fn flush(&mut self) -> Result<()> {
@@ -183,8 +191,8 @@ where
}
#[inline]
fn name(&self) -> String {
self.name_()
fn name(&self) -> &str {
self.name.as_str()
}
#[inline]
@@ -198,7 +206,7 @@ where
}
#[inline]
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
@@ -212,7 +220,8 @@ impl<I, T> Clone for RawVec<I, T> {
fn clone(&self) -> Self {
Self {
version: self.version,
pathbuf: self.pathbuf.clone(),
parent: self.parent.clone(),
name: self.name.clone(),
mmap: self.mmap.clone(),
pushed: vec![],
phantom: PhantomData,
@@ -243,8 +252,8 @@ where
}
#[inline]
fn path(&self) -> &Path {
self.vec.path()
fn name(&self) -> &str {
self.vec.name()
}
}
+15 -12
View File
@@ -1,4 +1,7 @@
use std::{path::Path, time::Duration};
use std::{
path::{Path, PathBuf},
time::Duration,
};
use arc_swap::ArcSwap;
use brk_core::{Result, Value, Version};
@@ -24,23 +27,23 @@ where
{
pub fn forced_import(
path: &Path,
value_name: &str,
name: &str,
version: Version,
format: Format,
) -> Result<Self> {
let path = I::path(path, value_name);
// let path = I::path(path, value_name);
if version == Version::ZERO {
dbg!(path, value_name);
dbg!(path, name);
panic!("Version must be at least 1, can't verify endianess otherwise");
}
if format.is_compressed() {
Ok(Self::Compressed(CompressedVec::forced_import(
&path, version,
path, name, version,
)?))
} else {
Ok(Self::Raw(RawVec::forced_import(&path, version)?))
Ok(Self::Raw(RawVec::forced_import(path, name, version)?))
}
}
}
@@ -97,7 +100,7 @@ where
}
#[inline]
fn path(&self) -> &Path {
fn path(&self) -> PathBuf {
match self {
StoredVec::Raw(v) => v.path(),
StoredVec::Compressed(v) => v.path(),
@@ -133,7 +136,7 @@ where
}
#[inline]
fn index_type_to_string(&self) -> String {
fn index_type_to_string(&self) -> &'static str {
I::to_string()
}
@@ -150,7 +153,7 @@ where
}
}
fn name(&self) -> String {
fn name(&self) -> &str {
match self {
StoredVec::Raw(v) => v.name(),
StoredVec::Compressed(v) => v.name(),
@@ -204,10 +207,10 @@ where
}
#[inline]
fn path(&self) -> &Path {
fn name(&self) -> &str {
match self {
Self::Compressed(i) => i.path(),
Self::Raw(i) => i.path(),
Self::Compressed(i) => i.name(),
Self::Raw(i) => i.name(),
}
}
}