Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

decl_storage: storage the key alongside the value to be able to iterate on key/value #4362

Closed
gui1117 opened this issue Dec 11, 2019 · 4 comments
Labels
J0-enhancement An additional feature request.
Milestone

Comments

@gui1117
Copy link
Contributor

gui1117 commented Dec 11, 2019

as follow up of #4185 (comment)

It would be interesting to introduce a way in decl_storage to tell to store the key alongside the value.
at the end the storage will have the data: (value, key).
This way when iterating on prefix we could actually iterate on (key, value) instead of just value.

This could be just a new attribute.

Implementing the StorageMap and other trait is not straightforward, here are some difficultyies:

  • StorageMap::append is implementing to work on the raw not decoded value. If the raw value is (value, key) we can't call scale_codec::EncodeAppend::append on it because we need to give only the raw data for the value.
    multiple solution here:

    • store actually (key, value), decode the key, the remaining byte are the raw value.
      not very good because get function will also have to decode the key.
    • store (value, key, key_length), so we can use key_length to split the raw data.
      (key_length is encoded on 4 byte or in a compact encoding but reversed so we can decode from the end)
    • implement a new EncodeAppendPreserving which does append on a raw data while preserving the suffix.
    • store (value, key) and just decode the value, thus there won't be any optimisation anymore, it will be same as using mutate.
  • StorageMap::swap: we can't swap the raw data anymore, solutions:

    • if we store (key, value) then we have to decode the key only and then we can swap split and swap the raw values
    • if we store (value, key) we have to decode the value and then we can split and swap the raw value
    • if we store (value, key, key_length) then we decode key_length and then we can split and swap the raw value.
@gui1117 gui1117 added this to the Ideas milestone Dec 11, 2019
@gui1117 gui1117 added the J0-enhancement An additional feature request. label Dec 11, 2019
@bkchr
Copy link
Member

bkchr commented Dec 11, 2019

for encode_append, we can't append on the raw data anymore as encode_append support only having the data as input.
we could extend encode_append to be able to append on some value while keeping the data in suffix.

I don't understand this. Could you explain it better? Why can we not append (key, value)?

@gui1117
Copy link
Contributor Author

gui1117 commented Dec 11, 2019

indeed I updated the message for more clarity, the initial idea was to store (value, key) so we don't have to decode the key on operation such as get and mutate (, similarly to what linked_map does by just putting the linkage in suffix).
If we store indeed (key, value) then append operation can work easily just decoding key and working on the raw value.

@bkchr
Copy link
Member

bkchr commented Dec 11, 2019

Ahh, yeah, now I understand :) We could maybe thing of a skip() method for skipping data in the encoded blob. So, we could use (key, data), skip the key and use data to append the stuff. Or we just prepend the length of the key.

@gui1117
Copy link
Contributor Author

gui1117 commented Mar 19, 2020

I think this should be close as we are now storing keys in hash

@gui1117 gui1117 closed this as completed Mar 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
J0-enhancement An additional feature request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants