mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 07:27:55 +00:00
Safe and sane multi-item storage removal (#11490)
* Fix overlay prefix removal result * Second part of the overlay prefix removal fix. * Report only items deleted from storage in clear_prefix * Fix kill_prefix * Formatting * Remove unused code * Fixes * Fixes * Introduce clear_prefix host function v3 * Formatting * Use v2 for now * Fixes * Formatting * Docs * Child prefix removal should also hide v3 for now * Fixes * Fixes * Formatting * Fixes * apply_to_keys_whle takes start_at * apply_to_keys_whle takes start_at * apply_to_keys_whle takes start_at * Cursor API; force limits * Use unsafe deprecated functions * Formatting * Fixes * Grumbles * Fixes * Docs * Some nitpicks 🙈 * Update primitives/externalities/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * Formatting * Fixes * cargo fmt * Fixes * Update primitives/io/src/lib.rs Co-authored-by: Keith Yeung <kungfukeith11@gmail.com> * Formatting * Fixes Co-authored-by: Bastian Köcher <info@kchr.de> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Keith Yeung <kungfukeith11@gmail.com>
This commit is contained in:
@@ -516,10 +516,34 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
|
||||
/// Calling this multiple times per block with a `limit` set leads always to the same keys being
|
||||
/// removed and the same result being returned. This happens because the keys to delete in the
|
||||
/// overlay are not taken into account when deleting keys in the backend.
|
||||
#[deprecated = "Use `clear_prefix` instead"]
|
||||
fn remove_prefix<KArg1>(k1: KArg1, limit: Option<u32>) -> sp_io::KillStorageResult
|
||||
where
|
||||
KArg1: ?Sized + EncodeLike<K1>;
|
||||
|
||||
/// Remove all values under the first key `k1` in the overlay and up to `maybe_limit` in the
|
||||
/// backend.
|
||||
///
|
||||
/// All values in the client overlay will be deleted, if `maybe_limit` is `Some` then up to
|
||||
/// that number of values are deleted from the client backend, otherwise all values in the
|
||||
/// client backend are deleted.
|
||||
///
|
||||
/// ## Cursors
|
||||
///
|
||||
/// The `maybe_cursor` parameter should be `None` for the first call to initial removal.
|
||||
/// If the resultant `maybe_cursor` is `Some`, then another call is required to complete the
|
||||
/// removal operation. This value must be passed in as the subsequent call's `maybe_cursor`
|
||||
/// parameter. If the resultant `maybe_cursor` is `None`, then the operation is complete and no
|
||||
/// items remain in storage provided that no items were added between the first calls and the
|
||||
/// final call.
|
||||
fn clear_prefix<KArg1>(
|
||||
k1: KArg1,
|
||||
limit: u32,
|
||||
maybe_cursor: Option<&[u8]>,
|
||||
) -> sp_io::MultiRemovalResults
|
||||
where
|
||||
KArg1: ?Sized + EncodeLike<K1>;
|
||||
|
||||
/// Iterate over values that share the first key.
|
||||
fn iter_prefix_values<KArg1>(k1: KArg1) -> PrefixIterator<V>
|
||||
where
|
||||
@@ -657,10 +681,42 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
|
||||
/// Calling this multiple times per block with a `limit` set leads always to the same keys being
|
||||
/// removed and the same result being returned. This happens because the keys to delete in the
|
||||
/// overlay are not taken into account when deleting keys in the backend.
|
||||
#[deprecated = "Use `clear_prefix` instead"]
|
||||
fn remove_prefix<KP>(partial_key: KP, limit: Option<u32>) -> sp_io::KillStorageResult
|
||||
where
|
||||
K: HasKeyPrefix<KP>;
|
||||
|
||||
/// Attempt to remove items from the map matching a `partial_key` prefix.
|
||||
///
|
||||
/// Returns [`MultiRemovalResults`](sp_io::MultiRemovalResults) to inform about the result. Once
|
||||
/// the resultant `maybe_cursor` field is `None`, then no further items remain to be deleted.
|
||||
///
|
||||
/// NOTE: After the initial call for any given map, it is important that no further items
|
||||
/// are inserted into the map which match the `partial key`. If so, then the map may not be
|
||||
/// empty when the resultant `maybe_cursor` is `None`.
|
||||
///
|
||||
/// # Limit
|
||||
///
|
||||
/// A `limit` must be provided in order to cap the maximum
|
||||
/// amount of deletions done in a single call. This is one fewer than the
|
||||
/// maximum number of backend iterations which may be done by this operation and as such
|
||||
/// represents the maximum number of backend deletions which may happen. A `limit` of zero
|
||||
/// implies that no keys will be deleted, though there may be a single iteration done.
|
||||
///
|
||||
/// # Cursor
|
||||
///
|
||||
/// A *cursor* may be passed in to this operation with `maybe_cursor`. `None` should only be
|
||||
/// passed once (in the initial call) for any given storage map and `partial_key`. Subsequent
|
||||
/// calls operating on the same map/`partial_key` should always pass `Some`, and this should be
|
||||
/// equal to the previous call result's `maybe_cursor` field.
|
||||
fn clear_prefix<KP>(
|
||||
partial_key: KP,
|
||||
limit: u32,
|
||||
maybe_cursor: Option<&[u8]>,
|
||||
) -> sp_io::MultiRemovalResults
|
||||
where
|
||||
K: HasKeyPrefix<KP>;
|
||||
|
||||
/// Iterate over values that share the partial prefix key.
|
||||
fn iter_prefix_values<KP>(partial_key: KP) -> PrefixIterator<V>
|
||||
where
|
||||
@@ -1111,8 +1167,36 @@ pub trait StoragePrefixedMap<Value: FullCodec> {
|
||||
/// Calling this multiple times per block with a `limit` set leads always to the same keys being
|
||||
/// removed and the same result being returned. This happens because the keys to delete in the
|
||||
/// overlay are not taken into account when deleting keys in the backend.
|
||||
#[deprecated = "Use `clear` instead"]
|
||||
fn remove_all(limit: Option<u32>) -> sp_io::KillStorageResult {
|
||||
sp_io::storage::clear_prefix(&Self::final_prefix(), limit)
|
||||
unhashed::clear_prefix(&Self::final_prefix(), limit, None).into()
|
||||
}
|
||||
|
||||
/// Attempt to remove all items from the map.
|
||||
///
|
||||
/// Returns [`MultiRemovalResults`](sp_io::MultiRemovalResults) to inform about the result. Once
|
||||
/// the resultant `maybe_cursor` field is `None`, then no further items remain to be deleted.
|
||||
///
|
||||
/// NOTE: After the initial call for any given map, it is important that no further items
|
||||
/// are inserted into the map. If so, then the map may not be empty when the resultant
|
||||
/// `maybe_cursor` is `None`.
|
||||
///
|
||||
/// # Limit
|
||||
///
|
||||
/// A `limit` must always be provided through in order to cap the maximum
|
||||
/// amount of deletions done in a single call. This is one fewer than the
|
||||
/// maximum number of backend iterations which may be done by this operation and as such
|
||||
/// represents the maximum number of backend deletions which may happen. A `limit` of zero
|
||||
/// implies that no keys will be deleted, though there may be a single iteration done.
|
||||
///
|
||||
/// # Cursor
|
||||
///
|
||||
/// A *cursor* may be passed in to this operation with `maybe_cursor`. `None` should only be
|
||||
/// passed once (in the initial call) for any given storage map. Subsequent calls
|
||||
/// operating on the same map should always pass `Some`, and this should be equal to the
|
||||
/// previous call result's `maybe_cursor` field.
|
||||
fn clear(limit: u32, maybe_cursor: Option<&[u8]>) -> sp_io::MultiRemovalResults {
|
||||
unhashed::clear_prefix(&Self::final_prefix(), Some(limit), maybe_cursor)
|
||||
}
|
||||
|
||||
/// Iter over all value of the storage.
|
||||
@@ -1427,7 +1511,7 @@ mod test {
|
||||
assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2, 3, 4]);
|
||||
|
||||
// test removal
|
||||
MyStorage::remove_all(None);
|
||||
let _ = MyStorage::clear(u32::max_value(), None);
|
||||
assert!(MyStorage::iter_values().collect::<Vec<_>>().is_empty());
|
||||
|
||||
// test migration
|
||||
@@ -1437,7 +1521,7 @@ mod test {
|
||||
assert!(MyStorage::iter_values().collect::<Vec<_>>().is_empty());
|
||||
MyStorage::translate_values(|v: u32| Some(v as u64));
|
||||
assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2]);
|
||||
MyStorage::remove_all(None);
|
||||
let _ = MyStorage::clear(u32::max_value(), None);
|
||||
|
||||
// test migration 2
|
||||
unhashed::put(&[&k[..], &vec![1][..]].concat(), &1u128);
|
||||
@@ -1449,7 +1533,7 @@ mod test {
|
||||
assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2, 3]);
|
||||
MyStorage::translate_values(|v: u128| Some(v as u64));
|
||||
assert_eq!(MyStorage::iter_values().collect::<Vec<_>>(), vec![1, 2, 3]);
|
||||
MyStorage::remove_all(None);
|
||||
let _ = MyStorage::clear(u32::max_value(), None);
|
||||
|
||||
// test that other values are not modified.
|
||||
assert_eq!(unhashed::get(&key_before[..]), Some(32u64));
|
||||
|
||||
Reference in New Issue
Block a user