Handle -> Tx

This commit is contained in:
Andrew Morris
2023-10-31 16:17:19 +11:00
parent 44802a1843
commit b2f523a83c
10 changed files with 38 additions and 47 deletions

View File

@@ -1,10 +1,10 @@
use std::rc::Rc;
use crate::rc_key::RcKey;
use crate::storage_backend_handle::StorageBackendHandle;
use crate::storage_entity::StorageEntity;
use crate::storage_entry::{StorageEntry, StorageEntryReader};
use crate::storage_ptr::StorageEntryPtr;
use crate::storage_tx::StorageTx;
use crate::{Storage, StorageBackend};
const NUMBER_TAG: u8 = 0;
@@ -26,7 +26,7 @@ impl DemoVal {
storage.sb.transaction(|sb| self.numbers_impl(sb))
}
fn write_to_entry<'a, E, Tx: StorageBackendHandle<'a, E>>(
fn write_to_entry<'a, E, Tx: StorageTx<'a, E>>(
&self,
tx: &mut Tx,
entry: &mut StorageEntry,
@@ -58,7 +58,7 @@ impl DemoVal {
Ok(())
}
fn read_from_entry<'a, E, Tx: StorageBackendHandle<'a, E>>(
fn read_from_entry<'a, E, Tx: StorageTx<'a, E>>(
_tx: &mut Tx,
reader: &mut StorageEntryReader,
) -> Result<DemoVal, E> {
@@ -87,10 +87,7 @@ impl DemoVal {
})
}
fn numbers_impl<'a, E, Tx: StorageBackendHandle<'a, E>>(
&self,
tx: &mut Tx,
) -> Result<Vec<u64>, E> {
fn numbers_impl<'a, E, Tx: StorageTx<'a, E>>(&self, tx: &mut Tx) -> Result<Vec<u64>, E> {
match &self {
DemoVal::Number(n) => Ok(vec![*n]),
DemoVal::Ptr(ptr) => {
@@ -110,7 +107,7 @@ impl DemoVal {
}
}
impl<'a, E, Tx: StorageBackendHandle<'a, E>> StorageEntity<'a, E, Tx> for DemoVal {
impl<'a, E, Tx: StorageTx<'a, E>> StorageEntity<'a, E, Tx> for DemoVal {
fn to_storage_entry(&self, tx: &mut Tx) -> Result<StorageEntry, E> {
let mut entry = StorageEntry {
ref_count: 1,

View File

@@ -7,15 +7,15 @@ mod demo_val;
mod rc_key;
mod sled_backend;
mod storage_backend;
mod storage_backend_handle;
mod storage_entity;
mod storage_entry;
mod storage_ptr;
mod storage_tx;
mod tests;
pub use self::storage::Storage;
pub use self::storage_backend::StorageBackend;
pub use self::storage_backend_handle::StorageBackendHandle;
pub use self::storage_tx::StorageTx;
pub use memory_backend::MemoryBackend;
pub use rc_key::RcKey;
pub use sled_backend::SledBackend;

View File

@@ -2,8 +2,7 @@ use std::collections::HashMap;
use std::fmt::Debug as DebugTrait;
use crate::{
rc_key::RcKey, storage_backend_handle::StorageBackendHandle, storage_ptr::StorageEntryPtr,
StorageBackend, StoragePtr,
rc_key::RcKey, storage_ptr::StorageEntryPtr, storage_tx::StorageTx, StorageBackend, StoragePtr,
};
#[derive(Default)]
@@ -22,13 +21,13 @@ impl MemoryBackend {
impl StorageBackend for MemoryBackend {
type Error<E: DebugTrait> = E;
type InTransactionError<E> = E;
type Handle<'a, E> = MemoryStorageHandle<'a>;
type Tx<'a, E> = MemoryTx<'a>;
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>>,
F: Fn(&mut Self::Tx<'_, E>) -> Result<T, Self::InTransactionError<E>>,
{
let mut handle = MemoryStorageHandle {
let mut handle = MemoryTx {
ref_deltas: Default::default(),
cache: Default::default(),
storage: self,
@@ -50,13 +49,13 @@ impl StorageBackend for MemoryBackend {
}
}
pub struct MemoryStorageHandle<'a> {
pub struct MemoryTx<'a> {
ref_deltas: HashMap<(u64, u64, u64), i64>,
cache: HashMap<RcKey, StorageEntryPtr>,
storage: &'a mut MemoryBackend,
}
impl<'a, E> StorageBackendHandle<'a, E> for MemoryStorageHandle<'a> {
impl<'a, E> StorageTx<'a, E> for MemoryTx<'a> {
fn ref_deltas(&mut self) -> &mut HashMap<(u64, u64, u64), i64> {
&mut self.ref_deltas
}

View File

@@ -1,8 +1,7 @@
use std::{collections::HashMap, fmt::Debug as DebugTrait};
use crate::{
rc_key::RcKey, storage_backend_handle::StorageBackendHandle, storage_ptr::StorageEntryPtr,
StorageBackend, StoragePtr,
rc_key::RcKey, storage_ptr::StorageEntryPtr, storage_tx::StorageTx, StorageBackend, StoragePtr,
};
pub struct SledBackend {
@@ -29,14 +28,14 @@ impl SledBackend {
impl StorageBackend for SledBackend {
type Error<E: DebugTrait> = sled::transaction::TransactionError<E>;
type InTransactionError<E> = sled::transaction::ConflictableTransactionError<E>;
type Handle<'a, E> = SledBackendHandle<'a>;
type Tx<'a, E> = SledTx<'a>;
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>>,
F: Fn(&mut Self::Tx<'_, E>) -> Result<T, Self::InTransactionError<E>>,
{
self.db.transaction(|tx| {
let mut handle = SledBackendHandle {
let mut handle = SledTx {
ref_deltas: Default::default(),
cache: Default::default(),
tx,
@@ -59,15 +58,13 @@ impl StorageBackend for SledBackend {
}
}
pub struct SledBackendHandle<'a> {
pub struct SledTx<'a> {
ref_deltas: HashMap<(u64, u64, u64), i64>,
cache: HashMap<RcKey, StorageEntryPtr>,
tx: &'a sled::transaction::TransactionalTree,
}
impl<'a, E> StorageBackendHandle<'a, sled::transaction::ConflictableTransactionError<E>>
for SledBackendHandle<'a>
{
impl<'a, E> StorageTx<'a, sled::transaction::ConflictableTransactionError<E>> for SledTx<'a> {
fn ref_deltas(&mut self) -> &mut HashMap<(u64, u64, u64), i64> {
&mut self.ref_deltas
}

View File

@@ -1,6 +1,6 @@
use crate::storage_backend_handle::StorageBackendHandle;
use crate::storage_entity::StorageEntity;
use crate::storage_ptr::{tmp_at_ptr, tmp_count_ptr, StorageEntryPtr, StorageHeadPtr};
use crate::storage_tx::StorageTx;
use crate::StorageBackend;
pub struct Storage<SB: StorageBackend> {
@@ -12,14 +12,14 @@ impl<SB: StorageBackend> Storage<SB> {
Self { sb }
}
pub fn get_head<SE: for<'a> StorageEntity<'a, SB::InTransactionError<()>, SB::Handle<'a, ()>>>(
pub fn get_head<SE: for<'a> StorageEntity<'a, SB::InTransactionError<()>, SB::Tx<'a, ()>>>(
&mut self,
ptr: StorageHeadPtr,
) -> Result<Option<SE>, SB::Error<()>> {
self.sb.transaction(|sb| sb.get_head(ptr))
}
pub fn set_head<SE: for<'a> StorageEntity<'a, SB::InTransactionError<()>, SB::Handle<'a, ()>>>(
pub fn set_head<SE: for<'a> StorageEntity<'a, SB::InTransactionError<()>, SB::Tx<'a, ()>>>(
&mut self,
ptr: StorageHeadPtr,
value: &SE,
@@ -31,9 +31,7 @@ impl<SB: StorageBackend> Storage<SB> {
self.sb.transaction(|sb| sb.remove_head(ptr))
}
pub fn store_tmp<
SE: for<'a> StorageEntity<'a, SB::InTransactionError<()>, SB::Handle<'a, ()>>,
>(
pub fn store_tmp<SE: for<'a> StorageEntity<'a, SB::InTransactionError<()>, SB::Tx<'a, ()>>>(
&mut self,
value: &SE,
) -> Result<StorageEntryPtr, SB::Error<()>> {

View File

@@ -1,15 +1,15 @@
use std::fmt::Debug as DebugTrait;
use crate::storage_backend_handle::StorageBackendHandle;
use crate::storage_tx::StorageTx;
pub trait StorageBackend {
type Error<E: DebugTrait>: DebugTrait;
type InTransactionError<E>;
type Handle<'a, E>: StorageBackendHandle<'a, Self::InTransactionError<E>>;
type Tx<'a, E>: StorageTx<'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>>;
F: Fn(&mut Self::Tx<'_, E>) -> Result<T, Self::InTransactionError<E>>;
fn is_empty(&self) -> bool;

View File

@@ -1,6 +1,6 @@
use crate::{storage_backend_handle::StorageBackendHandle, storage_entry::StorageEntry};
use crate::{storage_entry::StorageEntry, storage_tx::StorageTx};
pub trait StorageEntity<'a, E, Tx: StorageBackendHandle<'a, E>>: Sized {
pub trait StorageEntity<'a, E, Tx: StorageTx<'a, E>>: Sized {
fn to_storage_entry(&self, tx: &mut Tx) -> Result<StorageEntry, E>;
fn from_storage_entry(tx: &mut Tx, entry: StorageEntry) -> Result<Self, E>;
}

View File

@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::{RcKey, StorageEntity, StorageEntryPtr, StorageHeadPtr, StoragePtr};
pub trait StorageBackendHandle<'a, E>: Sized {
pub trait StorageTx<'a, E>: Sized {
fn ref_deltas(&mut self) -> &mut HashMap<(u64, u64, u64), i64>;
fn cache(&mut self) -> &mut HashMap<RcKey, StorageEntryPtr>;
fn read_bytes<T>(&self, ptr: StoragePtr<T>) -> Result<Option<Vec<u8>>, E>;