StorageMap
2019-10-25 本文已影响0人
空乱木
StorageMap是Substrate编程中常用的类型,所以列举出来,以便之后查询起来更加的方便;
Substrate项目中的路径:/substrate/srml/support/src/storage/mod.rs
最常用的方法如下,具体的实现请参考代码。
- exists 根据Key查询数据在Map 中是否存在
- insert 向Map中插入一条新的记录
- remove 从Map中移除一条记录
- get 根据Key查询Map中的记录
/// A strongly-typed map in storage.
pub trait StorageMap<K: Codec, V: Codec> {
/// The type that get/take return.
type Query;
/// Get the prefix key in storage.
fn prefix() -> &'static [u8];
/// Get the storage key used to fetch a value corresponding to a specific key.
fn key_for<KeyArg: Borrow<K>>(key: KeyArg) -> Vec<u8>;
/// Does the value (explicitly) exist in storage?
fn exists<KeyArg: Borrow<K>>(key: KeyArg) -> bool;
/// Load the value associated with the given key from the map.
fn get<KeyArg: Borrow<K>>(key: KeyArg) -> Self::Query;
/// Swap the values of two keys.
fn swap<KeyArg1: Borrow<K>, KeyArg2: Borrow<K>>(key1: KeyArg1, key2: KeyArg2);
/// Store a value to be associated with the given key from the map.
fn insert<KeyArg: Borrow<K>, ValArg: Borrow<V>>(key: KeyArg, val: ValArg);
/// Store a value under this key into the provided storage instance; this can take any reference
/// type that derefs to `T` (and has `Encode` implemented).
fn insert_ref<KeyArg: Borrow<K>, ValArg: ?Sized + Encode>(key: KeyArg, val: &ValArg) where V: AsRef<ValArg>;
/// Remove the value under a key.
fn remove<KeyArg: Borrow<K>>(key: KeyArg);
/// Mutate the value under a key.
fn mutate<KeyArg: Borrow<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R;
/// Take the value under a key.
fn take<KeyArg: Borrow<K>>(key: KeyArg) -> Self::Query;
}