Skip to content

Commit 1bdd257

Browse files
committed
Further deprecation of IntKey in storage-plus snapshot
1 parent 49cd2d4 commit 1bdd257

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

packages/storage-plus/src/indexed_map.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,10 +1433,9 @@ mod test {
14331433

14341434
mod inclusive_bound {
14351435
use super::*;
1436-
use crate::U64Key;
14371436

14381437
struct Indexes<'a> {
1439-
secondary: MultiIndex<'a, (U64Key, Vec<u8>), u64>,
1438+
secondary: MultiIndex<'a, (u64, Vec<u8>), u64>,
14401439
}
14411440

14421441
impl<'a> IndexList<u64> for Indexes<'a> {
@@ -1451,7 +1450,7 @@ mod test {
14511450
fn composite_key_query() {
14521451
let indexes = Indexes {
14531452
secondary: MultiIndex::new(
1454-
|secondary, k| (U64Key::new(*secondary), k),
1453+
|secondary, k| (*secondary, k),
14551454
"test_map",
14561455
"test_map__secondary",
14571456
),
@@ -1468,7 +1467,7 @@ mod test {
14681467
.prefix_range(
14691468
&store,
14701469
None,
1471-
Some(PrefixBound::inclusive(1)),
1470+
Some(PrefixBound::inclusive(1u64)),
14721471
Order::Ascending,
14731472
)
14741473
.collect::<Result<_, _>>()

packages/storage-plus/src/map.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ mod test {
298298
assert_eq!(b"john".to_vec().as_slice(), &key[9..13]);
299299
assert_eq!(b"maria".to_vec().as_slice(), &key[13..]);
300300

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

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

395395
// not under other key
396396
let different = TRIPLE
397-
.may_load(&store, (b"owners", 10u8.into(), "ecipient"))
397+
.may_load(&store, (b"owners", 10u8, "ecipient"))
398398
.unwrap();
399399
assert_eq!(None, different);
400400

401401
// matches under a proper copy
402-
let same = TRIPLE
403-
.load(&store, (b"owner", 10u8.into(), "recipient"))
404-
.unwrap();
402+
let same = TRIPLE.load(&store, (b"owner", 10u8, "recipient")).unwrap();
405403
assert_eq!(1234, same);
406404
}
407405

packages/storage-plus/src/snapshot/mod.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ pub use item::SnapshotItem;
66
pub use map::SnapshotMap;
77

88
use crate::de::KeyDeserialize;
9-
use crate::{Bound, Map, Prefixer, PrimaryKey, U64Key};
9+
use crate::{Bound, Map, Prefixer, PrimaryKey};
1010
use cosmwasm_std::{Order, StdError, StdResult, Storage};
1111
use serde::de::DeserializeOwned;
1212
use serde::{Deserialize, Serialize};
1313

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

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

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

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

5149
pub fn remove_checkpoint(&self, store: &mut dyn Storage, height: u64) -> StdResult<()> {
5250
let count = self
5351
.checkpoints
54-
.may_load(store, height.into())?
52+
.may_load(store, height)?
5553
.unwrap_or_default();
5654
if count <= 1 {
57-
self.checkpoints.remove(store, height.into());
55+
self.checkpoints.remove(store, height);
5856
Ok(())
5957
} else {
60-
self.checkpoints.save(store, height.into(), &(count - 1))
58+
self.checkpoints.save(store, height, &(count - 1))
6159
}
6260
}
6361
}
@@ -86,7 +84,7 @@ where
8684
.transpose()?;
8785
if let Some((height, _)) = checkpoint {
8886
// any changelog for the given key since then?
89-
let start = Bound::inclusive(U64Key::from(height));
87+
let start = Bound::inclusive(height);
9088
let first = self
9189
.changelog
9290
.prefix(k.clone())
@@ -107,7 +105,7 @@ where
107105
let has = match self.strategy {
108106
Strategy::EveryBlock => true,
109107
Strategy::Never => false,
110-
Strategy::Selected => self.checkpoints.may_load(store, height.into())?.is_some(),
108+
Strategy::Selected => self.checkpoints.may_load(store, height)?.is_some(),
111109
};
112110
match has {
113111
true => Ok(()),
@@ -116,10 +114,7 @@ where
116114
}
117115

118116
pub fn has_changelog(&self, store: &mut dyn Storage, key: K, height: u64) -> StdResult<bool> {
119-
Ok(self
120-
.changelog
121-
.may_load(store, (key, U64Key::from(height)))?
122-
.is_some())
117+
Ok(self.changelog.may_load(store, (key, height))?.is_some())
123118
}
124119

125120
pub fn write_changelog(
@@ -130,7 +125,7 @@ where
130125
old: Option<T>,
131126
) -> StdResult<()> {
132127
self.changelog
133-
.save(store, (key, U64Key::from(height)), &ChangeSet { old })
128+
.save(store, (key, height), &ChangeSet { old })
134129
}
135130

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

149144
// this will look for the first snapshot of height >= given height
150145
// If None, there is no snapshot since that time.
151-
let start = Bound::inclusive(U64Key::new(height));
146+
let start = Bound::inclusive(height.to_be_bytes().to_vec());
152147
let first = self
153148
.changelog
154149
.prefix(key)

0 commit comments

Comments
 (0)