use alloc::vec::Vec;
use codec::{Codec, Decode, Encode};
pub use subsoil::core::storage::{ChildInfo, ChildType, StateVersion};
pub use subsoil::io::{KillStorageResult, MultiRemovalResults};
pub fn get<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8]) -> Option<T> {
match child_info.child_type() {
ChildType::ParentKeyId => {
let storage_key = child_info.storage_key();
subsoil::io::default_child_storage::get(storage_key, key).and_then(|v| {
Decode::decode(&mut &v[..]).map(Some).unwrap_or_else(|_| {
log::error!(
target: "runtime::storage",
"Corrupted state in child trie at {:?}/{:?}",
storage_key,
key,
);
None
})
})
},
}
}
pub fn get_or_default<T: Decode + Sized + Default>(child_info: &ChildInfo, key: &[u8]) -> T {
get(child_info, key).unwrap_or_default()
}
pub fn get_or<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8], default_value: T) -> T {
get(child_info, key).unwrap_or(default_value)
}
pub fn get_or_else<T: Decode + Sized, F: FnOnce() -> T>(
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
get(child_info, key).unwrap_or_else(default_value)
}
pub fn put<T: Encode>(child_info: &ChildInfo, key: &[u8], value: &T) {
match child_info.child_type() {
ChildType::ParentKeyId => value.using_encoded(|slice| {
subsoil::io::default_child_storage::set(child_info.storage_key(), key, slice)
}),
}
}
pub fn take<T: Decode + Sized>(child_info: &ChildInfo, key: &[u8]) -> Option<T> {
let r = get(child_info, key);
if r.is_some() {
kill(child_info, key);
}
r
}
pub fn take_or_default<T: Codec + Sized + Default>(child_info: &ChildInfo, key: &[u8]) -> T {
take(child_info, key).unwrap_or_default()
}
pub fn take_or<T: Codec + Sized>(child_info: &ChildInfo, key: &[u8], default_value: T) -> T {
take(child_info, key).unwrap_or(default_value)
}
pub fn take_or_else<T: Codec + Sized, F: FnOnce() -> T>(
child_info: &ChildInfo,
key: &[u8],
default_value: F,
) -> T {
take(child_info, key).unwrap_or_else(default_value)
}
pub fn exists(child_info: &ChildInfo, key: &[u8]) -> bool {
match child_info.child_type() {
ChildType::ParentKeyId => {
subsoil::io::default_child_storage::exists(child_info.storage_key(), key)
},
}
}
#[deprecated = "Use `clear_storage` instead"]
pub fn kill_storage(child_info: &ChildInfo, limit: Option<u32>) -> KillStorageResult {
match child_info.child_type() {
ChildType::ParentKeyId => {
subsoil::io::default_child_storage::storage_kill(child_info.storage_key(), limit)
},
}
}
pub fn clear_storage(
child_info: &ChildInfo,
maybe_limit: Option<u32>,
_maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
let r = match child_info.child_type() {
ChildType::ParentKeyId => {
subsoil::io::default_child_storage::storage_kill(child_info.storage_key(), maybe_limit)
},
};
use subsoil::io::KillStorageResult::*;
let (maybe_cursor, backend) = match r {
AllRemoved(db) => (None, db),
SomeRemaining(db) => (Some(child_info.storage_key().to_vec()), db),
};
MultiRemovalResults { maybe_cursor, backend, unique: backend, loops: backend }
}
pub fn kill(child_info: &ChildInfo, key: &[u8]) {
match child_info.child_type() {
ChildType::ParentKeyId => {
subsoil::io::default_child_storage::clear(child_info.storage_key(), key);
},
}
}
pub fn get_raw(child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
match child_info.child_type() {
ChildType::ParentKeyId => {
subsoil::io::default_child_storage::get(child_info.storage_key(), key)
},
}
}
pub fn put_raw(child_info: &ChildInfo, key: &[u8], value: &[u8]) {
match child_info.child_type() {
ChildType::ParentKeyId => {
subsoil::io::default_child_storage::set(child_info.storage_key(), key, value)
},
}
}
pub fn root(child_info: &ChildInfo, version: StateVersion) -> Vec<u8> {
match child_info.child_type() {
ChildType::ParentKeyId => {
subsoil::io::default_child_storage::root(child_info.storage_key(), version)
},
}
}
pub fn len(child_info: &ChildInfo, key: &[u8]) -> Option<u32> {
match child_info.child_type() {
ChildType::ParentKeyId => {
let mut buffer = [0; 0];
subsoil::io::default_child_storage::read(child_info.storage_key(), key, &mut buffer, 0)
},
}
}