Skip to content

Commit

Permalink
Change direction of the iterator (Sovereign-Labs#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen-stig authored Oct 27, 2023
1 parent 06c118e commit 77317c7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
13 changes: 13 additions & 0 deletions full-node/db/sov-schema-db/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ where
Ok(())
}

/// Reverses iterator direction.
pub fn rev(self) -> Self {
let new_direction = match self.direction {
ScanDirection::Forward => ScanDirection::Backward,
ScanDirection::Backward => ScanDirection::Forward,
};
SchemaIterator {
db_iter: self.db_iter,
direction: new_direction,
phantom: Default::default(),
}
}

fn next_impl(&mut self) -> Result<Option<(S::Key, S::Value)>> {
let _timer = SCHEMADB_ITER_LATENCY_SECONDS
.with_label_values(&[S::COLUMN_FAMILY_NAME])
Expand Down
13 changes: 0 additions & 13 deletions full-node/db/sov-schema-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,6 @@ impl DB {
self.iter_with_direction::<S>(opts, ScanDirection::Forward)
}

/// Returns a backward [`SchemaIterator`] on a certain schema with the default read options.
pub fn rev_iter<S: Schema>(&self) -> anyhow::Result<SchemaIterator<S>> {
self.iter_with_direction::<S>(Default::default(), ScanDirection::Backward)
}

/// Returns a backward [`SchemaIterator`] on a certain schema with the provided read options.
pub fn rev_iter_with_opts<S: Schema>(
&self,
opts: ReadOptions,
) -> anyhow::Result<SchemaIterator<S>> {
self.iter_with_direction::<S>(opts, ScanDirection::Backward)
}

/// Writes a group of records wrapped in a [`SchemaBatch`].
pub fn write_schemas(&self, batch: SchemaBatch) -> anyhow::Result<()> {
let _timer = SCHEMADB_BATCH_COMMIT_LATENCY_SECONDS
Expand Down
2 changes: 1 addition & 1 deletion full-node/db/sov-schema-db/tests/iterator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl TestDB {
}

fn rev_iter(&self) -> SchemaIterator<TestSchema> {
self.db.rev_iter().expect("Failed to create iterator.")
self.db.iter().expect("Failed to create iterator.").rev()
}
}

Expand Down

0 comments on commit 77317c7

Please sign in to comment.