Skip to content

Commit

Permalink
Further deprecation of IntKey in storage-plus snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
ueco-jb committed Nov 30, 2021
1 parent 49cd2d4 commit 3a6d5ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
10 changes: 4 additions & 6 deletions packages/storage-plus/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ mod test {
assert_eq!(b"john".to_vec().as_slice(), &key[9..13]);
assert_eq!(b"maria".to_vec().as_slice(), &key[13..]);

let path = TRIPLE.key((b"john", 8u8.into(), "pedro"));
let path = TRIPLE.key((b"john", 8u8, "pedro"));
let key = path.deref();
// this should be prefixed(allow) || prefixed(john) || maria
assert_eq!(
Expand Down Expand Up @@ -387,21 +387,19 @@ mod test {
let mut store = MockStorage::new();

// save and load on a triple composite key
let triple = TRIPLE.key((b"owner", 10u8.into(), "recipient"));
let triple = TRIPLE.key((b"owner", 10u8, "recipient"));
assert_eq!(None, triple.may_load(&store).unwrap());
triple.save(&mut store, &1234).unwrap();
assert_eq!(1234, triple.load(&store).unwrap());

// not under other key
let different = TRIPLE
.may_load(&store, (b"owners", 10u8.into(), "ecipient"))
.may_load(&store, (b"owners", 10u8, "ecipient"))
.unwrap();
assert_eq!(None, different);

// matches under a proper copy
let same = TRIPLE
.load(&store, (b"owner", 10u8.into(), "recipient"))
.unwrap();
let same = TRIPLE.load(&store, (b"owner", 10u8, "recipient")).unwrap();
assert_eq!(1234, same);
}

Expand Down
31 changes: 13 additions & 18 deletions packages/storage-plus/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ pub use item::SnapshotItem;
pub use map::SnapshotMap;

use crate::de::KeyDeserialize;
use crate::{Bound, Map, Prefixer, PrimaryKey, U64Key};
use crate::{Bound, Map, Prefixer, PrimaryKey};
use cosmwasm_std::{Order, StdError, StdResult, Storage};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

/// Structure holding a map of checkpoints composited from
/// height (as U64Key) and counter of how many times it has
/// height (as u64) and counter of how many times it has
/// been checkpointed (as u32).
/// Stores all changes in changelog.
#[derive(Debug, Clone)]
pub(crate) struct Snapshot<'a, K, T> {
checkpoints: Map<'a, U64Key, u32>,
checkpoints: Map<'a, u64, u32>,

// this stores all changes (key, height). Must differentiate between no data written,
// and explicit None (just inserted)
pub changelog: Map<'a, (K, U64Key), ChangeSet<T>>,
pub changelog: Map<'a, (K, u64), ChangeSet<T>>,

// How aggressive we are about checkpointing all data
strategy: Strategy,
Expand All @@ -42,22 +42,20 @@ impl<'a, K, T> Snapshot<'a, K, T> {

pub fn add_checkpoint(&self, store: &mut dyn Storage, height: u64) -> StdResult<()> {
self.checkpoints
.update::<_, StdError>(store, height.into(), |count| {
Ok(count.unwrap_or_default() + 1)
})?;
.update::<_, StdError>(store, height, |count| Ok(count.unwrap_or_default() + 1))?;
Ok(())
}

pub fn remove_checkpoint(&self, store: &mut dyn Storage, height: u64) -> StdResult<()> {
let count = self
.checkpoints
.may_load(store, height.into())?
.may_load(store, height)?
.unwrap_or_default();
if count <= 1 {
self.checkpoints.remove(store, height.into());
self.checkpoints.remove(store, height);
Ok(())
} else {
self.checkpoints.save(store, height.into(), &(count - 1))
self.checkpoints.save(store, height, &(count - 1))
}
}
}
Expand Down Expand Up @@ -86,7 +84,7 @@ where
.transpose()?;
if let Some((height, _)) = checkpoint {
// any changelog for the given key since then?
let start = Bound::inclusive(U64Key::from(height));
let start = Bound::inclusive(height);
let first = self
.changelog
.prefix(k.clone())
Expand All @@ -107,7 +105,7 @@ where
let has = match self.strategy {
Strategy::EveryBlock => true,
Strategy::Never => false,
Strategy::Selected => self.checkpoints.may_load(store, height.into())?.is_some(),
Strategy::Selected => self.checkpoints.may_load(store, height)?.is_some(),
};
match has {
true => Ok(()),
Expand All @@ -116,10 +114,7 @@ where
}

pub fn has_changelog(&self, store: &mut dyn Storage, key: K, height: u64) -> StdResult<bool> {
Ok(self
.changelog
.may_load(store, (key, U64Key::from(height)))?
.is_some())
Ok(self.changelog.may_load(store, (key, height))?.is_some())
}

pub fn write_changelog(
Expand All @@ -130,7 +125,7 @@ where
old: Option<T>,
) -> StdResult<()> {
self.changelog
.save(store, (key, U64Key::from(height)), &ChangeSet { old })
.save(store, (key, height), &ChangeSet { old })
}

// may_load_at_height reads historical data from given checkpoints.
Expand All @@ -148,7 +143,7 @@ where

// this will look for the first snapshot of height >= given height
// If None, there is no snapshot since that time.
let start = Bound::inclusive(U64Key::new(height));
let start = Bound::inclusive(height.to_be_bytes().to_vec());
let first = self
.changelog
.prefix(key)
Expand Down

0 comments on commit 3a6d5ac

Please sign in to comment.