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

Split indexes impl into different files #526

Closed
wants to merge 17 commits into from
Closed
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
12 changes: 12 additions & 0 deletions packages/storage-plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,15 @@ Another example that is similar, but returning only the `token_id`s, using the `
.collect();
```
Now `pks` contains `token_id` values (as `Vec<u8>`s) for the given `owner`.

### Index keys deserialization

To deserialize keys of indexes (using the `*_de` functions), there are currently some requirements / limitations:

- For `UniqueIndex`: The primary key (`PK`) type needs to be specified, in order to deserialize the primary key to it.
This generic type comes with a default of `()`, which means that no deserialization / data will be provided
for the primary key.

- For `MultiIndex`: The last element of the index tuple must be specified with the type you want it to be deserialized.
That is, the last tuple element serves as a marker for the deserialization type (in the same way `PK` does it in
`UniqueIndex`).
217 changes: 202 additions & 15 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ where
mod test {
use super::*;

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

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

// Future Note: this can likely be macro-derived
Expand Down Expand Up @@ -307,7 +307,11 @@ mod test {
// Can we make it easier to define this? (less wordy generic)
fn build_map<'a>() -> IndexedMap<'a, &'a str, Data, DataIndexes<'a>> {
let indexes = DataIndexes {
name: MultiIndex::new(|d, k| (d.name.as_bytes().to_vec(), k), "data", "data__name"),
name: MultiIndex::new(
|d, k| (d.name.clone(), unsafe { String::from_utf8_unchecked(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 @@ -395,7 +399,7 @@ mod test {
let count = map
.idx
.name
.prefix(b"Maria".to_vec())
.prefix("Maria".to_string())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(2, count);
Expand All @@ -407,7 +411,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name
.prefix(b"Maria".to_vec())
.prefix("Maria".to_string())
.range(&store, None, None, Order::Ascending)
.collect::<StdResult<_>>()
.unwrap();
Expand All @@ -420,7 +424,7 @@ mod test {
let count = map
.idx
.name
.prefix(b"Marib".to_vec())
.prefix("Marib".to_string())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);
Expand All @@ -429,7 +433,7 @@ mod test {
let count = map
.idx
.name
.prefix(b"Mari`".to_vec())
.prefix("Mari`".to_string())
.range(&store, None, None, Order::Ascending)
.count();
assert_eq!(0, count);
Expand All @@ -438,15 +442,15 @@ mod test {
let count = map
.idx
.name
.prefix(b"Maria5".to_vec())
.prefix("Maria5".to_string())
.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 = (b"Maria".to_vec(), b"".to_vec());
let key = ("Maria".to_string(), "".to_string());
// 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 @@ -460,7 +464,7 @@ mod test {

// index_key() over MultiIndex works (non-empty pk)
// Build key including a non-empty pk
let key = (b"Maria".to_vec(), b"1".to_vec());
let key = ("Maria".to_string(), "1".to_string());
// 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 @@ -544,7 +548,7 @@ mod test {
let marias: Vec<_> = map
.idx
.name
.prefix(b"Maria".to_vec())
.prefix("Maria".to_string())
.range(&store, None, None, Order::Descending)
.collect::<StdResult<_>>()
.unwrap();
Expand All @@ -559,6 +563,62 @@ mod test {
assert_eq!(marias[1].1, data1);
}

#[test]
fn range_de_simple_key_by_multi_index() {
let mut store = MockStorage::new();
let map = build_map();

// save data
let data1 = Data {
name: "Maria".to_string(),
last_name: "".to_string(),
age: 42,
};
let pk = "5627";
map.save(&mut store, pk, &data1).unwrap();

let data2 = Data {
name: "Juan".to_string(),
last_name: "Perez".to_string(),
age: 13,
};
let pk = "5628";
map.save(&mut store, pk, &data2).unwrap();

let data3 = Data {
name: "Maria".to_string(),
last_name: "Williams".to_string(),
age: 24,
};
let pk = "5629";
map.save(&mut store, pk, &data3).unwrap();

let data4 = Data {
name: "Maria Luisa".to_string(),
last_name: "Bemberg".to_string(),
age: 12,
};
let pk = "5630";
map.save(&mut store, pk, &data4).unwrap();

let marias: Vec<_> = map
.idx
.name
.prefix_de("Maria".to_string())
.range_de(&store, None, None, Order::Descending)
.collect::<StdResult<_>>()
.unwrap();
let count = marias.len();
assert_eq!(2, count);

// Sorted by (descending) pk
assert_eq!(marias[0].0, "5629");
assert_eq!(marias[1].0, "5627");
// Data is correct
assert_eq!(marias[0].1, data3);
assert_eq!(marias[1].1, data1);
}

#[test]
fn range_composite_key_by_multi_index() {
let mut store = MockStorage::new();
Expand Down Expand Up @@ -624,6 +684,71 @@ mod test {
assert_eq!(data3, marias[1].1);
}

#[test]
fn range_de_composite_key_by_multi_index() {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
};
let map = IndexedMap::new("data", indexes);

// save data
let data1 = Data {
name: "Maria".to_string(),
last_name: "".to_string(),
age: 42,
};
let pk1: &[u8] = b"5627";
map.save(&mut store, pk1, &data1).unwrap();

let data2 = Data {
name: "Juan".to_string(),
last_name: "Perez".to_string(),
age: 13,
};
let pk2: &[u8] = b"5628";
map.save(&mut store, pk2, &data2).unwrap();

let data3 = Data {
name: "Maria".to_string(),
last_name: "Young".to_string(),
age: 24,
};
let pk3: &[u8] = b"5629";
map.save(&mut store, pk3, &data3).unwrap();

let data4 = Data {
name: "Maria Luisa".to_string(),
last_name: "Bemberg".to_string(),
age: 43,
};
let pk4: &[u8] = b"5630";
map.save(&mut store, pk4, &data4).unwrap();

let marias: Vec<_> = map
.idx
.name_age
.sub_prefix_de(b"Maria".to_vec())
.range_de(&store, None, None, Order::Descending)
.collect::<StdResult<_>>()
.unwrap();
let count = marias.len();
assert_eq!(2, count);

// Remaining part (age) of the index keys, plus pks (bytes) (sorted by age descending)
assert_eq!((42, pk1.to_vec()), marias[0].0);
assert_eq!((24, pk3.to_vec()), marias[1].0);

// Data
assert_eq!(data1, marias[0].1);
assert_eq!(data3, marias[1].1);
}

#[test]
fn unique_index_enforced() {
let mut store = MockStorage::new();
Expand Down Expand Up @@ -698,7 +823,7 @@ mod test {
-> usize {
map.idx
.name
.prefix(name.as_bytes().to_vec())
.prefix(name.to_string())
.keys(store, None, None, Order::Ascending)
.count()
};
Expand Down Expand Up @@ -764,6 +889,39 @@ mod test {
assert_eq!(datas[0], ages[3].1);
}

#[test]
fn unique_index_simple_key_range_de() {
let mut store = MockStorage::new();
let map = build_map();

// save data
let (pks, datas) = save_data(&mut store, &map);

let res: StdResult<Vec<_>> = map
.idx
.age
.range_de(&store, None, None, Order::Ascending)
.collect();
let ages = res.unwrap();

let count = ages.len();
assert_eq!(5, count);

// The pks, sorted by age ascending
assert_eq!(pks[4], ages[4].0);
assert_eq!(pks[3], ages[0].0);
assert_eq!(pks[1], ages[1].0);
assert_eq!(pks[2], ages[2].0);
assert_eq!(pks[0], ages[3].0);

// The associated data
assert_eq!(datas[4], ages[4].1);
assert_eq!(datas[3], ages[0].1);
assert_eq!(datas[1], ages[1].1);
assert_eq!(datas[2], ages[2].1);
assert_eq!(datas[0], ages[3].1);
}

#[test]
fn unique_index_composite_key_range() {
let mut store = MockStorage::new();
Expand Down Expand Up @@ -793,6 +951,35 @@ mod test {
assert_eq!(datas[1], marias[1].1);
}

#[test]
fn unique_index_composite_key_range_de() {
let mut store = MockStorage::new();
let map = build_map();

// save data
let (pks, datas) = save_data(&mut store, &map);

let res: StdResult<Vec<_>> = map
.idx
.name_lastname
.prefix_de(b"Maria".to_vec())
.range_de(&store, None, None, Order::Ascending)
.collect();
let marias = res.unwrap();

// Only two people are called "Maria"
let count = marias.len();
assert_eq!(2, count);

// The pks
assert_eq!(pks[0], marias[0].0);
assert_eq!(pks[1], marias[1].0);

// The associated data
assert_eq!(datas[0], marias[0].1);
assert_eq!(datas[1], marias[1].1);
}

#[test]
#[cfg(feature = "iterator")]
fn range_de_simple_string_key() {
Expand Down
4 changes: 2 additions & 2 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ where
mod test {
use super::*;

use crate::indexes::{index_string_tuple, index_triple, MultiIndex, UniqueIndex};
use crate::{Index, U32Key};
use crate::indexes::{index_string_tuple, index_triple};
use crate::{Index, MultiIndex, U32Key, UniqueIndex};
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{MemoryStorage, Order};
use serde::{Deserialize, Serialize};
Expand Down
Loading