Introduce prefixed storage with enumeration (#4185)

* Introduce storage_next allowing iteration.  (without childtries)

* Implement prefixed storage

* impl cache in client_storage_cache (needs test)

* switch overlay change to btreemap

* Revert "impl cache in client_storage_cache"

This reverts commit c91a4848916eba87184b3dc4722cea81aec9339d.

the storage cache cannot be used this way

* Revert "Implement prefixed storage"

This reverts commit 4931088126a427082d7310ed7e83b8eea966bc20.

* Impl StoragePrefixedMap for all map storages

* remove comment

* Move all overlays to BTreeMap

* btreemap iteration improvment

* impl for child tries

* impl tests for childs

* fix

* remove cache comment

* Fix grumble
This commit is contained in:
thiolliere
2019-12-09 20:55:11 +01:00
committed by Bastian Köcher
parent fb1eb9d9e4
commit e5b6935c2a
25 changed files with 711 additions and 58 deletions
@@ -16,7 +16,9 @@
//! Basic implementation for Externalities.
use std::{collections::HashMap, any::{TypeId, Any}, iter::FromIterator};
use std::{
collections::{HashMap, BTreeMap}, any::{TypeId, Any}, iter::FromIterator, mem, ops::Bound
};
use crate::backend::{Backend, InMemory};
use hash_db::Hasher;
use trie::{TrieConfiguration, default_child_trie_root};
@@ -54,8 +56,8 @@ impl BasicExternalities {
/// Consume self and returns inner storages
pub fn into_storages(self) -> (
HashMap<Vec<u8>, Vec<u8>>,
HashMap<Vec<u8>, HashMap<Vec<u8>, Vec<u8>>>,
BTreeMap<Vec<u8>, Vec<u8>>,
HashMap<Vec<u8>, BTreeMap<Vec<u8>, Vec<u8>>>,
) {
(self.top, self.children)
}
@@ -68,8 +70,8 @@ impl BasicExternalities {
f: impl FnOnce() -> R,
) -> R {
let mut ext = Self {
top: storage.0.drain().collect(),
children: storage.1.drain().collect(),
top: mem::replace(&mut storage.0, BTreeMap::default()),
children: mem::replace(&mut storage.1, HashMap::default()),
};
let r = ext.execute_with(f);
@@ -105,8 +107,8 @@ impl Default for BasicExternalities {
fn default() -> Self { Self::new(Default::default(), Default::default()) }
}
impl From<HashMap<Vec<u8>, Vec<u8>>> for BasicExternalities {
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
impl From<BTreeMap<Vec<u8>, Vec<u8>>> for BasicExternalities {
fn from(hashmap: BTreeMap<Vec<u8>, Vec<u8>>) -> Self {
BasicExternalities {
top: hashmap,
children: Default::default(),
@@ -151,6 +153,17 @@ impl Externalities for BasicExternalities {
Externalities::child_storage(self, storage_key, key)
}
fn next_storage_key(&self, key: &[u8]) -> Option<Vec<u8>> {
let range = (Bound::Excluded(key), Bound::Unbounded);
self.top.range::<[u8], _>(range).next().map(|(k, _)| k).cloned()
}
fn next_child_storage_key(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
let range = (Bound::Excluded(key), Bound::Unbounded);
self.children.get(storage_key.as_ref())
.and_then(|child| child.range::<[u8], _>(range).next().map(|(k, _)| k).cloned())
}
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
@@ -190,12 +203,28 @@ impl Externalities for BasicExternalities {
return;
}
self.top.retain(|key, _| !key.starts_with(prefix));
let to_remove = self.top.range::<[u8], _>((Bound::Included(prefix), Bound::Unbounded))
.map(|(k, _)| k)
.take_while(|k| k.starts_with(prefix))
.cloned()
.collect::<Vec<_>>();
for key in to_remove {
self.top.remove(&key);
}
}
fn clear_child_prefix(&mut self, storage_key: ChildStorageKey, prefix: &[u8]) {
if let Some(child) = self.children.get_mut(storage_key.as_ref()) {
child.retain(|key, _| !key.starts_with(prefix));
let to_remove = child.range::<[u8], _>((Bound::Included(prefix), Bound::Unbounded))
.map(|(k, _)| k)
.take_while(|k| k.starts_with(prefix))
.cloned()
.collect::<Vec<_>>();
for key in to_remove {
child.remove(&key);
}
}
}