Creating the Application State
The struct
which defines your application's state can be found in
src/state.rs
. An application state is the data that is persisted on storage
between transactions.
To represent our counter, we're going to need a single u64
. To persist the
counter we'll be using Linera's view paradigm.
Views are a little like an ORM, however instead of mapping data structures to a relational database like Postgres, they are instead mapped onto key-value stores like RocksDB.
In vanilla Rust, we might represent our Counter as so:
// do not use this
struct Counter {
value: u64
}
However, to persist your data, you'll need to replace the existing Application
state struct in src/state.rs
with the following view:
/// The application state.
#[derive(RootView, async_graphql::SimpleObject)]
#[view(context = "ViewStorageContext")]
pub struct Counter {
pub value: RegisterView<u64>,
}
and all other occurrences of Application
in your app.
The RegisterView<T>
supports modifying a single value of type T
. There are
different types of views for different use-cases, but the majority of common
data structures have already been implemented:
- A
Vec
orVecDeque
corresponds to aLogView
- A
BTreeMap
corresponds to aMapView
if its values are primitive, or toCollectionView
if its values are other views; - A
Queue
corresponds to aQueueView
For an exhaustive list refer to the Views documentation.
Finally, run cargo check
to ensure that your changes compile.