Frame remove_all with size limit. (#9106)

* remove prefixed content with limit.

* test match

* factor comment and factor ext limit removal.

* fix benchmark

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
cheme
2021-06-15 15:23:58 +02:00
committed by GitHub
parent 5f0257f3b3
commit 693b39f43b
36 changed files with 312 additions and 239 deletions
@@ -25,7 +25,7 @@ use crate::{warn, debug};
use hash_db::{self, Hasher, Prefix};
use sp_trie::{Trie, MemoryDB, PrefixedMemoryDB, DBValue,
empty_child_trie_root, read_trie_value, read_child_trie_value,
for_keys_in_child_trie, KeySpacedDB, TrieDBIterator};
KeySpacedDB, TrieDBIterator};
use sp_trie::trie_types::{TrieDB, TrieError, Layout};
use crate::{backend::Consolidate, StorageKey, StorageValue};
use sp_core::storage::ChildInfo;
@@ -189,29 +189,30 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackendEssence<S, H> where H::Out:
.map_err(map_e)
}
/// Retrieve all entries keys of child storage and call `f` for each of those keys.
/// Retrieve all entries keys of a storage and call `f` for each of those keys.
/// Aborts as soon as `f` returns false.
pub fn apply_to_child_keys_while<F: FnMut(&[u8]) -> bool>(
pub fn apply_to_keys_while<F: FnMut(&[u8]) -> bool>(
&self,
child_info: &ChildInfo,
f: F,
child_info: Option<&ChildInfo>,
prefix: Option<&[u8]>,
mut f: F,
) {
let root = match self.child_root(child_info) {
Ok(v) => v.unwrap_or_else(|| empty_child_trie_root::<Layout<H>>().encode()),
Err(e) => {
debug!(target: "trie", "Error while iterating child storage: {}", e);
return;
}
let mut child_root = H::Out::default();
let root = if let Some(child_info) = child_info.as_ref() {
let root_vec = match self.child_root(child_info) {
Ok(v) => v.unwrap_or_else(|| empty_child_trie_root::<Layout<H>>().encode()),
Err(e) => {
debug!(target: "trie", "Error while iterating child storage: {}", e);
return;
}
};
child_root.as_mut().copy_from_slice(&root_vec);
&child_root
} else {
&self.root
};
if let Err(e) = for_keys_in_child_trie::<Layout<H>, _, _>(
child_info.keyspace(),
self,
&root,
f,
) {
debug!(target: "trie", "Error while iterating child storage: {}", e);
}
self.trie_iter_inner(root, prefix, |k, _v| f(k), child_info)
}
/// Execute given closure for all keys starting with prefix.
@@ -230,30 +231,38 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackendEssence<S, H> where H::Out:
};
let mut root = H::Out::default();
root.as_mut().copy_from_slice(&root_vec);
self.keys_values_with_prefix_inner(&root, prefix, |k, _v| f(k), Some(child_info))
self.trie_iter_inner(&root, Some(prefix), |k, _v| { f(k); true }, Some(child_info))
}
/// Execute given closure for all keys starting with prefix.
pub fn for_keys_with_prefix<F: FnMut(&[u8])>(&self, prefix: &[u8], mut f: F) {
self.keys_values_with_prefix_inner(&self.root, prefix, |k, _v| f(k), None)
self.trie_iter_inner(&self.root, Some(prefix), |k, _v| { f(k); true }, None)
}
fn keys_values_with_prefix_inner<F: FnMut(&[u8], &[u8])>(
fn trie_iter_inner<F: FnMut(&[u8], &[u8]) -> bool>(
&self,
root: &H::Out,
prefix: &[u8],
prefix: Option<&[u8]>,
mut f: F,
child_info: Option<&ChildInfo>,
) {
let mut iter = move |db| -> sp_std::result::Result<(), Box<TrieError<H::Out>>> {
let trie = TrieDB::<H>::new(db, root)?;
for x in TrieDBIterator::new_prefixed(&trie, prefix)? {
let iter = if let Some(prefix) = prefix.as_ref() {
TrieDBIterator::new_prefixed(&trie, prefix)?
} else {
TrieDBIterator::new(&trie)?
};
for x in iter {
let (key, value) = x?;
debug_assert!(key.starts_with(prefix));
debug_assert!(prefix.as_ref().map(|prefix| key.starts_with(prefix)).unwrap_or(true));
f(&key, &value);
if !f(&key, &value) {
break;
}
}
Ok(())
@@ -271,8 +280,8 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackendEssence<S, H> where H::Out:
}
/// Execute given closure for all key and values starting with prefix.
pub fn for_key_values_with_prefix<F: FnMut(&[u8], &[u8])>(&self, prefix: &[u8], f: F) {
self.keys_values_with_prefix_inner(&self.root, prefix, f, None)
pub fn for_key_values_with_prefix<F: FnMut(&[u8], &[u8])>(&self, prefix: &[u8], mut f: F) {
self.trie_iter_inner(&self.root, Some(prefix), |k, v| { f(k, v); true }, None)
}
}