cosmos / cosmos-sdk

:chains: A Framework for Building High Value Public Blockchains :sparkles:
https://cosmos.network/
Apache License 2.0
6.19k stars 3.58k forks source link

[Feature]: Add `RefKeys()` method to collections `MultiIterator` #21091

Open oren-lava opened 1 month ago

oren-lava commented 1 month ago

Summary

Add a RefKeys() method to collections MultiIterator to allow iteration over reference keys without explicitly knowing their value.

Problem Definition

Assume that an indexed map is created for BaseAccount as described in the Cosmos docs. Now, instead of using an account number as a unique index, we use a multi index which is the origin country of the account.

Normally, many account can share an origin country so the multi index fits perfectly. Now, assume I wish to perform some processing on accounts that share the same country index. Since it's no an integer index, there is no clear set of strings that I can iterate over to check which countries exist in my collection.

If there was a method for a MultiIterator to fetch all the existing reference keys, I could iterate over them, use the MatchExact method and get all the account of a specific country.

Proposed Feature

I propose adding a method called RefKeys() to MultiIterator that will have an almost identical implementation of the PrimaryKeys() method but instead will return a list of the reference keys.

oren-lava commented 2 weeks ago

Hi @JulianToledano, Are you working on this? I'll gladly take this on me :)

JulianToledano commented 2 weeks ago

Hi @JulianToledano, Are you working on this? I'll gladly take this on me :)

sure thing, go ahead!! 👍

oren-lava commented 2 weeks ago

Done working on it. Eager to see your review @JulianToledano :)