diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index f20a65ec6..fca7ee1a4 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -409,7 +409,7 @@ pub fn query_all_allowances( // we use raw addresses here.... let start = start_after.map(Bound::exclusive); - let res: StdResult> = ALLOWANCES + let allowances = ALLOWANCES .range(deps.storage, start, None, Order::Ascending) .filter(|item| { if let Ok((_, allow)) = item { @@ -420,16 +420,14 @@ pub fn query_all_allowances( }) .take(limit) .map(|item| { - item.and_then(|(k, allow)| { - Ok(AllowanceInfo { - spender: String::from_utf8(k)?, - balance: allow.balance, - expires: allow.expires, - }) + item.map(|(addr, allow)| AllowanceInfo { + spender: addr.into(), + balance: allow.balance, + expires: allow.expires, }) }) - .collect(); - Ok(AllAllowancesResponse { allowances: res? }) + .collect::>>()?; + Ok(AllAllowancesResponse { allowances }) } // return a list of all permissions here @@ -441,19 +439,17 @@ pub fn query_all_permissions( let limit = calc_limit(limit); let start = start_after.map(Bound::exclusive); - let res: StdResult> = PERMISSIONS + let permissions = PERMISSIONS .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|item| { - item.and_then(|(k, perm)| { - Ok(PermissionsInfo { - spender: String::from_utf8(k)?, - permissions: perm, - }) + item.map(|(addr, perm)| PermissionsInfo { + spender: addr.into(), + permissions: perm, }) }) - .collect(); - Ok(AllPermissionsResponse { permissions: res? }) + .collect::>>()?; + Ok(AllPermissionsResponse { permissions }) } // Migrate contract if version is lower than current version diff --git a/contracts/cw1155-base/src/contract.rs b/contracts/cw1155-base/src/contract.rs index d52940a7a..e3195cff3 100644 --- a/contracts/cw1155-base/src/contract.rs +++ b/contracts/cw1155-base/src/contract.rs @@ -1,7 +1,7 @@ use cosmwasm_std::entry_point; use cosmwasm_std::{ - to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Record, Response, StdResult, - SubMsg, Uint128, + to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, SubMsg, + Uint128, }; use cw_storage_plus::Bound; @@ -478,10 +478,10 @@ pub fn query(deps: Deps, env: Env, msg: Cw1155QueryMsg) -> StdResult { } } -fn parse_approval(item: StdResult>) -> StdResult { - item.and_then(|(k, expires)| { - let spender = String::from_utf8(k)?; - Ok(cw1155::Approval { spender, expires }) +fn build_approval(item: StdResult<(Addr, Expiration)>) -> StdResult { + item.map(|(addr, expires)| cw1155::Approval { + spender: addr.into(), + expires, }) } @@ -501,7 +501,7 @@ fn query_all_approvals( .range(deps.storage, start, None, Order::Ascending) .filter(|r| include_expired || r.is_err() || !r.as_ref().unwrap().1.is_expired(&env.block)) .take(limit) - .map(parse_approval) + .map(build_approval) .collect::>()?; Ok(ApprovedForAllResponse { operators }) } @@ -517,9 +517,8 @@ fn query_tokens( let tokens = BALANCES .prefix(&owner) - .range(deps.storage, start, None, Order::Ascending) + .keys(deps.storage, start, None, Order::Ascending) .take(limit) - .map(|item| item.map(|(k, _)| String::from_utf8(k).unwrap())) .collect::>()?; Ok(TokensResponse { tokens }) } @@ -532,9 +531,8 @@ fn query_all_tokens( let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let start = start_after.map(Bound::exclusive); let tokens = TOKENS - .range(deps.storage, start, None, Order::Ascending) + .keys(deps.storage, start, None, Order::Ascending) .take(limit) - .map(|item| item.map(|(k, _)| String::from_utf8(k).unwrap())) .collect::>()?; Ok(TokensResponse { tokens }) } diff --git a/contracts/cw20-atomic-swap/src/state.rs b/contracts/cw20-atomic-swap/src/state.rs index ac67aa6b0..0a1202630 100644 --- a/contracts/cw20-atomic-swap/src/state.rs +++ b/contracts/cw20-atomic-swap/src/state.rs @@ -1,7 +1,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use cosmwasm_std::{Addr, Binary, BlockInfo, Order, StdError, StdResult, Storage}; +use cosmwasm_std::{Addr, Binary, BlockInfo, Order, StdResult, Storage}; use cw_storage_plus::{Bound, Map}; use cw20::{Balance, Expiration}; @@ -34,7 +34,6 @@ pub fn all_swap_ids( SWAPS .keys(storage, start, None, Order::Ascending) .take(limit) - .map(|k| String::from_utf8(k).map_err(|_| StdError::invalid_utf8("Parsing swap id"))) .collect() } diff --git a/contracts/cw20-base/src/enumerable.rs b/contracts/cw20-base/src/enumerable.rs index 2f97be5fa..cb7dc74fc 100644 --- a/contracts/cw20-base/src/enumerable.rs +++ b/contracts/cw20-base/src/enumerable.rs @@ -18,22 +18,19 @@ pub fn query_all_allowances( let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let start = start_after.map(Bound::exclusive); - let allowances: StdResult> = ALLOWANCES + let allowances = ALLOWANCES .prefix(&owner_addr) .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|item| { - let (k, v) = item?; - Ok(AllowanceInfo { - spender: String::from_utf8(k)?, - allowance: v.allowance, - expires: v.expires, + item.map(|(addr, allow)| AllowanceInfo { + spender: addr.into(), + allowance: allow.allowance, + expires: allow.expires, }) }) - .collect(); - Ok(AllAllowancesResponse { - allowances: allowances?, - }) + .collect::>()?; + Ok(AllAllowancesResponse { allowances }) } pub fn query_all_accounts( @@ -44,15 +41,13 @@ pub fn query_all_accounts( let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let start = start_after.map(Bound::exclusive); - let accounts: Result, _> = BALANCES + let accounts = BALANCES .keys(deps.storage, start, None, Order::Ascending) - .map(String::from_utf8) .take(limit) - .collect(); + .map(|item| item.map(Into::into)) + .collect::>()?; - Ok(AllAccountsResponse { - accounts: accounts?, - }) + Ok(AllAccountsResponse { accounts }) } #[cfg(test)] diff --git a/contracts/cw20-escrow/src/state.rs b/contracts/cw20-escrow/src/state.rs index 4539f826c..3940d6160 100644 --- a/contracts/cw20-escrow/src/state.rs +++ b/contracts/cw20-escrow/src/state.rs @@ -1,7 +1,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use cosmwasm_std::{Addr, Coin, Env, Order, StdError, StdResult, Storage, Timestamp}; +use cosmwasm_std::{Addr, Coin, Env, Order, StdResult, Storage, Timestamp}; use cw_storage_plus::Map; use cw20::{Balance, Cw20CoinVerified}; @@ -96,7 +96,6 @@ pub const ESCROWS: Map<&str, Escrow> = Map::new("escrow"); pub fn all_escrow_ids(storage: &dyn Storage) -> StdResult> { ESCROWS .keys(storage, None, None, Order::Ascending) - .map(|k| String::from_utf8(k).map_err(|_| StdError::invalid_utf8("parsing escrow key"))) .collect() } diff --git a/contracts/cw20-ics20/src/contract.rs b/contracts/cw20-ics20/src/contract.rs index a8834eb96..132c08dd6 100644 --- a/contracts/cw20-ics20/src/contract.rs +++ b/contracts/cw20-ics20/src/contract.rs @@ -151,32 +151,30 @@ fn query_port(deps: Deps) -> StdResult { } fn query_list(deps: Deps) -> StdResult { - let channels: StdResult> = CHANNEL_INFO - .range(deps.storage, None, None, Order::Ascending) + let channels = CHANNEL_INFO + .range_raw(deps.storage, None, None, Order::Ascending) .map(|r| r.map(|(_, v)| v)) - .collect(); - Ok(ListChannelsResponse { - channels: channels?, - }) + .collect::>()?; + Ok(ListChannelsResponse { channels }) } // make public for ibc tests pub fn query_channel(deps: Deps, id: String) -> StdResult { let info = CHANNEL_INFO.load(deps.storage, &id)?; // this returns Vec<(outstanding, total)> - let state: StdResult> = CHANNEL_STATE + let state = CHANNEL_STATE .prefix(&id) .range(deps.storage, None, None, Order::Ascending) .map(|r| { - let (k, v) = r?; - let denom = String::from_utf8(k)?; - let outstanding = Amount::from_parts(denom.clone(), v.outstanding); - let total = Amount::from_parts(denom, v.total_sent); - Ok((outstanding, total)) + r.map(|(denom, v)| { + let outstanding = Amount::from_parts(denom.clone(), v.outstanding); + let total = Amount::from_parts(denom, v.total_sent); + (outstanding, total) + }) }) - .collect(); + .collect::>>()?; // we want (Vec, Vec) - let (balances, total_sent) = state?.into_iter().unzip(); + let (balances, total_sent) = state.into_iter().unzip(); Ok(ChannelResponse { info, diff --git a/contracts/cw3-fixed-multisig/src/contract.rs b/contracts/cw3-fixed-multisig/src/contract.rs index 287946334..2f2ef70d5 100644 --- a/contracts/cw3-fixed-multisig/src/contract.rs +++ b/contracts/cw3-fixed-multisig/src/contract.rs @@ -17,9 +17,7 @@ use utils::Expiration; use crate::error::ContractError; use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; -use crate::state::{ - next_id, parse_id, Ballot, Config, Proposal, BALLOTS, CONFIG, PROPOSALS, VOTERS, -}; +use crate::state::{next_id, Ballot, Config, Proposal, BALLOTS, CONFIG, PROPOSALS, VOTERS}; // version info for migration info const CONTRACT_NAME: &str = "crates.io:cw3-fixed-multisig"; @@ -315,13 +313,13 @@ fn list_proposals( let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let start = start_after.map(Bound::exclusive_int); - let props: StdResult> = PROPOSALS + let proposals = PROPOSALS .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|p| map_proposal(&env.block, &threshold, p)) - .collect(); + .collect::>()?; - Ok(ProposalListResponse { proposals: props? }) + Ok(ProposalListResponse { proposals }) } fn reverse_proposals( @@ -338,30 +336,31 @@ fn reverse_proposals( let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let end = start_before.map(Bound::exclusive_int); - let props: StdResult> = PROPOSALS + let proposals = PROPOSALS .range(deps.storage, None, end, Order::Descending) .take(limit) .map(|p| map_proposal(&env.block, &threshold, p)) - .collect(); + .collect::>()?; - Ok(ProposalListResponse { proposals: props? }) + Ok(ProposalListResponse { proposals }) } fn map_proposal( block: &BlockInfo, threshold: &ThresholdResponse, - item: StdResult<(Vec, Proposal)>, + item: StdResult<(u64, Proposal)>, ) -> StdResult { - let (key, prop) = item?; - let status = prop.current_status(block); - Ok(ProposalResponse { - id: parse_id(&key)?, - title: prop.title, - description: prop.description, - msgs: prop.msgs, - status, - expires: prop.expires, - threshold: threshold.clone(), + item.map(|(id, prop)| { + let status = prop.current_status(block); + ProposalResponse { + id, + title: prop.title, + description: prop.description, + msgs: prop.msgs, + status, + expires: prop.expires, + threshold: threshold.clone(), + } }) } @@ -385,21 +384,20 @@ fn list_votes( let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let start = start_after.map(Bound::exclusive); - let votes: StdResult> = BALLOTS + let votes = BALLOTS .prefix(proposal_id) .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|item| { - let (key, ballot) = item?; - Ok(VoteInfo { - voter: String::from_utf8(key)?, + item.map(|(addr, ballot)| VoteInfo { + voter: addr.into(), vote: ballot.vote, weight: ballot.weight, }) }) - .collect(); + .collect::>()?; - Ok(VoteListResponse { votes: votes? }) + Ok(VoteListResponse { votes }) } fn query_voter(deps: Deps, voter: String) -> StdResult { @@ -416,19 +414,18 @@ fn list_voters( let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let start = start_after.map(Bound::exclusive); - let voters: StdResult> = VOTERS + let voters = VOTERS .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|item| { - let (key, weight) = item?; - Ok(VoterDetail { - addr: String::from_utf8(key)?, + item.map(|(addr, weight)| VoterDetail { + addr: addr.into(), weight, }) }) - .collect(); + .collect::>()?; - Ok(VoterListResponse { voters: voters? }) + Ok(VoterListResponse { voters }) } #[cfg(test)] diff --git a/contracts/cw3-flex-multisig/src/contract.rs b/contracts/cw3-flex-multisig/src/contract.rs index eafc2b221..38169b6b5 100644 --- a/contracts/cw3-flex-multisig/src/contract.rs +++ b/contracts/cw3-flex-multisig/src/contract.rs @@ -18,9 +18,7 @@ use utils::{maybe_addr, Expiration}; use crate::error::ContractError; use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; -use crate::state::{ - next_id, parse_id, Ballot, Config, Proposal, Votes, BALLOTS, CONFIG, PROPOSALS, -}; +use crate::state::{next_id, Ballot, Config, Proposal, Votes, BALLOTS, CONFIG, PROPOSALS}; // version info for migration info const CONTRACT_NAME: &str = "crates.io:cw3-flex-multisig"; @@ -317,13 +315,13 @@ fn list_proposals( ) -> StdResult { let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize; let start = start_after.map(Bound::exclusive_int); - let props: StdResult> = PROPOSALS + let proposals = PROPOSALS .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|p| map_proposal(&env.block, p)) - .collect(); + .collect::>()?; - Ok(ProposalListResponse { proposals: props? }) + Ok(ProposalListResponse { proposals }) } fn reverse_proposals( @@ -345,19 +343,20 @@ fn reverse_proposals( fn map_proposal( block: &BlockInfo, - item: StdResult<(Vec, Proposal)>, + item: StdResult<(u64, Proposal)>, ) -> StdResult { - let (key, prop) = item?; - let status = prop.current_status(block); - let threshold = prop.threshold.to_response(prop.total_weight); - Ok(ProposalResponse { - id: parse_id(&key)?, - title: prop.title, - description: prop.description, - msgs: prop.msgs, - status, - expires: prop.expires, - threshold, + item.map(|(id, prop)| { + let status = prop.current_status(block); + let threshold = prop.threshold.to_response(prop.total_weight); + ProposalResponse { + id, + title: prop.title, + description: prop.description, + msgs: prop.msgs, + status, + expires: prop.expires, + threshold, + } }) } @@ -382,21 +381,20 @@ fn list_votes( let addr = maybe_addr(deps.api, start_after)?; let start = addr.map(|addr| Bound::exclusive(addr.as_ref())); - let votes: StdResult> = BALLOTS + let votes = BALLOTS .prefix(proposal_id) .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|item| { - let (voter, ballot) = item?; - Ok(VoteInfo { - voter: String::from_utf8(voter)?, + item.map(|(addr, ballot)| VoteInfo { + voter: addr.into(), vote: ballot.vote, weight: ballot.weight, }) }) - .collect(); + .collect::>()?; - Ok(VoteListResponse { votes: votes? }) + Ok(VoteListResponse { votes }) } fn query_voter(deps: Deps, voter: String) -> StdResult { diff --git a/contracts/cw4-group/src/contract.rs b/contracts/cw4-group/src/contract.rs index 1600d020e..e41e9d9f6 100644 --- a/contracts/cw4-group/src/contract.rs +++ b/contracts/cw4-group/src/contract.rs @@ -192,19 +192,18 @@ fn list_members( let addr = maybe_addr(deps.api, start_after)?; let start = addr.map(|addr| Bound::exclusive(addr.to_string())); - let members: StdResult> = MEMBERS + let members = MEMBERS .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|item| { - let (key, weight) = item?; - Ok(Member { - addr: String::from_utf8(key)?, + item.map(|(addr, weight)| Member { + addr: addr.into(), weight, }) }) - .collect(); + .collect::>()?; - Ok(MemberListResponse { members: members? }) + Ok(MemberListResponse { members }) } #[cfg(test)] diff --git a/contracts/cw4-stake/src/contract.rs b/contracts/cw4-stake/src/contract.rs index 2b4d86774..635a20080 100644 --- a/contracts/cw4-stake/src/contract.rs +++ b/contracts/cw4-stake/src/contract.rs @@ -341,19 +341,18 @@ fn list_members( let addr = maybe_addr(deps.api, start_after)?; let start = addr.map(|addr| Bound::exclusive(addr.as_ref())); - let members: StdResult> = MEMBERS + let members = MEMBERS .range(deps.storage, start, None, Order::Ascending) .take(limit) .map(|item| { - let (key, weight) = item?; - Ok(Member { - addr: String::from_utf8(key)?, + item.map(|(addr, weight)| Member { + addr: addr.into(), weight, }) }) - .collect(); + .collect::>()?; - Ok(MemberListResponse { members: members? }) + Ok(MemberListResponse { members }) } #[cfg(test)] diff --git a/packages/controllers/src/claim.rs b/packages/controllers/src/claim.rs index 693fd2e7e..b4d1b502c 100644 --- a/packages/controllers/src/claim.rs +++ b/packages/controllers/src/claim.rs @@ -118,7 +118,7 @@ mod test { assert_eq!( claims .0 - .range(&deps.storage, None, None, Order::Ascending) + .range_raw(&deps.storage, None, None, Order::Ascending) .collect::>>() .unwrap() .len(), diff --git a/packages/multi-test/src/wasm.rs b/packages/multi-test/src/wasm.rs index 9b98bf509..1e1ef5872 100644 --- a/packages/multi-test/src/wasm.rs +++ b/packages/multi-test/src/wasm.rs @@ -791,7 +791,7 @@ where fn next_address(&self, storage: &dyn Storage) -> Addr { // FIXME: quite inefficient if we actually had 100s of contracts let count = CONTRACTS - .range(storage, None, None, Order::Ascending) + .range_raw(storage, None, None, Order::Ascending) .count(); // we make this longer so it is not rejected by tests Addr::unchecked(format!("Contract #{}", count.to_string())) diff --git a/packages/storage-plus/src/indexed_map.rs b/packages/storage-plus/src/indexed_map.rs index 580182a31..d9a7df53c 100644 --- a/packages/storage-plus/src/indexed_map.rs +++ b/packages/storage-plus/src/indexed_map.rs @@ -129,23 +129,13 @@ where self.primary.may_load(store, key) } - // use prefix to scan -> range - pub fn prefix(&self, p: K::Prefix) -> Prefix, T> { - Prefix::new(self.pk_namespace, &p.prefix()) - } - - // use sub_prefix to scan -> range - pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix, T> { - Prefix::new(self.pk_namespace, &p.prefix()) - } - // use no_prefix to scan -> range - fn no_prefix(&self) -> Prefix, T> { + fn no_prefix_raw(&self) -> Prefix, T> { Prefix::new(self.pk_namespace, &[]) } } -// short-cut for simple keys, rather than .prefix(()).range(...) +// short-cut for simple keys, rather than .prefix(()).range_raw(...) impl<'a, K, T, I> IndexedMap<'a, K, T, I> where K: PrimaryKey<'a>, @@ -154,7 +144,7 @@ where { // I would prefer not to copy code from Prefix, but no other way // with lifetimes (create Prefix inside function and return ref = no no) - pub fn range<'c>( + pub fn range_raw<'c>( &self, store: &'c dyn Storage, min: Option, @@ -164,7 +154,17 @@ where where T: 'c, { - self.no_prefix().range(store, min, max, order) + self.no_prefix_raw().range_raw(store, min, max, order) + } + + pub fn keys_raw<'c>( + &self, + store: &'c dyn Storage, + min: Option, + max: Option, + order: cosmwasm_std::Order, + ) -> Box> + 'c> { + self.no_prefix_raw().keys_raw(store, min, max, order) } } @@ -175,12 +175,12 @@ where T: Serialize + DeserializeOwned + Clone, I: IndexList, { - /// While `range` over a `prefix` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the `Prefix` + /// While `range_raw` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range_raw` accepts bounds for the lowest and highest elements of the `Prefix` /// itself, and iterates over those (inclusively or exclusively, depending on `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range<'c>( + pub fn prefix_range_raw<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -204,11 +204,11 @@ where K: PrimaryKey<'a>, I: IndexList, { - pub fn sub_prefix_de(&self, p: K::SubPrefix) -> Prefix { + pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix { Prefix::new(self.pk_namespace, &p.prefix()) } - pub fn prefix_de(&self, p: K::Prefix) -> Prefix { + pub fn prefix(&self, p: K::Prefix) -> Prefix { Prefix::new(self.pk_namespace, &p.prefix()) } } @@ -220,13 +220,13 @@ where K: PrimaryKey<'a> + KeyDeserialize, I: IndexList, { - /// While `range_de` over a `prefix_de` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range_de` accepts bounds for the lowest and highest elements of the + /// While `range` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the /// `Prefix` itself, and iterates over those (inclusively or exclusively, depending on /// `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range_de<'c>( + pub fn prefix_range<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -244,7 +244,7 @@ where Box::new(mapped) } - pub fn range_de<'c>( + pub fn range<'c>( &self, store: &'c dyn Storage, min: Option, @@ -255,10 +255,10 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().range_de(store, min, max, order) + self.no_prefix().range(store, min, max, order) } - pub fn keys_de<'c>( + pub fn keys<'c>( &self, store: &'c dyn Storage, min: Option, @@ -269,10 +269,10 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().keys_de(store, min, max, order) + self.no_prefix().keys(store, min, max, order) } - fn no_prefix_de(&self) -> Prefix { + fn no_prefix(&self) -> Prefix { Prefix::new(self.pk_namespace, &[]) } } @@ -415,7 +415,7 @@ mod test { .idx .name .prefix("Maria".to_string()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(2, count); @@ -424,7 +424,7 @@ mod test { .idx .name .prefix("Maria".to_string()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect::>() .unwrap(); assert_eq!(2, marias.len()); @@ -437,7 +437,7 @@ mod test { .idx .name .prefix("Marib".to_string()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(0, count); @@ -446,7 +446,7 @@ mod test { .idx .name .prefix("Mari`".to_string()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(0, count); @@ -455,7 +455,7 @@ mod test { .idx .name .prefix("Maria5".to_string()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(0, count); @@ -469,7 +469,7 @@ mod test { let count = map .idx .name - .range(&store, Some(Bound::inclusive(key)), None, Order::Ascending) + .range_raw(&store, Some(Bound::inclusive(key)), None, Order::Ascending) .count(); // gets from the first "Maria" until the end assert_eq!(4, count); @@ -484,7 +484,7 @@ mod test { let count = map .idx .name - .range(&store, Some(Bound::exclusive(key)), None, Order::Ascending) + .range_raw(&store, Some(Bound::exclusive(key)), None, Order::Ascending) .count(); // gets from the 2nd "Maria" until the end assert_eq!(3, count); @@ -497,7 +497,7 @@ mod test { let count = map .idx .age - .range( + .range_raw( &store, Some(Bound::inclusive(age_key)), None, @@ -520,7 +520,7 @@ mod test { } #[test] - fn range_simple_key_by_multi_index() { + fn range_raw_simple_key_by_multi_index() { let mut store = MockStorage::new(); let map = build_map(); @@ -561,7 +561,7 @@ mod test { .idx .name .prefix("Maria".to_string()) - .range(&store, None, None, Order::Descending) + .range_raw(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -576,7 +576,7 @@ mod test { } #[test] - fn range_de_simple_key_by_multi_index() { + fn range_simple_key_by_multi_index() { let mut store = MockStorage::new(); let map = build_map(); @@ -616,8 +616,8 @@ mod test { let marias: Vec<_> = map .idx .name - .prefix_de("Maria".to_string()) - .range_de(&store, None, None, Order::Descending) + .prefix("Maria".to_string()) + .range(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -632,7 +632,7 @@ mod test { } #[test] - fn range_composite_key_by_multi_index() { + fn range_raw_composite_key_by_multi_index() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -677,7 +677,7 @@ mod test { .idx .name_age .sub_prefix(b"Maria".to_vec()) - .range(&store, None, None, Order::Descending) + .range_raw(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -693,7 +693,7 @@ mod test { } #[test] - fn range_de_composite_key_by_multi_index() { + fn range_composite_key_by_multi_index() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -737,8 +737,8 @@ mod test { let marias: Vec<_> = map .idx .name_age - .sub_prefix_de(b"Maria".to_vec()) - .range_de(&store, None, None, Order::Descending) + .sub_prefix(b"Maria".to_vec()) + .range(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -828,7 +828,7 @@ mod test { map.idx .name .prefix(name.to_string()) - .keys(store, None, None, Order::Ascending) + .keys_raw(store, None, None, Order::Ascending) .count() }; @@ -861,7 +861,7 @@ mod test { } #[test] - fn range_simple_key_by_unique_index() { + fn range_raw_simple_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_map(); @@ -871,7 +871,7 @@ mod test { let res: StdResult> = map .idx .age - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let ages = res.unwrap(); @@ -894,7 +894,7 @@ mod test { } #[test] - fn range_de_simple_key_by_unique_index() { + fn range_simple_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_map(); @@ -904,7 +904,7 @@ mod test { let res: StdResult> = map .idx .age - .range_de(&store, None, None, Order::Ascending) + .range(&store, None, None, Order::Ascending) .collect(); let ages = res.unwrap(); @@ -927,7 +927,7 @@ mod test { } #[test] - fn range_composite_key_by_unique_index() { + fn range_raw_composite_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_map(); @@ -938,7 +938,7 @@ mod test { .idx .name_lastname .prefix(b"Maria".to_vec()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let marias = res.unwrap(); @@ -956,7 +956,7 @@ mod test { } #[test] - fn range_de_composite_key_by_unique_index() { + fn range_composite_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_map(); @@ -966,8 +966,8 @@ mod test { let res: StdResult> = map .idx .name_lastname - .prefix_de(b"Maria".to_vec()) - .range_de(&store, None, None, Order::Ascending) + .prefix(b"Maria".to_vec()) + .range(&store, None, None, Order::Ascending) .collect(); let marias = res.unwrap(); @@ -986,7 +986,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_de_simple_string_key() { + fn range_simple_string_key() { let mut store = MockStorage::new(); let map = build_map(); @@ -994,7 +994,7 @@ mod test { let (pks, datas) = save_data(&mut store, &map); // let's try to iterate! - let all: StdResult> = map.range_de(&store, None, None, Order::Ascending).collect(); + let all: StdResult> = map.range(&store, None, None, Order::Ascending).collect(); let all = all.unwrap(); assert_eq!( all, @@ -1007,7 +1007,7 @@ mod test { // let's try to iterate over a range let all: StdResult> = map - .range_de( + .range( &store, Some(Bound::Inclusive(b"3".to_vec())), None, @@ -1029,7 +1029,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefix_de_simple_string_key() { + fn prefix_simple_string_key() { let mut store = MockStorage::new(); let map = build_map(); @@ -1037,11 +1037,11 @@ mod test { let (pks, datas) = save_data(&mut store, &map); // Let's prefix and iterate. - // This is similar to calling range() directly, but added here for completeness / prefix_de + // This is similar to calling range() directly, but added here for completeness / prefix // type checks let all: StdResult> = map - .prefix_de(()) - .range_de(&store, None, None, Order::Ascending) + .prefix(()) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!( @@ -1056,7 +1056,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefix_de_composite_key() { + fn prefix_composite_key() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -1099,8 +1099,8 @@ mod test { // let's prefix and iterate let result: StdResult> = map - .prefix_de("2") - .range_de(&store, None, None, Order::Ascending) + .prefix("2") + .range(&store, None, None, Order::Ascending) .collect(); let result = result.unwrap(); assert_eq!( @@ -1111,7 +1111,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefix_de_triple_key() { + fn prefix_triple_key() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -1154,8 +1154,8 @@ mod test { // let's prefix and iterate let result: StdResult> = map - .prefix_de(("1", "2")) - .range_de(&store, None, None, Order::Ascending) + .prefix(("1", "2")) + .range(&store, None, None, Order::Ascending) .collect(); let result = result.unwrap(); assert_eq!(result, [("5628".to_string(), data2),]); @@ -1163,7 +1163,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn sub_prefix_de_triple_key() { + fn sub_prefix_triple_key() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -1206,8 +1206,8 @@ mod test { // let's sub-prefix and iterate let result: StdResult> = map - .sub_prefix_de("1") - .range_de(&store, None, None, Order::Ascending) + .sub_prefix("1") + .range(&store, None, None, Order::Ascending) .collect(); let result = result.unwrap(); assert_eq!( @@ -1221,7 +1221,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefix_range_de_simple_key() { + fn prefix_range_simple_key() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -1262,9 +1262,9 @@ mod test { let pk4 = ("3", "5630"); map.save(&mut store, pk4, &data4).unwrap(); - // let's try to iterate! + // let's prefix-range and iterate let result: StdResult> = map - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive("2")), None, @@ -1281,9 +1281,9 @@ mod test { ] ); - // let's try to iterate over a range + // let's try to iterate over a more restrictive prefix-range! let result: StdResult> = map - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive("2")), Some(PrefixBound::exclusive("3")), @@ -1302,7 +1302,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefix_range_de_triple_key() { + fn prefix_range_triple_key() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -1345,7 +1345,7 @@ mod test { // let's prefix-range and iterate let result: StdResult> = map - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive(("1", "2"))), None, @@ -1373,7 +1373,7 @@ mod test { // let's prefix-range over inclusive bounds on both sides let result: StdResult> = map - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive(("1", "2"))), Some(PrefixBound::inclusive(("2", "1"))), @@ -1429,7 +1429,7 @@ mod test { let items: Vec<_> = map .idx .secondary - .prefix_range( + .prefix_range_raw( &store, None, Some(PrefixBound::inclusive(1u64)), diff --git a/packages/storage-plus/src/indexed_snapshot.rs b/packages/storage-plus/src/indexed_snapshot.rs index 8b4470927..7b2eb7b82 100644 --- a/packages/storage-plus/src/indexed_snapshot.rs +++ b/packages/storage-plus/src/indexed_snapshot.rs @@ -174,23 +174,13 @@ where self.primary.may_load(store, key) } - // use prefix to scan -> range - pub fn prefix(&self, p: K::Prefix) -> Prefix, T> { - Prefix::new(self.pk_namespace, &p.prefix()) - } - - // use sub_prefix to scan -> range - pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix, T> { - Prefix::new(self.pk_namespace, &p.prefix()) - } - // use no_prefix to scan -> range - pub fn no_prefix(&self) -> Prefix, T> { + pub fn no_prefix_raw(&self) -> Prefix, T> { Prefix::new(self.pk_namespace, &[]) } } -// short-cut for simple keys, rather than .prefix(()).range(...) +// short-cut for simple keys, rather than .prefix(()).range_raw(...) impl<'a, K, T, I> IndexedSnapshotMap<'a, K, T, I> where K: PrimaryKey<'a> + Prefixer<'a> + KeyDeserialize, @@ -199,7 +189,7 @@ where { // I would prefer not to copy code from Prefix, but no other way // with lifetimes (create Prefix inside function and return ref = no no) - pub fn range<'c>( + pub fn range_raw<'c>( &self, store: &'c dyn Storage, min: Option, @@ -209,7 +199,17 @@ where where T: 'c, { - self.no_prefix().range(store, min, max, order) + self.no_prefix_raw().range_raw(store, min, max, order) + } + + pub fn keys_raw<'c>( + &self, + store: &'c dyn Storage, + min: Option, + max: Option, + order: cosmwasm_std::Order, + ) -> Box> + 'c> { + self.no_prefix_raw().keys_raw(store, min, max, order) } } @@ -220,11 +220,11 @@ where K: PrimaryKey<'a>, I: IndexList, { - pub fn sub_prefix_de(&self, p: K::SubPrefix) -> Prefix { + pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix { Prefix::new(self.pk_namespace, &p.prefix()) } - pub fn prefix_de(&self, p: K::Prefix) -> Prefix { + pub fn prefix(&self, p: K::Prefix) -> Prefix { Prefix::new(self.pk_namespace, &p.prefix()) } } @@ -236,13 +236,13 @@ where K: PrimaryKey<'a> + KeyDeserialize, I: IndexList, { - /// While `range_de` over a `prefix_de` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range_de` accepts bounds for the lowest and highest elements of the + /// While `range` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the /// `Prefix` itself, and iterates over those (inclusively or exclusively, depending on /// `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range_de<'c>( + pub fn prefix_range<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -260,7 +260,7 @@ where Box::new(mapped) } - pub fn range_de<'c>( + pub fn range<'c>( &self, store: &'c dyn Storage, min: Option, @@ -271,10 +271,10 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().range_de(store, min, max, order) + self.no_prefix().range(store, min, max, order) } - pub fn keys_de<'c>( + pub fn keys<'c>( &self, store: &'c dyn Storage, min: Option, @@ -285,10 +285,10 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().keys_de(store, min, max, order) + self.no_prefix().keys(store, min, max, order) } - fn no_prefix_de(&self) -> Prefix { + fn no_prefix(&self) -> Prefix { Prefix::new(self.pk_namespace, &[]) } } @@ -431,7 +431,7 @@ mod test { .idx .name .prefix(b"Maria".to_vec()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(2, count); @@ -440,7 +440,7 @@ mod test { .idx .name .prefix(b"Maria".to_vec()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect::>() .unwrap(); assert_eq!(2, marias.len()); @@ -453,7 +453,7 @@ mod test { .idx .name .prefix(b"Marib".to_vec()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(0, count); @@ -462,7 +462,7 @@ mod test { .idx .name .prefix(b"Mari`".to_vec()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(0, count); @@ -471,7 +471,7 @@ mod test { .idx .name .prefix(b"Maria5".to_vec()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .count(); assert_eq!(0, count); @@ -488,7 +488,7 @@ mod test { } #[test] - fn range_simple_key_by_multi_index() { + fn range_raw_simple_key_by_multi_index() { let mut store = MockStorage::new(); let map = build_snapshot_map(); let mut height = 1; @@ -533,7 +533,7 @@ mod test { .idx .name .prefix(b"Maria".to_vec()) - .range(&store, None, None, Order::Descending) + .range_raw(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -548,7 +548,7 @@ mod test { } #[test] - fn range_de_simple_key_by_multi_index() { + fn range_simple_key_by_multi_index() { let mut store = MockStorage::new(); let map = build_snapshot_map(); let mut height = 1; @@ -592,8 +592,8 @@ mod test { let marias: Vec<_> = map .idx .name - .prefix_de(b"Maria".to_vec()) - .range_de(&store, None, None, Order::Descending) + .prefix(b"Maria".to_vec()) + .range(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -608,7 +608,7 @@ mod test { } #[test] - fn range_composite_key_by_multi_index() { + fn range_raw_composite_key_by_multi_index() { let mut store = MockStorage::new(); let mut height = 2; @@ -658,7 +658,7 @@ mod test { .idx .name_age .sub_prefix(b"Maria".to_vec()) - .range(&store, None, None, Order::Descending) + .range_raw(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -674,7 +674,7 @@ mod test { } #[test] - fn range_de_composite_key_by_multi_index() { + fn range_composite_key_by_multi_index() { let mut store = MockStorage::new(); let mut height = 2; @@ -723,8 +723,8 @@ mod test { let marias: Vec<_> = map .idx .name_age - .sub_prefix_de(b"Maria".to_vec()) - .range_de(&store, None, None, Order::Descending) + .sub_prefix(b"Maria".to_vec()) + .range(&store, None, None, Order::Descending) .collect::>() .unwrap(); let count = marias.len(); @@ -819,7 +819,7 @@ mod test { map.idx .name .prefix(name.as_bytes().to_vec()) - .keys(store, None, None, Order::Ascending) + .keys_raw(store, None, None, Order::Ascending) .count() }; @@ -853,7 +853,7 @@ mod test { } #[test] - fn range_simple_key_by_unique_index() { + fn range_raw_simple_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_snapshot_map(); @@ -863,7 +863,7 @@ mod test { let res: StdResult> = map .idx .age - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let ages = res.unwrap(); @@ -884,7 +884,7 @@ mod test { } #[test] - fn range_de_simple_key_by_unique_index() { + fn range_simple_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_snapshot_map(); @@ -894,7 +894,7 @@ mod test { let res: StdResult> = map .idx .age - .range_de(&store, None, None, Order::Ascending) + .range(&store, None, None, Order::Ascending) .collect(); let ages = res.unwrap(); @@ -915,7 +915,7 @@ mod test { } #[test] - fn range_composite_key_by_unique_index() { + fn range_raw_composite_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_snapshot_map(); @@ -926,7 +926,7 @@ mod test { .idx .name_lastname .prefix(b"Maria".to_vec()) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let marias = res.unwrap(); @@ -944,7 +944,7 @@ mod test { } #[test] - fn range_de_composite_key_by_unique_index() { + fn range_composite_key_by_unique_index() { let mut store = MockStorage::new(); let map = build_snapshot_map(); @@ -954,8 +954,8 @@ mod test { let res: StdResult> = map .idx .name_lastname - .prefix_de(b"Maria".to_vec()) - .range_de(&store, None, None, Order::Ascending) + .prefix(b"Maria".to_vec()) + .range(&store, None, None, Order::Ascending) .collect(); let marias = res.unwrap(); @@ -974,7 +974,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_de_simple_string_key() { + fn range_simple_string_key() { let mut store = MockStorage::new(); let map = build_snapshot_map(); @@ -982,7 +982,7 @@ mod test { let (pks, datas) = save_data(&mut store, &map); // let's try to iterate! - let all: StdResult> = map.range_de(&store, None, None, Order::Ascending).collect(); + let all: StdResult> = map.range(&store, None, None, Order::Ascending).collect(); let all = all.unwrap(); assert_eq!( all, @@ -995,7 +995,7 @@ mod test { // let's try to iterate over a range let all: StdResult> = map - .range_de( + .range( &store, Some(Bound::Inclusive(b"3".to_vec())), None, @@ -1017,7 +1017,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefix_de_simple_string_key() { + fn prefix_simple_string_key() { let mut store = MockStorage::new(); let map = build_snapshot_map(); @@ -1025,11 +1025,11 @@ mod test { let (pks, datas) = save_data(&mut store, &map); // Let's prefix and iterate. - // This is similar to calling range() directly, but added here for completeness / prefix_de + // This is similar to calling range() directly, but added here for completeness / prefix // type checks let all: StdResult> = map - .prefix_de(()) - .range_de(&store, None, None, Order::Ascending) + .prefix(()) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!( @@ -1044,19 +1044,19 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn sub_prefix_de_simple_string_key() { + fn sub_prefix_simple_string_key() { let mut store = MockStorage::new(); let map = build_snapshot_map(); // save data let (pks, datas) = save_data(&mut store, &map); - // Let's prefix and iterate. - // This is similar to calling range() directly, but added here for completeness / sub_prefix_de + // Let's sub-prefix and iterate. + // This is similar to calling range() directly, but added here for completeness / sub_prefix // type checks let all: StdResult> = map - .sub_prefix_de(()) - .range_de(&store, None, None, Order::Ascending) + .sub_prefix(()) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!( @@ -1071,7 +1071,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefix_range_de_simple_key() { + fn prefix_range_simple_key() { let mut store = MockStorage::new(); let indexes = DataCompositeMultiIndex { @@ -1113,9 +1113,9 @@ mod test { let pk4: (&str, &str) = ("3", "5630"); map.save(&mut store, pk4, &data4, 1).unwrap(); - // let's try to iterate! + // let's prefix-range and iterate let result: StdResult> = map - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive("2")), None, @@ -1132,9 +1132,9 @@ mod test { ] ); - // let's try to iterate over a range + // let's try to iterate over a more restrictive prefix-range! let result: StdResult> = map - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive("2")), Some(PrefixBound::exclusive("3")), diff --git a/packages/storage-plus/src/indexes/multi.rs b/packages/storage-plus/src/indexes/multi.rs index 4b8bd5c8e..91a4f29c7 100644 --- a/packages/storage-plus/src/indexes/multi.rs +++ b/packages/storage-plus/src/indexes/multi.rs @@ -143,33 +143,23 @@ where T: Serialize + DeserializeOwned + Clone, IK: PrimaryKey<'a> + Prefixer<'a>, { - pub fn prefix(&self, p: IK) -> Prefix, T> { - Prefix::with_deserialization_function( - self.idx_namespace, - &p.prefix(), - self.pk_namespace, - deserialize_multi_v, - ) - } - - pub fn sub_prefix(&self, p: IK::Prefix) -> Prefix, T> { - Prefix::with_deserialization_function( - self.idx_namespace, - &p.prefix(), - self.pk_namespace, - deserialize_multi_v, - ) - } - - fn no_prefix(&self) -> Prefix, T> { - Prefix::with_deserialization_function( + fn no_prefix_raw(&self) -> Prefix, T> { + Prefix::with_deserialization_functions( self.idx_namespace, &[], self.pk_namespace, deserialize_multi_v, + deserialize_multi_v, ) } +} +impl<'a, IK, T, PK> MultiIndex<'a, IK, T, PK> +where + PK: PrimaryKey<'a> + KeyDeserialize, + T: Serialize + DeserializeOwned + Clone, + IK: PrimaryKey<'a> + Prefixer<'a>, +{ pub fn index_key(&self, k: IK) -> Vec { k.joined_extra_key(b"") } @@ -177,25 +167,27 @@ where #[cfg(test)] pub fn count(&self, store: &dyn Storage, p: IK) -> usize { let prefix = self.prefix(p); - prefix.keys(store, None, None, Order::Ascending).count() + prefix.keys_raw(store, None, None, Order::Ascending).count() } #[cfg(test)] pub fn all_pks(&self, store: &dyn Storage, p: IK) -> Vec> { let prefix = self.prefix(p); prefix - .keys(store, None, None, Order::Ascending) + .keys_raw(store, None, None, Order::Ascending) .collect::>>() } #[cfg(test)] pub fn all_items(&self, store: &dyn Storage, p: IK) -> StdResult>> { let prefix = self.prefix(p); - prefix.range(store, None, None, Order::Ascending).collect() + prefix + .range_raw(store, None, None, Order::Ascending) + .collect() } } -// short-cut for simple keys, rather than .prefix(()).range(...) +// short-cut for simple keys, rather than .prefix(()).range_raw(...) impl<'a, IK, T, PK> MultiIndex<'a, IK, T, PK> where T: Serialize + DeserializeOwned + Clone, @@ -203,7 +195,7 @@ where { // I would prefer not to copy code from Prefix, but no other way // with lifetimes (create Prefix inside function and return ref = no no) - pub fn range<'c>( + pub fn range_raw<'c>( &'c self, store: &'c dyn Storage, min: Option, @@ -213,26 +205,26 @@ where where T: 'c, { - self.no_prefix().range(store, min, max, order) + self.no_prefix_raw().range_raw(store, min, max, order) } - pub fn keys<'c>( + pub fn keys_raw<'c>( &'c self, store: &'c dyn Storage, min: Option, max: Option, order: Order, ) -> Box> + 'c> { - self.no_prefix().keys(store, min, max, order) + self.no_prefix_raw().keys_raw(store, min, max, order) } - /// While `range` over a `prefix` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the + /// While `range_raw` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range_raw` accepts bounds for the lowest and highest elements of the /// `Prefix` itself, and iterates over those (inclusively or exclusively, depending on /// `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range<'c>( + pub fn prefix_range_raw<'c>( &'c self, store: &'c dyn Storage, min: Option>, @@ -256,21 +248,23 @@ where T: Serialize + DeserializeOwned + Clone, IK: PrimaryKey<'a> + Prefixer<'a>, { - pub fn prefix_de(&self, p: IK) -> Prefix { - Prefix::with_deserialization_function( + pub fn prefix(&self, p: IK) -> Prefix { + Prefix::with_deserialization_functions( self.idx_namespace, &p.prefix(), self.pk_namespace, deserialize_multi_kv::, + deserialize_multi_v, ) } - pub fn sub_prefix_de(&self, p: IK::Prefix) -> Prefix { - Prefix::with_deserialization_function( + pub fn sub_prefix(&self, p: IK::Prefix) -> Prefix { + Prefix::with_deserialization_functions( self.idx_namespace, &p.prefix(), self.pk_namespace, deserialize_multi_kv::, + deserialize_multi_v, ) } } @@ -282,13 +276,13 @@ where T: Serialize + DeserializeOwned + Clone, IK: PrimaryKey<'a> + KeyDeserialize + Prefixer<'a>, { - /// While `range_de` over a `prefix_de` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range_de` accepts bounds for the lowest and highest elements of the + /// While `range` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the /// `Prefix` itself, and iterates over those (inclusively or exclusively, depending on /// `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range_de<'c>( + pub fn prefix_range<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -307,7 +301,7 @@ where Box::new(mapped) } - pub fn range_de<'c>( + pub fn range<'c>( &self, store: &'c dyn Storage, min: Option, @@ -318,10 +312,10 @@ where T: 'c, PK::Output: 'static, { - self.no_prefix_de().range_de(store, min, max, order) + self.no_prefix().range(store, min, max, order) } - pub fn keys_de<'c>( + pub fn keys<'c>( &self, store: &'c dyn Storage, min: Option, @@ -332,15 +326,16 @@ where T: 'c, PK::Output: 'static, { - self.no_prefix_de().keys_de(store, min, max, order) + self.no_prefix().keys(store, min, max, order) } - fn no_prefix_de(&self) -> Prefix { - Prefix::with_deserialization_function( + fn no_prefix(&self) -> Prefix { + Prefix::with_deserialization_functions( self.idx_namespace, &[], self.pk_namespace, deserialize_multi_kv::, + deserialize_multi_v, ) } } diff --git a/packages/storage-plus/src/indexes/unique.rs b/packages/storage-plus/src/indexes/unique.rs index 13d033475..6f729db10 100644 --- a/packages/storage-plus/src/indexes/unique.rs +++ b/packages/storage-plus/src/indexes/unique.rs @@ -112,22 +112,14 @@ where k.joined_key() } - pub fn prefix(&self, p: IK::Prefix) -> Prefix, T> { - Prefix::with_deserialization_function(self.idx_namespace, &p.prefix(), &[], |_, _, kv| { - deserialize_unique_v(kv) - }) - } - - pub fn sub_prefix(&self, p: IK::SubPrefix) -> Prefix, T> { - Prefix::with_deserialization_function(self.idx_namespace, &p.prefix(), &[], |_, _, kv| { - deserialize_unique_v(kv) - }) - } - - fn no_prefix(&self) -> Prefix, T> { - Prefix::with_deserialization_function(self.idx_namespace, &[], &[], |_, _, kv| { - deserialize_unique_v(kv) - }) + fn no_prefix_raw(&self) -> Prefix, T> { + Prefix::with_deserialization_functions( + self.idx_namespace, + &[], + &[], + |_, _, kv| deserialize_unique_v(kv), + |_, _, kv| deserialize_unique_v(kv), + ) } /// returns all items that match this secondary index, always by pk Ascending @@ -140,7 +132,7 @@ where } } -// short-cut for simple keys, rather than .prefix(()).range(...) +// short-cut for simple keys, rather than .prefix(()).range_raw(...) impl<'a, IK, T, PK> UniqueIndex<'a, IK, T, PK> where T: Serialize + DeserializeOwned + Clone, @@ -148,7 +140,7 @@ where { // I would prefer not to copy code from Prefix, but no other way // with lifetimes (create Prefix inside function and return ref = no no) - pub fn range<'c>( + pub fn range_raw<'c>( &self, store: &'c dyn Storage, min: Option, @@ -158,17 +150,17 @@ where where T: 'c, { - self.no_prefix().range(store, min, max, order) + self.no_prefix_raw().range_raw(store, min, max, order) } - pub fn keys<'c>( + pub fn keys_raw<'c>( &self, store: &'c dyn Storage, min: Option, max: Option, order: Order, ) -> Box> + 'c> { - self.no_prefix().keys(store, min, max, order) + self.no_prefix_raw().keys_raw(store, min, max, order) } } @@ -179,13 +171,13 @@ where T: Serialize + DeserializeOwned + Clone, IK: PrimaryKey<'a>, { - /// While `range_de` over a `prefix_de` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range_de` accepts bounds for the lowest and highest elements of the + /// While `range` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the /// `Prefix` itself, and iterates over those (inclusively or exclusively, depending on /// `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range_de<'c>( + pub fn prefix_range<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -204,7 +196,7 @@ where Box::new(mapped) } - pub fn range_de<'c>( + pub fn range<'c>( &self, store: &'c dyn Storage, min: Option, @@ -215,10 +207,10 @@ where T: 'c, PK::Output: 'static, { - self.no_prefix_de().range_de(store, min, max, order) + self.no_prefix().range(store, min, max, order) } - pub fn keys_de<'c>( + pub fn keys<'c>( &self, store: &'c dyn Storage, min: Option, @@ -229,24 +221,36 @@ where T: 'c, PK::Output: 'static, { - self.no_prefix_de().keys_de(store, min, max, order) + self.no_prefix().keys(store, min, max, order) } - pub fn prefix_de(&self, p: IK::Prefix) -> Prefix { - Prefix::with_deserialization_function(self.idx_namespace, &p.prefix(), &[], |_, _, kv| { - deserialize_unique_kv::(kv) - }) + pub fn prefix(&self, p: IK::Prefix) -> Prefix { + Prefix::with_deserialization_functions( + self.idx_namespace, + &p.prefix(), + &[], + |_, _, kv| deserialize_unique_kv::(kv), + |_, _, kv| deserialize_unique_v(kv), + ) } - pub fn sub_prefix_de(&self, p: IK::SubPrefix) -> Prefix { - Prefix::with_deserialization_function(self.idx_namespace, &p.prefix(), &[], |_, _, kv| { - deserialize_unique_kv::(kv) - }) + pub fn sub_prefix(&self, p: IK::SubPrefix) -> Prefix { + Prefix::with_deserialization_functions( + self.idx_namespace, + &p.prefix(), + &[], + |_, _, kv| deserialize_unique_kv::(kv), + |_, _, kv| deserialize_unique_v(kv), + ) } - fn no_prefix_de(&self) -> Prefix { - Prefix::with_deserialization_function(self.idx_namespace, &[], &[], |_, _, kv| { - deserialize_unique_kv::(kv) - }) + fn no_prefix(&self) -> Prefix { + Prefix::with_deserialization_functions( + self.idx_namespace, + &[], + &[], + |_, _, kv| deserialize_unique_kv::(kv), + |_, _, kv| deserialize_unique_v(kv), + ) } } diff --git a/packages/storage-plus/src/map.rs b/packages/storage-plus/src/map.rs index 2b623e82f..320c7a64c 100644 --- a/packages/storage-plus/src/map.rs +++ b/packages/storage-plus/src/map.rs @@ -50,17 +50,7 @@ where } #[cfg(feature = "iterator")] - pub fn prefix(&self, p: K::Prefix) -> Prefix, T> { - Prefix::new(self.namespace, &p.prefix()) - } - - #[cfg(feature = "iterator")] - pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix, T> { - Prefix::new(self.namespace, &p.prefix()) - } - - #[cfg(feature = "iterator")] - pub(crate) fn no_prefix(&self) -> Prefix, T> { + pub(crate) fn no_prefix_raw(&self) -> Prefix, T> { Prefix::new(self.namespace, &[]) } @@ -125,16 +115,16 @@ where T: Serialize + DeserializeOwned, K: PrimaryKey<'a>, { - pub fn sub_prefix_de(&self, p: K::SubPrefix) -> Prefix { + pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix { Prefix::new(self.namespace, &p.prefix()) } - pub fn prefix_de(&self, p: K::Prefix) -> Prefix { + pub fn prefix(&self, p: K::Prefix) -> Prefix { Prefix::new(self.namespace, &p.prefix()) } } -// short-cut for simple keys, rather than .prefix(()).range(...) +// short-cut for simple keys, rather than .prefix(()).range_raw(...) #[cfg(feature = "iterator")] impl<'a, K, T> Map<'a, K, T> where @@ -143,12 +133,12 @@ where // Other cases need to call prefix() first K: PrimaryKey<'a>, { - /// While `range` over a `prefix` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the `Prefix` + /// While `range_raw` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range_raw` accepts bounds for the lowest and highest elements of the `Prefix` /// itself, and iterates over those (inclusively or exclusively, depending on `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range<'c>( + pub fn prefix_range_raw<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -164,7 +154,7 @@ where Box::new(mapped) } - pub fn range<'c>( + pub fn range_raw<'c>( &self, store: &'c dyn Storage, min: Option, @@ -174,10 +164,10 @@ where where T: 'c, { - self.no_prefix().range(store, min, max, order) + self.no_prefix_raw().range_raw(store, min, max, order) } - pub fn keys<'c>( + pub fn keys_raw<'c>( &self, store: &'c dyn Storage, min: Option, @@ -187,7 +177,7 @@ where where T: 'c, { - self.no_prefix().keys(store, min, max, order) + self.no_prefix_raw().keys_raw(store, min, max, order) } } @@ -197,13 +187,13 @@ where T: Serialize + DeserializeOwned, K: PrimaryKey<'a> + KeyDeserialize, { - /// While `range_de` over a `prefix_de` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range_de` accepts bounds for the lowest and highest elements of the + /// While `range` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the /// `Prefix` itself, and iterates over those (inclusively or exclusively, depending on /// `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range_de<'c>( + pub fn prefix_range<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -221,7 +211,7 @@ where Box::new(mapped) } - pub fn range_de<'c>( + pub fn range<'c>( &self, store: &'c dyn Storage, min: Option, @@ -232,10 +222,10 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().range_de(store, min, max, order) + self.no_prefix().range(store, min, max, order) } - pub fn keys_de<'c>( + pub fn keys<'c>( &self, store: &'c dyn Storage, min: Option, @@ -246,10 +236,10 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().keys_de(store, min, max, order) + self.no_prefix().keys(store, min, max, order) } - fn no_prefix_de(&self) -> Prefix { + fn no_prefix(&self) -> Prefix { Prefix::new(self.namespace, &[]) } } @@ -405,7 +395,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_simple_key() { + fn range_raw_simple_key() { let mut store = MockStorage::new(); // save and load on two keys @@ -422,7 +412,9 @@ mod test { PEOPLE.save(&mut store, b"jim", &data2).unwrap(); // let's try to iterate! - let all: StdResult> = PEOPLE.range(&store, None, None, Order::Ascending).collect(); + let all: StdResult> = PEOPLE + .range_raw(&store, None, None, Order::Ascending) + .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); assert_eq!( @@ -435,7 +427,7 @@ mod test { // let's try to iterate over a range let all: StdResult> = PEOPLE - .range( + .range_raw( &store, Some(Bound::Inclusive(b"j".to_vec())), None, @@ -451,7 +443,7 @@ mod test { // let's try to iterate over a more restrictive range let all: StdResult> = PEOPLE - .range( + .range_raw( &store, Some(Bound::Inclusive(b"jo".to_vec())), None, @@ -465,7 +457,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_de_simple_string_key() { + fn range_simple_string_key() { let mut store = MockStorage::new(); // save and load on two keys @@ -482,9 +474,7 @@ mod test { PEOPLE.save(&mut store, b"jim", &data2).unwrap(); // let's try to iterate! - let all: StdResult> = PEOPLE - .range_de(&store, None, None, Order::Ascending) - .collect(); + let all: StdResult> = PEOPLE.range(&store, None, None, Order::Ascending).collect(); let all = all.unwrap(); assert_eq!(2, all.len()); assert_eq!( @@ -497,7 +487,7 @@ mod test { // let's try to iterate over a range let all: StdResult> = PEOPLE - .range_de( + .range( &store, Some(Bound::Inclusive(b"j".to_vec())), None, @@ -513,7 +503,7 @@ mod test { // let's try to iterate over a more restrictive range let all: StdResult> = PEOPLE - .range_de( + .range( &store, Some(Bound::Inclusive(b"jo".to_vec())), None, @@ -527,7 +517,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_de_simple_integer_key() { + fn range_simple_integer_key() { let mut store = MockStorage::new(); // save and load on two keys @@ -545,7 +535,7 @@ mod test { // let's try to iterate! let all: StdResult> = PEOPLE_ID - .range_de(&store, None, None, Order::Ascending) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); @@ -553,7 +543,7 @@ mod test { // let's try to iterate over a range let all: StdResult> = PEOPLE_ID - .range_de( + .range( &store, Some(Bound::inclusive_int(56u32)), None, @@ -566,7 +556,7 @@ mod test { // let's try to iterate over a more restrictive range let all: StdResult> = PEOPLE_ID - .range_de( + .range( &store, Some(Bound::inclusive_int(57u32)), None, @@ -580,7 +570,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_composite_key() { + fn range_raw_composite_key() { let mut store = MockStorage::new(); // save and load on three keys, one under different owner @@ -596,7 +586,7 @@ mod test { // let's try to iterate! let all: StdResult> = ALLOWANCE - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(3, all.len()); @@ -612,7 +602,7 @@ mod test { // let's try to iterate over a prefix let all: StdResult> = ALLOWANCE .prefix(b"owner") - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); @@ -624,7 +614,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_de_composite_key() { + fn range_composite_key() { let mut store = MockStorage::new(); // save and load on three keys, one under different owner @@ -640,7 +630,7 @@ mod test { // let's try to iterate! let all: StdResult> = ALLOWANCE - .range_de(&store, None, None, Order::Ascending) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(3, all.len()); @@ -653,10 +643,10 @@ mod test { ] ); - // let's try to iterate over a prefix_de + // let's try to iterate over a prefix let all: StdResult> = ALLOWANCE - .prefix_de(b"owner") - .range_de(&store, None, None, Order::Ascending) + .prefix(b"owner") + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); @@ -668,7 +658,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_triple_key() { + fn range_raw_triple_key() { let mut store = MockStorage::new(); // save and load on three keys, one under different owner @@ -686,7 +676,9 @@ mod test { .unwrap(); // let's try to iterate! - let all: StdResult> = TRIPLE.range(&store, None, None, Order::Ascending).collect(); + let all: StdResult> = TRIPLE + .range_raw(&store, None, None, Order::Ascending) + .collect(); let all = all.unwrap(); assert_eq!(4, all.len()); assert_eq!( @@ -714,7 +706,7 @@ mod test { // let's iterate over a prefix let all: StdResult> = TRIPLE .prefix((b"owner", 9)) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); @@ -729,11 +721,11 @@ mod test { // let's iterate over a sub prefix let all: StdResult> = TRIPLE .sub_prefix(b"owner") - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(3, all.len()); - // Use range_de() if you want key deserialization + // Use range() if you want key deserialization assert_eq!( all, vec![ @@ -746,7 +738,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn range_de_triple_key() { + fn range_triple_key() { let mut store = MockStorage::new(); // save and load on three keys, one under different owner @@ -764,9 +756,7 @@ mod test { .unwrap(); // let's try to iterate! - let all: StdResult> = TRIPLE - .range_de(&store, None, None, Order::Ascending) - .collect(); + let all: StdResult> = TRIPLE.range(&store, None, None, Order::Ascending).collect(); let all = all.unwrap(); assert_eq!(4, all.len()); assert_eq!( @@ -779,10 +769,10 @@ mod test { ] ); - // let's iterate over a sub_prefix_de + // let's iterate over a sub_prefix let all: StdResult> = TRIPLE - .sub_prefix_de(b"owner") - .range_de(&store, None, None, Order::Ascending) + .sub_prefix(b"owner") + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(3, all.len()); @@ -795,10 +785,10 @@ mod test { ] ); - // let's iterate over a prefix_de + // let's iterate over a prefix let all: StdResult> = TRIPLE - .prefix_de((b"owner", 9)) - .range_de(&store, None, None, Order::Ascending) + .prefix((b"owner", 9)) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); @@ -941,7 +931,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn readme_with_range() -> StdResult<()> { + fn readme_with_range_raw() -> StdResult<()> { let mut store = MockStorage::new(); // save and load on two keys @@ -957,7 +947,9 @@ mod test { PEOPLE.save(&mut store, b"jim", &data2)?; // iterate over them all - let all: StdResult> = PEOPLE.range(&store, None, None, Order::Ascending).collect(); + let all: StdResult> = PEOPLE + .range_raw(&store, None, None, Order::Ascending) + .collect(); assert_eq!( all?, vec![(b"jim".to_vec(), data2), (b"john".to_vec(), data.clone())] @@ -965,7 +957,7 @@ mod test { // or just show what is after jim let all: StdResult> = PEOPLE - .range( + .range_raw( &store, Some(Bound::Exclusive(b"jim".to_vec())), None, @@ -982,7 +974,7 @@ mod test { // get all under one key let all: StdResult> = ALLOWANCE .prefix(b"owner") - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect(); assert_eq!( all?, @@ -992,7 +984,7 @@ mod test { // Or ranges between two items (even reverse) let all: StdResult> = ALLOWANCE .prefix(b"owner") - .range( + .range_raw( &store, Some(Bound::Exclusive(b"spender1".to_vec())), Some(Bound::Inclusive(b"spender2".to_vec())), @@ -1006,7 +998,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefixed_range_works() { + fn prefixed_range_raw_works() { // this is designed to look as much like a secondary index as possible // we want to query over a range of u32 for the first key and all subkeys const AGES: Map<(u32, Vec), u64> = Map::new("ages"); @@ -1022,21 +1014,20 @@ mod test { // typical range under one prefix as a control let fives = AGES .prefix(5) - .range(&store, None, None, Order::Ascending) + .range_raw(&store, None, None, Order::Ascending) .collect::>>() .unwrap(); assert_eq!(fives.len(), 2); assert_eq!(fives, vec![(vec![7, 8, 9], 789), (vec![9, 8, 7], 987)]); let keys: Vec<_> = AGES - .no_prefix() - .keys(&store, None, None, Order::Ascending) + .keys_raw(&store, None, None, Order::Ascending) .collect(); println!("keys: {:?}", keys); // using inclusive bounds both sides let include = AGES - .prefix_range( + .prefix_range_raw( &store, Some(PrefixBound::inclusive(3u32)), Some(PrefixBound::inclusive(7u32)), @@ -1050,7 +1041,7 @@ mod test { // using exclusive bounds both sides let exclude = AGES - .prefix_range( + .prefix_range_raw( &store, Some(PrefixBound::exclusive(3u32)), Some(PrefixBound::exclusive(7u32)), @@ -1064,7 +1055,7 @@ mod test { // using inclusive in descending let include = AGES - .prefix_range( + .prefix_range_raw( &store, Some(PrefixBound::inclusive(3u32)), Some(PrefixBound::inclusive(5u32)), @@ -1078,7 +1069,7 @@ mod test { // using exclusive in descending let include = AGES - .prefix_range( + .prefix_range_raw( &store, Some(PrefixBound::exclusive(2u32)), Some(PrefixBound::exclusive(5u32)), @@ -1093,7 +1084,7 @@ mod test { #[test] #[cfg(feature = "iterator")] - fn prefixed_range_de_works() { + fn prefixed_range_works() { // this is designed to look as much like a secondary index as possible // we want to query over a range of u32 for the first key and all subkeys const AGES: Map<(u32, &str), u64> = Map::new("ages"); @@ -1108,7 +1099,7 @@ mod test { // typical range under one prefix as a control let fives = AGES - .prefix_de(5) + .prefix(5) .range(&store, None, None, Order::Ascending) .collect::>>() .unwrap(); @@ -1118,15 +1109,12 @@ mod test { vec![("789".to_string(), 789), ("987".to_string(), 987)] ); - let keys: Vec<_> = AGES - .no_prefix() - .keys_de(&store, None, None, Order::Ascending) - .collect(); + let keys: Vec<_> = AGES.keys(&store, None, None, Order::Ascending).collect(); println!("keys: {:?}", keys); // using inclusive bounds both sides let include = AGES - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive(3u32)), Some(PrefixBound::inclusive(7u32)), @@ -1140,7 +1128,7 @@ mod test { // using exclusive bounds both sides let exclude = AGES - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::exclusive(3u32)), Some(PrefixBound::exclusive(7u32)), @@ -1154,7 +1142,7 @@ mod test { // using inclusive in descending let include = AGES - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::inclusive(3u32)), Some(PrefixBound::inclusive(5u32)), @@ -1168,7 +1156,7 @@ mod test { // using exclusive in descending let include = AGES - .prefix_range_de( + .prefix_range( &store, Some(PrefixBound::exclusive(2u32)), Some(PrefixBound::exclusive(5u32)), diff --git a/packages/storage-plus/src/prefix.rs b/packages/storage-plus/src/prefix.rs index 808d62638..5c210052a 100644 --- a/packages/storage-plus/src/prefix.rs +++ b/packages/storage-plus/src/prefix.rs @@ -67,10 +67,11 @@ impl<'a, K: Prefixer<'a>> PrefixBound<'a, K> { } } +type DeserializeVFn = fn(&dyn Storage, &[u8], Record) -> StdResult>; + type DeserializeKvFn = fn(&dyn Storage, &[u8], Record) -> StdResult<(::Output, T)>; -#[allow(dead_code)] pub fn default_deserializer_v( _: &dyn Storage, _: &[u8], @@ -98,7 +99,8 @@ where // see https://doc.rust-lang.org/std/marker/struct.PhantomData.html#unused-type-parameters for why this is needed data: PhantomData, pk_name: Vec, - de_fn: DeserializeKvFn, + de_fn_kv: DeserializeKvFn, + de_fn_v: DeserializeVFn, } impl Deref for Prefix @@ -119,48 +121,50 @@ where T: Serialize + DeserializeOwned, { pub fn new(top_name: &[u8], sub_names: &[Key]) -> Self { - Prefix::with_deserialization_function( + Prefix::with_deserialization_functions( top_name, sub_names, &[], default_deserializer_kv::, + default_deserializer_v, ) } - pub fn with_deserialization_function( + pub fn with_deserialization_functions( top_name: &[u8], sub_names: &[Key], pk_name: &[u8], - de_fn: DeserializeKvFn, + de_fn_kv: DeserializeKvFn, + de_fn_v: DeserializeVFn, ) -> Self { let storage_prefix = nested_namespaces_with_key(&[top_name], sub_names, b""); Prefix { storage_prefix, data: PhantomData, pk_name: pk_name.to_vec(), - de_fn, + de_fn_kv, + de_fn_v, } } - pub fn range<'a>( + pub fn range_raw<'a>( &self, store: &'a dyn Storage, min: Option, max: Option, order: Order, - ) -> Box> + 'a> + ) -> Box>> + 'a> where T: 'a, - K::Output: 'a, { - let de_fn = self.de_fn; + let de_fn = self.de_fn_v; let pk_name = self.pk_name.clone(); let mapped = range_with_prefix(store, &self.storage_prefix, min, max, order) - .map(move |kv| (de_fn)(store, &*pk_name, kv)); + .map(move |kv| (de_fn)(store, &pk_name, kv)); Box::new(mapped) } - pub fn keys<'a>( + pub fn keys_raw<'a>( &self, store: &'a dyn Storage, min: Option, @@ -172,7 +176,7 @@ where Box::new(mapped) } - pub fn range_de<'a>( + pub fn range<'a>( &self, store: &'a dyn Storage, min: Option, @@ -183,14 +187,14 @@ where T: 'a, K::Output: 'static, { - let de_fn = self.de_fn; + let de_fn = self.de_fn_kv; let pk_name = self.pk_name.clone(); let mapped = range_with_prefix(store, &self.storage_prefix, min, max, order) - .map(move |kv| (de_fn)(store, &*pk_name, kv)); + .map(move |kv| (de_fn)(store, &pk_name, kv)); Box::new(mapped) } - pub fn keys_de<'a>( + pub fn keys<'a>( &self, store: &'a dyn Storage, min: Option, @@ -201,10 +205,10 @@ where T: 'a, K::Output: 'static, { - let de_fn = self.de_fn; + let de_fn = self.de_fn_kv; let pk_name = self.pk_name.clone(); let mapped = range_with_prefix(store, &self.storage_prefix, min, max, order) - .map(move |kv| (de_fn)(store, &*pk_name, kv).map(|(k, _)| Ok(k))) + .map(move |kv| (de_fn)(store, &pk_name, kv).map(|(k, _)| Ok(k))) .flatten(); Box::new(mapped) } @@ -326,7 +330,8 @@ mod test { storage_prefix: b"foo".to_vec(), data: PhantomData::, pk_name: vec![], - de_fn: |_, _, kv| deserialize_kv::, u64>(kv), + de_fn_kv: |_, _, kv| deserialize_kv::, u64>(kv), + de_fn_v: |_, _, kv| deserialize_v(kv), }; // set some data, we care about "foo" prefix @@ -345,16 +350,18 @@ mod test { let expected_reversed: Vec<(Vec, u64)> = expected.iter().rev().cloned().collect(); // let's do the basic sanity check - let res: StdResult> = prefix.range(&store, None, None, Order::Ascending).collect(); + let res: StdResult> = prefix + .range_raw(&store, None, None, Order::Ascending) + .collect(); assert_eq!(&expected, &res.unwrap()); let res: StdResult> = prefix - .range(&store, None, None, Order::Descending) + .range_raw(&store, None, None, Order::Descending) .collect(); assert_eq!(&expected_reversed, &res.unwrap()); // now let's check some ascending ranges let res: StdResult> = prefix - .range( + .range_raw( &store, Some(Bound::Inclusive(b"ra".to_vec())), None, @@ -364,7 +371,7 @@ mod test { assert_eq!(&expected[1..], res.unwrap().as_slice()); // skip excluded let res: StdResult> = prefix - .range( + .range_raw( &store, Some(Bound::Exclusive(b"ra".to_vec())), None, @@ -374,7 +381,7 @@ mod test { assert_eq!(&expected[2..], res.unwrap().as_slice()); // if we exclude something a little lower, we get matched let res: StdResult> = prefix - .range( + .range_raw( &store, Some(Bound::Exclusive(b"r".to_vec())), None, @@ -385,7 +392,7 @@ mod test { // now let's check some descending ranges let res: StdResult> = prefix - .range( + .range_raw( &store, None, Some(Bound::Inclusive(b"ra".to_vec())), @@ -395,7 +402,7 @@ mod test { assert_eq!(&expected_reversed[1..], res.unwrap().as_slice()); // skip excluded let res: StdResult> = prefix - .range( + .range_raw( &store, None, Some(Bound::Exclusive(b"ra".to_vec())), @@ -405,7 +412,7 @@ mod test { assert_eq!(&expected_reversed[2..], res.unwrap().as_slice()); // if we exclude something a little higher, we get matched let res: StdResult> = prefix - .range( + .range_raw( &store, None, Some(Bound::Exclusive(b"rb".to_vec())), @@ -416,7 +423,7 @@ mod test { // now test when both sides are set let res: StdResult> = prefix - .range( + .range_raw( &store, Some(Bound::Inclusive(b"ra".to_vec())), Some(Bound::Exclusive(b"zi".to_vec())), @@ -426,7 +433,7 @@ mod test { assert_eq!(&expected[1..2], res.unwrap().as_slice()); // and descending let res: StdResult> = prefix - .range( + .range_raw( &store, Some(Bound::Inclusive(b"ra".to_vec())), Some(Bound::Exclusive(b"zi".to_vec())), @@ -436,7 +443,7 @@ mod test { assert_eq!(&expected[1..2], res.unwrap().as_slice()); // Include both sides let res: StdResult> = prefix - .range( + .range_raw( &store, Some(Bound::Inclusive(b"ra".to_vec())), Some(Bound::Inclusive(b"zi".to_vec())), @@ -446,7 +453,7 @@ mod test { assert_eq!(&expected_reversed[..2], res.unwrap().as_slice()); // Exclude both sides let res: StdResult> = prefix - .range( + .range_raw( &store, Some(Bound::Exclusive(b"ra".to_vec())), Some(Bound::Exclusive(b"zi".to_vec())), diff --git a/packages/storage-plus/src/snapshot/map.rs b/packages/storage-plus/src/snapshot/map.rs index fb5a4c864..ef1d572b4 100644 --- a/packages/storage-plus/src/snapshot/map.rs +++ b/packages/storage-plus/src/snapshot/map.rs @@ -69,16 +69,8 @@ where self.primary.key(k) } - pub fn prefix(&self, p: K::Prefix) -> Prefix, T> { - self.primary.prefix(p) - } - - pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix, T> { - self.primary.sub_prefix(p) - } - - fn no_prefix(&self) -> Prefix, T> { - self.primary.no_prefix() + fn no_prefix_raw(&self) -> Prefix, T> { + self.primary.no_prefix_raw() } /// load old value and store changelog @@ -164,7 +156,7 @@ where } } -// short-cut for simple keys, rather than .prefix(()).range(...) +// short-cut for simple keys, rather than .prefix(()).range_raw(...) impl<'a, K, T> SnapshotMap<'a, K, T> where T: Serialize + DeserializeOwned + Clone, @@ -172,7 +164,7 @@ where { // I would prefer not to copy code from Prefix, but no other way // with lifetimes (create Prefix inside function and return ref = no no) - pub fn range<'c>( + pub fn range_raw<'c>( &self, store: &'c dyn Storage, min: Option, @@ -182,7 +174,20 @@ where where T: 'c, { - self.no_prefix().range(store, min, max, order) + self.no_prefix_raw().range_raw(store, min, max, order) + } + + pub fn keys_raw<'c>( + &self, + store: &'c dyn Storage, + min: Option, + max: Option, + order: cosmwasm_std::Order, + ) -> Box> + 'c> + where + T: 'c, + { + self.no_prefix_raw().keys_raw(store, min, max, order) } } @@ -192,13 +197,13 @@ where T: Serialize + DeserializeOwned, K: PrimaryKey<'a> + KeyDeserialize, { - /// While `range_de` over a `prefix_de` fixes the prefix to one element and iterates over the - /// remaining, `prefix_range_de` accepts bounds for the lowest and highest elements of the + /// While `range` over a `prefix` fixes the prefix to one element and iterates over the + /// remaining, `prefix_range` accepts bounds for the lowest and highest elements of the /// `Prefix` itself, and iterates over those (inclusively or exclusively, depending on /// `PrefixBound`). /// There are some issues that distinguish these two, and blindly casting to `Vec` doesn't /// solve them. - pub fn prefix_range_de<'c>( + pub fn prefix_range<'c>( &self, store: &'c dyn Storage, min: Option>, @@ -216,7 +221,7 @@ where Box::new(mapped) } - pub fn range_de<'c>( + pub fn range<'c>( &self, store: &'c dyn Storage, min: Option, @@ -227,10 +232,10 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().range_de(store, min, max, order) + self.no_prefix().range(store, min, max, order) } - pub fn keys_de<'c>( + pub fn keys<'c>( &self, store: &'c dyn Storage, min: Option, @@ -241,18 +246,18 @@ where T: 'c, K::Output: 'static, { - self.no_prefix_de().keys_de(store, min, max, order) + self.no_prefix().keys(store, min, max, order) } - pub fn prefix_de(&self, p: K::Prefix) -> Prefix { + pub fn prefix(&self, p: K::Prefix) -> Prefix { Prefix::new(self.primary.namespace(), &p.prefix()) } - pub fn sub_prefix_de(&self, p: K::SubPrefix) -> Prefix { + pub fn sub_prefix(&self, p: K::SubPrefix) -> Prefix { Prefix::new(self.primary.namespace(), &p.prefix()) } - fn no_prefix_de(&self) -> Prefix { + fn no_prefix(&self) -> Prefix { Prefix::new(self.primary.namespace(), &[]) } } @@ -328,7 +333,7 @@ mod tests { const VALUES_START_5: &[(&str, Option)] = &[("A", Some(8)), ("B", None), ("C", Some(13)), ("D", None)]; - // Same as `init_data`, but we have a composite key for testing range_de. + // Same as `init_data`, but we have a composite key for testing range. fn init_data_composite_key(map: &TestMapCompositeKey, storage: &mut dyn Storage) { map.save(storage, ("A", "B"), &5, 1).unwrap(); map.save(storage, ("B", "A"), &7, 2).unwrap(); @@ -455,23 +460,21 @@ mod tests { #[test] #[cfg(feature = "iterator")] - fn range_de_simple_string_key() { + fn range_simple_string_key() { use cosmwasm_std::Order; let mut store = MockStorage::new(); init_data(&EVERY, &mut store); // let's try to iterate! - let all: StdResult> = EVERY - .range_de(&store, None, None, Order::Ascending) - .collect(); + let all: StdResult> = EVERY.range(&store, None, None, Order::Ascending).collect(); let all = all.unwrap(); assert_eq!(2, all.len()); assert_eq!(all, vec![("C".into(), 13), ("D".into(), 22)]); // let's try to iterate over a range let all: StdResult> = EVERY - .range_de( + .range( &store, Some(Bound::Inclusive(b"C".to_vec())), None, @@ -484,7 +487,7 @@ mod tests { // let's try to iterate over a more restrictive range let all: StdResult> = EVERY - .range_de( + .range( &store, Some(Bound::Inclusive(b"D".to_vec())), None, @@ -498,7 +501,7 @@ mod tests { #[test] #[cfg(feature = "iterator")] - fn range_de_composite_key() { + fn range_composite_key() { use cosmwasm_std::Order; let mut store = MockStorage::new(); @@ -506,7 +509,7 @@ mod tests { // let's try to iterate! let all: StdResult> = EVERY_COMPOSITE_KEY - .range_de(&store, None, None, Order::Ascending) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); @@ -521,7 +524,7 @@ mod tests { #[test] #[cfg(feature = "iterator")] - fn prefix_range_de_composite_key() { + fn prefix_range_composite_key() { use cosmwasm_std::Order; let mut store = MockStorage::new(); @@ -529,7 +532,7 @@ mod tests { // let's prefix-range and iterate let all: StdResult> = EVERY_COMPOSITE_KEY - .prefix_range_de( + .prefix_range( &store, None, Some(PrefixBound::exclusive("C")), @@ -543,7 +546,7 @@ mod tests { #[test] #[cfg(feature = "iterator")] - fn prefix_de_composite_key() { + fn prefix_composite_key() { use cosmwasm_std::Order; let mut store = MockStorage::new(); @@ -551,8 +554,8 @@ mod tests { // let's prefix and iterate let all: StdResult> = EVERY_COMPOSITE_KEY - .prefix_de("C") - .range_de(&store, None, None, Order::Ascending) + .prefix("C") + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(1, all.len()); @@ -561,7 +564,7 @@ mod tests { #[test] #[cfg(feature = "iterator")] - fn sub_prefix_de_composite_key() { + fn sub_prefix_composite_key() { use cosmwasm_std::Order; let mut store = MockStorage::new(); @@ -569,10 +572,10 @@ mod tests { // Let's sub-prefix and iterate. // This is similar to calling range() directly, but added here for completeness / - // sub_prefix_de type checks + // sub_prefix type checks let all: StdResult> = EVERY_COMPOSITE_KEY - .sub_prefix_de(()) - .range_de(&store, None, None, Order::Ascending) + .sub_prefix(()) + .range(&store, None, None, Order::Ascending) .collect(); let all = all.unwrap(); assert_eq!(2, all.len()); diff --git a/packages/storage-plus/src/snapshot/mod.rs b/packages/storage-plus/src/snapshot/mod.rs index d0418ce2c..c20fe2e8d 100644 --- a/packages/storage-plus/src/snapshot/mod.rs +++ b/packages/storage-plus/src/snapshot/mod.rs @@ -79,7 +79,7 @@ where // most recent checkpoint let checkpoint = self .checkpoints - .range(store, None, None, Order::Descending) + .range_raw(store, None, None, Order::Descending) .next() .transpose()?; if let Some((height, _)) = checkpoint { @@ -88,7 +88,7 @@ where let first = self .changelog .prefix(k.clone()) - .range(store, Some(start), None, Order::Ascending) + .range_raw(store, Some(start), None, Order::Ascending) .next() .transpose()?; if first.is_none() { @@ -147,7 +147,7 @@ where let first = self .changelog .prefix(key) - .range(store, Some(start), None, Order::Ascending) + .range_raw(store, Some(start), None, Order::Ascending) .next(); if let Some(r) = first { diff --git a/packages/utils/src/pagination.rs b/packages/utils/src/pagination.rs index 615993561..7af917acd 100644 --- a/packages/utils/src/pagination.rs +++ b/packages/utils/src/pagination.rs @@ -36,7 +36,7 @@ pub fn calc_range_start_string(start_after: Option) -> Option> { #[cfg(test)] mod test { use super::*; - use cosmwasm_std::{testing::mock_dependencies, Order, StdError}; + use cosmwasm_std::{testing::mock_dependencies, Order}; use cw_storage_plus::{Bound, Map}; pub const HOLDERS: Map<&Addr, usize> = Map::new("some_data"); @@ -46,12 +46,6 @@ mod test { Addr::unchecked(format!("addr{:0>8}", i)) } - fn deser_holder_kv(holder_kv: Result<(Vec, usize), StdError>) -> (String, usize) { - let (k_bytes, v) = holder_kv.unwrap(); - let key = std::str::from_utf8(&k_bytes).unwrap().to_string(); - (key, v) - } - #[test] fn calc_range_start_works_as_expected() { let total_elements_count = 100; @@ -72,15 +66,15 @@ mod test { let start = calc_range_start(start_after).map(Bound::exclusive); - let holders: Vec<(String, usize)> = HOLDERS - .range(&deps.storage, start, None, Order::Ascending) - .map(deser_holder_kv) + let holders = HOLDERS + .keys(&deps.storage, start, None, Order::Ascending) .take(LIMIT) - .collect(); + .collect::>>() + .unwrap(); for (i, holder) in holders.into_iter().enumerate() { let global_index = j * LIMIT + i; - assert_eq!(holder.0, addr_from_i(global_index)); + assert_eq!(holder, addr_from_i(global_index)); } } } @@ -101,15 +95,15 @@ mod test { let end = calc_range_end(end_before).map(Bound::exclusive); - let holders: Vec<(String, usize)> = HOLDERS - .range(&deps.storage, None, end, Order::Descending) - .map(deser_holder_kv) + let holders = HOLDERS + .keys(&deps.storage, None, end, Order::Descending) .take(LIMIT) - .collect(); + .collect::>>() + .unwrap(); for (i, holder) in holders.into_iter().enumerate() { let global_index = total_elements_count - i - j * LIMIT - 1; - assert_eq!(holder.0, addr_from_i(global_index)); + assert_eq!(holder, addr_from_i(global_index)); } } }