-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_changes.go
121 lines (103 loc) · 2.45 KB
/
db_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package handler
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/goydb/goydb/pkg/model"
)
type DBChanges struct {
Base
}
// FIXME: make config
const maxTimeout = time.Second * 60
func (s *DBChanges) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if _, ok := (Authenticator{Base: s.Base, RequiresAdmin: true}.Do(w, r)); !ok {
return
}
db := Database{Base: s.Base}.Do(w, r)
if db == nil {
return
}
query := r.URL.Query()
includeDocs := boolOption("include_docs", false, query)
options := model.ChangesOptions{
Since: strings.ReplaceAll(query.Get("since"), `"`, ""),
Limit: int(intOption("limit", 1000, query)),
Timeout: durationOption("timeout", time.Millisecond, maxTimeout, query),
}
changes, pending, err := db.Changes(r.Context(), &options)
if err != nil {
WriteError(w, http.StatusInternalServerError, err.Error())
return
}
if includeDocs {
err := db.EnrichDocuments(r.Context(), changes)
if err != nil {
WriteError(w, http.StatusInternalServerError, err.Error())
return
}
}
feed := query.Get("feed")
if feed == "" {
feed = "normal"
}
w.Header().Set("Content-Type", "application/json")
if feed == "normal" {
fmt.Fprintln(w, `{"results":[`)
} else if feed == "continuous" {
w.Header().Set("Transfer-Encoding", "chunked")
}
// print an empty line like couchdb
if options.Limit == 0 {
fmt.Fprintln(w, "")
}
var lastSeq uint64
for i, doc := range changes {
if lastSeq < doc.LocalSeq {
lastSeq = doc.LocalSeq
}
cd := &ChangeDoc{
Seq: strconv.FormatUint(doc.LocalSeq, 10),
ID: doc.ID,
Deleted: doc.Deleted,
Changes: []Revisions{
{Rev: doc.Rev},
},
}
if includeDocs {
cd.Doc = doc.Data
}
err := json.NewEncoder(w).Encode(cd)
if err != nil {
log.Println(err)
break
}
if feed == "normal" {
if i < len(changes)-1 {
fmt.Fprint(w, `,`)
}
}
}
if options.Limit == 0 && lastSeq == 0 {
lastSeq, _ = strconv.ParseUint(options.Since, 10, 64)
}
if feed == "normal" {
fmt.Fprintln(w, `],`)
fmt.Fprintf(w, `"last_seq":"%d","pending":"%d"}`, lastSeq, pending)
}
}
type ChangeDoc struct {
Seq string `json:"seq"`
ID string `json:"id"`
Changes []Revisions `json:"changes"`
Deleted bool `json:"deleted,omitempty"`
Doc map[string]interface{} `json:"doc,omitempty"`
}
type Revisions struct {
Rev string `json:"rev"`
}