cleanup(db): Give forward and reverse database iterators explicit names (#8063)

* Give forward and reverse database iterators explicit names

* clippy: Ignore large error variants
This commit is contained in:
teor 2023-12-07 07:57:43 +10:00 committed by GitHub
parent 1be140b9ee
commit 9fec7116ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 30 deletions

View File

@ -56,6 +56,9 @@ rustflags = [
"-Wmissing_docs", "-Wmissing_docs",
# TODOs: # TODOs:
# Fix this lint eventually.
"-Aclippy::result_large_err",
# `cargo fix` might help do these fixes, # `cargo fix` might help do these fixes,
# or add a config.toml to sub-directories which should allow these lints, # or add a config.toml to sub-directories which should allow these lints,
# or try allowing the lint in the specific module (lib.rs doesn't seem to work in some cases) # or try allowing the lint in the specific module (lib.rs doesn't seem to work in some cases)

View File

@ -156,7 +156,7 @@ impl WriteDisk for DiskWriteBatch {
} }
// TODO: convert zs_delete_range() to take std::ops::RangeBounds // TODO: convert zs_delete_range() to take std::ops::RangeBounds
// see zs_range_iter() for an example of the edge cases // see zs_forward_range_iter() for an example of the edge cases
fn zs_delete_range<C, K>(&mut self, cf: &C, from: K, to: K) fn zs_delete_range<C, K>(&mut self, cf: &C, from: K, to: K)
where where
C: rocksdb::AsColumnFamilyRef, C: rocksdb::AsColumnFamilyRef,
@ -329,7 +329,7 @@ impl ReadDisk for DiskDb {
V: FromDisk, V: FromDisk,
{ {
// Reading individual values from iterators does not seem to cause database hangs. // Reading individual values from iterators does not seem to cause database hangs.
self.zs_range_iter(cf, .., false).next() self.zs_forward_range_iter(cf, ..).next()
} }
#[allow(clippy::unwrap_in_result)] #[allow(clippy::unwrap_in_result)]
@ -340,7 +340,7 @@ impl ReadDisk for DiskDb {
V: FromDisk, V: FromDisk,
{ {
// Reading individual values from iterators does not seem to cause database hangs. // Reading individual values from iterators does not seem to cause database hangs.
self.zs_range_iter(cf, .., true).next() self.zs_reverse_range_iter(cf, ..).next()
} }
#[allow(clippy::unwrap_in_result)] #[allow(clippy::unwrap_in_result)]
@ -351,7 +351,7 @@ impl ReadDisk for DiskDb {
V: FromDisk, V: FromDisk,
{ {
// Reading individual values from iterators does not seem to cause database hangs. // Reading individual values from iterators does not seem to cause database hangs.
self.zs_range_iter(cf, lower_bound.., false).next() self.zs_forward_range_iter(cf, lower_bound..).next()
} }
#[allow(clippy::unwrap_in_result)] #[allow(clippy::unwrap_in_result)]
@ -362,7 +362,7 @@ impl ReadDisk for DiskDb {
V: FromDisk, V: FromDisk,
{ {
// Reading individual values from iterators does not seem to cause database hangs. // Reading individual values from iterators does not seem to cause database hangs.
self.zs_range_iter(cf, ..=upper_bound, true).next() self.zs_reverse_range_iter(cf, ..=upper_bound).next()
} }
fn zs_items_in_range_ordered<C, K, V, R>(&self, cf: &C, range: R) -> BTreeMap<K, V> fn zs_items_in_range_ordered<C, K, V, R>(&self, cf: &C, range: R) -> BTreeMap<K, V>
@ -372,7 +372,7 @@ impl ReadDisk for DiskDb {
V: FromDisk, V: FromDisk,
R: RangeBounds<K>, R: RangeBounds<K>,
{ {
self.zs_range_iter(cf, range, false).collect() self.zs_forward_range_iter(cf, range).collect()
} }
fn zs_items_in_range_unordered<C, K, V, R>(&self, cf: &C, range: R) -> HashMap<K, V> fn zs_items_in_range_unordered<C, K, V, R>(&self, cf: &C, range: R) -> HashMap<K, V>
@ -382,7 +382,7 @@ impl ReadDisk for DiskDb {
V: FromDisk, V: FromDisk,
R: RangeBounds<K>, R: RangeBounds<K>,
{ {
self.zs_range_iter(cf, range, false).collect() self.zs_forward_range_iter(cf, range).collect()
} }
} }
@ -402,18 +402,13 @@ impl DiskWriteBatch {
} }
impl DiskDb { impl DiskDb {
/// Returns an iterator over the items in `cf` in `range`. /// Returns a forward iterator over the items in `cf` in `range`.
///
/// Accepts a `reverse` argument. If it is `true`, creates the iterator with an
/// [`IteratorMode`](rocksdb::IteratorMode) of [`End`](rocksdb::IteratorMode::End), or
/// [`From`](rocksdb::IteratorMode::From) with [`Direction::Reverse`](rocksdb::Direction::Reverse).
/// ///
/// Holding this iterator open might delay block commit transactions. /// Holding this iterator open might delay block commit transactions.
pub fn zs_range_iter<C, K, V, R>( pub fn zs_forward_range_iter<C, K, V, R>(
&self, &self,
cf: &C, cf: &C,
range: R, range: R,
reverse: bool,
) -> impl Iterator<Item = (K, V)> + '_ ) -> impl Iterator<Item = (K, V)> + '_
where where
C: rocksdb::AsColumnFamilyRef, C: rocksdb::AsColumnFamilyRef,
@ -421,14 +416,12 @@ impl DiskDb {
V: FromDisk, V: FromDisk,
R: RangeBounds<K>, R: RangeBounds<K>,
{ {
self.zs_range_iter_with_direction(cf, range, reverse) self.zs_range_iter_with_direction(cf, range, false)
} }
/// Returns a reverse iterator over the items in `cf` in `range`. /// Returns a reverse iterator over the items in `cf` in `range`.
/// ///
/// Holding this iterator open might delay block commit transactions. /// Holding this iterator open might delay block commit transactions.
///
/// This code is copied from `zs_range_iter()`, but with the mode reversed.
pub fn zs_reverse_range_iter<C, K, V, R>( pub fn zs_reverse_range_iter<C, K, V, R>(
&self, &self,
cf: &C, cf: &C,

View File

@ -420,13 +420,13 @@ impl AddressTransaction {
/// address. Starts at the first UTXO, or at the `query` start height, whichever is greater. /// address. Starts at the first UTXO, or at the `query` start height, whichever is greater.
/// Ends at the maximum possible transaction index for the end height. /// Ends at the maximum possible transaction index for the end height.
/// ///
/// Used to look up transactions with [`DiskDb::zs_range_iter`][1]. /// Used to look up transactions with [`DiskDb::zs_forward_range_iter`][1].
/// ///
/// The transaction locations in the: /// The transaction locations in the:
/// - start bound might be invalid, if it is based on the `query` start height. /// - start bound might be invalid, if it is based on the `query` start height.
/// - end bound will always be invalid. /// - end bound will always be invalid.
/// ///
/// But this is not an issue, since [`DiskDb::zs_range_iter`][1] will fetch all existing /// But this is not an issue, since [`DiskDb::zs_forward_range_iter`][1] will fetch all existing
/// (valid) values in the range. /// (valid) values in the range.
/// ///
/// [1]: super::super::disk_db::DiskDb /// [1]: super::super::disk_db::DiskDb

View File

@ -78,7 +78,7 @@ impl ZebraDb {
) -> impl Iterator<Item = (RawBytes, Arc<HistoryTree>)> + '_ { ) -> impl Iterator<Item = (RawBytes, Arc<HistoryTree>)> + '_ {
let history_tree_cf = self.db.cf_handle("history_tree").unwrap(); let history_tree_cf = self.db.cf_handle("history_tree").unwrap();
self.db.zs_range_iter(&history_tree_cf, .., false) self.db.zs_forward_range_iter(&history_tree_cf, ..)
} }
// Value pool methods // Value pool methods

View File

@ -155,7 +155,7 @@ impl ZebraDb {
&self, &self,
) -> impl Iterator<Item = (RawBytes, Arc<sprout::tree::NoteCommitmentTree>)> + '_ { ) -> impl Iterator<Item = (RawBytes, Arc<sprout::tree::NoteCommitmentTree>)> + '_ {
let sprout_trees = self.db.cf_handle("sprout_note_commitment_tree").unwrap(); let sprout_trees = self.db.cf_handle("sprout_note_commitment_tree").unwrap();
self.db.zs_range_iter(&sprout_trees, .., false) self.db.zs_forward_range_iter(&sprout_trees, ..)
} }
// # Sapling trees // # Sapling trees
@ -209,7 +209,7 @@ impl ZebraDb {
R: std::ops::RangeBounds<Height>, R: std::ops::RangeBounds<Height>,
{ {
let sapling_trees = self.db.cf_handle("sapling_note_commitment_tree").unwrap(); let sapling_trees = self.db.cf_handle("sapling_note_commitment_tree").unwrap();
self.db.zs_range_iter(&sapling_trees, range, false) self.db.zs_forward_range_iter(&sapling_trees, range)
} }
/// Returns the Sapling note commitment trees in the reversed range, in decreasing height order. /// Returns the Sapling note commitment trees in the reversed range, in decreasing height order.
@ -259,7 +259,7 @@ impl ZebraDb {
.unwrap(); .unwrap();
self.db self.db
.zs_range_iter(&sapling_subtrees, range, false) .zs_forward_range_iter(&sapling_subtrees, range)
.collect() .collect()
} }
@ -335,7 +335,7 @@ impl ZebraDb {
R: std::ops::RangeBounds<Height>, R: std::ops::RangeBounds<Height>,
{ {
let orchard_trees = self.db.cf_handle("orchard_note_commitment_tree").unwrap(); let orchard_trees = self.db.cf_handle("orchard_note_commitment_tree").unwrap();
self.db.zs_range_iter(&orchard_trees, range, false) self.db.zs_forward_range_iter(&orchard_trees, range)
} }
/// Returns the Orchard note commitment trees in the reversed range, in decreasing height order. /// Returns the Orchard note commitment trees in the reversed range, in decreasing height order.
@ -385,7 +385,7 @@ impl ZebraDb {
.unwrap(); .unwrap();
self.db self.db
.zs_range_iter(&orchard_subtrees, range, false) .zs_forward_range_iter(&orchard_subtrees, range)
.collect() .collect()
} }

View File

@ -242,11 +242,7 @@ impl ZebraDb {
AddressTransaction::address_iterator_range(address_location, query_height_range); AddressTransaction::address_iterator_range(address_location, query_height_range);
self.db self.db
.zs_range_iter( .zs_forward_range_iter(&tx_loc_by_transparent_addr_loc, transaction_location_range)
&tx_loc_by_transparent_addr_loc,
transaction_location_range,
false,
)
.map(|(tx_loc, ())| tx_loc) .map(|(tx_loc, ())| tx_loc)
.collect() .collect()
} }