mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Split out storage_backend
This commit is contained in:
@@ -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};
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 }
|
||||
|
||||
20
storage/src/storage_backend.rs
Normal file
20
storage/src/storage_backend.rs
Normal 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;
|
||||
}
|
||||
@@ -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},
|
||||
};
|
||||
|
||||
|
||||
@@ -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>)) {
|
||||
|
||||
Reference in New Issue
Block a user