Skip to content

Commit

Permalink
Implement read cache for Val
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Dec 21, 2023
1 parent 299b986 commit 63798c4
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 9 deletions.
2 changes: 2 additions & 0 deletions storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod demo_val;

mod errors;
mod rc_key;
mod read_cache;
mod sled_backend;
mod storage_auto_ptr;
mod storage_backend;
Expand All @@ -21,6 +22,7 @@ pub use self::storage_io::{StorageReader, StorageTxMut};
pub use errors::GenericError;
pub use memory_backend::MemoryBackend;
pub use rc_key::RcKey;
pub use read_cache::ReadCache;
pub use sled_backend::SledBackend;
pub use storage_auto_ptr::StorageAutoPtr;
pub use storage_entity::StorageEntity;
Expand Down
15 changes: 13 additions & 2 deletions storage/src/memory_backend.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
use std::{cell::RefCell, collections::HashMap, error::Error, rc::Weak};
use std::{
cell::{RefCell, RefMut},
collections::HashMap,
error::Error,
rc::Weak,
};

use crate::{
rc_key::RcKey,
storage_io::{StorageReader, StorageTxMut},
storage_ptr::StorageEntryPtr,
GenericError, StorageBackend, StoragePtr,
GenericError, ReadCache, StorageBackend, StoragePtr,
};

#[derive(Default)]
pub struct MemoryBackend {
data: HashMap<(u64, u64, u64), Vec<u8>>,
read_cache: RefCell<ReadCache>,
}

impl MemoryBackend {
pub fn new() -> Self {
Self {
data: HashMap::new(),
read_cache: Default::default(),
}
}
}
Expand Down Expand Up @@ -67,6 +74,10 @@ impl StorageBackend for MemoryBackend {
self.data.is_empty()
}

fn get_read_cache(&self) -> RefMut<ReadCache> {
self.read_cache.borrow_mut()
}

#[cfg(test)]
fn len(&self) -> usize {
self.data.len()
Expand Down
3 changes: 3 additions & 0 deletions storage/src/read_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use std::{any::Any, collections::HashMap};

pub type ReadCache = HashMap<(u64, u64, u64), Box<dyn Any>>;
17 changes: 15 additions & 2 deletions storage/src/sled_backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{cell::RefCell, collections::HashMap, error::Error, fmt::Display, rc::Weak};
use std::{
cell::{RefCell, RefMut},
collections::HashMap,
error::Error,
fmt::Display,
rc::Weak,
};

use sled::transaction::{
ConflictableTransactionError, TransactionError, UnabortableTransactionError,
Expand All @@ -8,11 +14,12 @@ use crate::{
rc_key::RcKey,
storage_io::{StorageReader, StorageTxMut},
storage_ptr::StorageEntryPtr,
GenericError, StorageBackend, StoragePtr,
GenericError, ReadCache, StorageBackend, StoragePtr,
};

pub struct SledBackend {
db: sled::Db,
read_cache: RefCell<ReadCache>,
}

impl SledBackend {
Expand All @@ -22,12 +29,14 @@ impl SledBackend {
{
Ok(Self {
db: sled::open(path)?,
read_cache: Default::default(),
})
}

pub fn open_in_memory() -> Result<Self, sled::Error> {
Ok(Self {
db: sled::Config::new().temporary(true).open()?,
read_cache: Default::default(),
})
}
}
Expand Down Expand Up @@ -91,6 +100,10 @@ impl StorageBackend for SledBackend {
self.db.is_empty()
}

fn get_read_cache(&self) -> RefMut<ReadCache> {
self.read_cache.borrow_mut()
}

#[cfg(test)]
fn len(&self) -> usize {
self.db.len()
Expand Down
2 changes: 1 addition & 1 deletion storage/src/storage_auto_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{StorageBackend, StorageEntity, StorageEntryPtr};

pub struct StorageAutoPtr<SB: StorageBackend, SE: StorageEntity<SB>> {
pub(crate) _marker: std::marker::PhantomData<SE>,
pub(crate) sb: Weak<RefCell<SB>>, // TODO: Does this need to be weak?
pub sb: Weak<RefCell<SB>>, // TODO: Does this need to be weak?
pub ptr: StorageEntryPtr,
}

Expand Down
6 changes: 4 additions & 2 deletions storage/src/storage_backend.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{
cell::RefCell,
cell::{RefCell, RefMut},
error::Error,
rc::{Rc, Weak},
};

use crate::{
storage_io::{StorageReader, StorageTxMut},
GenericError, StoragePtr,
GenericError, ReadCache, StoragePtr,
};

pub trait StorageBackend: Sized {
Expand All @@ -30,6 +30,8 @@ pub trait StorageBackend: Sized {

fn is_empty(&self) -> bool;

fn get_read_cache(&self) -> RefMut<ReadCache>;

#[cfg(test)]
fn len(&self) -> usize;
}
Expand Down
23 changes: 21 additions & 2 deletions valuescript_vm/src/vs_storage_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
rc::Rc,
};

use storage::{StorageAutoPtr, StorageBackend, StorageEntryPtr};
use storage::{StorageAutoPtr, StorageBackend, StorageEntity, StorageEntryPtr, StorageReader};

use crate::vs_value::{ToVal, Val};

Expand All @@ -19,7 +19,26 @@ pub struct StorageValAutoPtr<SB: StorageBackend + 'static> {

impl<SB: StorageBackend> ValResolver for StorageValAutoPtr<SB> {
fn resolve(&self) -> Val {
self.ptr.resolve().unwrap().unwrap()
let sb = match self.ptr.sb.upgrade() {
Some(sb) => sb,
None => panic!("Storage backend dropped"),
};

let borrow = sb.borrow();
let mut read_cache = borrow.get_read_cache();

if let Some(cache_box) = read_cache.get(&self.ptr.ptr.data) {
if let Some(cache_val) = cache_box.downcast_ref::<Val>() {
return cache_val.clone();
}
}

let entry = sb.read(self.ptr.ptr).unwrap().expect("Unresolved ptr");
let res = Val::from_storage_entry(&sb, entry).expect("Failed to deserialize Val");

read_cache.insert(self.ptr.ptr.data, Box::new(res.clone()));

res
}

fn ptr(&self) -> StorageEntryPtr {
Expand Down

0 comments on commit 63798c4

Please sign in to comment.