Skip to content

Commit

Permalink
self-referential iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Sep 4, 2024
1 parent ecf01fa commit e879162
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ serde = { version = "1.0.204", features = ["derive"] }
aper_derive = {path = "./aper-derive", version="0.5.0"}
chrono = { version = "0.4.38", features = ["serde"] }
tracing = "0.1.40"
self_cell = "1.0.4"
40 changes: 40 additions & 0 deletions aper/src/store/iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::{core::StoreLayer, PrefixMap, PrefixMapValue};
use crate::Bytes;
use self_cell::self_cell;
use std::{marker::PhantomData, sync::MutexGuard};

struct StoreIteratorInner<'a> {
iters: Vec<std::collections::btree_map::Iter<'a, Bytes, PrefixMapValue>>,
}

self_cell! {
struct StoreIterator<'a> {
owner: MutexGuard<'a, Vec<StoreLayer>>,

#[covariant]
dependent: StoreIteratorInner,
}
}

impl<'a> StoreIterator<'a> {
fn from_guard(prefix: Vec<Bytes>, guard: MutexGuard<'a, Vec<StoreLayer>>) -> Self {
StoreIterator::new(guard, |guard| {
let mut iters = Vec::new();

for layer in guard.iter() {
match layer.layer.get(&prefix) {
None => continue,
Some(PrefixMap::DeletedPrefixMap) => {
iters.clear();
continue;
}
Some(PrefixMap::Children(map)) => {
iters.push(map.iter());
}
}
}

StoreIteratorInner { iters }
})
}
}
1 change: 1 addition & 0 deletions aper/src/store/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod core;
mod handle;
mod iter;
mod prefix_map;

pub use core::Store;
Expand Down

0 comments on commit e879162

Please sign in to comment.