-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add a method to delete all Map elements under a prefix #29
Comments
@webmaster128 @ueco-jb I did a quick and dirty implementation here: main...steak-enjoyers:cw-storage-plus:prefix-clear The idea is that impl Map {
pub fn clear(&self, store: &mut dyn Storage) {
self.no_prefix_raw().clear(store)
}
pub fn is_empty(&self, store: &dyn Storage) -> bool {
self.no_prefix_raw().is_empty(store)
}
} I consider this implementation "dirty" because here: impl Prefix {
pub fn clear(&self, store: &mut dyn Storage) {
const TAKE: usize = 10;
let mut cleared = false;
while !cleared {
let paths = self
.keys_raw(store, None, None, Order::Ascending) // (1)
.map(|raw_key| concat(&self.storage_prefix, &raw_key)) // (2)
.take(TAKE)
.collect::<Vec<_>>();
paths.iter().for_each(|path| store.remove(path));
cleared = paths.len() < TAKE;
}
}
} On line (1), in Thoughts? I suggest adding a impl Prefix {
pub fn clear(&self, store: &mut dyn Storage) {
const TAKE: usize = 10;
let mut cleared = false;
while !cleared {
let paths = self
.paths_raw(store, None, None, Order::Ascending)
.take(TAKE)
.collect::<Vec<_>>();
paths.iter().for_each(|path| store.remove(path));
cleared = paths.len() < TAKE;
}
}
} |
Sounds all good to me. I think one think we should introduce here is an optional limit for removals. Otherwise you can never support cases where the overall gas consumtion does not fit in a block. By keeping the limit optional, the existing Map.clear() can be supported by the same implementation in a compatible way. |
Done in #45. Will be released in 1.1 |
Now we have
Map::clear
method which deletes all elements in the map, it'd be helpful to also have aPrefix::clear
method to delete all elements under a specific prefix, for example:The same can be done for the
is_empty
method:The text was updated successfully, but these errors were encountered: