2

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?

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

1 Answer 1

5

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
}
1
  • 1
    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, 2018 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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