Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Storage::range_keys and Storage::range_values #1748

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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