From 0c4ae6fd0b5e84114af7f6a1290a18659df0b458 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Thu, 2 Nov 2023 22:50:57 +1100 Subject: [PATCH] Implement StorageReader for Storage --- storage/src/memory_backend.rs | 4 ++++ storage/src/sled_backend.rs | 4 ++++ storage/src/storage.rs | 13 +++++++++++++ storage/src/storage_backend.rs | 4 +++- storage/src/storage_io.rs | 1 - 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/storage/src/memory_backend.rs b/storage/src/memory_backend.rs index cba1ab9..fd648fc 100644 --- a/storage/src/memory_backend.rs +++ b/storage/src/memory_backend.rs @@ -25,6 +25,10 @@ impl StorageBackend for MemoryBackend { type Tx<'a> = MemoryTx<'a>; type TxMut<'a> = MemoryTxMut<'a>; + fn read_bytes(&self, ptr: StoragePtr) -> Result>, GenericError> { + Ok(self.data.get(&ptr.data).cloned()) + } + fn transaction(&self, self_weak: Weak>, f: F) -> Result> where F: Fn(&mut Self::Tx<'_>) -> Result, diff --git a/storage/src/sled_backend.rs b/storage/src/sled_backend.rs index a53500d..8f04d1e 100644 --- a/storage/src/sled_backend.rs +++ b/storage/src/sled_backend.rs @@ -37,6 +37,10 @@ impl StorageBackend for SledBackend { type Tx<'a> = SledTx<'a>; type TxMut<'a> = SledTxMut<'a>; + fn read_bytes(&self, ptr: StoragePtr) -> Result>, GenericError> { + Ok(self.db.get(ptr.to_bytes())?.map(|value| value.to_vec())) + } + fn transaction(&self, self_weak: Weak>, f: F) -> Result> where F: Fn(&mut Self::Tx<'_>) -> Result, diff --git a/storage/src/storage.rs b/storage/src/storage.rs index 7bd7e08..53023b1 100644 --- a/storage/src/storage.rs +++ b/storage/src/storage.rs @@ -118,3 +118,16 @@ impl Storage { }) } } + +impl StorageReader for Storage { + fn read_bytes( + &self, + ptr: crate::StoragePtr, + ) -> Result>, crate::GenericError> { + self.sb.borrow().read_bytes(ptr) + } + + fn get_backend(&self) -> std::rc::Weak> { + Rc::downgrade(&self.sb) + } +} diff --git a/storage/src/storage_backend.rs b/storage/src/storage_backend.rs index 15bfca5..4a6a051 100644 --- a/storage/src/storage_backend.rs +++ b/storage/src/storage_backend.rs @@ -2,7 +2,7 @@ use std::{cell::RefCell, error::Error, rc::Weak}; use crate::{ storage_io::{StorageReader, StorageTxMut}, - GenericError, + GenericError, StoragePtr, }; pub trait StorageBackend: Sized { @@ -10,6 +10,8 @@ pub trait StorageBackend: Sized { type Tx<'a>: StorageReader; type TxMut<'a>: StorageTxMut; + fn read_bytes(&self, ptr: StoragePtr) -> Result>, GenericError>; + fn transaction(&self, self_weak: Weak>, f: F) -> Result> where F: Fn(&mut Self::Tx<'_>) -> Result; diff --git a/storage/src/storage_io.rs b/storage/src/storage_io.rs index caef8d7..69439a4 100644 --- a/storage/src/storage_io.rs +++ b/storage/src/storage_io.rs @@ -10,7 +10,6 @@ use crate::{ pub trait StorageReader: Sized { fn read_bytes(&self, ptr: StoragePtr) -> Result>, GenericError>; - fn get_backend(&self) -> Weak>; fn get_auto_ptr>(