Skip to content

Commit a3f03fc

Browse files
committed
starknet_patricia_storage: move is_gatherable to ReadOnlyStorage
1 parent 1741bec commit a3f03fc

8 files changed

Lines changed: 45 additions & 33 deletions

File tree

crates/starknet_patricia_storage/src/aerospike_storage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ impl ReadOnlyStorage for AerospikeStorage {
173173
async fn mget_mut(&mut self, keys: &[&DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> {
174174
ImmutableReadOnlyStorage::mget(self, keys).await
175175
}
176+
177+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
178+
Some(self)
179+
}
176180
}
177181

178182
impl Storage for AerospikeStorage {
@@ -256,10 +260,6 @@ impl Storage for AerospikeStorage {
256260
fn get_async_self(&self) -> Option<impl AsyncStorage> {
257261
Some(self.clone())
258262
}
259-
260-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
261-
Some(self)
262-
}
263263
}
264264

265265
impl GatherableStorage for AerospikeStorage {}

crates/starknet_patricia_storage/src/map_storage.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ impl ReadOnlyStorage for MapStorage {
6060
async fn mget_mut(&mut self, keys: &[&DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> {
6161
ImmutableReadOnlyStorage::mget(self, keys).await
6262
}
63+
64+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
65+
Some(self)
66+
}
6367
}
6468

6569
impl Storage for MapStorage {
@@ -102,10 +106,6 @@ impl Storage for MapStorage {
102106
// Need a concrete Option type.
103107
None::<NullStorage>
104108
}
105-
106-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
107-
Some(self)
108-
}
109109
}
110110

111111
impl GatherableStorage for MapStorage {}
@@ -303,7 +303,7 @@ impl<S: Storage + ImmutableReadOnlyStorage + 'static> GatherableStorage for Cach
303303
}
304304

305305
// TODO(Nimrod): Find a way to share the implementation with `ImmutableReadOnlyStorage`.
306-
impl<S: Storage> ReadOnlyStorage for CachedStorage<S> {
306+
impl<S: Storage + ImmutableReadOnlyStorage + 'static> ReadOnlyStorage for CachedStorage<S> {
307307
async fn get_mut(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
308308
self.reads.fetch_add(1, Ordering::Relaxed);
309309
if let Some(cached_value) = self.cache.get(key) {
@@ -346,6 +346,10 @@ impl<S: Storage> ReadOnlyStorage for CachedStorage<S> {
346346

347347
Ok(values)
348348
}
349+
350+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
351+
Some(self)
352+
}
349353
}
350354

351355
impl<S: Storage + ImmutableReadOnlyStorage + 'static> Storage for CachedStorage<S> {
@@ -420,8 +424,4 @@ impl<S: Storage + ImmutableReadOnlyStorage + 'static> Storage for CachedStorage<
420424
// Need a concrete Option type.
421425
None::<NullStorage>
422426
}
423-
424-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
425-
Some(self)
426-
}
427427
}

crates/starknet_patricia_storage/src/mdbx_storage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ impl ReadOnlyStorage for MdbxStorage {
132132
async fn mget_mut(&mut self, keys: &[&DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> {
133133
ImmutableReadOnlyStorage::mget(self, keys).await
134134
}
135+
136+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
137+
Some(self)
138+
}
135139
}
136140

137141
impl Storage for MdbxStorage {
@@ -191,10 +195,6 @@ impl Storage for MdbxStorage {
191195
fn get_async_self(&self) -> Option<impl AsyncStorage> {
192196
Some(self.clone())
193197
}
194-
195-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
196-
Some(self)
197-
}
198198
}
199199

200200
impl GatherableStorage for MdbxStorage {}

crates/starknet_patricia_storage/src/reads_collector_storage.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::storage_trait::{
22
DbHashMap,
33
DbKey,
44
DbValue,
5+
GatherableStorage,
56
ImmutableReadOnlyStorage,
7+
NullStorage,
68
PatriciaStorageResult,
79
ReadOnlyStorage,
810
};
@@ -43,4 +45,8 @@ impl<'a, S: ImmutableReadOnlyStorage> ReadOnlyStorage for ReadsCollectorStorage<
4345
}
4446
Ok(values)
4547
}
48+
49+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
50+
None::<&mut NullStorage>
51+
}
4652
}

