Skip to main content

ContextReadState

Trait ContextReadState 

Source
pub trait ContextReadState<'ctx>: SealedContext<'ctx> {
    // Provided methods
    fn get<T: Deserialize + 'static>(
        &self,
        key: &'ctx str,
    ) -> impl Future<Output = Result<Option<T>, TerminalError>> + 'ctx { ... }
    fn get_keys(
        &'ctx self,
    ) -> impl Future<Output = Result<Vec<String>, TerminalError>> + 'ctx { ... }
}
Expand description

§Reading state

You can store key-value state in Restate. Restate makes sure the state is consistent with the processing of the code execution.

This feature is only available for Virtual Objects and Workflows:

  • For Virtual Objects, the state is isolated per Virtual Object and lives forever (across invocations for that object).
  • For Workflows, you can think of it as if every workflow execution is a new object. So the state is isolated to a single workflow execution. The state can only be mutated by the run handler of the workflow. The other handlers can only read the state.
    /// 1. Listing state keys
    /// List all the state keys that have entries in the state store for this object, via:
    let keys = ctx.get_keys().await?;

    /// 2. Getting a state value
    let my_string = ctx
        .get::<String>("my-key")
        .await?
        .unwrap_or(String::from("my-default"));
    let my_number = ctx.get::<f64>("my-number-key").await?.unwrap_or_default();

    /// 3. Setting a state value
    ctx.set("my-key", String::from("my-value"));

    /// 4. Clearing a state value
    ctx.clear("my-key");

    /// 5. Clearing all state values
    /// Deletes all the state in Restate for the object
    ctx.clear_all();

For more info about serialization, see crate::serde

§Command-line introspection

You can inspect and edit the K/V state stored in Restate via psql and the CLI. Have a look at the introspection docs for more information.

Provided Methods§

Source

fn get<T: Deserialize + 'static>( &self, key: &'ctx str, ) -> impl Future<Output = Result<Option<T>, TerminalError>> + 'ctx

Get state

Source

fn get_keys( &'ctx self, ) -> impl Future<Output = Result<Vec<String>, TerminalError>> + 'ctx

Get state keys

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<'ctx, CTX: SealedContext<'ctx> + SealedCanReadState> ContextReadState<'ctx> for CTX