dynamo
dynamo copied to clipboard
how to keep element sorted in batch
I expected this:
table.Batch(ID).Get(dynamo.Keys{"01", nil}, dynamo.Keys{"02", nil}).All(&elements)
To return element with ID "01", then element with ID "02", but it returns in the opposite way. Is there a way to maintain order?
Unfortunately there is not. This is part of how DynamoDB works:
When designing your application, keep in mind that DynamoDB does not return items in any particular order. https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html
BTW I don't think this is impossible. This library does actually store all of your requests in order. It should be possible with All, but it won't work with Iter. Maybe I could add a SortedAll method. I'll reopen this and think about it.
+1 for SortedAll
. It sounds fine to me to be a feature handled by the client.
hi @guregu , I started to look at the implementation. I found 2 ways to accomplish it:
- while creating the
bgIter
object, having an unmarshal function, which sort while iterating. I feel it has a probably a perf cost. And the way i saw it there werenil
elements, for non returning items - sort after iterating, in
AllSorted
function, but i'm not very comfortable withreflect
lib
can you give me your opinion, or an alternative way to manage it?
@jney I've given it some thought, and I think you'd probably have to keep track of item metadata inside of the iterator. BatchGet knows the names of the primary keys, so you'd have to read the primary key data from the items before they are unmarshaled and somehow associate them with the actual unmarshaled item. Then you can sort by the keys vs. the input order. With just reflection I'm not sure if there's a way you can grab the keys of any arbitrary object. Unfortunately this means extra overhead so we'd most likely have to add an option for sorting before the iterator is created.