Skip to content

Commit

Permalink
Merge pull request #1748 from CosmWasm/add-range_keys-range_values
Browse files Browse the repository at this point in the history
Add Storage::range_keys and Storage::range_values
  • Loading branch information
webmaster128 authored Jun 27, 2023
2 parents 50d0b6f + a9d2b0a commit ebc5773
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ and this project adheres to
was checked when it was first uploaded. ([#1635])
- cosmwasm-vm: Allow sign extension Wasm opcodes in static validation. This
allows contracts to be compiled with Rust 1.70.0 and above. ([#1727])
- cosmwasm-std: Add trait functions `Storage::range_keys` and
`Storage::range_values`. The default implementations just use
`Storage::range`. Later this can be implemented more efficiently. ([#1748])

[#1635]: https://github.com/CosmWasm/cosmwasm/pull/1635
[#1647]: https://github.com/CosmWasm/cosmwasm/pull/1647
[#1684]: https://github.com/CosmWasm/cosmwasm/pull/1684
[#1687]: https://github.com/CosmWasm/cosmwasm/pull/1687
[#1727]: https://github.com/CosmWasm/cosmwasm/issues/1727
[#1748]: https://github.com/CosmWasm/cosmwasm/pull/1748

### Changed

Expand Down
37 changes: 35 additions & 2 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,52 @@ pub trait Storage {
/// is not great yet and might not be possible in all backends. But we're trying to get there.
fn get(&self, key: &[u8]) -> Option<Vec<u8>>;

#[cfg(feature = "iterator")]
/// Allows iteration over a set of key/value pairs, either forwards or backwards.
///
/// The bound `start` is inclusive and `end` is exclusive.
///
/// If `start` is lexicographically greater than or equal to `end`, an empty range is described, mo matter of the order.
#[cfg(feature = "iterator")]
fn range<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
order: Order,
) -> Box<dyn Iterator<Item = Record> + 'a>;

/// Allows iteration over a set of keys, either forwards or backwards.
///
/// The bound `start` is inclusive and `end` is exclusive.
/// If `start` is lexicographically greater than or equal to `end`, an empty range is described, mo matter of the order.
///
/// The default implementation uses [`Storage::range`] and discards the values. More efficient
/// implementations might be possible depending on the storage.
#[cfg(feature = "iterator")]
fn range_keys<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'a> {
Box::new(self.range(start, end, order).map(|(k, _v)| k))
}

/// Allows iteration over a set of values, either forwards or backwards.
///
/// The bound `start` is inclusive and `end` is exclusive.
/// If `start` is lexicographically greater than or equal to `end`, an empty range is described, mo matter of the order.
///
/// The default implementation uses [`Storage::range`] and discards the keys. More efficient implementations
/// might be possible depending on the storage.
#[cfg(feature = "iterator")]
fn range_values<'a>(
&'a self,
start: Option<&[u8]>,
end: Option<&[u8]>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'a> {
Box::new(self.range(start, end, order).map(|(_k, v)| v))
}

fn set(&mut self, key: &[u8], value: &[u8]);

/// Removes a database entry at `key`.
Expand Down

0 comments on commit ebc5773

Please sign in to comment.