Skip to content

Commit

Permalink
maybe progress
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Sep 4, 2024
1 parent a8cdb11 commit 998be02
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
28 changes: 27 additions & 1 deletion aper/src/data_structures/atom_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{AperSync, StoreHandle};
use crate::{AperSync, StoreHandle, StoreIterator};
use serde::{de::DeserializeOwned, Serialize};

pub struct AtomMap<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
Expand Down Expand Up @@ -36,4 +36,30 @@ impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> AtomMap<K
pub fn delete(&mut self, key: &K) {
self.map.delete(bincode::serialize(key).unwrap());
}

pub fn iter(&self) -> AtomMapIter<K, V> {
AtomMapIter {
iter: self.map.iter(),
_phantom: std::marker::PhantomData,
}
}
}

pub struct AtomMapIter<'a, K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
iter: StoreIterator<'a>,
_phantom: std::marker::PhantomData<(K, V)>,
}

impl<'a, K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> Iterator
for AtomMapIter<'a, K, V>
{
type Item = (K, V);

fn next(&mut self) -> Option<Self::Item> {
// TODO: wrong
let n = self.iter.next()?;
let key = bincode::deserialize(&n.0).unwrap();
let value = bincode::deserialize(&n.1).unwrap();
Some((key, value))
}
}
5 changes: 5 additions & 0 deletions aper/src/store/handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
core::Store,
iter::StoreIterator,
prefix_map::{PrefixMap, PrefixMapValue},
};
use crate::{
Expand Down Expand Up @@ -102,6 +103,10 @@ impl StoreHandle {
top_layer.dirty.insert(pfx.clone());
}
}

pub fn iter(&self) -> StoreIterator {
StoreIterator::from_guard(self.prefix.clone(), self.map.inner.layers.lock().unwrap())
}
}

impl Debug for Store {
Expand Down
13 changes: 11 additions & 2 deletions aper/src/store/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{core::StoreLayer, PrefixMap, PrefixMapValue};
use crate::Bytes;
use self_cell::self_cell;
use std::cell::RefCell;
use std::collections::btree_map::Iter as BTreeMapIter;
use std::collections::BinaryHeap;
use std::{marker::PhantomData, sync::MutexGuard};
Expand Down Expand Up @@ -94,16 +95,24 @@ impl<'a> Iterator for StoreIteratorInner<'a> {
}

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

#[covariant]
dependent: StoreIteratorInner,
}
}

impl<'a> Iterator for StoreIterator<'a> {
type Item = (&'a Bytes, &'a Bytes);

fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}

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

Expand Down
1 change: 1 addition & 0 deletions aper/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ mod prefix_map;

pub use core::Store;
pub use handle::StoreHandle;
pub use iter::StoreIterator;
pub use prefix_map::{PrefixMap, PrefixMapValue};

0 comments on commit 998be02

Please sign in to comment.