Skip to content

Commit

Permalink
Merge pull request #44 from jamsocket/paulgb/iterator
Browse files Browse the repository at this point in the history
iterator progress
  • Loading branch information
paulgb committed Sep 4, 2024
2 parents e879162 + 8ad83c4 commit a8ffb3a
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 127 deletions.
4 changes: 4 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 @@ -17,3 +17,4 @@ 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"
bytes = { version = "1.7.1", features = ["serde"] }
16 changes: 10 additions & 6 deletions aper/aper-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl MacroState {
let field = syn::Ident::new(field, proc_macro2::Span::call_site());
let name = Literal::byte_string(field.to_string().as_bytes());
quote! {
#field: aper::AperSync::attach(store.child(#name))
#field: aper::AperSync::attach(store.child(
aper::Bytes::from_static(#name)
))
}
});
quote! {
Expand All @@ -68,7 +70,9 @@ impl MacroState {
let fields = (0..*fields).map(|i| {
let i = Literal::byte_string(i.to_be_bytes().as_slice());
quote! {
aper::AperSync::attach(store.child(#i))
aper::AperSync::attach(store.child(
aper::Bytes::from_static(#i)
))
}
});
quote! {
Expand Down Expand Up @@ -130,8 +134,8 @@ mod tests {
impl aper::AperSync for MyStruct {
fn attach(mut store: aper::StoreHandle) -> Self {
MyStruct {
field1: aper::AperSync::attach(store.child(b"field1")),
field2: aper::AperSync::attach(store.child(b"field2"))
field1: aper::AperSync::attach(store.child(aper::Bytes::from_static(b"field1"))),
field2: aper::AperSync::attach(store.child(aper::Bytes::from_static(b"field2")))
}
}
}
Expand All @@ -153,8 +157,8 @@ mod tests {
impl aper::AperSync for MyStruct {
fn attach(mut store: aper::StoreHandle) -> Self {
MyStruct(
aper::AperSync::attach(store.child(b"\0\0\0\0\0\0\0\0")),
aper::AperSync::attach(store.child(b"\0\0\0\0\0\0\0\x01"))
aper::AperSync::attach(store.child(aper::Bytes::from_static(b"\0\0\0\0\0\0\0\0"))),
aper::AperSync::attach(store.child(aper::Bytes::from_static(b"\0\0\0\0\0\0\0\x01")))
)
}
}
Expand Down
7 changes: 2 additions & 5 deletions aper/src/aper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ use crate::{
Mutation,
};
use serde::{Deserialize, Serialize};
use std::{
collections::{HashSet, VecDeque},
fmt::Debug,
};
use std::{collections::VecDeque, fmt::Debug};

pub trait AperSync {
fn attach(map: StoreHandle) -> Self;

fn listen<F: Fn() -> bool + 'static + Send + Sync>(&self, listener: F) {
fn listen<F: Fn() -> bool + 'static + Send + Sync>(&self, _listener: F) {
// Default implementation does nothing.
}
}
Expand Down
8 changes: 2 additions & 6 deletions aper/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::{
borrow::Borrow,
cell::RefCell,
sync::{
atomic::{AtomicU32, AtomicU64},
Arc, Mutex,
},
sync::{atomic::AtomicU32, Arc, Mutex},
};

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -226,7 +222,7 @@ impl<A: Aper> ServerHandle<A> {
}
}
}
MessageToServer::RequestState { latest_version } => {
MessageToServer::RequestState { .. } => {
let server = self.server.lock().unwrap();
let c = server.borrow();
let mutations = c.state_snapshot();
Expand Down
10 changes: 7 additions & 3 deletions aper/src/data_structures/atom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{AperSync, Store, StoreHandle};
use crate::{AperSync, StoreHandle};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};

pub struct Atom<T: Serialize + DeserializeOwned + Default> {
Expand All @@ -22,12 +23,15 @@ impl<T: Serialize + DeserializeOwned + Default> AperSync for Atom<T> {
impl<T: Serialize + DeserializeOwned + Default> Atom<T> {
pub fn get(&self) -> T {
self.map
.get(&vec![])
.get(&Bytes::new())
.map(|bytes| bincode::deserialize(&bytes).expect("Couldn't deserialize"))
.unwrap_or_default()
}

pub fn set(&mut self, value: T) {
self.map.set(vec![], bincode::serialize(&value).unwrap());
self.map.set(
Bytes::new(),
Bytes::from(bincode::serialize(&value).unwrap()),
);
}
}
60 changes: 55 additions & 5 deletions aper/src/data_structures/atom_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{AperSync, StoreHandle};
use crate::{AperSync, StoreHandle, StoreIterator};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};

pub struct AtomMap<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
Expand All @@ -22,18 +23,67 @@ impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> AperSync
impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> AtomMap<K, V> {
pub fn get(&self, key: &K) -> Option<V> {
self.map
.get(&bincode::serialize(key).unwrap())
.get(&Bytes::from(bincode::serialize(key).unwrap()))
.map(|bytes| bincode::deserialize(&bytes).unwrap())
}

pub fn set(&mut self, key: &K, value: &V) {
self.map.set(
bincode::serialize(key).unwrap(),
bincode::serialize(value).unwrap(),
Bytes::from(bincode::serialize(key).unwrap()),
Bytes::from(bincode::serialize(value).unwrap()),
);
}

pub fn delete(&mut self, key: &K) {
self.map.delete(bincode::serialize(key).unwrap());
self.map
.delete(Bytes::from(bincode::serialize(key).unwrap()));
}

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

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

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

fn next(&mut self) -> Option<Self::Item> {
let n = self.iter.next()?;
let key = bincode::deserialize(&n.0).unwrap();
let value = bincode::deserialize(&n.1).unwrap();
Some((key, value))
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn atom_map_iter() {
let store = crate::Store::default();
let mut map = AtomMap::<String, String>::attach(store.handle());

map.set(&"h-insert".to_string(), &"b".to_string());
map.set(&"a-insert".to_string(), &"a".to_string());
map.set(&"z-insert".to_string(), &"c".to_string());
map.set(&"f-insert".to_string(), &"d".to_string());

let mut iter = map.iter();

assert_eq!(iter.next(), Some(("a-insert".to_string(), "a".to_string())));
assert_eq!(iter.next(), Some(("f-insert".to_string(), "d".to_string())));
assert_eq!(iter.next(), Some(("h-insert".to_string(), "b".to_string())));
assert_eq!(iter.next(), Some(("z-insert".to_string(), "c".to_string())));
}
}
10 changes: 6 additions & 4 deletions aper/src/data_structures/fixed_array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{AperSync, StoreHandle};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};

#[derive(Clone)]
Expand All @@ -22,7 +23,7 @@ impl<const N: u32, T: Serialize + DeserializeOwned + Default> AperSync for Fixed

impl<const N: u32, T: Serialize + DeserializeOwned + Default> FixedArray<N, T> {
pub fn get(&self, index: u32) -> T {
if let Some(bytes) = self.map.get(&index.to_be_bytes().to_vec()) {
if let Some(bytes) = self.map.get(&Bytes::from(index.to_be_bytes().to_vec())) {
bincode::deserialize(&bytes).unwrap()
} else {
T::default()
Expand All @@ -31,8 +32,9 @@ impl<const N: u32, T: Serialize + DeserializeOwned + Default> FixedArray<N, T> {

pub fn set(&mut self, index: u32, value: T) {
assert!(index < N);
let value = bincode::serialize(&value).unwrap();
self.map.set(index.to_be_bytes().to_vec(), value);
let value = Bytes::from(bincode::serialize(&value).unwrap());
self.map
.set(Bytes::from(index.to_be_bytes().to_vec()), value);
}

pub fn iter(&self) -> FixedArrayIterator<T> {
Expand Down Expand Up @@ -61,7 +63,7 @@ impl<T: Serialize + DeserializeOwned + Default> Iterator for FixedArrayIterator<
}

let key = self.index.to_be_bytes().to_vec();
let value = self.tree_ref.get(&key);
let value = self.tree_ref.get(&Bytes::from(key));
self.index += 1;

Some(
Expand Down
7 changes: 4 additions & 3 deletions aper/src/data_structures/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{AperSync, StoreHandle};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};

pub struct Map<K: Serialize + DeserializeOwned, V: AperSync> {
Expand All @@ -22,16 +23,16 @@ impl<K: Serialize + DeserializeOwned, V: AperSync> AperSync for Map<K, V> {
impl<K: Serialize + DeserializeOwned, V: AperSync> Map<K, V> {
pub fn get(&mut self, key: &K) -> Option<V> {
let key = bincode::serialize(key).unwrap();
Some(V::attach(self.map.child(&key)))
Some(V::attach(self.map.child(Bytes::from(key))))
}

pub fn get_or_create(&mut self, key: &K) -> V {
let key = bincode::serialize(key).unwrap();
V::attach(self.map.child(&key))
V::attach(self.map.child(Bytes::from(key)))
}

pub fn delete(&mut self, key: &K) {
let key = bincode::serialize(key).unwrap();
self.map.delete_child(&key);
self.map.delete_child(Bytes::from(key));
}
}
5 changes: 2 additions & 3 deletions aper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(unused, clippy::type_complexity)]
#![allow(clippy::type_complexity)]

mod aper;
pub mod connection;
Expand All @@ -8,6 +8,7 @@ mod store;

pub use aper::*;
pub use aper_derive::AperSync;
pub use bytes::Bytes;
use serde::{Deserialize, Serialize};
pub use store::*;

Expand All @@ -16,5 +17,3 @@ pub struct Mutation {
pub prefix: Vec<Bytes>,
pub entries: PrefixMap,
}

pub type Bytes = Vec<u8>;
Loading

0 comments on commit a8ffb3a

Please sign in to comment.