Skip to content

Commit

Permalink
Add another naive solution for offset
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelavila committed Mar 21, 2019
1 parent 28e5b7b commit 7a8c70e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
6 changes: 5 additions & 1 deletion mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func (d *Datastore) Query(q query.Query) (query.Results, error) {
prefix := ds.NewKey(q.Prefix)
dses, mounts, rests := d.lookupAll(prefix)

// offset needs to be applied after the results are aggregated
offset := q.Offset
q.Offset = 0

queries := &querySet{
query: q,
heads: make([]*queryResults, 0, len(dses)),
Expand All @@ -256,7 +260,7 @@ func (d *Datastore) Query(q query.Query) (query.Results, error) {
Close: queries.close,
})

return query.NaiveLimit(qr, q.Limit), nil
return query.NaiveLimit(query.NaiveOffset(qr, offset), q.Limit), nil
}

func (d *Datastore) Close() error {
Expand Down
51 changes: 51 additions & 0 deletions mount/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,57 @@ func TestQueryLimitCrossWithSort(t *testing.T) {
}
}

func TestQueryLimitAndOffsetCrossWithSort(t *testing.T) {
mapds1 := sync.MutexWrap(datastore.NewMapDatastore())
mapds2 := sync.MutexWrap(datastore.NewMapDatastore())
mapds3 := sync.MutexWrap(datastore.NewMapDatastore())
m := mount.New([]mount.Mount{
{Prefix: datastore.NewKey("/rok"), Datastore: mapds1},
{Prefix: datastore.NewKey("/zoo"), Datastore: mapds2},
{Prefix: datastore.NewKey("/noop"), Datastore: mapds3},
})

m.Put(datastore.NewKey("/rok/0"), []byte("ghi"))
m.Put(datastore.NewKey("/zoo/0"), []byte("123"))
m.Put(datastore.NewKey("/rok/1"), []byte("def"))
m.Put(datastore.NewKey("/zoo/1"), []byte("167"))
m.Put(datastore.NewKey("/zoo/2"), []byte("345"))
m.Put(datastore.NewKey("/rok/3"), []byte("abc"))
m.Put(datastore.NewKey("/zoo/3"), []byte("456"))

q := query.Query{Limit: 3, Offset: 2, Orders: []query.Order{query.OrderByKey{}}}
res, err := m.Query(q)
if err != nil {
t.Fatalf("Query fail: %v\n", err)
}

entries, err := res.Rest()
if err != nil {
t.Fatalf("Query Results.Rest fail: %v\n", err)
}

expect := []string{
"/rok/3",
"/zoo/0",
"/zoo/1",
}

if len(entries) != len(expect) {
t.Fatalf("expected %d entries, but got %d", len(expect), len(entries))
}

for i, e := range expect {
if e != entries[i].Key {
t.Errorf("expected key %s, but got %s", e, entries[i].Key)
}
}

err = res.Close()
if err != nil {
t.Errorf("result.Close failed %d", err)
}
}

func TestLookupPrio(t *testing.T) {
mapds0 := datastore.NewMapDatastore()
mapds1 := datastore.NewMapDatastore()
Expand Down
2 changes: 1 addition & 1 deletion query/query_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NaiveOffset(qr Results, offset int) Results {
}
}()

return DerivedResults(qr, ch)
return ResultsWithChan(qr.Query(), ch)
}

// NaiveOrder reorders results according to given orders.
Expand Down

0 comments on commit 7a8c70e

Please sign in to comment.