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

Deprecate pks #345

Merged
merged 4 commits into from
Jul 23, 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
6 changes: 3 additions & 3 deletions contracts/cw721-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,13 @@ fn query_tokens(
let start = start_after.map(Bound::exclusive);

let owner_addr = deps.api.addr_validate(&owner)?;
let res: Result<Vec<_>, _> = tokens()
let pks: Vec<_> = tokens()
.idx
.owner
.pks(deps.storage, owner_addr, start, None, Order::Ascending)
.prefix(owner_addr)
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
.keys(deps.storage, start, None, Order::Ascending)
.take(limit)
.collect();
let pks = res?;

let res: Result<Vec<_>, _> = pks.iter().map(|v| String::from_utf8(v.to_vec())).collect();
let tokens = res.map_err(StdError::invalid_utf8)?;
Expand Down
9 changes: 4 additions & 5 deletions packages/storage-plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,19 @@ Notice this uses `prefix()`, explained above in the `Map` section.
Now `tokens` contains `(token_id, TokenInfo)` pairs for the given `owner`.
The pk values are `Vec<u8>`, as this is a limitation of the current implementation.

Another example that is slightly similar, but returning only the `token_id`s, using the `pks()` method:
Another example that is similar, but returning only the `token_id`s, using the `keys()` method:
```rust
let res: Result<Vec<_>, _> = tokens()
let pks: Vec<_> = tokens()
.idx
.owner
.pks(
.prefix(owner_addr)
.keys(
deps.storage,
owner_addr,
start,
None,
Order::Ascending,
)
.take(limit)
.collect();
let pks = res?;
```
Now `pks` contains `token_id` values (as `Vec<u8>`s) for the given `owner`.
9 changes: 2 additions & 7 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,8 @@ mod test {
-> usize {
map.idx
.name
.pks(
store,
name.as_bytes().to_vec(),
None,
None,
Order::Ascending,
)
.prefix(name.as_bytes().to_vec())
.keys(store, None, None, Order::Ascending)
.count()
};

Expand Down
9 changes: 2 additions & 7 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,8 @@ mod test {
-> usize {
map.idx
.name
.pks(
store,
name.as_bytes().to_vec(),
None,
None,
Order::Ascending,
)
.prefix(name.as_bytes().to_vec())
.keys(store, None, None, Order::Ascending)
.count()
};

Expand Down
59 changes: 31 additions & 28 deletions packages/storage-plus/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,43 +142,24 @@ where
k.joined_key()
}

// FIXME?: Move to Prefix<T> for ergonomics
pub fn pks<'c>(
&self,
store: &'c dyn Storage,
p: K::Prefix,
min: Option<Bound>,
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = StdResult<Vec<u8>>> + 'c>
where
T: 'c,
{
let prefix = self.prefix(p);
let mapped = prefix.range(store, min, max, order).map(|res| {
let t = res?;
Ok(t.0)
});
Box::new(mapped)
}

#[cfg(test)]
pub fn count(&self, store: &dyn Storage, p: K::Prefix) -> usize {
self.pks(store, p, None, None, Order::Ascending).count()
let prefix = self.prefix(p);
prefix.keys(store, None, None, Order::Ascending).count()
}

#[cfg(test)]
pub fn all_pks(&self, store: &dyn Storage, p: K::Prefix) -> Vec<Vec<u8>> {
self.pks(store, p, None, None, Order::Ascending)
.collect::<StdResult<Vec<Vec<u8>>>>()
.unwrap()
let prefix = self.prefix(p);
prefix
.keys(store, None, None, Order::Ascending)
.collect::<Vec<Vec<u8>>>()
}

#[cfg(test)]
pub fn all_items(&self, store: &dyn Storage, prefix: K::Prefix) -> StdResult<Vec<Pair<T>>> {
self.prefix(prefix)
.range(store, None, None, Order::Ascending)
.collect()
pub fn all_items(&self, store: &dyn Storage, p: K::Prefix) -> StdResult<Vec<Pair<T>>> {
let prefix = self.prefix(p);
prefix.range(store, None, None, Order::Ascending).collect()
}
}

Expand All @@ -204,6 +185,17 @@ where
self.sub_prefix(K::SubPrefix::new())
.range(store, min, max, order)
}

pub fn keys<'c>(
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
&'c self,
store: &'c dyn Storage,
min: Option<Bound>,
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'c> {
self.sub_prefix(K::SubPrefix::new())
.keys(store, min, max, order)
}
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -319,4 +311,15 @@ where
self.sub_prefix(K::SubPrefix::new())
.range(store, min, max, order)
}

pub fn keys<'c>(
&self,
store: &'c dyn Storage,
min: Option<Bound>,
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'c> {
self.sub_prefix(K::SubPrefix::new())
.keys(store, min, max, order)
}
}
5 changes: 1 addition & 4 deletions packages/storage-plus/src/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ where
min: Option<Bound>,
max: Option<Bound>,
order: Order,
) -> Box<dyn Iterator<Item = Vec<u8>> + 'a>
where
T: 'a,
{
) -> Box<dyn Iterator<Item = Vec<u8>> + 'a> {
let mapped =
range_with_prefix(store, &self.storage_prefix, min, max, order).map(|(k, _)| k);
Box::new(mapped)
Expand Down