Commit a706e049 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

leveldb query: limit + offset

parent 9978dc41
......@@ -79,9 +79,21 @@ func (d *datastore) Query(q dsq.Query) (*dsq.Results, error) {
}
i := d.DB.NewIterator(rnge, nil)
// offset
if q.Offset > 0 {
for j := 0; j < q.Offset; j++ {
i.Next()
}
}
var es []dsq.Entry
for i.Next() {
// limit
if q.Limit > 0 && len(es) >= q.Limit {
break
}
k := ds.NewKey(string(i.Key())).String()
e := dsq.Entry{Key: k}
......@@ -98,10 +110,15 @@ func (d *datastore) Query(q dsq.Query) (*dsq.Results, error) {
return nil, err
}
// Now, apply remaining pieces.
q2 := q
q2.Offset = 0 // already applied
q2.Limit = 0 // already applied
// TODO: make this async with:
// qr := dsq.ResultsWithEntriesChan(q, ch)
qr := dsq.ResultsWithEntries(q, es)
qr = q.ApplyTo(qr)
qr = q2.ApplyTo(qr)
qr.Query = q // set it back
return qr, nil
}
......
......@@ -59,21 +59,35 @@ func TestQuery(t *testing.T) {
t.Fatal(err)
}
expect := []string{
expectMatches(t, []string{
"/a/b",
"/a/b/c",
"/a/b/d",
"/a/c",
"/a/d",
}, rs.AllEntries())
// test offset and limit
rs, err = d.Query(dsq.Query{Prefix: "/a/", Offset: 2, Limit: 2})
if err != nil {
t.Fatal(err)
}
re := rs.AllEntries()
if len(re) != len(expect) {
t.Error("not enough", expect, re)
expectMatches(t, []string{
"/a/b/d",
"/a/c",
}, rs.AllEntries())
}
func expectMatches(t *testing.T, expect []string, actual []dsq.Entry) {
if len(actual) != len(expect) {
t.Error("not enough", expect, actual)
}
for _, k := range expect {
found := false
for _, e := range re {
for _, e := range actual {
if e.Key == k {
found = true
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment