Lets say there are about 10,000 addresses mapped through Map<&Addr, User> = Map::new("users") and User is a struct variable with 4 Uint128 field in it, whats the best way to update all these 10,000 addresses? I mean, for example, let’s say that one of those Uint128 field in balance and I want to update this field for all the 10,000 addresses.
If you are updating the values for “all” of your keys then I would consider creating a different storage approach where you “index” (set the key to be unique and meaningful) to the value or even a single key to all of the values you tend to update at once. For example, you could store a Vec as an Item and just read and write it once to/from storage. Where UserBalance has the Addr and Balance defined. Or, you could create a Map<Addr, Balance> and still loop over them all in storage—but then you have maps for each “property” balance and the other three.
I personally like for loops, just go for it and loop through all 10000 addresses and update them. And, see what happens with fees on testnet.
I create a autoincrement counter to represent the primary key of my most important structure(s). And then use that to store vectors of them, keyed or “indexed” so I can at least get all their ids at one time and then loop over them “hydrate/instantiate” them from their primary storage and update them. But this depends on have a good data design (Entity Relationships) for on-chain storage.
I also use concatenated strings as key when I have a multipart key, because the typed key concatenation and IndexedMap have been difficult for me to comprehend—and I prefer this approach. It seems to work well. But, I’m also open to other approaches.
Here’s examples of storage for the project I’m working on…
let User_Addresses: StdResult<Vec<_>> = USERS.range(deps.storage, None, None, Order::Ascending).collect();
Now, in the above code, I assume all the data is collected into the variable User_Addresses. How do I loop through the User_Addresses and get Addr value?
Also, I am trying the following code:
for keys in USERS.keys(deps.storage, None, None, Order::Ascending).into_iter()
{
}
Here too, I am unable to access the addresses. Here is the Storage code.