Skip to content

Commit

Permalink
Merge pull request #928 from go-kivik/allDocsLimit
Browse files Browse the repository at this point in the history
Add limit and skip support for _all_docs view
  • Loading branch information
flimzy authored Apr 8, 2024
2 parents 536ce2c + 8f50a69 commit 3744e05
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
8 changes: 7 additions & 1 deletion x/sqlite/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (d *db) AllDocs(ctx context.Context, options driver.Options) (driver.Rows,
optConflicts, _ = opts["conflicts"].(bool)
optDescending, _ = opts["descending"].(bool)
optIncludeDocs, _ = opts["include_docs"].(bool)
optLimit, _ = opts["limit"].(int)
optSkip, _ = opts["skip"].(int)
)

direction := "ASC"
Expand All @@ -70,6 +72,9 @@ func (d *db) AllDocs(ctx context.Context, options driver.Options) (driver.Rows,
where = append(where, fmt.Sprintf("rev.id %s $%d", startKeyOp(optDescending), len(args)+1))
args = append(args, startkey)
}
if optLimit == 0 {
optLimit = -1
}

query := fmt.Sprintf(d.query(`
WITH RankedRevisions AS (
Expand Down Expand Up @@ -101,7 +106,8 @@ func (d *db) AllDocs(ctx context.Context, options driver.Options) (driver.Rows,
WHERE %[2]s
GROUP BY rev.id, rev.rev, rev.rev_id
ORDER BY id %[1]s
`), direction, strings.Join(where, " AND "))
LIMIT %[3]d OFFSET %[4]d
`), direction, strings.Join(where, " AND "), optLimit, optSkip)
results, err := d.db.QueryContext(ctx, query, args...) //nolint:rowserrcheck // Err checked in Next
if err != nil {
return nil, err
Expand Down
61 changes: 59 additions & 2 deletions x/sqlite/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,65 @@ func TestDBAllDocs(t *testing.T) {
},
}
})
tests.Add("limit=2 returns first two documents only", func(t *testing.T) interface{} {
d := newDB(t)
rev1 := d.tPut("cat", map[string]string{"cat": "meow"})
_ = d.tPut("dog", map[string]string{"dog": "woof"})
rev3 := d.tPut("cow", map[string]string{"cow": "moo"})

return test{
db: d,
options: kivik.Param("limit", 2),
want: []rowResult{
{
ID: "cat",
Rev: rev1,
Value: `{"value":{"rev":"` + rev1 + `"}}` + "\n",
},
{
ID: "cow",
Rev: rev3,
Value: `{"value":{"rev":"` + rev3 + `"}}` + "\n",
},
},
}
})
tests.Add("skip=2 skips first two documents", func(t *testing.T) interface{} {
d := newDB(t)
_ = d.tPut("cat", map[string]string{"cat": "meow"})
rev2 := d.tPut("dog", map[string]string{"dog": "woof"})
_ = d.tPut("cow", map[string]string{"cow": "moo"})

return test{
db: d,
options: kivik.Param("skip", 2),
want: []rowResult{
{
ID: "dog",
Rev: rev2,
Value: `{"value":{"rev":"` + rev2 + `"}}` + "\n",
},
},
}
})
tests.Add("limit=1,skip=1 skips 1, limits 1", func(t *testing.T) interface{} {
d := newDB(t)
_ = d.tPut("cat", map[string]string{"cat": "meow"})
_ = d.tPut("dog", map[string]string{"dog": "woof"})
rev3 := d.tPut("cow", map[string]string{"cow": "moo"})

return test{
db: d,
options: kivik.Params(map[string]interface{}{"limit": 1, "skip": 1}),
want: []rowResult{
{
ID: "cow",
Rev: rev3,
Value: `{"value":{"rev":"` + rev3 + `"}}` + "\n",
},
},
}
})

/*
TODO:
Expand All @@ -491,9 +550,7 @@ func TestDBAllDocs(t *testing.T) {
- att_encoding_infio
- key
- keys
- limit
- reduce
- skip
- sorted
- stable
- statle
Expand Down

0 comments on commit 3744e05

Please sign in to comment.