Skip to content

Commit

Permalink
use bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Sep 4, 2024
1 parent ffb769e commit ba9d575
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 31 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"] }
8 changes: 6 additions & 2 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
8 changes: 6 additions & 2 deletions aper/src/data_structures/atom.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 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()),
);
}
}
10 changes: 6 additions & 4 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, StoreIterator};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};

pub struct AtomMap<K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned> {
Expand All @@ -22,19 +23,20 @@ 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> {
Expand Down
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));
}
}
3 changes: 1 addition & 2 deletions aper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
10 changes: 5 additions & 5 deletions aper/src/store/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ mod test {
let store = Store::default();
let mut handle = store.handle();

let mut child_handle = handle.child(b"foo");
let _ = child_handle.child(b"bar");
let mut child_handle = handle.child(Bytes::from_static(b"foo"));
let _ = child_handle.child(Bytes::from_static(b"bar"));

assert_eq!(
store.prefixes(),
Expand All @@ -233,10 +233,10 @@ mod test {
let store = Store::default();
let mut handle = store.handle();

let mut child_handle = handle.child(b"foo");
let _ = child_handle.child(b"bar");
let mut child_handle = handle.child(Bytes::from_static(b"foo"));
let _ = child_handle.child(Bytes::from_static(b"bar"));

handle.delete_child(b"foo".as_slice());
handle.delete_child(Bytes::from_static(b"foo"));

assert_eq!(store.prefixes(), vec![] as Vec<Vec<Bytes>>);
}
Expand Down
8 changes: 4 additions & 4 deletions aper/src/store/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ impl StoreHandle {
map.insert(key, PrefixMapValue::Deleted);
}

pub fn child(&mut self, path_part: &[u8]) -> Self {
pub fn child(&mut self, path_part: Bytes) -> Self {
let mut prefix = self.prefix.clone();
prefix.push(path_part.to_vec());
prefix.push(path_part);
self.map.ensure(&prefix);
Self {
map: self.map.clone(),
prefix,
}
}

pub fn delete_child(&mut self, path_part: &[u8]) {
pub fn delete_child(&mut self, path_part: Bytes) {
let mut prefix = self.prefix.clone();
prefix.push(path_part.to_vec());
prefix.push(path_part);

let mut layers = self.map.inner.layers.write().unwrap();

Expand Down
13 changes: 8 additions & 5 deletions aper/tests/listener.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use aper::{
data_structures::{atom::Atom, fixed_array::FixedArray},
Aper, AperClient, AperSync, Mutation, PrefixMap, PrefixMapValue,
Aper, AperClient, AperSync, Bytes, Mutation, PrefixMap, PrefixMapValue,
};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, sync::mpsc::channel};
Expand Down Expand Up @@ -35,9 +35,12 @@ impl Aper for SimpleStruct {
}

fn create_mutation(prefix: Vec<&[u8]>, entries: Vec<(Vec<u8>, PrefixMapValue)>) -> Mutation {
let entries = entries.into_iter().collect::<BTreeMap<_, _>>();
let entries = entries
.into_iter()
.map(|(k, v)| (Bytes::from(k.to_vec()), v))
.collect::<BTreeMap<Bytes, _>>();
let entries = PrefixMap::Children(entries);
let prefix = prefix.iter().map(|x| x.to_vec()).collect();
let prefix = prefix.iter().map(|x| Bytes::from(x.to_vec())).collect();
Mutation { prefix, entries }
}

Expand Down Expand Up @@ -101,7 +104,7 @@ fn test_mutate_listener_simple() {
vec![b"atom_i32"],
vec![(
b"".to_vec(),
PrefixMapValue::Value(42i32.to_le_bytes().to_vec()),
PrefixMapValue::Value(Bytes::from(42i32.to_le_bytes().to_vec())),
)],
)],
None,
Expand Down Expand Up @@ -178,7 +181,7 @@ fn test_mutate_listener_incidental() {
vec![b"rhs"],
vec![(
b"".to_vec(),
PrefixMapValue::Value(26i32.to_le_bytes().to_vec()),
PrefixMapValue::Value(Bytes::from(26i32.to_le_bytes().to_vec())),
)],
)],
None,
Expand Down

0 comments on commit ba9d575

Please sign in to comment.