Skip to content

Commit a8ffb3a

Browse files
authored
Merge pull request #44 from jamsocket/paulgb/iterator
iterator progress
2 parents e879162 + 8ad83c4 commit a8ffb3a

File tree

16 files changed

+376
-127
lines changed

16 files changed

+376
-127
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aper/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ aper_derive = {path = "./aper-derive", version="0.5.0"}
1717
chrono = { version = "0.4.38", features = ["serde"] }
1818
tracing = "0.1.40"
1919
self_cell = "1.0.4"
20+
bytes = { version = "1.7.1", features = ["serde"] }

aper/aper-derive/src/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ impl MacroState {
5555
let field = syn::Ident::new(field, proc_macro2::Span::call_site());
5656
let name = Literal::byte_string(field.to_string().as_bytes());
5757
quote! {
58-
#field: aper::AperSync::attach(store.child(#name))
58+
#field: aper::AperSync::attach(store.child(
59+
aper::Bytes::from_static(#name)
60+
))
5961
}
6062
});
6163
quote! {
@@ -68,7 +70,9 @@ impl MacroState {
6870
let fields = (0..*fields).map(|i| {
6971
let i = Literal::byte_string(i.to_be_bytes().as_slice());
7072
quote! {
71-
aper::AperSync::attach(store.child(#i))
73+
aper::AperSync::attach(store.child(
74+
aper::Bytes::from_static(#i)
75+
))
7276
}
7377
});
7478
quote! {
@@ -130,8 +134,8 @@ mod tests {
130134
impl aper::AperSync for MyStruct {
131135
fn attach(mut store: aper::StoreHandle) -> Self {
132136
MyStruct {
133-
field1: aper::AperSync::attach(store.child(b"field1")),
134-
field2: aper::AperSync::attach(store.child(b"field2"))
137+
field1: aper::AperSync::attach(store.child(aper::Bytes::from_static(b"field1"))),
138+
field2: aper::AperSync::attach(store.child(aper::Bytes::from_static(b"field2")))
135139
}
136140
}
137141
}
@@ -153,8 +157,8 @@ mod tests {
153157
impl aper::AperSync for MyStruct {
154158
fn attach(mut store: aper::StoreHandle) -> Self {
155159
MyStruct(
156-
aper::AperSync::attach(store.child(b"\0\0\0\0\0\0\0\0")),
157-
aper::AperSync::attach(store.child(b"\0\0\0\0\0\0\0\x01"))
160+
aper::AperSync::attach(store.child(aper::Bytes::from_static(b"\0\0\0\0\0\0\0\0"))),
161+
aper::AperSync::attach(store.child(aper::Bytes::from_static(b"\0\0\0\0\0\0\0\x01")))
158162
)
159163
}
160164
}

aper/src/aper.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ use crate::{
44
Mutation,
55
};
66
use serde::{Deserialize, Serialize};
7-
use std::{
8-
collections::{HashSet, VecDeque},
9-
fmt::Debug,
10-
};
7+
use std::{collections::VecDeque, fmt::Debug};
118

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

15-
fn listen<F: Fn() -> bool + 'static + Send + Sync>(&self, listener: F) {
12+
fn listen<F: Fn() -> bool + 'static + Send + Sync>(&self, _listener: F) {
1613
// Default implementation does nothing.
1714
}
1815
}

aper/src/connection.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ use dashmap::DashMap;
44
use serde::{Deserialize, Serialize};
55
use std::{
66
borrow::Borrow,
7-
cell::RefCell,
8-
sync::{
9-
atomic::{AtomicU32, AtomicU64},
10-
Arc, Mutex,
11-
},
7+
sync::{atomic::AtomicU32, Arc, Mutex},
128
};
139

1410
#[derive(Serialize, Deserialize, Clone, Debug)]
@@ -226,7 +222,7 @@ impl<A: Aper> ServerHandle<A> {
226222
}
227223
}
228224
}
229-
MessageToServer::RequestState { latest_version } => {
225+
MessageToServer::RequestState { .. } => {
230226
let server = self.server.lock().unwrap();
231227
let c = server.borrow();
232228
let mutations = c.state_snapshot();

aper/src/data_structures/atom.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{AperSync, Store, StoreHandle};
1+
use crate::{AperSync, StoreHandle};
2+
use bytes::Bytes;
23
use serde::{de::DeserializeOwned, Serialize};
34

45
pub struct Atom<T: Serialize + DeserializeOwned + Default> {
@@ -22,12 +23,15 @@ impl<T: Serialize + DeserializeOwned + Default> AperSync for Atom<T> {
2223
impl<T: Serialize + DeserializeOwned + Default> Atom<T> {
2324
pub fn get(&self) -> T {
2425
self.map
25-
.get(&vec![])
26+
.get(&Bytes::new())
2627
.map(|bytes| bincode::deserialize(&bytes).expect("Couldn't deserialize"))
2728
.unwrap_or_default()
2829
}
2930

3031
pub fn set(&mut self, value: T) {
31-
self.map.set(vec![], bincode::serialize(&value).unwrap());
32+
self.map.set(
33+
Bytes::new(),
34+
Bytes::from(bincode::serialize(&value).unwrap()),
35+
);
3236
}
3337
}

aper/src/data_structures/atom_map.rs

+55-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{AperSync, StoreHandle};
1+
use crate::{AperSync, StoreHandle, StoreIterator};
2+
use bytes::Bytes;
23
use serde::{de::DeserializeOwned, Serialize};
34

45
pub struct AtomMap<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
@@ -22,18 +23,67 @@ impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> AperSync
2223
impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> AtomMap<K, V> {
2324
pub fn get(&self, key: &K) -> Option<V> {
2425
self.map
25-
.get(&bincode::serialize(key).unwrap())
26+
.get(&Bytes::from(bincode::serialize(key).unwrap()))
2627
.map(|bytes| bincode::deserialize(&bytes).unwrap())
2728
}
2829

2930
pub fn set(&mut self, key: &K, value: &V) {
3031
self.map.set(
31-
bincode::serialize(key).unwrap(),
32-
bincode::serialize(value).unwrap(),
32+
Bytes::from(bincode::serialize(key).unwrap()),
33+
Bytes::from(bincode::serialize(value).unwrap()),
3334
);
3435
}
3536

3637
pub fn delete(&mut self, key: &K) {
37-
self.map.delete(bincode::serialize(key).unwrap());
38+
self.map
39+
.delete(Bytes::from(bincode::serialize(key).unwrap()));
40+
}
41+
42+
pub fn iter(&self) -> AtomMapIter<K, V> {
43+
AtomMapIter {
44+
iter: self.map.iter(),
45+
_phantom: std::marker::PhantomData,
46+
}
47+
}
48+
}
49+
50+
pub struct AtomMapIter<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
51+
iter: StoreIterator,
52+
_phantom: std::marker::PhantomData<(K, V)>,
53+
}
54+
55+
impl<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> Iterator
56+
for AtomMapIter<K, V>
57+
{
58+
type Item = (K, V);
59+
60+
fn next(&mut self) -> Option<Self::Item> {
61+
let n = self.iter.next()?;
62+
let key = bincode::deserialize(&n.0).unwrap();
63+
let value = bincode::deserialize(&n.1).unwrap();
64+
Some((key, value))
65+
}
66+
}
67+
68+
#[cfg(test)]
69+
mod test {
70+
use super::*;
71+
72+
#[test]
73+
fn atom_map_iter() {
74+
let store = crate::Store::default();
75+
let mut map = AtomMap::<String, String>::attach(store.handle());
76+
77+
map.set(&"h-insert".to_string(), &"b".to_string());
78+
map.set(&"a-insert".to_string(), &"a".to_string());
79+
map.set(&"z-insert".to_string(), &"c".to_string());
80+
map.set(&"f-insert".to_string(), &"d".to_string());
81+
82+
let mut iter = map.iter();
83+
84+
assert_eq!(iter.next(), Some(("a-insert".to_string(), "a".to_string())));
85+
assert_eq!(iter.next(), Some(("f-insert".to_string(), "d".to_string())));
86+
assert_eq!(iter.next(), Some(("h-insert".to_string(), "b".to_string())));
87+
assert_eq!(iter.next(), Some(("z-insert".to_string(), "c".to_string())));
3888
}
3989
}

aper/src/data_structures/fixed_array.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{AperSync, StoreHandle};
2+
use bytes::Bytes;
23
use serde::{de::DeserializeOwned, Serialize};
34

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

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

3233
pub fn set(&mut self, index: u32, value: T) {
3334
assert!(index < N);
34-
let value = bincode::serialize(&value).unwrap();
35-
self.map.set(index.to_be_bytes().to_vec(), value);
35+
let value = Bytes::from(bincode::serialize(&value).unwrap());
36+
self.map
37+
.set(Bytes::from(index.to_be_bytes().to_vec()), value);
3638
}
3739

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

6365
let key = self.index.to_be_bytes().to_vec();
64-
let value = self.tree_ref.get(&key);
66+
let value = self.tree_ref.get(&Bytes::from(key));
6567
self.index += 1;
6668

6769
Some(

aper/src/data_structures/map.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{AperSync, StoreHandle};
2+
use bytes::Bytes;
23
use serde::{de::DeserializeOwned, Serialize};
34

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

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

3334
pub fn delete(&mut self, key: &K) {
3435
let key = bincode::serialize(key).unwrap();
35-
self.map.delete_child(&key);
36+
self.map.delete_child(Bytes::from(key));
3637
}
3738
}

aper/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused, clippy::type_complexity)]
1+
#![allow(clippy::type_complexity)]
22

33
mod aper;
44
pub mod connection;
@@ -8,6 +8,7 @@ mod store;
88

99
pub use aper::*;
1010
pub use aper_derive::AperSync;
11+
pub use bytes::Bytes;
1112
use serde::{Deserialize, Serialize};
1213
pub use store::*;
1314

@@ -16,5 +17,3 @@ pub struct Mutation {
1617
pub prefix: Vec<Bytes>,
1718
pub entries: PrefixMap,
1819
}
19-
20-
pub type Bytes = Vec<u8>;

0 commit comments

Comments
 (0)