Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to keep element sorted in batch #78

Open
jney opened this issue Nov 21, 2018 · 5 comments
Open

how to keep element sorted in batch #78

jney opened this issue Nov 21, 2018 · 5 comments

Comments

@jney
Copy link

jney commented Nov 21, 2018

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?

@guregu
Copy link
Owner

guregu commented Nov 21, 2018

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 guregu closed this as completed Nov 21, 2018
@guregu
Copy link
Owner

guregu commented Nov 21, 2018

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 guregu reopened this Nov 21, 2018
@jney
Copy link
Author

jney commented Nov 21, 2018

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

@jney
Copy link
Author

jney commented Jan 8, 2019

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?

@guregu
Copy link
Owner

guregu commented Feb 26, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants