-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ members = [ | |
"compile_to_rust_tests", | ||
"bench", | ||
"radix_tree", | ||
"storage", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::storage::{RawStorage, StorageKey}; | ||
|
||
pub struct MemoryStorage { | ||
data: HashMap<StorageKey, Vec<u8>>, | ||
} | ||
|
||
impl MemoryStorage { | ||
pub fn new() -> Self { | ||
Self { | ||
data: HashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
impl RawStorage for MemoryStorage { | ||
fn read(&self, key: StorageKey) -> Option<Vec<u8>> { | ||
self.data.get(&key).cloned() | ||
} | ||
|
||
fn write(&mut self, key: StorageKey, data: Option<Vec<u8>>) { | ||
match data { | ||
Some(data) => self.data.insert(key, data), | ||
None => self.data.remove(&key), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RS: RawStorage> { | ||
rng: ThreadRng, | ||
rs: RS, | ||
} | ||
|
||
pub trait RawStorage { | ||
fn read(&self, key: StorageKey) -> Option<Vec<u8>>; | ||
fn write(&mut self, key: StorageKey, data: Option<Vec<u8>>); | ||
} | ||
|
||
impl<RS: RawStorage> Storage<RS> { | ||
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::<u8>::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<f64> { | ||
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<StorageKey>, | ||
data: Vec<u8>, | ||
} | ||
|
||
#[derive(Default)] | ||
enum StorageVal { | ||
#[default] | ||
Void, | ||
Number(f64), | ||
Array(Rc<Vec<StorageVal>>), | ||
Ref(u64), | ||
} | ||
|
||
struct StorageValWithRefs { | ||
val: StorageVal, | ||
refs: Rc<Vec<StorageKey>>, | ||
} |