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

Range with no prefix support #433

Merged
merged 6 commits into from
Sep 20, 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
11 changes: 7 additions & 4 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::indexes::Index;
use crate::keys::{EmptyPrefix, Prefixer, PrimaryKey};
use crate::keys::{Prefixer, PrimaryKey};
use crate::map::Map;
use crate::prefix::{Bound, Prefix};
use crate::Path;
Expand Down Expand Up @@ -136,6 +136,11 @@ where
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> {
Prefix::new(self.pk_namespace, &[])
}
}

// short-cut for simple keys, rather than .prefix(()).range(...)
Expand All @@ -144,7 +149,6 @@ where
K: PrimaryKey<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
K::SubPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -158,8 +162,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
.range(store, min, max, order)
self.no_prefix().range(store, min, max, order)
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std::{StdError, StdResult, Storage};
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::keys::{EmptyPrefix, Prefixer, PrimaryKey};
use crate::keys::{Prefixer, PrimaryKey};
use crate::prefix::{Bound, Prefix};
use crate::snapshot::SnapshotMap;
use crate::{IndexList, Path, Strategy};
Expand Down Expand Up @@ -160,6 +160,11 @@ where
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> {
Prefix::new(self.pk_namespace, &[])
}
}

// short-cut for simple keys, rather than .prefix(()).range(...)
Expand All @@ -168,7 +173,6 @@ where
K: PrimaryKey<'a> + Prefixer<'a>,
T: Serialize + DeserializeOwned + Clone,
I: IndexList<T>,
K::SubPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -182,8 +186,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
.range(store, min, max, order)
self.no_prefix().range(store, min, max, order)
}
}

Expand Down
30 changes: 19 additions & 11 deletions packages/storage-plus/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use serde::{Deserialize, Serialize};
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, Prefix, Prefixer, PrimaryKey, U32Key};

Expand Down Expand Up @@ -138,6 +137,15 @@ where
)
}

fn no_prefix(&self) -> Prefix<T> {
Prefix::with_deserialization_function(
self.idx_namespace,
&[],
self.pk_namespace,
deserialize_multi_kv,
)
}

pub fn index_key(&self, k: K) -> Vec<u8> {
k.joined_key()
}
Expand Down Expand Up @@ -168,7 +176,6 @@ impl<'a, K, T> MultiIndex<'a, K, T>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
K::SubPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -182,8 +189,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
.range(store, min, max, order)
self.no_prefix().range(store, min, max, order)
}

pub fn keys<'c>(
Expand All @@ -193,8 +199,7 @@ where
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'c> {
self.sub_prefix(K::SubPrefix::new())
.keys(store, min, max, order)
self.no_prefix().keys(store, min, max, order)
}
}

Expand Down Expand Up @@ -279,6 +284,12 @@ where
})
}

fn no_prefix(&self) -> Prefix<T> {
Prefix::with_deserialization_function(self.idx_namespace, &[], &[], |_, _, kv| {
deserialize_unique_kv(kv)
})
}

/// returns all items that match this secondary index, always by pk Ascending
pub fn item(&self, store: &dyn Storage, idx: K) -> StdResult<Option<Pair<T>>> {
let data = self
Expand All @@ -294,7 +305,6 @@ impl<'a, K, T> UniqueIndex<'a, K, T>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
K::SubPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -308,8 +318,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
.range(store, min, max, order)
self.no_prefix().range(store, min, max, order)
}

pub fn keys<'c>(
Expand All @@ -319,7 +328,6 @@ where
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'c> {
self.sub_prefix(K::SubPrefix::new())
.keys(store, min, max, order)
self.no_prefix().keys(store, min, max, order)
}
}
9 changes: 0 additions & 9 deletions packages/storage-plus/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,6 @@ impl<'a> Prefixer<'a> for &'a str {
}
}

// this is a marker for the Map.range() helper, so we can detect () in Generic bounds
pub trait EmptyPrefix {
fn new() -> Self;
}

impl EmptyPrefix for () {
fn new() {}
}

