The storage interface
Access high performance storage and databases from a Rust API
This page is outdated. Please visit here to check out how to access storage interface Rust APIs.
The SSVM storage interface provides a Rust API that allows programs to persist arbitrary data into a key value store. The data store is configured and started by the SSVM and hence is transparent to the Rust application. Rust developers can view this as an abstract storage space for application data.
The storage interface provides much higher performance and data throughput than using WASI calls to access database on the file system or via network. It is also much easier to work with than most database APIs in Rust.
The example source code for this tutorial is here.
Dependency
You will need to include the rust_storage_interface_library crate in your Cargo.toml
. In many cases, you will also need serde dependency for storing and loading structs. Add dependency for serialize_deserialize_u8_i32 if you wish to store Vec<u8>
or &[u8]
byte arrays.
In your Rust code, do this.
Store and load primitive types
With a simple API call, you can store and load Rust data of primitive types.
Store and load string data
Strings are similarly easy. With string capabilities, you can store and load complex JSON structures.
Store and load structs data
With serde, it is easy to store a struct. Notice that, when you load the data, you will need to pass in an "empty" struct of the same type in order for the the compiler to know the returned struct type.
Store and load binary data
Often times, it is easier just to store data in binary format as a byte array. Think file content, images, videos, and binary serialized structs etc. With the storage interface, we do that by packing and unpacking the byte array into i32
arrays.
For more examples and how it works under the hood, please review the spec document.
Last updated