-
Notifications
You must be signed in to change notification settings - Fork 355
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
Question about MultiIndex
#466
Comments
That should work. I recommend you to follow the docs at https://docs.cosmwasm.com/tutorials/storage/indexes/ If you post more specific questions / code snippets, we could help you better. |
I have checked those docs, but it doesn't give me an answer. Sample:
So here my value is indexed by
This will work as expected, I will first filter by address using
How do I prefix and then use range to get the same functionality as I was getting without using multi index. Bonus Question:How to parse Vec as primary key to the actual type? I've noticed when tuples are used it contains extra bytes in the beginning.
|
The primary key (that is, the suffix of the full key, after you prefixed it). In raw format, because the 2nd element of the index is a To construct one, and assuming this is a If it comes as an If this is a pagination parameter, it's usually enough, when iterating in the client, to use the last received value (while it's non-null) and send it as the |
Use |
There are examples of this in in the We're are also working on a version of Stay tuned. |
but it's not a string, my primary key is |
|
Ah, I see, you're right, your primary key is composite. You'll have to deserialize it manually. This is relatively non-trivial, as the first element is length prefixed. I'll post a code snippet in a follow-up. |
No. A way to build the bound is Update: If you want to keep owned_tokens().idx.collection.prefix(collection).keys(
deps.storage,
Some(Bound::exclusive((owner, U64Key::new(token_id)).joined_key())),
Some(Bound::inclusive((owner, U64Key::new(u64::MAX)).joined_key())),
Order::Ascending,
) Assuming Finally, for pagination, you could either Of course, if That's why using |
To deserialize the composite: // `value` has the original value. It has to be mutable (to use this example. You can always use `split_at` and work with slices).
// helper to deserialize the length
fn parse_length(value: &[u8]) -> StdResult<usize> {
Ok(u16::from_be_bytes(
value
.try_into()
.map_err(|_| StdError::generic_err("Could not read 2 byte length"))?,
)
.into())
}
let mut tu = value.split_off(2);
let t_len = parse_length(&value)?;
let u = tu.split_off(t_len);
// Now `tu` is the `Addr`, and `u` is the `String`
let addr = Addr::unchecked(from_utf8(tu)?);
let string = from_utf8(u)?; |
Makes sense? As I said previously, we're working on ways to make all this (much?) simpler to use. |
Thanks a lot for responses, now it makes sense. I had suspicions that it's the case but needed to verify it. Any chance that you could introduce |
We recently added But it will not work / it's still not supported here (i.e. after a owned_tokens().idx.collection.prefix(collection).prefix_keys(
deps.storage,
Some(PrefixBound::inclusive(owner)),
Some(PrefixBound::inclusive(owner)),
Order::Ascending,
) I.e. give me all the keys that start with |
In any case, take a look at the code above. It will work, and properly support pagination / continuation. I mentioned some stuff about fixed vs. variable length addresses, that I now deleted, because that's only an issue in case you want to continue iterating past |
I am trying to utilize |
Just use |
Thanks. |
Yeah, it's just a wrapper around it. That I'm planning to deprecate soon; along with |
Hey, I am struggling to make
MultiIndex
work the way I need, maybe it's not possible at all at this stage. Hope to get some advice.Basically I have a primary key which is a tuple
(Addr, String)
and additional indexcollection
which is aString
. I need to be able to query by secondary index(by collection) and do filtering and pagination by primary key.I can do this with two maps:
Primary: Map<(Addr, String), Value>
+SecondaryIndex: Map<(String, (Addr, String)), Empty>
and then I can do something like this:This is probably doable but not very efficient and hard to maintain I would like to do something:
Any advice is appreciated.
The text was updated successfully, but these errors were encountered: