Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9d98d15
initial impl
shawntabrizi Jun 12, 2021
989d02b
expose in pallet_prelude
shawntabrizi Jun 12, 2021
a6321b1
temp test
shawntabrizi Jun 12, 2021
9ce58da
Apply suggestions from code review
gui1117 Jun 14, 2021
899eea4
implement with macro help.
gui1117 Jun 14, 2021
b5b74a1
Merge remote-tracking branch 'origin/master' into shawntabrizi-counte…
gui1117 Jun 15, 2021
43e635f
test for macro generation
gui1117 Jun 15, 2021
9885891
add iterable functions, some test and fixes
gui1117 Jun 15, 2021
2406021
Merge remote-tracking branch 'origin/master' into shawntabrizi-counte…
gui1117 Jun 16, 2021
4f31878
fix merge
gui1117 Jun 16, 2021
c588233
doc
gui1117 Jun 16, 2021
9c362a6
Update frame/support/src/storage/types/counted_map.rs
gui1117 Jul 5, 2021
ed44b79
Merge remote-tracking branch 'origin/master' into gui-shawntabrizi-co…
gui1117 Aug 6, 2021
cdb3fa3
fix merge
gui1117 Aug 6, 2021
f7417cf
fmt
gui1117 Aug 6, 2021
61208a4
fix spelling
gui1117 Aug 6, 2021
30da101
improve on removal
gui1117 Aug 6, 2021
c546c2e
fix partial storage info
gui1117 Aug 6, 2021
1e11c16
fmt
gui1117 Aug 6, 2021
497bc69
add license
gui1117 Aug 7, 2021
cc249fb
suggested renames
gui1117 Aug 7, 2021
4d6de59
fix typo
gui1117 Aug 7, 2021
3313f73
Merge remote-tracking branch 'origin/master' into gui-shawntabrizi-co…
gui1117 Sep 15, 2021
a4c7f3f
fix test
gui1117 Sep 15, 2021
4841dbb
fmt
gui1117 Sep 15, 2021
b3d2111
fix ui tests
gui1117 Sep 16, 2021
4b62aab
clearer doc
gui1117 Sep 16, 2021
448937d
better doc
gui1117 Sep 16, 2021
cc13432
add metadata test
gui1117 Sep 16, 2021
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
Prev Previous commit
Next Next commit
add iterable functions, some test and fixes
  • Loading branch information
gui1117 committed Jun 15, 2021
commit 98858912f7536dccd67ed4bc4de20229077801ad
35 changes: 0 additions & 35 deletions frame/example/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,41 +181,6 @@ fn counted_map_works() {
assert_eq!(CountedMap::<Test>::count(), 0);
CountedMap::<Test>::insert(3, 3);
assert_eq!(CountedMap::<Test>::count(), 1);
CountedMap::<Test>::insert(3, 5);
assert_eq!(CountedMap::<Test>::count(), 1);
CountedMap::<Test>::insert(5, 5);
assert_eq!(CountedMap::<Test>::count(), 2);
CountedMap::<Test>::remove(5);
assert_eq!(CountedMap::<Test>::count(), 1);
// Does nothing
CountedMap::<Test>::take(4);
assert_eq!(CountedMap::<Test>::count(), 1);
CountedMap::<Test>::take(3);
assert_eq!(CountedMap::<Test>::count(), 0);

CountedMap::<Test>::mutate(3, |old| {
if old.is_none() {
*old = Some(69)
} else {
*old = Some(420)
}
});
assert_eq!(CountedMap::<Test>::get(3), Some(69));
assert_eq!(CountedMap::<Test>::count(), 1);

CountedMap::<Test>::mutate(3, |old| {
if old.is_none() {
*old = Some(69)
} else {
*old = Some(420)
}
});
assert_eq!(CountedMap::<Test>::get(3), Some(420));
assert_eq!(CountedMap::<Test>::count(), 1);

CountedMap::<Test>::mutate(3, |old| *old = None);
assert_eq!(CountedMap::<Test>::get(3), None);
assert_eq!(CountedMap::<Test>::count(), 0);
})
}

Expand Down
3 changes: 3 additions & 0 deletions frame/support/src/storage/generator/double_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ impl<K1, K2, V, G> storage::StorageDoubleMap<K1, K2, V> for G where
previous_key: prefix,
drain: false,
closure: |_raw_key, mut raw_value| V::decode(&mut raw_value),
phantom: Default::default(),
}
}

Expand Down Expand Up @@ -352,6 +353,7 @@ impl<
let mut key_material = G::Hasher2::reverse(raw_key_without_prefix);
Ok((K2::decode(&mut key_material)?, V::decode(&mut raw_value)?))
},
phantom: Default::default(),
}
}

Expand All @@ -374,6 +376,7 @@ impl<
let k2 = K2::decode(&mut k2_material)?;
Ok((k1, k2, V::decode(&mut raw_value)?))
},
phantom: Default::default(),
}
}

Expand Down
1 change: 1 addition & 0 deletions frame/support/src/storage/generator/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl<
let mut key_material = G::Hasher::reverse(raw_key_without_prefix);
Ok((K::decode(&mut key_material)?, V::decode(&mut raw_value)?))
},
phantom: Default::default(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions frame/support/src/storage/generator/nmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ where
previous_key: prefix,
drain: false,
closure: |_raw_key, mut raw_value| V::decode(&mut raw_value),
phantom: Default::default(),
}
}

Expand Down Expand Up @@ -325,6 +326,7 @@ impl<K: ReversibleKeyGenerator, V: FullCodec, G: StorageNMap<K, V>>
let partial_key = K::decode_partial_key(raw_key_without_prefix)?;
Ok((partial_key, V::decode(&mut raw_value)?))
},
phantom: Default::default(),
}
}

Expand All @@ -347,6 +349,7 @@ impl<K: ReversibleKeyGenerator, V: FullCodec, G: StorageNMap<K, V>>
let (final_key, _) = K::decode_final_key(raw_key_without_prefix)?;
Ok((final_key, V::decode(&mut raw_value)?))
},
phantom: Default::default(),
}
}

Expand Down
7 changes: 4 additions & 3 deletions frame/support/src/storage/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub fn storage_iter_with_suffix<T: Decode + Sized>(
Ok((raw_key_without_prefix.to_vec(), value))
};

PrefixIterator { prefix, previous_key, drain: false, closure }
PrefixIterator { prefix, previous_key, drain: false, closure, phantom: Default::default() }
}

/// Construct iterator to iterate over map items in `module` for the map called `item`.
Expand Down Expand Up @@ -203,7 +203,7 @@ pub fn storage_key_iter_with_suffix<K: Decode + Sized, T: Decode + Sized, H: Rev
let value = T::decode(&mut &raw_value[..])?;
Ok((key, value))
};
PrefixIterator { prefix, previous_key, drain: false, closure }
PrefixIterator { prefix, previous_key, drain: false, closure, phantom: Default::default() }
}

/// Get a particular value in storage by the `module`, the map's `item` name and the key `hash`.
Expand Down Expand Up @@ -332,11 +332,12 @@ pub fn move_prefix(from_prefix: &[u8], to_prefix: &[u8]) {
return
}

let iter = PrefixIterator {
let iter = PrefixIterator::<_> {
prefix: from_prefix.to_vec(),
previous_key: from_prefix.to_vec(),
drain: true,
closure: |key, value| Ok((key.to_vec(), value.to_vec())),
phantom: Default::default(),
};

for (key, value) in iter {
Expand Down
25 changes: 20 additions & 5 deletions frame/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,28 +666,41 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter;
}

/// Iterate over a prefix and decode raw_key and raw_value into `T`.
/// Iterate or drain over a prefix and decode raw_key and raw_value into `T`.
///
/// If any decoding fails it skips it and continues to the next key.
pub struct PrefixIterator<T> {
///
/// If draining, then the hook `OnRemoval::on_removal` is called after each removal.
pub struct PrefixIterator<T, OnRemoval=()> {
prefix: Vec<u8>,
previous_key: Vec<u8>,
/// If true then value are removed while iterating
drain: bool,
/// Function that take `(raw_key_without_prefix, raw_value)` and decode `T`.
/// `raw_key_without_prefix` is the raw storage key without the prefix iterated on.
closure: fn(&[u8], &[u8]) -> Result<T, codec::Error>,
phantom: core::marker::PhantomData<OnRemoval>,
}

/// Trait for specialising on removal logic of [`PrefixIterator`].
pub trait PrefixIteratorOnRemoval {
fn on_removal();
}

/// No-op implement.
impl PrefixIteratorOnRemoval for () {
fn on_removal() {}
}

impl<T> PrefixIterator<T> {
impl<T, OnRemoval> PrefixIterator<T, OnRemoval> {
/// Mutate this iterator into a draining iterator; items iterated are removed from storage.
pub fn drain(mut self) -> Self {
self.drain = true;
self
}
}

impl<T> Iterator for PrefixIterator<T> {
impl<T, OnRemoval: PrefixIteratorOnRemoval> Iterator for PrefixIterator<T, OnRemoval> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -708,7 +721,8 @@ impl<T> Iterator for PrefixIterator<T> {
}
};
if self.drain {
unhashed::kill(&self.previous_key)
unhashed::kill(&self.previous_key);
OnRemoval::on_removal();
}
let raw_key_without_prefix = &self.previous_key[self.prefix.len()..];
let item = match (self.closure)(raw_key_without_prefix, &raw_value[..]) {
Expand Down Expand Up @@ -894,6 +908,7 @@ pub trait StoragePrefixedMap<Value: FullCodec> {
previous_key: prefix.to_vec(),
drain: false,
closure: |_raw_key, mut raw_value| Value::decode(&mut raw_value),
phantom: Default::default(),
}
}

Expand Down
Loading