impl<'a> PrimaryKey<'a> for Vec<u8> {
type Prefix = ();
type SubPrefix = ();
Expand Down
42 changes: 35 additions & 7 deletions packages/storage-plus/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use serde::Serialize;
use std::marker::PhantomData;

use crate::helpers::query_raw;
use crate::keys::PrimaryKey;
#[cfg(feature = "iterator")]
use crate::keys::{EmptyPrefix, Prefixer};
use crate::keys::Prefixer;
use crate::keys::PrimaryKey;
use crate::path::Path;
#[cfg(feature = "iterator")]
use crate::prefix::{Bound, Prefix};
Expand Down Expand Up @@ -48,6 +48,11 @@ where
Prefix::new(self.namespace, &p.prefix())
}

#[cfg(feature = "iterator")]
pub(crate) fn no_prefix(&self) -> Prefix<T> {
Prefix::new(self.namespace, &[])
}

pub fn save(&self, store: &mut dyn Storage, k: K, data: &T) -> StdResult<()> {
self.key(k).save(store, data)
}
Expand Down Expand Up @@ -109,7 +114,6 @@ impl<'a, K, T> Map<'a, K, T>
where
T: Serialize + DeserializeOwned,
K: PrimaryKey<'a>,
K::SubPrefix: EmptyPrefix,
{
pub fn range<'c>(
&self,
Expand All @@ -121,8 +125,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
.range(store, min, max, order)
self.no_prefix().range(store, min, max, order)
}

pub fn keys<'c>(
Expand All @@ -135,8 +138,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
.keys(store, min, max, order)
self.no_prefix().keys(store, min, max, order)
}
}

Expand Down Expand Up @@ -401,6 +403,32 @@ mod test {
.unwrap();

// let's try to iterate!
let all: StdResult<Vec<_>> = TRIPLE.range(&store, None, None, Order::Ascending).collect();
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
let all = all.unwrap();
assert_eq!(4, all.len());
assert_eq!(
all,
vec![
(
(b"owner".to_vec(), U8Key::new(9), b"recipient".to_vec()).joined_key(),
1000
),
(
(b"owner".to_vec(), U8Key::new(9), b"recipient2".to_vec()).joined_key(),
3000
),
(
(b"owner".to_vec(), U8Key::new(10), b"recipient3".to_vec()).joined_key(),
3000
),
(
(b"owner2".to_vec(), U8Key::new(9), b"recipient".to_vec()).joined_key(),
5000
)
]
);

// let's iterate over a prefix
let all: StdResult<Vec<_>> = TRIPLE
.prefix((b"owner", 9u8.into()))
.range(&store, None, None, Order::Ascending)
Expand Down
10 changes: 6 additions & 4 deletions packages/storage-plus/src/snapshot/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::Serialize;

use cosmwasm_std::{StdError, StdResult, Storage};

use crate::keys::{EmptyPrefix, PrimaryKey};
use crate::keys::PrimaryKey;
use crate::map::Map;
use crate::path::Path;
use crate::prefix::Prefix;
Expand Down Expand Up @@ -66,6 +66,10 @@ where
self.primary.sub_prefix(p)
}

fn no_prefix(&self) -> Prefix<T> {
self.primary.no_prefix()
}

/// load old value and store changelog
fn write_change(&self, store: &mut dyn Storage, k: K, height: u64) -> StdResult<()> {
// if there is already data in the changelog for this key and block, do not write more
Expand Down Expand Up @@ -155,7 +159,6 @@ impl<'a, K, T> SnapshotMap<'a, K, T>
where
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a> + Prefixer<'a>,
K::SubPrefix: EmptyPrefix,
{
// I would prefer not to copy code from Prefix, but no other way
// with lifetimes (create Prefix inside function and return ref = no no)
Expand All @@ -169,8 +172,7 @@ where
where
T: 'c,
{
self.sub_prefix(K::SubPrefix::new())
.range(store, min, max, order)
self.no_prefix().range(store, min, max, order)
}
}

Expand Down