Links

Storing Data

fn ws_set_db(key_ptr: *const u8, key_size: i32, value_ptr: *const u8, value_size: i32) -> i32;
Notice: W3bstream Alpha release only includes a storage interface implementation in the form of a simple key-value database. Later releases of W3bstream will provide further storage options, both centralized and decentralized.
A W3bstream applet can store an object in the key-value host storage using the ws_set_db W3bstream function. As any other W3bstream exported functions, it must be imported before it can be used in an applet:
Go
Rust
// go:wasm-module env
// export ws_set_db
func _ws_set_db(key_ptr, key_size, value_ptr, value_size uint32) int32
#[link(wasm_import_module = "env")]
extern "C" {
fn ws_set_db(
key_ptr: *const u8,
key_size: i32,
value_ptr: *const u8,
value_size: i32)
-> i32;
}
The example below shows how you can store a string in the key-value storage:
Rust
#[link(wasm_import_module = "env")]
extern "C" {
fn ws_set_db(key_ptr: *const u8, key_size: i32, value_ptr: *const u8, value_size: i32) -> i32;
}
#[no_mangle]
// This handler will be matched by the default Project event strategy in W3bstream
pub extern "C" fn start(event_id: i32) -> i32 {
// Storing some strings in the key-value storage
set_db("first_name", "Vitalik");
set_db("last_name","Buterin");
set_db("age", "23");
return 0;
}
pub fn set_db(key: &String, value: String) -> Result<()> {
match unsafe {
ws_set_db(
key.as_ptr(),
key.len() as _,
value.as_ptr(),
value.len() as _, )
} {
0 => Ok(()),
_ => bail!("Error while storing the value"),
}
}
💡 Learn more