Skip to content

Commit

Permalink
Improve DemoVal
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Oct 30, 2023
1 parent f2021b1 commit 6fbca5a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
47 changes: 20 additions & 27 deletions storage/src/demo_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ use crate::storage_entity::StorageEntity;
use crate::storage_entry::{StorageEntry, StorageEntryReader};
use crate::storage_ops::StorageOps;
use crate::storage_ptr::StorageEntryPtr;

#[cfg(test)]
use crate::{Storage, StorageBackend};

#[derive(Default, Debug, Clone)]
const NUMBER_TAG: u8 = 0;
const ARRAY_TAG: u8 = 1;
const PTR_TAG: u8 = 2;

#[derive(Debug, Clone)]
pub enum DemoVal {
#[default]
Void,
Number(u64),
Ptr(StorageEntryPtr),
Array(Rc<Vec<DemoVal>>),
Ptr(StorageEntryPtr),
}

impl DemoVal {
#[cfg(test)]
pub(crate) fn numbers<SB: StorageBackend>(
&self,
storage: &mut Storage<SB>,
Expand All @@ -33,30 +32,27 @@ impl DemoVal {
entry: &mut StorageEntry,
) -> Result<(), E> {
match self {
DemoVal::Void => {
entry.data.push(0);
}
DemoVal::Number(n) => {
entry.data.push(1);
entry.data.push(NUMBER_TAG);
entry.data.extend(n.to_le_bytes());
}
DemoVal::Ptr(ptr) => {
entry.data.push(2);
entry.refs.push(*ptr);
}
DemoVal::Array(arr) => 'b: {
let key = RcKey::from(arr.clone());

if let Some(ptr) = tx.cache_get(key.clone()) {
entry.data.push(2);
entry.data.push(PTR_TAG);
entry.refs.push(ptr);
break 'b;
}

let ptr = tx.store_and_cache(self, key)?;
entry.data.push(2);
entry.data.push(PTR_TAG);
entry.refs.push(ptr);
}
DemoVal::Ptr(ptr) => {
entry.data.push(PTR_TAG);
entry.refs.push(*ptr);
}
};

Ok(())
Expand All @@ -69,16 +65,11 @@ impl DemoVal {
let tag = reader.read_u8().unwrap();

Ok(match tag {
0 => DemoVal::Void,
1 => {
NUMBER_TAG => {
let n = reader.read_u64().unwrap();
DemoVal::Number(n)
}
2 => {
let ptr = reader.read_ref().unwrap();
DemoVal::Ptr(ptr)
}
3 => {
ARRAY_TAG => {
let len = reader.read_u64().unwrap();
let mut items = Vec::new();

Expand All @@ -88,14 +79,16 @@ impl DemoVal {

DemoVal::Array(Rc::new(items))
}
PTR_TAG => {
let ptr = reader.read_ref().unwrap();
DemoVal::Ptr(ptr)
}
_ => panic!("Invalid tag"),
})
}

#[cfg(test)]
fn numbers_impl<E, SO: StorageOps<E>>(&self, tx: &mut SO) -> Result<Vec<u64>, E> {
match &self {
DemoVal::Void => Ok(Vec::new()),
DemoVal::Number(n) => Ok(vec![*n]),
DemoVal::Ptr(ptr) => {
let entry = tx.read(*ptr)?.unwrap();
Expand Down Expand Up @@ -124,7 +117,7 @@ impl StorageEntity for DemoVal {

match self {
DemoVal::Array(arr) => {
entry.data.push(3);
entry.data.push(ARRAY_TAG);

entry
.data
Expand Down
3 changes: 3 additions & 0 deletions storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod memory_backend;
mod storage;

#[cfg(test)]
mod demo_val;

mod rc_key;
mod sled_backend;
mod storage_backend;
Expand All @@ -15,4 +17,5 @@ pub use self::storage::Storage;
pub use self::storage_backend::StorageBackend;
pub use memory_backend::MemoryBackend;
pub use sled_backend::SledBackend;
pub use storage_entry::{StorageEntry, StorageEntryReader};
pub use storage_ptr::{storage_head_ptr, StoragePtr};
6 changes: 3 additions & 3 deletions storage/src/storage_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub struct StorageEntry {
}

pub struct StorageEntryReader<'a> {
pub(crate) entry: &'a StorageEntry,
pub(crate) refs_i: usize,
pub(crate) data_i: usize,
pub entry: &'a StorageEntry,
pub refs_i: usize,
pub data_i: usize,
}

impl<'a> StorageEntryReader<'a> {
Expand Down

0 comments on commit 6fbca5a

Please sign in to comment.