-
Notifications
You must be signed in to change notification settings - Fork 392
feat: Move key family helpers to top level #9726
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| use std::default::Default; | ||
|
|
||
| use crate::{registry::Registry, storage::with_chunks}; | ||
| use ic_registry_canister_chunkify::decode_high_capacity_registry_value; | ||
| use ic_registry_transport::pb::v1::HighCapacityRegistryValue; | ||
|
|
||
| /// Similar to `get_key_family` on the `RegistryClient`, return a list of | ||
| /// tuples, (ID, value). This strips the prefix from the key and returns the | ||
| /// value as a decoded struct. | ||
| /// | ||
| /// This function must return keys in order of their bytes, which should | ||
| /// also be the same order as the string representations. | ||
| pub(crate) fn get_key_family<T: prost::Message + Default>( | ||
| registry: &Registry, | ||
| prefix: &str, | ||
| ) -> Vec<(String, T)> { | ||
| get_key_family_iter(registry, prefix).collect() | ||
| } | ||
|
|
||
| /// This function must return keys in order of their bytes, which should | ||
| /// also be the same order as the string representations. | ||
| pub(crate) fn get_key_family_iter<'a, T: prost::Message + Default>( | ||
| registry: &'a Registry, | ||
| prefix: &'a str, | ||
| ) -> impl Iterator<Item = (String, T)> + 'a { | ||
| get_key_family_iter_at_version(registry, prefix, registry.latest_version()) | ||
| } | ||
|
|
||
| /// This function must return keys in order of their bytes, which should | ||
| /// also be the same order as the string representations. | ||
| pub(crate) fn get_key_family_iter_at_version<'a, T: prost::Message + Default>( | ||
| registry: &'a Registry, | ||
| prefix: &'a str, | ||
| version: u64, | ||
| ) -> impl Iterator<Item = (String, T)> + 'a { | ||
| get_key_family_raw_iter_at_version(registry, prefix, version).filter_map(|(id, value)| { | ||
| let latest_value: Option<T> = | ||
| with_chunks(|chunks| decode_high_capacity_registry_value::<T, _>(value, chunks)); | ||
|
|
||
| let latest_value = latest_value?; | ||
|
|
||
| Some((id, latest_value)) | ||
| }) | ||
| } | ||
|
|
||
| /// This function must return keys in order of their bytes, which should | ||
| /// also be the same order as the string representations. | ||
| pub(crate) fn get_key_family_raw_iter_at_version<'a>( | ||
| registry: &'a Registry, | ||
| prefix: &'a str, | ||
| version: u64, | ||
| ) -> impl Iterator<Item = (String, &'a HighCapacityRegistryValue)> + 'a { | ||
| let prefix_bytes = prefix.as_bytes(); | ||
| let start = prefix_bytes.to_vec(); | ||
|
|
||
| // Note, using the 'store' which is a BTreeMap is what guarantees the order of keys. | ||
| registry | ||
| .store | ||
| .range(start..) | ||
| .take_while(|(k, _)| k.starts_with(prefix_bytes)) | ||
| .filter_map(move |(key, values)| { | ||
| let latest_value: &HighCapacityRegistryValue = | ||
| values.iter().rev().find(|value| value.version <= version)?; | ||
|
|
||
| if !latest_value.is_present() { | ||
| return None; // Deleted or otherwise empty value. | ||
| } | ||
|
|
||
| let id = key | ||
| .strip_prefix(prefix_bytes) | ||
| .and_then(|v| std::str::from_utf8(v).ok()) | ||
| .unwrap() | ||
| .to_string(); | ||
|
|
||
| Some((id, latest_value)) | ||
| }) | ||
| } | ||
|
|
||
| #[path = "key_family_tests.rs"] | ||
| #[cfg(test)] | ||
| mod tests; |
7 changes: 6 additions & 1 deletion
7
...mutations/node_management/common_tests.rs → ...y/canister/src/common/key_family_tests.rs
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub const LOG_PREFIX: &str = "[Registry] "; | ||
|
|
||
| pub mod key_family; | ||
| #[cfg(test)] | ||
| pub mod test_helpers; |
2 changes: 1 addition & 1 deletion
2
rs/registry/canister/src/get_node_providers_monthly_xdr_rewards.rs
This file contains hidden or 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 hidden or 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
3 changes: 2 additions & 1 deletion
3
rs/registry/canister/src/mutations/do_remove_node_operators.rs
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.