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

Feature/limited translate #2

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
75 changes: 74 additions & 1 deletion frame/support/src/storage/generator/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

use crate::{
hash::{ReversibleStorageHasher, StorageHasher},
storage::{self, storage_prefix, unhashed, KeyPrefixIterator, PrefixIterator, StorageAppend},
storage::{
self, storage_prefix, unhashed, KeyPrefixIterator, PrefixIterator, StorageAppend,
TranslateResult,
},
Never,
};
use codec::{Decode, Encode, EncodeLike, FullCodec, FullEncode};
Expand Down Expand Up @@ -207,6 +210,76 @@ where
}
}
}

fn single_translate<O: Decode, F: FnMut(K, O) -> Option<V>>(
last_processed_key: Option<Vec<u8>>,
mut f: F,
) -> TranslateResult {
let prefix = G::prefix_hash();
let previous_key = last_processed_key.unwrap_or(prefix.clone());

let mut result = TranslateResult::default();

if let Some(next) =
sp_io::storage::next_key(&previous_key).filter(|n| n.starts_with(&prefix))
{
result.previous_key = Some(next.clone());
result.number += 1;

let value = match unhashed::get::<O>(&next) {
Some(value) => value,
None => {
log::error!("Invalid translate: fail to decode old value");
return result
},
};

let mut key_material = G::Hasher::reverse(&next[prefix.len()..]);
let key = match K::decode(&mut key_material) {
Ok(key) => key,
Err(_) => {
log::error!("Invalid translate: fail to decode key");
return result
},
};

match f(key, value) {
Some(new) => unhashed::put::<V>(&previous_key, &new),
None => unhashed::kill(&previous_key),
}
} else {
result.is_finalized = true;
}

result
}

fn limited_translate<O: Decode, F: FnMut(K, O) -> Option<V>>(
limit: Option<u32>,
mut last_processed_key: Option<Vec<u8>>,
mut f: F,
) -> TranslateResult {
let limit = limit.unwrap_or(u32::MAX);
let mut result = TranslateResult::default();

for _ in 0..limit {
let temp_res = Self::single_translate(last_processed_key, &mut f);

// TODO: add custom methods to the result object and make this code cleaner!

if temp_res.is_finalized {
result.is_finalized = true;
break
}

result.previous_key = temp_res.previous_key.clone();
result.number += 1;

last_processed_key = temp_res.previous_key;
}

result
}
}

impl<K: FullEncode, V: FullCodec, G: StorageMap<K, V>> storage::StorageMap<K, V> for G {
Expand Down
32 changes: 32 additions & 0 deletions frame/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,38 @@ pub trait IterableStorageMap<K: FullEncode, V: FullCodec>: StorageMap<K, V> {
///
/// NOTE: If a value fail to decode because storage is corrupted then it is skipped.
fn translate<O: Decode, F: FnMut(K, O) -> Option<V>>(f: F);

/// limit - If Some(x), limits number of translations that can be done by up to **x**
/// last_processed_key - If Some(k), starts iteration from key after **k**
///
/// returns report on how much values were processed
fn limited_translate<O: Decode, F: FnMut(K, O) -> Option<V>>(
limit: Option<u32>,
last_processed_key: Option<Vec<u8>>,
f: F,
) -> TranslateResult;

/// last_processed_key - If Some(k), starts iteration from key after **k**
///
/// returns report on whether the value was processed and if any more remain
fn single_translate<O: Decode, F: FnMut(K, O) -> Option<V>>(
last_processed_key: Option<Vec<u8>>,
f: F,
) -> TranslateResult;
}

#[derive(Default)]
pub struct TranslateResult {
/// Number of successfully translated values
pub number: u32,
/// `true` in case no more values remain
pub is_finalized: bool,
/// Previous processed key, if any.
pub previous_key: Option<Vec<u8>>,
}

impl TranslateResult {
// TODO
}

/// A strongly-typed double map in storage whose secondary keys and values can be iterated over.
Expand Down