From 13d98d328bb312f678fe0b308f3b2aac0d209f6c Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 24 Jan 2019 09:59:56 -0800 Subject: [PATCH 1/2] fix application or ordering for interface change https://github.com/ipfs/go-datastore/pull/110 --- datastore.go | 4 ++-- package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/datastore.go b/datastore.go index fac802e..fdc6531 100644 --- a/datastore.go +++ b/datastore.go @@ -339,8 +339,8 @@ func (t *txn) Query(q dsq.Query) (dsq.Results, error) { for _, f := range q.Filters { qr = dsq.NaiveFilter(qr, f) } - for _, o := range q.Orders { - qr = dsq.NaiveOrder(qr, o) + if len(q.Orders) > 0 { + qr = dsq.NaiveOrder(qr, q.Orders...) } return qr, nil diff --git a/package.json b/package.json index 48eacf3..0806c20 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ }, { "author": "jbenet", - "hash": "Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM", + "hash": "QmPGYyi1DtuWyUkG3PtvLz1xb4ScjnUvwJMCoX3cxeyxNr", "name": "go-datastore", - "version": "3.4.1" + "version": "3.5.0" }, { "author": "dgraph-io", From 8c2262c1f64a575c6d39d2184ba1ecdc9b175a14 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 24 Jan 2019 10:00:49 -0800 Subject: [PATCH 2/2] query: optimize order by key --- datastore.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/datastore.go b/datastore.go index fdc6531..f4b8dfe 100644 --- a/datastore.go +++ b/datastore.go @@ -274,6 +274,19 @@ func (t *txn) Query(q dsq.Query) (dsq.Results, error) { opt := badger.DefaultIteratorOptions opt.PrefetchValues = !q.KeysOnly + // Special case order by key. + orders := q.Orders + if len(orders) > 0 { + switch q.Orders[0].(type) { + case dsq.OrderByKey, *dsq.OrderByKey: + // Already ordered by key. + orders = nil + case dsq.OrderByKeyDescending, *dsq.OrderByKeyDescending: + orders = nil + opt.Reverse = true + } + } + txn := t.txn it := txn.NewIterator(opt) @@ -339,8 +352,8 @@ func (t *txn) Query(q dsq.Query) (dsq.Results, error) { for _, f := range q.Filters { qr = dsq.NaiveFilter(qr, f) } - if len(q.Orders) > 0 { - qr = dsq.NaiveOrder(qr, q.Orders...) + if len(orders) > 0 { + qr = dsq.NaiveOrder(qr, orders...) } return qr, nil