forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changes.go
61 lines (52 loc) · 1.25 KB
/
changes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package couchdb
import (
"context"
"encoding/json"
"io"
"github.com/flimzy/kivik"
"github.com/flimzy/kivik/driver"
"github.com/flimzy/kivik/errors"
"github.com/go-kivik/couchdb/chttp"
)
// Changes returns the changes stream for the database.
func (d *db) Changes(ctx context.Context, opts map[string]interface{}) (driver.Changes, error) {
overrideOpts := map[string]interface{}{
"feed": "continuous",
"since": "now",
"heartbeat": 6000,
}
options, err := optionsToParams(opts, overrideOpts)
if err != nil {
return nil, err
}
resp, err := d.Client.DoReq(ctx, kivik.MethodGet, d.path("_changes", options), nil)
if err != nil {
return nil, err
}
if err = chttp.ResponseError(resp); err != nil {
return nil, err
}
return newChangesRows(resp.Body), nil
}
type changesRows struct {
body io.ReadCloser
dec *json.Decoder
}
func newChangesRows(r io.ReadCloser) *changesRows {
return &changesRows{
body: r,
}
}
var _ driver.Changes = &changesRows{}
func (r *changesRows) Close() error {
return r.body.Close()
}
func (r *changesRows) Next(row *driver.Change) error {
if r.dec == nil {
r.dec = json.NewDecoder(r.body)
}
if !r.dec.More() {
return io.EOF
}
return errors.WrapStatus(kivik.StatusBadResponse, r.dec.Decode(row))
}