dynamo icon indicating copy to clipboard operation
dynamo copied to clipboard

how to keep element sorted in batch

Open jney opened this issue 6 years ago • 5 comments

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?

jney avatar Nov 21 '18 16:11 jney

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

guregu avatar Nov 21 '18 16:11 guregu

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.

guregu avatar Nov 21 '18 16:11 guregu

+1 for SortedAll. It sounds fine to me to be a feature handled by the client.

jney avatar Nov 21 '18 16:11 jney

hi @guregu , I started to look at the implementation. I found 2 ways to accomplish it:

  1. 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 were nil elements, for non returning items
  2. sort after iterating, in AllSorted function, but i'm not very comfortable with reflect lib

can you give me your opinion, or an alternative way to manage it?

jney avatar Jan 08 '19 09:01 jney

@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.

guregu avatar Feb 26 '19 18:02 guregu