1 个不稳定版本
0.2.2 | 2024年7月30日 |
---|
#523 在 数据库接口
118 每月下载次数
用于 mavryk-smart-rollup
340KB
7K SLoC
Mavryk Smart Rollup 内核的事务型存储。
此 crate 支持处理更新存储的对象。所有对象都存储在持久存储中。
要使用此 crate,提供对象的定义。对象结构应遵循以下指南
- 可以从 OwnedPath 创建,即,它实现了
From<OwnedPath>
- 它有直接在持久存储上操作的获取器和设置器,每个获取器和设置器都应接受一个 Runtime 作为参数以执行操作(在设置器的情况下为
mut
)。
注意 对象必须只查看以其 OwnedPath
为前缀的持久存储。
要使用此 crate,创建所需的价值结构和存储对象,如下所示
use mavryk_smart_rollup_host::runtime::Runtime;
use mavryk_smart_rollup_host::path::{concat, RefPath, OwnedPath};
use mavryk_smart_rollup_storage::storage::Storage;
use mavryk_smart_rollup_mock::MockHost;
struct MyValue {
path: OwnedPath,
}
const VALUE_PATH: RefPath = RefPath::assert_from(b"/value");
impl MyValue {
pub fn setter(&mut self, host: &mut impl Runtime, v: &str) {
let value_path = concat(&self.path, &VALUE_PATH)
.expect("Could not get path for value");
host.store_write(&value_path, v.as_bytes(), 0)
.expect("Could not set value");
}
pub fn getter(
&mut self,
host: &impl Runtime,
) -> Vec<u8> {
let value_path = concat(&self.path, &VALUE_PATH)
.expect("Could not get path for value");
host.store_read(&value_path, 0, 1024)
.expect("Could not read value")
}
}
impl From<OwnedPath> for MyValue {
fn from(path: OwnedPath) -> Self {
Self { path }
}
}
const VALUES_PATH: RefPath = RefPath::assert_from(b"/values");
let mut host = MockHost::default();
let mut storage = Storage::<MyValue>::init(&VALUES_PATH)
.expect("Could not create storage interface");
storage.begin_transaction(&mut host)
.expect("Could not begin transaction");
let value_id = RefPath::assert_from(b"/my.value.id");
let mut value = storage.create_new(&mut host, &value_id)
.expect("Could not create new value")
.expect("Value already exists");
value.setter(&mut host, "some value");
storage.commit_transaction(&mut host)
.expect("Could not commit transaction");
依赖项
~15MB
~287K SLoC