Skip to content

Commit

Permalink
Implement StorageReader for Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Nov 2, 2023
1 parent fdb50bc commit 0c4ae6f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions storage/src/memory_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl StorageBackend for MemoryBackend {
type Tx<'a> = MemoryTx<'a>;
type TxMut<'a> = MemoryTxMut<'a>;

fn read_bytes<T>(&self, ptr: StoragePtr<T>) -> Result<Option<Vec<u8>>, GenericError> {
Ok(self.data.get(&ptr.data).cloned())
}

fn transaction<F, T>(&self, self_weak: Weak<RefCell<Self>>, f: F) -> Result<T, Box<dyn Error>>
where
F: Fn(&mut Self::Tx<'_>) -> Result<T, GenericError>,
Expand Down
4 changes: 4 additions & 0 deletions storage/src/sled_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl StorageBackend for SledBackend {
type Tx<'a> = SledTx<'a>;
type TxMut<'a> = SledTxMut<'a>;

fn read_bytes<T>(&self, ptr: StoragePtr<T>) -> Result<Option<Vec<u8>>, GenericError> {
Ok(self.db.get(ptr.to_bytes())?.map(|value| value.to_vec()))
}

fn transaction<F, T>(&self, self_weak: Weak<RefCell<Self>>, f: F) -> Result<T, Box<dyn Error>>
where
F: Fn(&mut Self::Tx<'_>) -> Result<T, GenericError>,
Expand Down
13 changes: 13 additions & 0 deletions storage/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,16 @@ impl<SB: StorageBackend> Storage<SB> {
})
}
}

impl<SB: StorageBackend> StorageReader<SB> for Storage<SB> {
fn read_bytes<T>(
&self,
ptr: crate::StoragePtr<T>,
) -> Result<Option<Vec<u8>>, crate::GenericError> {
self.sb.borrow().read_bytes(ptr)
}

fn get_backend(&self) -> std::rc::Weak<RefCell<SB>> {
Rc::downgrade(&self.sb)
}
}
4 changes: 3 additions & 1 deletion storage/src/storage_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use std::{cell::RefCell, error::Error, rc::Weak};

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

pub trait StorageBackend: Sized {
type CustomError;
type Tx<'a>: StorageReader<Self>;
type TxMut<'a>: StorageTxMut<Self>;

fn read_bytes<T>(&self, ptr: StoragePtr<T>) -> Result<Option<Vec<u8>>, GenericError>;

fn transaction<F, T>(&self, self_weak: Weak<RefCell<Self>>, f: F) -> Result<T, Box<dyn Error>>
where
F: Fn(&mut Self::Tx<'_>) -> Result<T, GenericError>;
Expand Down
1 change: 0 additions & 1 deletion storage/src/storage_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{

pub trait StorageReader<SB: StorageBackend>: Sized {
fn read_bytes<T>(&self, ptr: StoragePtr<T>) -> Result<Option<Vec<u8>>, GenericError>;

fn get_backend(&self) -> Weak<RefCell<SB>>;

fn get_auto_ptr<SE: StorageEntity<SB>>(
Expand Down

0 comments on commit 0c4ae6f

Please sign in to comment.