Skip to content

Commit

Permalink
test ordered queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 9, 2019
1 parent b503c74 commit af06d32
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
12 changes: 8 additions & 4 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,26 @@ func (a *accessor) Delete(key ds.Key) (err error) {

func (a *accessor) Query(q dsq.Query) (dsq.Results, error) {
var rnge *util.Range

// make a copy of the query for the fallback naive query implementation.
// don't modify the original so res.Query() returns the correct results.
qNaive := q
if q.Prefix != "" {
rnge = util.BytesPrefix([]byte(q.Prefix))
q.Prefix = ""
qNaive.Prefix = ""
}
i := a.ldb.NewIterator(rnge, nil)
next := i.Next
if len(q.Orders) > 0 {
switch q.Orders[0].(type) {
case dsq.OrderByKey, *dsq.OrderByKey:
q.Orders = nil
qNaive.Orders = nil
case dsq.OrderByKeyDescending, *dsq.OrderByKeyDescending:
next = func() bool {
next = i.Prev
return i.Last()
}
q.Orders = nil
qNaive.Orders = nil
default:
}
}
Expand All @@ -153,7 +157,7 @@ func (a *accessor) Query(q dsq.Query) (dsq.Results, error) {
return nil
},
})
return dsq.NaiveQueryApply(q, r), nil
return dsq.NaiveQueryApply(qNaive, r), nil
}

// DiskUsage returns the current disk size used by this levelDB.
Expand Down
46 changes: 46 additions & 0 deletions ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"sort"
"testing"

ds "github.com/ipfs/go-datastore"
Expand Down Expand Up @@ -101,6 +102,33 @@ func testQuery(t *testing.T, d *Datastore) {
"/a/b/d",
"/a/c",
}, rs)

// test order

rs, err = d.Query(dsq.Query{Orders: []dsq.Order{dsq.OrderByKey{}}})
if err != nil {
t.Fatal(err)
}

keys := make([]string, 0, len(testcases))
for k := range testcases {
keys = append(keys, k)
}
sort.Strings(keys)

expectOrderedMatches(t, keys, rs)

rs, err = d.Query(dsq.Query{Orders: []dsq.Order{dsq.OrderByKeyDescending{}}})
if err != nil {
t.Fatal(err)
}

// reverse
for i, j := 0, len(keys)-1; i < j; i, j = i+1, j-1 {
keys[i], keys[j] = keys[j], keys[i]
}

expectOrderedMatches(t, keys, rs)
}

func TestQuery(t *testing.T) {
Expand All @@ -125,6 +153,7 @@ func TestQueryRespectsProcessMem(t *testing.T) {
}

func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
t.Helper()
actual, err := actualR.Rest()
if err != nil {
t.Error(err)
Expand All @@ -146,6 +175,23 @@ func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
}
}

func expectOrderedMatches(t *testing.T, expect []string, actualR dsq.Results) {
t.Helper()
actual, err := actualR.Rest()
if err != nil {
t.Error(err)
}

if len(actual) != len(expect) {
t.Error("not enough", expect, actual)
}
for i := range expect {
if expect[i] != actual[i].Key {
t.Errorf("expected %q, got %q", expect[i], actual[i].Key)
}
}
}

func testBatching(t *testing.T, d *Datastore) {
b, err := d.Batch()
if err != nil {
Expand Down

0 comments on commit af06d32

Please sign in to comment.