Skip to content

Commit

Permalink
Merge pull request #409 from CosmWasm/snapshot-item
Browse files Browse the repository at this point in the history
Snapshot item
  • Loading branch information
ethanfrey authored Sep 13, 2021
2 parents ad73c25 + 505be01 commit 5fc8ae9
Show file tree
Hide file tree
Showing 6 changed files with 707 additions and 363 deletions.
30 changes: 10 additions & 20 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,19 @@ use serde::Serialize;

use crate::keys::{EmptyPrefix, Prefixer, PrimaryKey};
use crate::prefix::{Bound, Prefix};
use crate::snapshot::SnapshotMap;
use crate::snapshot_map::SnapshotMap;
use crate::{IndexList, Path, Strategy};

/// IndexedSnapshotMap works like a SnapshotMap but has a secondary index
pub struct IndexedSnapshotMap<'a, K, T, I>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
{
pub struct IndexedSnapshotMap<'a, K, T, I> {
pk_namespace: &'a [u8],
primary: SnapshotMap<'a, K, T>,
/// This is meant to be read directly to get the proper types, like:
/// map.idx.owner.items(...)
pub idx: I,
}

impl<'a, K, T, I> IndexedSnapshotMap<'a, K, T, I>
where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
{
impl<'a, K, T, I> IndexedSnapshotMap<'a, K, T, I> {
pub fn new(
pk_namespace: &'a str,
checkpoints: &'a str,
Expand All @@ -43,22 +33,22 @@ where
idx: indexes,
}
}
}

impl<'a, K, T, I> IndexedSnapshotMap<'a, K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a> + Prefixer<'a>,
I: IndexList<T>,
{
pub fn add_checkpoint(&self, store: &mut dyn Storage, height: u64) -> StdResult<()> {
self.primary.add_checkpoint(store, height)
}

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

impl<'a, K, T, I> IndexedSnapshotMap<'a, K, T, I>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a> + Prefixer<'a>,
I: IndexList<T>,
{
pub fn may_load_at_height(
&self,
store: &dyn Storage,
Expand Down
10 changes: 10 additions & 0 deletions packages/storage-plus/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ pub trait PrimaryKey<'a>: Clone {
}
}

// Empty / no primary key
impl<'a> PrimaryKey<'a> for () {
type Prefix = ();
type SubPrefix = ();

fn key(&self) -> Vec<&[u8]> {
vec![]
}
}

impl<'a> PrimaryKey<'a> for &'a [u8] {
type Prefix = ();
type SubPrefix = ();
Expand Down
6 changes: 5 additions & 1 deletion packages/storage-plus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod map;
mod path;
mod prefix;
mod snapshot;
mod snapshot_item;
mod snapshot_map;

pub use endian::Endian;
#[cfg(feature = "iterator")]
Expand All @@ -27,5 +29,7 @@ pub use map::Map;
pub use path::Path;
#[cfg(feature = "iterator")]
pub use prefix::{range_with_prefix, Bound, Prefix};
pub use snapshot::Strategy;
pub use snapshot_item::SnapshotItem;
#[cfg(feature = "iterator")]
pub use snapshot::{SnapshotMap, Strategy};
pub use snapshot_map::SnapshotMap;
Loading

0 comments on commit 5fc8ae9

Please sign in to comment.