computer: store part 8

This commit is contained in:
nym21
2025-07-03 18:19:36 +02:00
parent 5810276156
commit be4e693a27
29 changed files with 745 additions and 364 deletions

View File

@@ -2,6 +2,15 @@ use brk_core::Version;
use super::{BoxedVecIterator, StoredIndex, StoredType};
pub fn i64_to_usize(i: i64, len: usize) -> usize {
if i >= 0 {
(i as usize).min(len)
} else {
let v = len as i64 + i;
if v < 0 { 0 } else { v as usize }
}
}
pub trait AnyVec: Send + Sync {
fn version(&self) -> Version;
fn name(&self) -> &str;
@@ -26,6 +35,12 @@ pub trait AnyVec: Send + Sync {
self.version()
)
}
#[inline]
fn i64_to_usize(&self, i: i64) -> usize {
let len = self.len();
i64_to_usize(i, len)
}
}
pub trait AnyIterableVec<I, T>: AnyVec {

View File

@@ -1,5 +1,7 @@
use brk_core::{Error, Result};
use crate::i64_to_usize;
use super::{AnyIterableVec, AnyVec, StoredIndex, StoredType};
pub trait CollectableVec<I, T>: AnyVec + AnyIterableVec<I, T>
@@ -25,7 +27,7 @@ where
}
#[inline]
fn i64_to_usize(i: i64, len: usize) -> usize {
fn i64_to_usize_(i: i64, len: usize) -> usize {
if i >= 0 {
(i as usize).min(len)
} else {
@@ -34,27 +36,19 @@ where
}
}
fn range_count(from: Option<i64>, to: Option<i64>, len: usize) -> usize {
let from = from.map(|i| Self::i64_to_usize(i, len));
let to = to.map(|i| Self::i64_to_usize(i, len));
(from.unwrap_or_default()..to.unwrap_or(len)).count()
}
#[doc(hidden)]
fn collect_signed_range(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<T>> {
let len = self.len();
let from = from.map(|i| Self::i64_to_usize(i, len));
let to = to.map(|i| Self::i64_to_usize(i, len));
let from = from.map(|i| self.i64_to_usize(i));
let to = to.map(|i| self.i64_to_usize(i));
self.collect_range(from, to)
}
#[inline]
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
self.collect_signed_range(from, to)?
self.collect_range(from, to)?
.into_iter()
.map(|v| serde_json::to_value(v).map_err(Error::from))
.collect::<Result<Vec<_>>>()
@@ -72,7 +66,18 @@ where
pub trait AnyCollectableVec: AnyVec {
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>>;
fn range_count(&self, from: Option<i64>, to: Option<i64>) -> usize {
let len = self.len();
let from = from.map(|i| i64_to_usize(i, len));
let to = to.map(|i| i64_to_usize(i, len));
(from.unwrap_or_default()..to.unwrap_or(len)).count()
}
fn range_weight(&self, from: Option<i64>, to: Option<i64>) -> usize {
self.range_count(from, to) * self.value_type_to_size_of()
}
}

View File

@@ -528,8 +528,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -397,8 +397,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -1336,8 +1336,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -166,8 +166,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -179,8 +179,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -230,8 +230,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -272,8 +272,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -359,8 +359,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}

View File

@@ -272,8 +272,8 @@ where
{
fn collect_range_serde_json(
&self,
from: Option<i64>,
to: Option<i64>,
from: Option<usize>,
to: Option<usize>,
) -> Result<Vec<serde_json::Value>> {
CollectableVec::collect_range_serde_json(self, from, to)
}