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

Bypass naive ordering when ordering by key (natural leveldb order) #22

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func (a *accessor) queryOrig(q dsq.Query) (dsq.Results, error) {
qr = dsq.NaiveFilter(qr, f)
}
for _, o := range q.Orders {
// leveldb iterators return entries sorted lexicographically by key, so we can bypass the naive ordering
// for OrderByKey. Since OrderByKey.Sort() has as struct receiver, the value stored in q.Orders can be a
// pointer or a struct, so we need to check for both for correctness.
switch o.(type) {
case dsq.OrderByKey, *dsq.OrderByKey:
continue
}
qr = dsq.NaiveOrder(qr, o)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need OrderByKey to override other orders (this currently just skips these). Given that keys are unique and have a total order, I believe we should:

  1. Start at the end.
  2. Work backwards until we find the last OrderByKey.
  3. Apply any remaining orders.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, in theory, optimize OrderByKeyDescending as well by taking advantage of the same total ordering. That is, stop at the last OrderByKeyDescending (if we reach one before we reach an OrderByKey). Unlike OrderByKey, we'll have to actually apply the OrderByKeyDescending order.

}
return qr, nil
Expand Down