diff --git a/Cargo.lock b/Cargo.lock index 18ffcab..6cff429 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -147,6 +147,15 @@ dependencies = [ "scoped-tls", ] +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -1330,6 +1339,16 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "storage" +version = "0.1.0" +dependencies = [ + "bincode", + "num-bigint", + "rand", + "serde", +] + [[package]] name = "string_cache" version = "0.8.4" diff --git a/Cargo.toml b/Cargo.toml index b8051bb..279b4da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ members = [ "compile_to_rust_tests", "bench", "radix_tree", + "storage", ] diff --git a/storage/Cargo.toml b/storage/Cargo.toml new file mode 100644 index 0000000..b79a8f4 --- /dev/null +++ b/storage/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "storage" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1", features = ["derive"] } +bincode = "1" +num-bigint = "0.4.3" +rand = "0.8.5" diff --git a/storage/src/lib.rs b/storage/src/lib.rs new file mode 100644 index 0000000..bf92e79 --- /dev/null +++ b/storage/src/lib.rs @@ -0,0 +1,15 @@ +mod memory_storage; +mod storage; + +#[cfg(test)] +mod tests { + use crate::{memory_storage::MemoryStorage, storage::Storage}; + + #[test] + fn number() { + let mut storage = Storage::new(MemoryStorage::new()); + + let key = storage.write_number(123.456); + assert_eq!(storage.read_number(key), Some(123.456)); + } +} diff --git a/storage/src/memory_storage.rs b/storage/src/memory_storage.rs new file mode 100644 index 0000000..8ece8b6 --- /dev/null +++ b/storage/src/memory_storage.rs @@ -0,0 +1,28 @@ +use std::collections::HashMap; + +use crate::storage::{RawStorage, StorageKey}; + +pub struct MemoryStorage { + data: HashMap>, +} + +impl MemoryStorage { + pub fn new() -> Self { + Self { + data: HashMap::new(), + } + } +} + +impl RawStorage for MemoryStorage { + fn read(&self, key: StorageKey) -> Option> { + self.data.get(&key).cloned() + } + + fn write(&mut self, key: StorageKey, data: Option>) { + match data { + Some(data) => self.data.insert(key, data), + None => self.data.remove(&key), + }; + } +} diff --git a/storage/src/storage.rs b/storage/src/storage.rs new file mode 100644 index 0000000..e4f27fc --- /dev/null +++ b/storage/src/storage.rs @@ -0,0 +1,67 @@ +use std::rc::Rc; + +use rand::{rngs::ThreadRng, thread_rng, Rng}; + +use serde::{ser::SerializeSeq, Serialize, Serializer}; + +#[derive(serde::Serialize, serde::Deserialize, Hash, PartialEq, Eq, Clone, Copy, Debug)] +pub struct StorageKey(u64, u64, u64); + +pub struct Storage { + rng: ThreadRng, + rs: RS, +} + +pub trait RawStorage { + fn read(&self, key: StorageKey) -> Option>; + fn write(&mut self, key: StorageKey, data: Option>); +} + +impl Storage { + pub fn new(rs: RS) -> Self { + Self { + rng: thread_rng(), + rs, + } + } + + pub fn random_key(&mut self) -> StorageKey { + StorageKey(self.rng.gen(), self.rng.gen(), self.rng.gen()) + } + + pub fn write_number(&mut self, number: f64) -> StorageKey { + let mut data = Vec::::new(); + data.extend_from_slice(&number.to_le_bytes()); + let key = self.random_key(); + self.rs.write(key, Some(data)); + key + } + + pub fn read_number(&self, key: StorageKey) -> Option { + let data = self.rs.read(key)?; + let mut bytes = [0u8; 8]; + bytes.copy_from_slice(&data); + Some(f64::from_le_bytes(bytes)) + } +} + +#[derive(serde::Serialize, serde::Deserialize)] +struct StoredRc { + ref_count: u64, + refs: Vec, + data: Vec, +} + +#[derive(Default)] +enum StorageVal { + #[default] + Void, + Number(f64), + Array(Rc>), + Ref(u64), +} + +struct StorageValWithRefs { + val: StorageVal, + refs: Rc>, +}