mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-26 23:59:58 -07:00
global: snapshot
This commit is contained in:
@@ -52,6 +52,10 @@ where
|
||||
Self::Raw(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> StoredVecIterator<'_, I, T> {
|
||||
self.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, T> DynamicVec for StoredVec<I, T>
|
||||
@@ -137,16 +141,6 @@ where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
{
|
||||
fn iter_from<F>(&mut self, index: I, f: F) -> Result<()>
|
||||
where
|
||||
F: FnMut((I, T, &mut dyn DynamicVec<I = I, T = T>)) -> Result<()>,
|
||||
{
|
||||
match self {
|
||||
StoredVec::Raw(v) => v.iter_from(index, f),
|
||||
StoredVec::Compressed(v) => v.iter_from(index, f),
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_range(&self, from: Option<usize>, to: Option<usize>) -> Result<Vec<Self::T>> {
|
||||
match self {
|
||||
StoredVec::Raw(v) => v.collect_range(from, to),
|
||||
@@ -250,6 +244,25 @@ where
|
||||
Compressed(CompressedVecIterator<'a, I, T>),
|
||||
}
|
||||
|
||||
impl<'a, I, T> StoredVecIterator<'a, I, T>
|
||||
where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
{
|
||||
pub fn get(&mut self, i: usize) -> Option<(I, Value<'a, T>)> {
|
||||
match self {
|
||||
Self::Compressed(iter) => {
|
||||
iter.set(i);
|
||||
iter.next()
|
||||
}
|
||||
Self::Raw(iter) => {
|
||||
iter.set(i);
|
||||
iter.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I, T> Iterator for StoredVecIterator<'a, I, T>
|
||||
where
|
||||
I: StoredIndex,
|
||||
|
||||
@@ -45,7 +45,7 @@ pub trait DynamicVec: Send + Sync {
|
||||
.map(Value::Owned))
|
||||
}
|
||||
fn get_stored_(&self, index: usize, mmap: &Mmap) -> Result<Option<Self::T>>;
|
||||
fn get_last(&self) -> Result<Option<Value<Self::T>>> {
|
||||
fn last(&self) -> Result<Option<Value<Self::T>>> {
|
||||
let len = self.len();
|
||||
if len == 0 {
|
||||
return Ok(None);
|
||||
|
||||
@@ -98,30 +98,6 @@ where
|
||||
Self::I::to_string()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn iter<F>(&mut self, f: F) -> Result<()>
|
||||
where
|
||||
F: FnMut(
|
||||
(
|
||||
Self::I,
|
||||
Self::T,
|
||||
&mut dyn DynamicVec<I = Self::I, T = Self::T>,
|
||||
),
|
||||
) -> Result<()>,
|
||||
{
|
||||
self.iter_from(Self::I::default(), f)
|
||||
}
|
||||
|
||||
fn iter_from<F>(&mut self, index: Self::I, f: F) -> Result<()>
|
||||
where
|
||||
F: FnMut(
|
||||
(
|
||||
Self::I,
|
||||
Self::T,
|
||||
&mut dyn DynamicVec<I = Self::I, T = Self::T>,
|
||||
),
|
||||
) -> Result<()>;
|
||||
|
||||
fn flush(&mut self) -> Result<()>;
|
||||
|
||||
fn truncate_if_needed(&mut self, index: Self::I) -> Result<()>;
|
||||
|
||||
@@ -201,6 +201,10 @@ where
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> CompressedVecIterator<'_, I, T> {
|
||||
self.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, T> DynamicVec for CompressedVec<I, T>
|
||||
@@ -293,37 +297,6 @@ where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
{
|
||||
fn iter_from<F>(&mut self, index: I, mut f: F) -> Result<()>
|
||||
where
|
||||
F: FnMut((I, T, &mut dyn DynamicVec<I = Self::I, T = Self::T>)) -> Result<()>,
|
||||
{
|
||||
if !self.is_pushed_empty() {
|
||||
return Err(Error::UnsupportedUnflushedState);
|
||||
}
|
||||
|
||||
let start = index.to_usize()?;
|
||||
|
||||
let stored_len = self.stored_len();
|
||||
if start >= stored_len {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mmap = self.mmap().load();
|
||||
let pages_meta = self.pages_meta.load();
|
||||
|
||||
(start..stored_len).try_for_each(|index| {
|
||||
let v = Self::cached_get_stored__(
|
||||
index,
|
||||
&mmap,
|
||||
stored_len,
|
||||
&mut self.decoded_page,
|
||||
&pages_meta,
|
||||
)?
|
||||
.unwrap();
|
||||
f((I::from(index), v, self as &mut dyn DynamicVec<I = I, T = T>))
|
||||
})
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -530,6 +503,12 @@ pub struct CompressedVecIterator<'a, I, T> {
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl<I, T> CompressedVecIterator<'_, I, T> {
|
||||
pub fn set(&mut self, i: usize) {
|
||||
self.index = i
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I, T> Iterator for CompressedVecIterator<'a, I, T>
|
||||
where
|
||||
I: StoredIndex,
|
||||
|
||||
@@ -62,6 +62,10 @@ where
|
||||
phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> RawVecIterator<'_, I, T> {
|
||||
self.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I, T> DynamicVec for RawVec<I, T>
|
||||
@@ -128,29 +132,6 @@ where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
{
|
||||
fn iter_from<F>(&mut self, index: I, mut f: F) -> Result<()>
|
||||
where
|
||||
F: FnMut((I, T, &mut dyn DynamicVec<I = Self::I, T = Self::T>)) -> Result<()>,
|
||||
{
|
||||
if !self.is_pushed_empty() {
|
||||
return Err(Error::UnsupportedUnflushedState);
|
||||
}
|
||||
|
||||
let start = index.to_usize()?;
|
||||
|
||||
let stored_len = self.stored_len();
|
||||
if start >= stored_len {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let guard = self.mmap.load();
|
||||
|
||||
(start..stored_len).try_for_each(|index| {
|
||||
let v = self.get_stored_(index, &guard)?.unwrap();
|
||||
f((I::from(index), v, self as &mut dyn DynamicVec<I = I, T = T>))
|
||||
})
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -247,6 +228,10 @@ pub struct RawVecIterator<'a, I, T> {
|
||||
|
||||
impl<I, T> RawVecIterator<'_, I, T> {
|
||||
const SIZE_OF_T: usize = size_of::<T>();
|
||||
|
||||
pub fn set(&mut self, i: usize) {
|
||||
self.index = i
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I, T> Iterator for RawVecIterator<'a, I, T>
|
||||
|
||||
Reference in New Issue
Block a user