I understand how to use primary and secondary keys for index. I want to search the primary key for all payments by a specific user. Then from that result set I want to search by a secondary key to see all payments at a specific location.

Is there a way to do this, or do I just loop through the iterator and check each one?

  • there's only secondary index, no secondary key, it is just table, they are all in the table scope – Jimmy Guo Jun 29 at 5:53
up vote 2 down vote accepted

For "multi-index" searches I use one index to perform the search on my primary, then simply loop through the iterator until I either find the item I'm looking for with the matching secondary key.

Obviously, you should search first with the key that will return the fewest matches so that the loop can abort as soon as the iterator's primary key no longer matches what you are looking for.

auto idx = fileReceipts.template get_index<N(byfid)>();
auto matchingItem = idx.lower_bound(fID);

// Advance to the first entry matching the specified vID
while (matchingItem != idx.end() && matchingItem->fID == fID &&
       matchingItem->vID != vID && matchingItem->user != user) {
    matchingItem++;
}

if (matchingItem != idx.end() && matchingItem->fID == fID &&
       matchingItem->vID == vID && matchingItem->user == user) {
      // Perform whatever operation you wish on the matchingItem
}
  • Fair enough, that is what I decided to do too. But it is good to know that this is an accepted practice amongst developers using eos. – msj121 Jul 1 at 4:14

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.