Skip to content

Commit

Permalink
Support local_seq view option
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed May 23, 2024
1 parent cc75bd8 commit 0264c97
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
4 changes: 3 additions & 1 deletion x/sqlite/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,14 @@ func (d *fullDoc) toMap() map[string]interface{} {
}
result["_attachments"] = attachments
}
if d.LocalSeq > 0 {
result["_local_seq"] = d.LocalSeq
}
/*
Conflicts []string `json:"_conflicts,omitempty"`
DeletedConflicts []string `json:"_deleted_conflicts,omitempty"`
RevsInfo []map[string]string `json:"_revs_info,omitempty"`
Revisions *revsInfo `json:"_revisions,omitempty"`
LocalSeq int `json:"_local_seq,omitempty"`
*/
return result
}
15 changes: 10 additions & 5 deletions x/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,24 +319,25 @@ const batchSize = 100
// ddoc revid and last_seq. If mode is "true", it will also update the index.
func (d *db) updateIndex(ctx context.Context, ddoc, view, mode string) (revision, error) {
var (
ddocRev revision
mapFuncJS *string
lastSeq int
includeDesign sql.NullBool
ddocRev revision
mapFuncJS *string
lastSeq int
includeDesign, localSeq sql.NullBool
)
err := d.db.QueryRowContext(ctx, d.query(`
SELECT
docs.rev,
docs.rev_id,
design.func_body,
design.include_design,
design.local_seq,
COALESCE(design.last_seq, 0) AS last_seq
FROM {{ .Docs }} AS docs
LEFT JOIN {{ .Design }} AS design ON docs.id = design.id AND docs.rev = design.rev AND docs.rev_id = design.rev_id AND design.func_type = 'map'
WHERE docs.id = $1
ORDER BY docs.rev DESC, docs.rev_id DESC
LIMIT 1
`), "_design/"+ddoc).Scan(&ddocRev.rev, &ddocRev.id, &mapFuncJS, &includeDesign, &lastSeq)
`), "_design/"+ddoc).Scan(&ddocRev.rev, &ddocRev.id, &mapFuncJS, &includeDesign, &localSeq, &lastSeq)
switch {
case errors.Is(err, sql.ErrNoRows):
return revision{}, &internal.Error{Status: http.StatusNotFound, Message: "missing"}
Expand Down Expand Up @@ -492,6 +493,10 @@ func (d *db) updateIndex(ctx context.Context, ddoc, view, mode string) (revision
continue
}

if localSeq.Bool {
full.LocalSeq = seq
}

if err := vm.Set("emit", emit(full.ID, rev)); err != nil {
return revision{}, err
}
Expand Down
31 changes: 28 additions & 3 deletions x/sqlite/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,34 @@ func TestDBQuery(t *testing.T) {
},
}
})
tests.Add("options.local_seq=true", func(t *testing.T) interface{} {
d := newDB(t)
_ = d.tPut("_design/foo", map[string]interface{}{
"key": "design",
"views": map[string]interface{}{
"bar": map[string]string{
"map": `function(doc) {
if (doc.key) {
emit(doc.key, doc._local_seq);
}
}`,
},
},
"options": map[string]interface{}{
"local_seq": true,
},
})
_ = d.tPut("a", map[string]interface{}{"key": "a"})

return test{
db: d,
ddoc: "_design/foo",
view: "_view/bar",
want: []rowResult{
{ID: "a", Key: `"a"`, Value: "2"},
},
}
})

/*
TODO:
Expand Down Expand Up @@ -1716,9 +1744,6 @@ func TestDBQuery(t *testing.T) {
- treat map non-exception errors as exceptions
- make sure local docs are properly skipped
- make sure deleted docs are properly skipped
- view options: https://docs.couchdb.org/en/stable/api/ddoc/views.html#view-options
- local_seq
*/

tests.Run(t, func(t *testing.T, tt test) {
Expand Down

0 comments on commit 0264c97

Please sign in to comment.