Skip to content

Commit b1f2bf4

Browse files
rymncDentosalxgreenx
authored
feat(iterators): allow key-only iteration (#2092)
## Linked Issues/PRs <!-- List of related issues/PRs --> - #1997 - #2076 ## Description <!-- List of detailed changes --> Implements `iter_store_keys` and `iter_keys` appropriately for in memory stores and rocksdb. The previous PR made them call it, and this one changes the underlying implementation to avoid value allocations. ## Checklist - [x] Breaking changes are clearly marked as such in the PR description and changelog (no breaking changes) - [x] New behavior is reflected in tests (tests which test this behaviour under the hood still pass) - [x] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (no changes to spec) ### Before requesting review - [x] I have reviewed the code myself - [x] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams [Add or remove entries as needed] - [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) - [ ] [Sway compiler](https://github.com/FuelLabs/sway/) - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) (for out-of-organization contributors, the person merging the PR will do this) - [ ] Someone else? --------- Co-authored-by: Hannes Karppila <[email protected]> Co-authored-by: green <[email protected]>
1 parent 08f4637 commit b1f2bf4

File tree

16 files changed

+486
-103
lines changed

16 files changed

+486
-103
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
### Added
1010
- [2051](https://github.com/FuelLabs/fuel-core/pull/2051): Add support for AWS KMS signing for the PoA consensus module. The new key can be specified with `--consensus-aws-kms AWS_KEY_ARN`.
11+
- [2092](https://github.com/FuelLabs/fuel-core/pull/2092): Allow iterating by keys in rocksdb, and other storages.
1112

1213
### Changed
1314

1415
#### Breaking
1516
- [2051](https://github.com/FuelLabs/fuel-core/pull/2051): Misdocumented `CONSENSUS_KEY` environ variable has been removed, use `CONSENSUS_KEY_SECRET` instead. Also raises MSRV to `1.79.0`.
1617

18+
1719
## [Version 0.33.0]
1820

1921
### Added

crates/fuel-core/src/service/genesis/importer/import_task.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ mod tests {
172172
},
173173
kv_store::{
174174
KVItem,
175+
KeyItem,
175176
KeyValueInspect,
176177
Value,
177178
},
@@ -553,6 +554,16 @@ mod tests {
553554
) -> BoxedIter<KVItem> {
554555
unimplemented!()
555556
}
557+
558+
fn iter_store_keys(
559+
&self,
560+
_: Self::Column,
561+
_: Option<&[u8]>,
562+
_: Option<&[u8]>,
563+
_: IterDirection,
564+
) -> BoxedIter<KeyItem> {
565+
unimplemented!()
566+
}
556567
}
557568

558569
impl TransactableStorage<BlockHeight> for BrokenTransactions {

crates/fuel-core/src/state.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub mod iterable_key_value_view;
3434
pub mod key_value_view;
3535
#[cfg(feature = "rocksdb")]
3636
pub mod rocks_db;
37+
#[cfg(feature = "rocksdb")]
38+
pub mod rocks_db_key_iterator;
3739

3840
pub type ColumnType<Description> = <Description as DatabaseDescription>::Column;
3941

@@ -157,4 +159,28 @@ where
157159
core::iter::empty().into_boxed()
158160
}
159161
}
162+
163+
fn iter_store_keys(
164+
&self,
165+
column: Self::Column,
166+
prefix: Option<&[u8]>,
167+
start: Option<&[u8]>,
168+
direction: IterDirection,
169+
) -> BoxedIter<fuel_core_storage::kv_store::KeyItem> {
170+
// We cannot define iter_store_keys appropriately for the `ChangesIterator`,
171+
// because we have to filter out the keys that were removed, which are
172+
// marked as `WriteOperation::Remove` in the value
173+
// copied as-is from the above function, but only to return keys
174+
if let Some(tree) = self.changes.get(&column.id()) {
175+
fuel_core_storage::iter::iterator(tree, prefix, start, direction)
176+
.filter_map(|(key, value)| match value {
177+
WriteOperation::Insert(_) => Some(key.clone().into()),
178+
WriteOperation::Remove => None,
179+
})
180+
.map(Ok)
181+
.into_boxed()
182+
} else {
183+
core::iter::empty().into_boxed()
184+
}
185+
}
160186
}

crates/fuel-core/src/state/data_source.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,14 @@ where
8686
) -> BoxedIter<KVItem> {
8787
self.data.iter_store(column, prefix, start, direction)
8888
}
89+
90+
fn iter_store_keys(
91+
&self,
92+
column: Self::Column,
93+
prefix: Option<&[u8]>,
94+
start: Option<&[u8]>,
95+
direction: IterDirection,
96+
) -> BoxedIter<fuel_core_storage::kv_store::KeyItem> {
97+
self.data.iter_store_keys(column, prefix, start, direction)
98+
}
8999
}

crates/fuel-core/src/state/generic_database.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ where
138138
) -> BoxedIter<KVItem> {
139139
self.storage.iter_store(column, prefix, start, direction)
140140
}
141+
142+
fn iter_store_keys(
143+
&self,
144+
column: Self::Column,
145+
prefix: Option<&[u8]>,
146+
start: Option<&[u8]>,
147+
direction: IterDirection,
148+
) -> BoxedIter<fuel_core_storage::kv_store::KeyItem> {
149+
self.storage
150+
.iter_store_keys(column, prefix, start, direction)
151+
}
141152
}
142153

143154
impl<Storage> AsRef<Storage> for GenericDatabase<Storage> {

crates/fuel-core/src/state/historical_rocksdb.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,17 @@ where
416416
self.db
417417
.iter_store(Column::OriginalColumn(column), prefix, start, direction)
418418
}
419+
420+
fn iter_store_keys(
421+
&self,
422+
column: Self::Column,
423+
prefix: Option<&[u8]>,
424+
start: Option<&[u8]>,
425+
direction: IterDirection,
426+
) -> BoxedIter<fuel_core_storage::kv_store::KeyItem> {
427+
self.db
428+
.iter_store_keys(Column::OriginalColumn(column), prefix, start, direction)
429+
}
419430
}
420431

421432
impl<Description> TransactableStorage<Description::Height>

crates/fuel-core/src/state/historical_rocksdb/view_at_height.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use crate::{
99
deserialize,
1010
height_key,
1111
},
12-
rocks_db::RocksDb,
12+
rocks_db::{
13+
KeyAndValue,
14+
RocksDb,
15+
},
1316
},
1417
};
1518
use fuel_core_storage::{
@@ -50,7 +53,7 @@ where
5053
let height_key = height_key(key, &self.height);
5154
let options = ReadOptions::default();
5255
let nearest_modification = read_history
53-
._iter_all(
56+
.iterator::<KeyAndValue>(
5457
Column::HistoricalDuplicateColumn(column),
5558
options,
5659
IteratorMode::From(&height_key, rocksdb::Direction::Forward),

crates/fuel-core/src/state/in_memory/memory_store.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ use crate::{
1515
use fuel_core_storage::{
1616
iter::{
1717
iterator,
18+
keys_iterator,
1819
BoxedIter,
1920
IntoBoxedIter,
2021
IterableStore,
2122
},
2223
kv_store::{
2324
KVItem,
25+
KeyItem,
2426
KeyValueInspect,
2527
StorageColumn,
2628
Value,
@@ -99,6 +101,22 @@ where
99101

100102
collection.into_iter().map(Ok)
101103
}
104+
105+
pub fn iter_all_keys(
106+
&self,
107+
column: Description::Column,
108+
prefix: Option<&[u8]>,
109+
start: Option<&[u8]>,
110+
direction: IterDirection,
111+
) -> impl Iterator<Item = KeyItem> {
112+
let lock = self.inner[column.as_usize()].lock().expect("poisoned");
113+
114+
let collection: Vec<_> = keys_iterator(&lock, prefix, start, direction)
115+
.map(|key| key.to_vec())
116+
.collect();
117+
118+
collection.into_iter().map(Ok)
119+
}
102120
}
103121

104122
impl<Description> KeyValueInspect for MemoryStore<Description>
@@ -129,6 +147,17 @@ where
129147
) -> BoxedIter<KVItem> {
130148
self.iter_all(column, prefix, start, direction).into_boxed()
131149
}
150+
151+
fn iter_store_keys(
152+
&self,
153+
column: Self::Column,
154+
prefix: Option<&[u8]>,
155+
start: Option<&[u8]>,
156+
direction: IterDirection,
157+
) -> BoxedIter<fuel_core_storage::kv_store::KeyItem> {
158+
self.iter_all_keys(column, prefix, start, direction)
159+
.into_boxed()
160+
}
132161
}
133162

134163
impl<Description> TransactableStorage<Description::Height> for MemoryStore<Description>

crates/fuel-core/src/state/in_memory/memory_view.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use crate::database::database_description::{
55
use fuel_core_storage::{
66
iter::{
77
iterator,
8+
keys_iterator,
89
BoxedIter,
910
IntoBoxedIter,
1011
IterDirection,
1112
IterableStore,
1213
},
1314
kv_store::{
1415
KVItem,
16+
KeyItem,
1517
KeyValueInspect,
1618
StorageColumn,
1719
Value,
@@ -47,6 +49,20 @@ where
4749
.map(|(key, value)| (key.clone().into(), value.clone()))
4850
.map(Ok)
4951
}
52+
53+
pub fn iter_all_keys(
54+
&self,
55+
column: Description::Column,
56+
prefix: Option<&[u8]>,
57+
start: Option<&[u8]>,
58+
direction: IterDirection,
59+
) -> impl Iterator<Item = KeyItem> + '_ {
60+
let btree = &self.inner[column.as_usize()];
61+
62+
keys_iterator(btree, prefix, start, direction)
63+
.map(|key| key.clone().into())
64+
.map(Ok)
65+
}
5066
}
5167

5268
impl<Description> KeyValueInspect for MemoryView<Description>
@@ -73,4 +89,15 @@ where
7389
) -> BoxedIter<KVItem> {
7490
self.iter_all(column, prefix, start, direction).into_boxed()
7591
}
92+
93+
fn iter_store_keys(
94+
&self,
95+
column: Self::Column,
96+
prefix: Option<&[u8]>,
97+
start: Option<&[u8]>,
98+
direction: IterDirection,
99+
) -> BoxedIter<KeyItem> {
100+
self.iter_all_keys(column, prefix, start, direction)
101+
.into_boxed()
102+
}
76103
}

crates/fuel-core/src/state/iterable_key_value_view.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use fuel_core_storage::{
66
},
77
kv_store::{
88
KVItem,
9+
KeyItem,
910
KeyValueInspect,
1011
StorageColumn,
1112
Value,
@@ -85,4 +86,14 @@ where
8586
) -> BoxedIter<KVItem> {
8687
self.0.iter_store(column, prefix, start, direction)
8788
}
89+
90+
fn iter_store_keys(
91+
&self,
92+
column: Self::Column,
93+
prefix: Option<&[u8]>,
94+
start: Option<&[u8]>,
95+
direction: IterDirection,
96+
) -> BoxedIter<KeyItem> {
97+
self.0.iter_store_keys(column, prefix, start, direction)
98+
}
8899
}

0 commit comments

Comments
 (0)