Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot item #409

Merged
merged 8 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () {
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
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