Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Implement iter for doublemap #5504

Merged
merged 10 commits into from
Apr 15, 2020
Merged

Implement iter for doublemap #5504

merged 10 commits into from
Apr 15, 2020

Conversation

gui1117
Copy link
Contributor

@gui1117 gui1117 commented Apr 2, 2020

Context

StorageDoubleMap is missing a function iter to be able to iterate on all values.

Done:

  • IterableDoubleMap:

    • fn iter and fn drain are renamed iter_prefix and drain_prefix (I used iter_prefix because this is how function working with k1 are called in StorageDoubleMap)
    • fn iter and fn drain are introduced, they iterate over the whole storage and decode K1 and K2 and Value
  • StorageDoubleMap: fn iter_prefix is renamed iter_prefix_values as it doesn't decode the keys, this name is consistent with StoragePrefixedMap::iter_values

@gui1117 gui1117 added A0-please_review Pull request needs code review. B1-apinoteworthy labels Apr 2, 2020
@gavofyork
Copy link
Member

@thiolliere Not building...

@gui1117 gui1117 added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Apr 14, 2020
@gui1117 gui1117 added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Apr 14, 2020
@gui1117
Copy link
Contributor Author

gui1117 commented Apr 14, 2020

Fixed

Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitpicks, otherwise looks good

@gui1117
Copy link
Contributor Author

gui1117 commented Apr 15, 2020

👍 nitpicks addressed


fn key_before_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert!(*last != 0, "mock function not implemented for this prefix");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are aware that rust panics automatically with debug_assertions enabled? (we have this in CI)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0b5b6e7c75f5163808b6fb16a6a2c1ae

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if you wanted to keep it more explicit, *last = last.checked_sub(1).unwrap() would be the way to go.

But using unwrap_or_else(|| unimplemented!()) is really weird.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can keep this now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are aware that rust panics automatically with debug_assertions enabled? (we have this in CI)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0b5b6e7c75f5163808b6fb16a6a2c1ae

I though CI was running in release mode, and so operation was just silently wrapping around.

But I guess best was: *last = last.checked_sub(1).expect("not implementation for mock") indeed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes in release, but with debug-assertions enabled: https://github.com/paritytech/substrate/blob/master/.gitlab-ci.yml#L191

_phantom: Default::default(),
drain: false,
closure: |raw_key_without_prefix, mut raw_value| {
let mut k1_k2_material = G::Hasher1::reverse(raw_key_without_prefix);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this make the assumption that reversing will return the part for k1 and the remainings input. but nothing prevent a hasher to implement ReversibleStorageHasher in a different way. I must update the doc of reversible hasher or introduce new function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you will push it to this pr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes added to the PR, or there is the branch gui-reversible-doc-and-constraint if wanted

@bkchr bkchr merged commit 4b91107 into master Apr 15, 2020
@bkchr bkchr deleted the gui-impl-iter-for-doublemap branch April 15, 2020 14:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants