use crate::database::{ database_description::DatabaseDescription, Database, }; use fuel_core_storage::{ structured_storage::StructuredStorage, Error as StorageError, Mappable, MerkleRoot, MerkleRootStorage, Result as StorageResult, StorageAsRef, StorageInspect, StorageRead, StorageSize, }; use std::borrow::Cow; #[cfg(feature = "test-helpers")] use fuel_core_storage::transactional::{ ConflictPolicy, Modifiable, StorageTransaction, }; #[cfg(feature = "test-helpers")] use fuel_core_storage::{ StorageAsMut, StorageBatchMutate, StorageMutate, StorageWrite, }; impl StorageInspect for Database where Description: DatabaseDescription, M: Mappable, for<'a> StructuredStorage<&'a Self>: StorageInspect, { type Error = StorageError; fn get(&self, key: &M::Key) -> StorageResult>> { let storage = StructuredStorage::new(self); let value = storage.storage::().get(key)?; if let Some(cow) = value { Ok(Some(Cow::Owned(cow.into_owned()))) } else { Ok(None) } } fn contains_key(&self, key: &M::Key) -> StorageResult { StructuredStorage::new(self) .storage::() .contains_key(key) } } #[cfg(feature = "test-helpers")] impl StorageMutate for Database where Description: DatabaseDescription, M: Mappable, for<'a> StructuredStorage<&'a Self>: StorageInspect, for<'a> StorageTransaction<&'a Self>: StorageMutate, Self: Modifiable, { fn insert( &mut self, key: &M::Key, value: &M::Value, ) -> StorageResult> { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); let prev = transaction.storage_as_mut::().insert(key, value)?; self.commit_changes(transaction.into_changes())?; Ok(prev) } fn remove(&mut self, key: &M::Key) -> StorageResult> { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); let prev = transaction.storage_as_mut::().remove(key)?; self.commit_changes(transaction.into_changes())?; Ok(prev) } } impl StorageSize for Database where Description: DatabaseDescription, M: Mappable, for<'a> StructuredStorage<&'a Self>: StorageSize, { fn size_of_value(&self, key: &M::Key) -> StorageResult> { <_ as StorageSize>::size_of_value(&StructuredStorage::new(self), key) } } impl MerkleRootStorage for Database where Description: DatabaseDescription, M: Mappable, for<'a> StructuredStorage<&'a Self>: MerkleRootStorage, { fn root(&self, key: &Key) -> StorageResult { StructuredStorage::new(self).storage::().root(key) } } impl StorageRead for Database where Description: DatabaseDescription, M: Mappable, for<'a> StructuredStorage<&'a Self>: StorageRead, { fn read(&self, key: &M::Key, buf: &mut [u8]) -> StorageResult> { StructuredStorage::new(self).storage::().read(key, buf) } fn read_alloc(&self, key: &M::Key) -> StorageResult>> { StructuredStorage::new(self).storage::().read_alloc(key) } } #[cfg(feature = "test-helpers")] impl StorageWrite for Database where Description: DatabaseDescription, M: Mappable, for<'a> StructuredStorage<&'a Self>: StorageInspect, for<'a> StorageTransaction<&'a Self>: StorageWrite, Self: Modifiable, { fn write(&mut self, key: &M::Key, buf: &[u8]) -> Result { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); let prev = <_ as StorageWrite>::write(&mut transaction, key, buf)?; self.commit_changes(transaction.into_changes())?; Ok(prev) } fn replace( &mut self, key: &M::Key, buf: &[u8], ) -> Result<(usize, Option>), Self::Error> { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); let prev = <_ as StorageWrite>::replace(&mut transaction, key, buf)?; self.commit_changes(transaction.into_changes())?; Ok(prev) } fn take(&mut self, key: &M::Key) -> Result>, Self::Error> { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); let prev = <_ as StorageWrite>::take(&mut transaction, key)?; self.commit_changes(transaction.into_changes())?; Ok(prev) } } #[cfg(feature = "test-helpers")] impl StorageBatchMutate for Database where Description: DatabaseDescription, M: Mappable, for<'a> StructuredStorage<&'a Self>: StorageInspect, for<'a> StorageTransaction<&'a Self>: StorageBatchMutate, Self: Modifiable, { fn init_storage<'a, Iter>(&mut self, set: Iter) -> StorageResult<()> where Iter: 'a + Iterator, M::Key: 'a, M::Value: 'a, { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); StorageBatchMutate::init_storage(&mut transaction, set)?; self.commit_changes(transaction.into_changes())?; Ok(()) } fn insert_batch<'a, Iter>(&mut self, set: Iter) -> StorageResult<()> where Iter: 'a + Iterator, M::Key: 'a, M::Value: 'a, { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); StorageBatchMutate::insert_batch(&mut transaction, set)?; self.commit_changes(transaction.into_changes())?; Ok(()) } fn remove_batch<'a, Iter>(&mut self, set: Iter) -> StorageResult<()> where Iter: 'a + Iterator, M::Key: 'a, { let mut transaction = StorageTransaction::transaction( &*self, ConflictPolicy::Overwrite, Default::default(), ); StorageBatchMutate::remove_batch(&mut transaction, set)?; self.commit_changes(transaction.into_changes())?; Ok(()) } }