diff --git a/crates/fuel-core/src/service/adapters/graphql_api/on_chain.rs b/crates/fuel-core/src/service/adapters/graphql_api/on_chain.rs index d6bbb27486..ed5fcdfac5 100644 --- a/crates/fuel-core/src/service/adapters/graphql_api/on_chain.rs +++ b/crates/fuel-core/src/service/adapters/graphql_api/on_chain.rs @@ -109,7 +109,7 @@ impl DatabaseMessages for OnChainIterableKeyValueView { &'a self, ids: BoxedIter<'a, &'a Nonce>, ) -> BoxedIter<'a, StorageResult> { - self.get_batch(ids) + >::get_batch(self, ids) .map(|result| result.and_then(|opt| opt.ok_or(not_found!(Messages)))) .into_boxed() } diff --git a/crates/fuel-core/src/state/data_source.rs b/crates/fuel-core/src/state/data_source.rs index cf107d0e1b..10ff17b504 100644 --- a/crates/fuel-core/src/state/data_source.rs +++ b/crates/fuel-core/src/state/data_source.rs @@ -44,6 +44,7 @@ where impl KeyValueInspect for DataSource where Description: DatabaseDescription, + Stage: Send + Sync, { type Column = Description::Column; @@ -76,6 +77,7 @@ where impl IterableStore for DataSource where Description: DatabaseDescription, + Stage: Send + Sync, { fn iter_store( &self, diff --git a/crates/fuel-core/src/state/generic_database.rs b/crates/fuel-core/src/state/generic_database.rs index e6eeee4dd1..ee82958abc 100644 --- a/crates/fuel-core/src/state/generic_database.rs +++ b/crates/fuel-core/src/state/generic_database.rs @@ -72,9 +72,8 @@ where { fn get_batch<'a>( &'a self, - keys: Box::Key> + 'a>, - ) -> Box::OwnedValue>>> + 'a> - { + keys: BoxedIter<'a, &'a M::Key>, + ) -> BoxedIter<'a, StorageResult::OwnedValue>>> { self.storage.get_batch(keys) } } diff --git a/crates/fuel-core/src/state/rocks_db.rs b/crates/fuel-core/src/state/rocks_db.rs index 17fb66a4ab..d902057c0a 100644 --- a/crates/fuel-core/src/state/rocks_db.rs +++ b/crates/fuel-core/src/state/rocks_db.rs @@ -724,16 +724,15 @@ where fn get_multi<'a>( &'a self, - keys: Box> + 'a>, + keys: BoxedIter<'a, Vec>, column: Self::Column, - ) -> Box>> + 'a> { + ) -> BoxedIter<'a, StorageResult>> { // TODO: Metrics let column = self.cf(column); let keys = keys.map(|key| (&column, key)); - let values = self - .db + self.db .multi_get_cf_opt(keys, &self.read_options) .into_iter() .map(|value_opt| { @@ -742,9 +741,8 @@ where StorageError::Other(DatabaseError::Other(e.into()).into()) }) .map(|value| value.map(Arc::new)) - }); - - Box::new(values) + }) + .into_boxed() } fn read( diff --git a/crates/storage/src/blueprint.rs b/crates/storage/src/blueprint.rs index 282a7f90ab..aa6a0e5765 100644 --- a/crates/storage/src/blueprint.rs +++ b/crates/storage/src/blueprint.rs @@ -9,6 +9,10 @@ use crate::{ Encode, Encoder, }, + iter::{ + BoxedIter, + IntoBoxedIter, + }, kv_store::{ BatchOperations, KeyValueInspect, @@ -19,9 +23,6 @@ use crate::{ }; use fuel_vm_private::prelude::MerkleRoot; -#[cfg(feature = "alloc")] -use alloc::boxed::Box; - pub mod merklized; pub mod plain; pub mod sparse; @@ -82,22 +83,25 @@ where /// Returns multiple values from the storage. fn get_multi<'a>( storage: &'a S, - keys: Box + 'a>, + keys: BoxedIter<'a, &'a M::Key>, column: S::Column, - ) -> Box>> + 'a> { - let keys = Box::new( - keys.map(|key| Self::KeyCodec::encode(&key).as_bytes().into_owned()), - ); - - Box::new(storage.get_multi(keys, column).map(|result| { - result.and_then(|opt| { - opt.map(|value| { - Self::ValueCodec::decode_from_value(value) - .map_err(crate::Error::Codec) + ) -> BoxedIter<'a, StorageResult>> { + let keys = keys + .map(|key| Self::KeyCodec::encode(&key).as_bytes().into_owned()) + .into_boxed(); + + storage + .get_multi(keys, column) + .map(|result| { + result.and_then(|opt| { + opt.map(|value| { + Self::ValueCodec::decode_from_value(value) + .map_err(crate::Error::Codec) + }) + .transpose() }) - .transpose() }) - })) + .into_boxed() } } diff --git a/crates/storage/src/kv_store.rs b/crates/storage/src/kv_store.rs index b69522e055..f54586f437 100644 --- a/crates/storage/src/kv_store.rs +++ b/crates/storage/src/kv_store.rs @@ -1,6 +1,10 @@ //! The module provides plain abstract definition of the key-value store. use crate::{ + iter::{ + BoxedIter, + IntoBoxedIter, + }, Error as StorageError, Result as StorageResult, }; @@ -26,7 +30,7 @@ pub type KVItem = StorageResult<(Key, Value)>; pub type KeyItem = StorageResult; /// A column of the storage. -pub trait StorageColumn: Copy + core::fmt::Debug { +pub trait StorageColumn: Copy + core::fmt::Debug + Send + Sync { /// Returns the name of the column. fn name(&self) -> String; @@ -41,7 +45,7 @@ pub trait StorageColumn: Copy + core::fmt::Debug { /// The definition of the key-value inspection store. #[impl_tools::autoimpl(for &T, &mut T, Box)] -pub trait KeyValueInspect { +pub trait KeyValueInspect: Send + Sync { /// The type of the column. type Column: StorageColumn; @@ -65,10 +69,10 @@ pub trait KeyValueInspect { /// Returns multiple values from the storage. fn get_multi<'a>( &'a self, - keys: Box> + 'a>, + keys: BoxedIter<'a, Vec>, column: Self::Column, - ) -> Box>> + 'a> { - Box::new(keys.map(move |key| self.get(&key, column))) + ) -> BoxedIter<'a, StorageResult>> { + keys.map(move |key| self.get(&key, column)).into_boxed() } /// Reads the value from the storage into the `buf` and returns the number of read bytes. diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 8b6fa2604f..185785cd7b 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -17,6 +17,7 @@ extern crate alloc; use anyhow::anyhow; use core::array::TryFromSliceError; use fuel_core_types::services::executor::Error as ExecutorError; +use iter::BoxedIter; #[cfg(feature = "alloc")] use alloc::{ @@ -182,8 +183,8 @@ pub trait StorageBatchInspect { /// TODO fn get_batch<'a>( &'a self, - keys: Box + 'a>, - ) -> Box>> + 'a>; + keys: BoxedIter<'a, &'a Type::Key>, + ) -> BoxedIter<'a, Result>>; } /// Creates `StorageError::NotFound` error with file and line information inside. diff --git a/crates/storage/src/structured_storage.rs b/crates/storage/src/structured_storage.rs index 9fe78dbdb5..9cc97270ca 100644 --- a/crates/storage/src/structured_storage.rs +++ b/crates/storage/src/structured_storage.rs @@ -59,9 +59,6 @@ use alloc::{ fmt::Debug, }; -// TODO: Format -#[cfg(feature = "alloc")] -use alloc::boxed::Box; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -281,8 +278,8 @@ where { fn get_batch<'a>( &'a self, - keys: Box + 'a>, - ) -> Box>> + 'a> { + keys: BoxedIter<'a, &'a M::Key>, + ) -> BoxedIter<'a, StorageResult>> { M::Blueprint::get_multi(self, keys, M::column()) } }