Skip to content

Commit

Permalink
Add Storage::range_keys and Storage::range_values
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Jun 27, 2023
1 parent 7943fa8 commit ef3d0e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ and this project adheres to
- cosmwasm-std: Add `FromStr` impl for `Coin`. ([#1684])
- cosmwasm-std: Add `Decimal::bps` and `Decimal256::bps` to create a decimal
from a basis point value ([#1715]).
- 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.

[#1635]: https://github.com/CosmWasm/cosmwasm/pull/1635
[#1647]: https://github.com/CosmWasm/cosmwasm/pull/1647
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 ef3d0e8

Please # to comment.