-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement range_de for SnapshotMap #497
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,12 +185,39 @@ where | |
} | ||
} | ||
|
||
#[cfg(feature = "iterator")] | ||
impl<'a, K, T> SnapshotMap<'a, K, T> | ||
where | ||
T: Serialize + DeserializeOwned, | ||
K: PrimaryKey<'a> + KeyDeserialize, | ||
{ | ||
pub fn range_de<'c>( | ||
&self, | ||
store: &'c dyn Storage, | ||
min: Option<Bound>, | ||
max: Option<Bound>, | ||
order: cosmwasm_std::Order, | ||
) -> Box<dyn Iterator<Item = StdResult<(K::Output, T)>> + 'c> | ||
where | ||
T: 'c, | ||
K::Output: 'static, | ||
{ | ||
self.no_prefix_de().range_de(store, min, max, order) | ||
} | ||
|
||
fn no_prefix_de(&self) -> Prefix<K, T> { | ||
Prefix::new(self.primary.namespace(), &[]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use cosmwasm_std::testing::MockStorage; | ||
|
||
type TestMap = SnapshotMap<'static, &'static [u8], u64>; | ||
type TestMapCompositeKey = SnapshotMap<'static, (&'static [u8], &'static [u8]), u64>; | ||
|
||
const NEVER: TestMap = | ||
SnapshotMap::new("never", "never__check", "never__change", Strategy::Never); | ||
const EVERY: TestMap = SnapshotMap::new( | ||
|
@@ -199,6 +226,12 @@ mod tests { | |
"every__change", | ||
Strategy::EveryBlock, | ||
); | ||
const EVERY_COMPOSITE_KEY: TestMapCompositeKey = SnapshotMap::new( | ||
"every", | ||
"every__check", | ||
"every__change", | ||
Strategy::EveryBlock, | ||
); | ||
const SELECT: TestMap = SnapshotMap::new( | ||
"select", | ||
"select__check", | ||
|
@@ -256,6 +289,31 @@ mod tests { | |
(b"D", None), | ||
]; | ||
|
||
// Same as `init_data`, but we have a composite key for testing range_de. | ||
fn init_data_composite_key(map: &TestMapCompositeKey, storage: &mut dyn Storage) { | ||
map.save(storage, (b"A", b"B"), &5, 1).unwrap(); | ||
map.save(storage, (b"B", b"A"), &7, 2).unwrap(); | ||
|
||
// checkpoint 3 | ||
map.add_checkpoint(storage, 3).unwrap(); | ||
|
||
// also use update to set - to ensure this works | ||
map.save(storage, (b"B", b"B"), &1, 3).unwrap(); | ||
map.update(storage, (b"A", b"B"), 3, |_| -> StdResult<u64> { Ok(8) }) | ||
.unwrap(); | ||
|
||
map.remove(storage, (b"B", b"A"), 4).unwrap(); | ||
map.save(storage, (b"B", b"B"), &13, 4).unwrap(); | ||
|
||
// checkpoint 5 | ||
map.add_checkpoint(storage, 5).unwrap(); | ||
map.remove(storage, (b"A", b"B"), 5).unwrap(); | ||
map.update(storage, (b"C", b"A"), 5, |_| -> StdResult<u64> { Ok(22) }) | ||
.unwrap(); | ||
// and delete it later (unknown if all data present) | ||
map.remove_checkpoint(storage, 5).unwrap(); | ||
} | ||
|
||
fn assert_final_values(map: &TestMap, storage: &dyn Storage) { | ||
for (k, v) in FINAL_VALUES.iter().cloned() { | ||
assert_eq!(v, map.may_load(storage, k).unwrap()); | ||
|
@@ -373,4 +431,70 @@ mod tests { | |
EVERY.may_load_at_height(&storage, b"C", 6).unwrap() | ||
); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "iterator")] | ||
fn range_de_simple_string_key() { | ||
use cosmwasm_std::Order; | ||
|
||
let mut store = MockStorage::new(); | ||
init_data(&EVERY, &mut store); | ||
|
||
// let's try to iterate! | ||
let all: StdResult<Vec<_>> = EVERY | ||
.range_de(&store, None, None, Order::Ascending) | ||
.collect(); | ||
let all = all.unwrap(); | ||
assert_eq!(2, all.len()); | ||
assert_eq!(all, vec![(b"C".to_vec(), 13), (b"D".to_vec(), 22)]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice demo showing the deserialisation. |
||
|
||
// let's try to iterate over a range | ||
let all: StdResult<Vec<_>> = EVERY | ||
.range_de( | ||
&store, | ||
Some(Bound::Inclusive(b"C".to_vec())), | ||
None, | ||
Order::Ascending, | ||
) | ||
.collect(); | ||
let all = all.unwrap(); | ||
assert_eq!(2, all.len()); | ||
assert_eq!(all, vec![(b"C".to_vec(), 13), (b"D".to_vec(), 22)]); | ||
|
||
// let's try to iterate over a more restrictive range | ||
let all: StdResult<Vec<_>> = EVERY | ||
.range_de( | ||
&store, | ||
Some(Bound::Inclusive(b"D".to_vec())), | ||
None, | ||
Order::Ascending, | ||
) | ||
.collect(); | ||
let all = all.unwrap(); | ||
assert_eq!(1, all.len()); | ||
assert_eq!(all, vec![(b"D".to_vec(), 22)]); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "iterator")] | ||
fn range_de_composite_key() { | ||
use cosmwasm_std::Order; | ||
|
||
let mut store = MockStorage::new(); | ||
init_data_composite_key(&EVERY_COMPOSITE_KEY, &mut store); | ||
|
||
// let's try to iterate! | ||
let all: StdResult<Vec<_>> = EVERY_COMPOSITE_KEY | ||
.range_de(&store, None, None, Order::Ascending) | ||
.collect(); | ||
let all = all.unwrap(); | ||
assert_eq!(2, all.len()); | ||
assert_eq!( | ||
all, | ||
vec![ | ||
((b"B".to_vec(), b"B".to_vec()), 13), | ||
((b"C".to_vec(), b"A".to_vec()), 22) | ||
] | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to remove this.
The next check also asserts this, but gives more info on failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair. We do this in other places I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we started removing some of those in recent PR reviews, which is why I point this out.
Nice to read other PR reviews also to see discussions on APIs and best practices... I think that is where the code style really gets hashed out