From b2f523a83ca23743cd9559020aba47b23749ecfc Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Tue, 31 Oct 2023 16:17:19 +1100 Subject: [PATCH] Handle -> Tx --- storage/src/demo_val.rs | 13 +++++-------- storage/src/lib.rs | 4 ++-- storage/src/memory_backend.rs | 13 ++++++------- storage/src/sled_backend.rs | 15 ++++++--------- storage/src/storage.rs | 10 ++++------ storage/src/storage_backend.rs | 6 +++--- storage/src/storage_entity.rs | 4 ++-- .../{storage_backend_handle.rs => storage_tx.rs} | 2 +- valuescript_vm/src/bytecode.rs | 4 ++-- valuescript_vm/src/val_storage.rs | 14 +++++++------- 10 files changed, 38 insertions(+), 47 deletions(-) rename storage/src/{storage_backend_handle.rs => storage_tx.rs} (98%) diff --git a/storage/src/demo_val.rs b/storage/src/demo_val.rs index 380509f..40a24f7 100644 --- a/storage/src/demo_val.rs +++ b/storage/src/demo_val.rs @@ -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 { @@ -87,10 +87,7 @@ impl DemoVal { }) } - fn numbers_impl<'a, E, Tx: StorageBackendHandle<'a, E>>( - &self, - tx: &mut Tx, - ) -> Result, E> { + fn numbers_impl<'a, E, Tx: StorageTx<'a, E>>(&self, tx: &mut Tx) -> Result, 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 { let mut entry = StorageEntry { ref_count: 1, diff --git a/storage/src/lib.rs b/storage/src/lib.rs index 71759e1..39e61f1 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -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; diff --git a/storage/src/memory_backend.rs b/storage/src/memory_backend.rs index 46efcae..b50f653 100644 --- a/storage/src/memory_backend.rs +++ b/storage/src/memory_backend.rs @@ -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; type InTransactionError = E; - type Handle<'a, E> = MemoryStorageHandle<'a>; + type Tx<'a, E> = MemoryTx<'a>; fn transaction(&mut self, f: F) -> Result> where - F: Fn(&mut Self::Handle<'_, E>) -> Result>, + F: Fn(&mut Self::Tx<'_, E>) -> Result>, { - 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, 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 } diff --git a/storage/src/sled_backend.rs b/storage/src/sled_backend.rs index d52b842..9bf6623 100644 --- a/storage/src/sled_backend.rs +++ b/storage/src/sled_backend.rs @@ -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 = sled::transaction::TransactionError; type InTransactionError = sled::transaction::ConflictableTransactionError; - type Handle<'a, E> = SledBackendHandle<'a>; + type Tx<'a, E> = SledTx<'a>; fn transaction(&mut self, f: F) -> Result> where - F: Fn(&mut Self::Handle<'_, E>) -> Result>, + F: Fn(&mut Self::Tx<'_, E>) -> Result>, { 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, tx: &'a sled::transaction::TransactionalTree, } -impl<'a, E> StorageBackendHandle<'a, sled::transaction::ConflictableTransactionError> - for SledBackendHandle<'a> -{ +impl<'a, E> StorageTx<'a, sled::transaction::ConflictableTransactionError> for SledTx<'a> { fn ref_deltas(&mut self) -> &mut HashMap<(u64, u64, u64), i64> { &mut self.ref_deltas } diff --git a/storage/src/storage.rs b/storage/src/storage.rs index d0fc1ed..65cc2f0 100644 --- a/storage/src/storage.rs +++ b/storage/src/storage.rs @@ -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 { @@ -12,14 +12,14 @@ impl Storage { Self { sb } } - pub fn get_head StorageEntity<'a, SB::InTransactionError<()>, SB::Handle<'a, ()>>>( + pub fn get_head StorageEntity<'a, SB::InTransactionError<()>, SB::Tx<'a, ()>>>( &mut self, ptr: StorageHeadPtr, ) -> Result, SB::Error<()>> { self.sb.transaction(|sb| sb.get_head(ptr)) } - pub fn set_head StorageEntity<'a, SB::InTransactionError<()>, SB::Handle<'a, ()>>>( + pub fn set_head StorageEntity<'a, SB::InTransactionError<()>, SB::Tx<'a, ()>>>( &mut self, ptr: StorageHeadPtr, value: &SE, @@ -31,9 +31,7 @@ impl Storage { 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 StorageEntity<'a, SB::InTransactionError<()>, SB::Tx<'a, ()>>>( &mut self, value: &SE, ) -> Result> { diff --git a/storage/src/storage_backend.rs b/storage/src/storage_backend.rs index 14d237e..db42ca0 100644 --- a/storage/src/storage_backend.rs +++ b/storage/src/storage_backend.rs @@ -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: DebugTrait; type InTransactionError; - type Handle<'a, E>: StorageBackendHandle<'a, Self::InTransactionError>; + type Tx<'a, E>: StorageTx<'a, Self::InTransactionError>; fn transaction(&mut self, f: F) -> Result> where - F: Fn(&mut Self::Handle<'_, E>) -> Result>; + F: Fn(&mut Self::Tx<'_, E>) -> Result>; fn is_empty(&self) -> bool; diff --git a/storage/src/storage_entity.rs b/storage/src/storage_entity.rs index 1ee5f69..65687e7 100644 --- a/storage/src/storage_entity.rs +++ b/storage/src/storage_entity.rs @@ -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; fn from_storage_entry(tx: &mut Tx, entry: StorageEntry) -> Result; } diff --git a/storage/src/storage_backend_handle.rs b/storage/src/storage_tx.rs similarity index 98% rename from storage/src/storage_backend_handle.rs rename to storage/src/storage_tx.rs index 00e2989..d58acb6 100644 --- a/storage/src/storage_backend_handle.rs +++ b/storage/src/storage_tx.rs @@ -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; fn read_bytes(&self, ptr: StoragePtr) -> Result>, E>; diff --git a/valuescript_vm/src/bytecode.rs b/valuescript_vm/src/bytecode.rs index 7f1610d..46ff038 100644 --- a/valuescript_vm/src/bytecode.rs +++ b/valuescript_vm/src/bytecode.rs @@ -1,6 +1,6 @@ use std::{cell::RefCell, collections::HashMap, fmt, ops::Index, rc::Rc, slice::SliceIndex}; -use storage::{StorageBackendHandle, StorageEntity}; +use storage::{StorageEntity, StorageTx}; use crate::{bytecode_decoder::BytecodeDecoder, vs_value::Val}; @@ -45,7 +45,7 @@ impl DecoderMaker for Rc { } } -impl<'a, E, Tx: StorageBackendHandle<'a, E>> StorageEntity<'a, E, Tx> for Bytecode { +impl<'a, E, Tx: StorageTx<'a, E>> StorageEntity<'a, E, Tx> for Bytecode { fn to_storage_entry(&self, _tx: &mut Tx) -> Result { Ok(storage::StorageEntry { ref_count: 1, diff --git a/valuescript_vm/src/val_storage.rs b/valuescript_vm/src/val_storage.rs index 2a43c44..5e61f3b 100644 --- a/valuescript_vm/src/val_storage.rs +++ b/valuescript_vm/src/val_storage.rs @@ -4,7 +4,7 @@ use num_bigint::BigInt; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive}; use storage::{ - RcKey, StorageBackendHandle, StorageEntity, StorageEntry, StorageEntryReader, StorageEntryWriter, + RcKey, StorageEntity, StorageEntry, StorageEntryReader, StorageEntryWriter, StorageTx, }; use crate::{ @@ -47,7 +47,7 @@ impl Tag { } } -impl<'a, E, Tx: StorageBackendHandle<'a, E>> StorageEntity<'a, E, Tx> for Val { +impl<'a, E, Tx: StorageTx<'a, E>> StorageEntity<'a, E, Tx> for Val { fn from_storage_entry(tx: &mut Tx, entry: StorageEntry) -> Result { let mut reader = StorageEntryReader::new(&entry); let res = read_from_entry(tx, &mut reader); @@ -156,7 +156,7 @@ impl<'a, E, Tx: StorageBackendHandle<'a, E>> StorageEntity<'a, E, Tx> for Val { } } -fn write_to_entry<'a, E, Tx: StorageBackendHandle<'a, E>>( +fn write_to_entry<'a, E, Tx: StorageTx<'a, E>>( val: &Val, tx: &mut Tx, writer: &mut StorageEntryWriter, @@ -246,7 +246,7 @@ fn write_to_entry<'a, E, Tx: StorageBackendHandle<'a, E>>( Ok(()) } -fn write_ptr_to_entry<'a, E, Tx: StorageBackendHandle<'a, E>>( +fn write_ptr_to_entry<'a, E, Tx: StorageTx<'a, E>>( tx: &mut Tx, writer: &mut StorageEntryWriter, key: RcKey, @@ -264,7 +264,7 @@ fn write_ptr_to_entry<'a, E, Tx: StorageBackendHandle<'a, E>>( 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 { @@ -422,7 +422,7 @@ fn read_symbol_from_entry(reader: &mut StorageEntryReader) -> VsSymbol { FromPrimitive::from_u64(reader.read_vlq().unwrap()).unwrap() } -fn read_ref_bytecode_from_entry<'a, E, Tx: StorageBackendHandle<'a, E>>( +fn read_ref_bytecode_from_entry<'a, E, Tx: StorageTx<'a, E>>( tx: &mut Tx, reader: &mut StorageEntryReader, ) -> Result, E> { @@ -433,7 +433,7 @@ fn read_ref_bytecode_from_entry<'a, E, Tx: StorageBackendHandle<'a, E>>( Ok(Rc::new(Bytecode::from_storage_entry(tx, entry)?)) } -fn write_ref_bytecode_to_entry<'a, E, Tx: StorageBackendHandle<'a, E>>( +fn write_ref_bytecode_to_entry<'a, E, Tx: StorageTx<'a, E>>( tx: &mut Tx, writer: &mut StorageEntryWriter, bytecode: &Rc,