Split out storage_backend

This commit is contained in:
Andrew Morris
2023-10-27 11:20:05 +11:00
parent fca4543c8e
commit 25e64ae09b
7 changed files with 32 additions and 30 deletions

View File

@@ -3,11 +3,13 @@ mod storage;
mod serde_rc;
mod sled_backend;
mod storage_backend;
mod storage_ops;
mod storage_ptr;
mod tests;
pub use self::storage::{Storage, StorageBackend};
pub use self::storage::Storage;
pub use self::storage_backend::StorageBackend;
pub use memory_backend::MemoryBackend;
pub use sled_backend::SledBackend;
pub use storage_ptr::{storage_head_ptr, StoragePtr};

View File

@@ -1,10 +1,7 @@
use std::collections::HashMap;
use std::fmt::Debug as DebugTrait;
use crate::{
storage::{StorageBackend, StorageBackendHandle},
StoragePtr,
};
use crate::{storage_backend::StorageBackendHandle, StorageBackend, StoragePtr};
#[derive(Default)]
pub struct MemoryBackend {

View File

@@ -1,9 +1,6 @@
use std::fmt::Debug as DebugTrait;
use crate::{
storage::{StorageBackend, StorageBackendHandle},
StoragePtr,
};
use crate::{storage_backend::StorageBackendHandle, StorageBackend, StoragePtr};
pub struct SledBackend {
db: sled::Db,

View File

@@ -1,32 +1,16 @@
use std::{fmt::Debug as DebugTrait, rc::Rc};
use std::rc::Rc;
use serde::{Deserialize, Serialize};
use crate::serde_rc::{deserialize_rc, serialize_rc};
use crate::storage_ops::StorageOps;
use crate::storage_ptr::{tmp_at_ptr, tmp_count_ptr, StorageEntryPtr, StorageHeadPtr, StoragePtr};
use crate::storage_ptr::{tmp_at_ptr, tmp_count_ptr, StorageEntryPtr, StorageHeadPtr};
use crate::StorageBackend;
pub struct Storage<SB: StorageBackend> {
sb: SB,
}
pub trait StorageBackendHandle<'a, E> {
fn read_bytes<T>(&self, key: StoragePtr<T>) -> Result<Option<Vec<u8>>, E>;
fn write_bytes<T>(&mut self, key: StoragePtr<T>, data: Option<Vec<u8>>) -> Result<(), E>;
}
pub trait StorageBackend {
type Error<E: DebugTrait>: DebugTrait;
type InTransactionError<E>;
type Handle<'a, E>: StorageBackendHandle<'a, Self::InTransactionError<E>>;
fn transaction<F, T, E: DebugTrait>(&mut self, f: F) -> Result<T, Self::Error<E>>
where
F: Fn(&mut Self::Handle<'_, E>) -> Result<T, Self::InTransactionError<E>>;
fn is_empty(&self) -> bool;
}
impl<SB: StorageBackend> Storage<SB> {
pub fn new(sb: SB) -> Self {
Self { sb }

View File

@@ -0,0 +1,20 @@
use std::fmt::Debug as DebugTrait;
use crate::storage_ptr::StoragePtr;
pub trait StorageBackendHandle<'a, E> {
fn read_bytes<T>(&self, key: StoragePtr<T>) -> Result<Option<Vec<u8>>, E>;
fn write_bytes<T>(&mut self, key: StoragePtr<T>, data: Option<Vec<u8>>) -> Result<(), E>;
}
pub trait StorageBackend {
type Error<E: DebugTrait>: DebugTrait;
type InTransactionError<E>;
type Handle<'a, E>: StorageBackendHandle<'a, Self::InTransactionError<E>>;
fn transaction<F, T, E: DebugTrait>(&mut self, f: F) -> Result<T, Self::Error<E>>
where
F: Fn(&mut Self::Handle<'_, E>) -> Result<T, Self::InTransactionError<E>>;
fn is_empty(&self) -> bool;
}

View File

@@ -2,7 +2,8 @@ use rand::thread_rng;
use serde::{Deserialize, Serialize};
use crate::{
storage::{StorageBackendHandle, StorageVal},
storage::StorageVal,
storage_backend::StorageBackendHandle,
storage_ptr::{StorageEntryPtr, StorageHeadPtr, StoragePtr},
};

View File

@@ -5,8 +5,9 @@ mod tests_ {
use crate::{
memory_backend::MemoryBackend,
sled_backend::SledBackend,
storage::{Storage, StorageBackend, StoragePoint, StorageVal},
storage::{Storage, StoragePoint, StorageVal},
storage_ptr::storage_head_ptr,
StorageBackend,
};
fn run(impl_memory: fn(&mut Storage<MemoryBackend>), impl_sled: fn(&mut Storage<SledBackend>)) {