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

Pkowned to vec u8 #290

Merged
merged 4 commits into from
May 3, 2021
Merged
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
4 changes: 2 additions & 2 deletions contracts/cw721-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::msg::{ExecuteMsg, InstantiateMsg, MintMsg, MinterResponse, QueryMsg};
use crate::state::{
increment_tokens, num_tokens, tokens, Approval, TokenInfo, CONTRACT_INFO, MINTER, OPERATORS,
};
use cw_storage_plus::{Bound, PkOwned};
use cw_storage_plus::Bound;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw721-base";
Expand Down Expand Up @@ -516,7 +516,7 @@ fn query_tokens(
.owner
.pks(
deps.storage,
PkOwned(Vec::from(owner_addr.as_ref())),
Vec::from(owner_addr.as_ref()),
start,
None,
Order::Ascending,
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw721-base/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

use cosmwasm_std::{Addr, BlockInfo, StdResult, Storage};
use cw721::{ContractInfoResponse, Expiration};
use cw_storage_plus::{Index, IndexList, IndexedMap, Item, Map, MultiIndex, PkOwned};
use cw_storage_plus::{Index, IndexList, IndexedMap, Item, Map, MultiIndex};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TokenInfo {
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn increment_tokens(storage: &mut dyn Storage) -> StdResult<u64> {

pub struct TokenIndexes<'a> {
// pk goes to second tuple element
pub owner: MultiIndex<'a, (PkOwned, PkOwned), TokenInfo>,
pub owner: MultiIndex<'a, (Vec<u8>, Vec<u8>), TokenInfo>,
}

impl<'a> IndexList<TokenInfo> for TokenIndexes<'a> {
Expand All @@ -66,7 +66,7 @@ impl<'a> IndexList<TokenInfo> for TokenIndexes<'a> {
pub fn tokens<'a>() -> IndexedMap<'a, &'a str, TokenInfo, TokenIndexes<'a>> {
let indexes = TokenIndexes {
owner: MultiIndex::new(
|d, k| (PkOwned(Vec::from(d.owner.as_ref())), PkOwned(k)),
|d, k| (Vec::from(d.owner.as_ref()), k),
"tokens",
"tokens__owner",
),
Expand Down
36 changes: 16 additions & 20 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod test {
use super::*;

use crate::indexes::{index_string_tuple, index_triple, MultiIndex, UniqueIndex};
use crate::{PkOwned, U32Key};
use crate::U32Key;
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{MemoryStorage, Order};
use serde::{Deserialize, Serialize};
Expand All @@ -182,9 +182,9 @@ mod test {

struct DataIndexes<'a> {
// Second arg is for storing pk
pub name: MultiIndex<'a, (PkOwned, PkOwned), Data>,
pub name: MultiIndex<'a, (Vec<u8>, Vec<u8>), Data>,
pub age: UniqueIndex<'a, U32Key, Data>,
pub name_lastname: UniqueIndex<'a, (PkOwned, PkOwned), Data>,
pub name_lastname: UniqueIndex<'a, (Vec<u8>, Vec<u8>), Data>,
}

// Future Note: this can likely be macro-derived
Expand All @@ -198,7 +198,7 @@ mod test {
// For composite multi index tests
struct DataCompositeMultiIndex<'a> {
// Third arg needed for storing pk
pub name_age: MultiIndex<'a, (PkOwned, U32Key, PkOwned), Data>,
pub name_age: MultiIndex<'a, (Vec<u8>, U32Key, Vec<u8>), Data>,
}

// Future Note: this can likely be macro-derived
Expand All @@ -212,11 +212,7 @@ mod test {
// Can we make it easier to define this? (less wordy generic)
fn build_map<'a>() -> IndexedMap<'a, &'a [u8], Data, DataIndexes<'a>> {
let indexes = DataIndexes {
name: MultiIndex::new(
|d, k| (PkOwned(d.name.as_bytes().to_vec()), PkOwned(k)),
"data",
"data__name",
),
name: MultiIndex::new(|d, k| (d.name.as_bytes().to_vec(), k), "data", "data__name"),
age: UniqueIndex::new(|d| U32Key::new(d.age), "data__age"),
name_lastname: UniqueIndex::new(
|d| index_string_tuple(&d.name, &d.last_name),
Expand Down Expand Up @@ -294,7 +290,7 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(2, count);
Expand All @@ -306,7 +302,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Ascending)
.collect::<StdResult<_>>()
.unwrap();
Expand All @@ -319,7 +315,7 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Marib".to_vec()))
.prefix(b"Marib".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);
Expand All @@ -328,7 +324,7 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Mari`".to_vec()))
.prefix(b"Mari`".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);
Expand All @@ -337,15 +333,15 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Maria5".to_vec()))
.prefix(b"Maria5".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);

// index_key() over MultiIndex works (empty pk)
// In a MultiIndex, an index key is composed by the index and the primary key.
// Primary key may be empty (so that to iterate over all elements that match just the index)
let key = (PkOwned(b"Maria".to_vec()), PkOwned(b"".to_vec()));
let key = (b"Maria".to_vec(), b"".to_vec());
// Use the index_key() helper to build the (raw) index key
let key = map.idx.name.index_key(key);
// Iterate using a bound over the raw key
Expand All @@ -359,7 +355,7 @@ mod test {

// index_key() over MultiIndex works (non-empty pk)
// Build key including a non-empty pk
let key = (PkOwned(b"Maria".to_vec()), PkOwned(b"1".to_vec()));
let key = (b"Maria".to_vec(), b"1".to_vec());
// Use the index_key() helper to build the (raw) index key
let key = map.idx.name.index_key(key);
// Iterate using a (exclusive) bound over the raw key.
Expand Down Expand Up @@ -443,7 +439,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Descending)
.collect::<StdResult<_>>()
.unwrap();
Expand Down Expand Up @@ -507,7 +503,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name_age
.sub_prefix(PkOwned(b"Maria".to_vec()))
.sub_prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Descending)
.collect::<StdResult<_>>()
.unwrap();
Expand Down Expand Up @@ -599,7 +595,7 @@ mod test {
.name
.pks(
store,
PkOwned(name.as_bytes().to_vec()),
name.as_bytes().to_vec(),
None,
None,
Order::Ascending,
Expand Down Expand Up @@ -677,7 +673,7 @@ mod test {
let res: StdResult<Vec<_>> = map
.idx
.name_lastname
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Ascending)
.collect();
let marias = res.unwrap();
Expand Down
32 changes: 14 additions & 18 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ mod test {
use super::*;

use crate::indexes::{index_string_tuple, index_triple, MultiIndex, UniqueIndex};
use crate::{Index, PkOwned, U32Key};
use crate::{Index, U32Key};
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{MemoryStorage, Order};
use serde::{Deserialize, Serialize};
Expand All @@ -216,9 +216,9 @@ mod test {

struct DataIndexes<'a> {
// Second arg is for storing pk
pub name: MultiIndex<'a, (PkOwned, PkOwned), Data>,
pub name: MultiIndex<'a, (Vec<u8>, Vec<u8>), Data>,
pub age: UniqueIndex<'a, U32Key, Data>,
pub name_lastname: UniqueIndex<'a, (PkOwned, PkOwned), Data>,
pub name_lastname: UniqueIndex<'a, (Vec<u8>, Vec<u8>), Data>,
}

// Future Note: this can likely be macro-derived
Expand All @@ -232,7 +232,7 @@ mod test {
// For composite multi index tests
struct DataCompositeMultiIndex<'a> {
// Third arg needed for storing pk
pub name_age: MultiIndex<'a, (PkOwned, U32Key, PkOwned), Data>,
pub name_age: MultiIndex<'a, (Vec<u8>, U32Key, Vec<u8>), Data>,
}

// Future Note: this can likely be macro-derived
Expand All @@ -246,11 +246,7 @@ mod test {
// Can we make it easier to define this? (less wordy generic)
fn build_snapshot_map<'a>() -> IndexedSnapshotMap<'a, &'a [u8], Data, DataIndexes<'a>> {
let indexes = DataIndexes {
name: MultiIndex::new(
|d, k| (PkOwned(d.name.as_bytes().to_vec()), PkOwned(k)),
"data",
"data__name",
),
name: MultiIndex::new(|d, k| (d.name.as_bytes().to_vec(), k), "data", "data__name"),
age: UniqueIndex::new(|d| U32Key::new(d.age), "data__age"),
name_lastname: UniqueIndex::new(
|d| index_string_tuple(&d.name, &d.last_name),
Expand Down Expand Up @@ -338,7 +334,7 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(2, count);
Expand All @@ -350,7 +346,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Ascending)
.collect::<StdResult<_>>()
.unwrap();
Expand All @@ -363,7 +359,7 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Marib".to_vec()))
.prefix(b"Marib".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);
Expand All @@ -372,7 +368,7 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Mari`".to_vec()))
.prefix(b"Mari`".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);
Expand All @@ -381,7 +377,7 @@ mod test {
let count = map
.idx
.name
.prefix(PkOwned(b"Maria5".to_vec()))
.prefix(b"Maria5".to_vec())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);
Expand Down Expand Up @@ -443,7 +439,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Descending)
.collect::<StdResult<_>>()
.unwrap();
Expand Down Expand Up @@ -512,7 +508,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name_age
.sub_prefix(PkOwned(b"Maria".to_vec()))
.sub_prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Descending)
.collect::<StdResult<_>>()
.unwrap();
Expand Down Expand Up @@ -609,7 +605,7 @@ mod test {
.name
.pks(
store,
PkOwned(name.as_bytes().to_vec()),
name.as_bytes().to_vec(),
None,
None,
Order::Ascending,
Expand Down Expand Up @@ -688,7 +684,7 @@ mod test {
let res: StdResult<Vec<_>> = map
.idx
.name_lastname
.prefix(PkOwned(b"Maria".to_vec()))
.prefix(b"Maria".to_vec())
.range(&store, None, None, Order::Ascending)
.collect();
let marias = res.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions packages/storage-plus/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ use cosmwasm_std::{from_slice, Binary, Order, Pair, StdError, StdResult, Storage
use crate::helpers::namespaces_with_key;
use crate::keys::EmptyPrefix;
use crate::map::Map;
use crate::{Bound, PkOwned, Prefix, Prefixer, PrimaryKey, U32Key};
use crate::{Bound, Prefix, Prefixer, PrimaryKey, U32Key};

pub fn index_string(data: &str) -> PkOwned {
PkOwned(data.as_bytes().to_vec())
pub fn index_string(data: &str) -> Vec<u8> {
data.as_bytes().to_vec()
}

pub fn index_tuple(name: &str, age: u32) -> (PkOwned, U32Key) {
pub fn index_tuple(name: &str, age: u32) -> (Vec<u8>, U32Key) {
(index_string(name), U32Key::new(age))
}

pub fn index_triple(name: &str, age: u32, pk: Vec<u8>) -> (PkOwned, U32Key, PkOwned) {
(index_string(name), U32Key::new(age), PkOwned(pk))
pub fn index_triple(name: &str, age: u32, pk: Vec<u8>) -> (Vec<u8>, U32Key, Vec<u8>) {
(index_string(name), U32Key::new(age), pk)
}

pub fn index_string_tuple(data1: &str, data2: &str) -> (PkOwned, PkOwned) {
pub fn index_string_tuple(data1: &str, data2: &str) -> (Vec<u8>, Vec<u8>) {
(index_string(data1), index_string(data2))
}

Expand Down
Loading