crates/starknet_patricia_storage/src/rocksdb_storage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ impl ReadOnlyStorage for RocksDbStorage {
331331
async fn mget_mut(&mut self, keys: &[&DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> {
332332
ImmutableReadOnlyStorage::mget(self, keys).await
333333
}
334+
335+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
336+
Some(self)
337+
}
334338
}
335339

336340
impl Storage for RocksDbStorage {
@@ -376,10 +380,6 @@ impl Storage for RocksDbStorage {
376380
fn get_async_self(&self) -> Option<impl AsyncStorage> {
377381
Some(self.clone())
378382
}
379-
380-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
381-
Some(self)
382-
}
383383
}
384384

385385
impl GatherableStorage for RocksDbStorage {}

crates/starknet_patricia_storage/src/short_key_storage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ macro_rules! define_short_key_storage {
8282
.mget_mut(small_keys.iter().collect::<Vec<&DbKey>>().as_slice())
8383
.await
8484
}
85+
86+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
87+
None::<&mut NullStorage>
88+
}
8589
}
8690

8791
impl<S: Storage> Storage for $name<S> {
@@ -123,10 +127,6 @@ macro_rules! define_short_key_storage {
123127
fn get_async_self(&self) -> Option<impl AsyncStorage> {
124128
Some($name::new(self.storage.get_async_self()?))
125129
}
126-
127-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
128-
None::<&mut NullStorage>
129-
}
130130
}
131131

132132
impl<S: AsyncStorage> Clone for $name<S> {

crates/starknet_patricia_storage/src/storage_trait.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ pub trait ReadOnlyStorage: Send + Sync {
197197
&mut self,
198198
keys: &[&DbKey],
199199
) -> impl Future<Output = PatriciaStorageResult<Vec<Option<DbValue>>>> + Send;
200+
201+
/// If the storage supports concurrent task execution via [GatherableStorage::gather], returns
202+
/// a mutable reference to it. Returns `None` otherwise.
203+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage>;
200204
}
201205

202206
/// A trait for the storage. Extends [ReadOnlyStorage] with write operations.
@@ -248,10 +252,6 @@ pub trait Storage: ReadOnlyStorage {
248252

249253
/// If the storage is async, returns an instance of the async storage.
250254
fn get_async_self(&self) -> Option<impl AsyncStorage>;
251-
252-
/// If the storage supports concurrent task execution via [GatherableStorage::gather], returns
253-
/// a mutable reference to it. Returns `None` otherwise.
254-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage>;
255255
}
256256

257257
/// A trait wrapper for [Storage] that supports concurrency.
@@ -299,6 +299,10 @@ impl ReadOnlyStorage for NullStorage {
299299
async fn mget_mut(&mut self, keys: &[&DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> {
300300
ImmutableReadOnlyStorage::mget(self, keys).await
301301
}
302+
303+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
304+
Some(self)
305+
}
302306
}
303307

304308
impl Storage for NullStorage {
@@ -331,10 +335,6 @@ impl Storage for NullStorage {
331335
fn get_async_self(&self) -> Option<impl AsyncStorage> {
332336
Some(self.clone())
333337
}
334-
335-
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
336-
Some(self)
337-
}
338338
}
339339

340340
impl GatherableStorage for NullStorage {}

crates/starknet_patricia_storage/src/two_layer_storage.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::storage_trait::{
22
DbKey,
33
DbValue,
4+
GatherableStorage,
45
ImmutableReadOnlyStorage,
6+
NullStorage,
57
PatriciaStorageResult,
68
ReadOnlyStorage,
79
};
@@ -67,4 +69,8 @@ where
6769
}
6870
Ok(out)
6971
}
72+
73+
fn as_gatherable_storage(&mut self) -> Option<&mut impl GatherableStorage> {
74+
None::<&mut NullStorage>
75+
}
7076
}

0 commit comments

Comments
 (0)