-
Notifications
You must be signed in to change notification settings - Fork 248
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
Paginating StorageMap #1139
Comments
Just FYI, the So, there is no need to do anything special yourself; calling |
But won't it loop form the beginning? How can I get values from the middle skipping some value? |
Yup; it'll loop from the beginning and handle pagination internally for you at the moment! So one approach to paginating is to hold onto the storate iterator you're handed back from the If you want more control over how you iterate storage keys, then currently you'll have to do that much more manually; see https://docs.rs/subxt/latest/subxt/storage/struct.Storage.html#method.fetch_keys to fetch some keys (with an optional starting key) and then use https://docs.rs/subxt/latest/subxt/storage/struct.Storage.html#method.fetch_raw to try to fetch values for those keys as you go. One downside of this is that you then need to decode the results yourself. The storage APIs are an area I'd like to improve, and I do agree that it should be possible to iterate from some starting key, to make the higher level APIs more useful. |
I had a look at the new interface, and as it stands it doesn't really support pagination by providing an initial key: https://paritytech.github.io/json-rpc-interface-spec/api/chainHead_unstable_storage.html What it does support is getting a list of all of the keys, and then asking for specific values for sets of those keys. What are you trying to achieve? It may be something that we can feed back into that spec. |
I want to get values from the middle, in lexicographic order. e.g. If total count of StorageMap is 100, I can get values and keys from 50 to 60. Not sure what will be better, in the storage api directly, or can be done with writing a custom runtime rpc function in the substrate itself. It will be more complicated with StorageDoubleMap etc, so custom runtime rpc may be better I think. In the documentation of fetch_keys it says:
fetch_key takes three argument: key, count: [u32], optional start_key Not sure what's the difference between key and start_key. |
I don't think that you're able to ask for eg "keys 50 to 60" in some map though (likely because the backend would then have to iterate through the storage to get to get 50 before returning results for you), so instead you have to know which key to start from, which means paginating from the beginning (this is fairly common in other APIs too for the same reason that providing a start item (often called a cursor) is more efficient than having to iterate to the Nth item in a DB). |
May be I can number the keys in another StorageMap, and fetch the key from the number, and than value from the key or use offchain indexing. |
FYI I did also open this on the new RPC spec: paritytech/json-rpc-interface-spec#85 The commend by Josep offhand is a good point; the new API, being light client friendly, may not be able to paginate through storage with a Depending on the number of keys you expect to have and exact use case, it may be then that you would need to just keep the iterator around, or fetch all keys up front and then paginate through the actual values, or something like that. |
To get the first 10 element with pass page size of 10
I want to paginate the storage map. How can I get the next 10 element or get range of value based on page number?
The text was updated successfully, but these errors were encountered: