Struct cw_storage_plus::SnapshotItem
source · pub struct SnapshotItem<T> { /* private fields */ }
Expand description
Item that maintains a snapshot of one or more checkpoints. We can query historical data as well as current state. What data is snapshotted depends on the Strategy.
Implementations§
source§impl<T> SnapshotItem<T>
impl<T> SnapshotItem<T>
sourcepub const fn new(
storage_key: &'static str,
checkpoints: &'static str,
changelog: &'static str,
strategy: Strategy
) -> Self
pub const fn new( storage_key: &'static str, checkpoints: &'static str, changelog: &'static str, strategy: Strategy ) -> Self
Creates a new SnapshotItem
with the given storage keys and strategy.
This is a const fn only suitable when all the storage keys provided are
static strings.
Example:
use cw_storage_plus::{SnapshotItem, Strategy};
SnapshotItem::<u64>::new(
"every",
"every__check",
"every__change",
Strategy::EveryBlock);
sourcepub fn new_dyn(
storage_key: impl Into<Namespace>,
checkpoints: impl Into<Namespace>,
changelog: impl Into<Namespace>,
strategy: Strategy
) -> Self
pub fn new_dyn( storage_key: impl Into<Namespace>, checkpoints: impl Into<Namespace>, changelog: impl Into<Namespace>, strategy: Strategy ) -> Self
Creates a new SnapshotItem
with the given storage keys and strategy.
Use this if you might need to handle dynamic strings. Otherwise, you might
prefer SnapshotItem::new
.
Example:
use cw_storage_plus::{SnapshotItem, Strategy};
let key = "every";
let checkpoints_key = format!("{}_check", key);
let changelog_key = format!("{}_change", key);
SnapshotItem::<u64>::new_dyn(
key,
checkpoints_key,
changelog_key,
Strategy::EveryBlock);
pub fn add_checkpoint( &self, store: &mut dyn Storage, height: u64 ) -> StdResult<()>
pub fn remove_checkpoint( &self, store: &mut dyn Storage, height: u64 ) -> StdResult<()>
pub fn changelog(&self) -> Map<u64, ChangeSet<T>>
source§impl<T> SnapshotItem<T>
impl<T> SnapshotItem<T>
pub fn save( &self, store: &mut dyn Storage, data: &T, height: u64 ) -> StdResult<()>
pub fn remove(&self, store: &mut dyn Storage, height: u64) -> StdResult<()>
sourcepub fn load(&self, store: &dyn Storage) -> StdResult<T>
pub fn load(&self, store: &dyn Storage) -> StdResult<T>
load will return an error if no data is set, or on parse error
sourcepub fn may_load(&self, store: &dyn Storage) -> StdResult<Option<T>>
pub fn may_load(&self, store: &dyn Storage) -> StdResult<Option<T>>
may_load will parse the data stored if present, returns Ok(None) if no data there. returns an error on parsing issues
pub fn may_load_at_height( &self, store: &dyn Storage, height: u64 ) -> StdResult<Option<T>>
pub fn assert_checkpointed( &self, store: &dyn Storage, height: u64 ) -> StdResult<()>
sourcepub fn update<A, E>(
&self,
store: &mut dyn Storage,
height: u64,
action: A
) -> Result<T, E>
pub fn update<A, E>( &self, store: &mut dyn Storage, height: u64, action: A ) -> Result<T, E>
Loads the data, perform the specified action, and store the result in the database. This is a shorthand for some common sequences, which may be useful.
If the data exists, action(Some(value))
is called. Otherwise action(None)
is called.
This is a bit more customized than needed to only read “old” value 1 time, not 2 per naive approach