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>

source

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);
source

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);
source

pub fn add_checkpoint( &self, store: &mut dyn Storage, height: u64 ) -> StdResult<()>

source

pub fn remove_checkpoint( &self, store: &mut dyn Storage, height: u64 ) -> StdResult<()>

source

pub fn changelog(&self) -> Map<u64, ChangeSet<T>>

source§

impl<T> SnapshotItem<T>

source

pub fn save( &self, store: &mut dyn Storage, data: &T, height: u64 ) -> StdResult<()>

source

pub fn remove(&self, store: &mut dyn Storage, height: u64) -> StdResult<()>

source

pub fn load(&self, store: &dyn Storage) -> StdResult<T>

load will return an error if no data is set, or on parse error

source

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

source

pub fn may_load_at_height( &self, store: &dyn Storage, height: u64 ) -> StdResult<Option<T>>

source

pub fn assert_checkpointed( &self, store: &dyn Storage, height: u64 ) -> StdResult<()>

source

pub fn update<A, E>( &self, store: &mut dyn Storage, height: u64, action: A ) -> Result<T, E>
where A: FnOnce(Option<T>) -> Result<T, E>, E: From<StdError>,

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

Auto Trait Implementations§

§

impl<T> Freeze for SnapshotItem<T>

§

impl<T> RefUnwindSafe for SnapshotItem<T>
where T: RefUnwindSafe,

§

impl<T> Send for SnapshotItem<T>
where T: Send,

§

impl<T> Sync for SnapshotItem<T>
where T: Sync,

§

impl<T> Unpin for SnapshotItem<T>
where T: Unpin,

§

impl<T> UnwindSafe for SnapshotItem<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<U> As for U

source§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.