#[cfg(feature = "state-in-transaction")]
mod committer;
#[cfg(feature = "state-in-transaction")]
mod dry_run_committer;
pub mod error;
pub mod hashmap;
#[cfg(feature = "state-merkle")]
pub mod merkle;
#[cfg(feature = "state-in-transaction")]
mod pruner;
#[cfg(feature = "state-in-transaction")]
mod reader;
#[cfg(feature = "state-in-transaction")]
mod state_trait;
use std::collections::HashMap;
pub use crate::state::error::{StatePruneError, StateReadError, StateWriteError};
#[cfg(feature = "state-in-transaction")]
pub use committer::Committer;
#[cfg(feature = "state-in-transaction")]
pub use dry_run_committer::DryRunCommitter;
#[cfg(feature = "state-in-transaction")]
pub use error::StateError;
#[cfg(feature = "state-in-transaction")]
pub use pruner::Pruner;
#[cfg(feature = "state-in-transaction")]
pub use reader::{Reader, ValueIter, ValueIterResult};
#[cfg(feature = "state-in-transaction")]
pub use state_trait::State;
#[derive(Debug)]
pub enum StateChange {
Set { key: String, value: Vec<u8> },
Delete { key: String },
}
impl Clone for StateChange {
fn clone(&self) -> Self {
match self {
StateChange::Set { key, value } => StateChange::Set {
key: key.clone(),
value: value.clone(),
},
StateChange::Delete { key } => StateChange::Delete { key: key.clone() },
}
}
}
impl StateChange {
pub fn key(&self) -> &str {
match self {
StateChange::Set { key, .. } => key,
StateChange::Delete { key } => key,
}
}
}
pub trait Write: Sync + Send {
type StateId;
type Key;
type Value;
fn commit(
&self,
state_id: &Self::StateId,
state_changes: &[StateChange],
) -> Result<Self::StateId, StateWriteError>;
fn compute_state_id(
&self,
state_id: &Self::StateId,
state_changes: &[StateChange],
) -> Result<Self::StateId, StateWriteError>;
}
pub trait Prune: Sync + Send {
type StateId;
type Key;
type Value;
fn prune(&self, state_ids: Vec<Self::StateId>) -> Result<Vec<Self::Key>, StatePruneError>;
}
pub trait Read: Send + Send {
type StateId;
type Key;
type Value;
fn get(
&self,
state_id: &Self::StateId,
keys: &[Self::Key],
) -> Result<HashMap<Self::Key, Self::Value>, StateReadError>;
fn clone_box(
&self,
) -> Box<dyn Read<StateId = Self::StateId, Key = Self::Key, Value = Self::Value>>;
}
impl<S, K, V> Clone for Box<dyn Read<StateId = S, Key = K, Value = V>> {
fn clone(&self) -> Box<dyn Read<StateId = S, Key = K, Value = V>> {
self.clone_box()
}
}