forked from cloudant-labs/go-cloudant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
335 lines (280 loc) · 7.88 KB
/
database.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package cloudant
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
)
var bulkUploadBuffer = 1000 // buffer for bulk upload channel
// AllRow represents a row in the json array returned by all_docs
type AllRow struct {
ID string `json:"id"`
Value AllRowValue `json:"value"`
Doc interface{} `json:"doc"`
}
// AllRowValue represents a part returned by _all_docs
type AllRowValue struct {
Rev string `json:"rev"`
}
// Change represents a part returned by _changes
type Change struct {
ID string
Rev string
Seq string
Deleted bool
Doc map[string]interface{} // Only present if Changes() called with include_docs=true
}
// ChangeRow represents a part returned by _changes
type ChangeRow struct {
ID string `json:"id"`
Seq string `json:"seq"` // If using CouchDB1.6, this is a number
Changes []ChangeRowChanges `json:"changes"`
Deleted bool `json:"deleted"`
Doc map[string]interface{} `json:"doc"`
}
// UnmarshalJSON is here for coping with CouchDB1.6's sequence IDs being
// numbers, not strings as in Cloudant and CouchDB2.X.
//
// See https://play.golang.org/p/BytXCeHMvt
func (c *ChangeRow) UnmarshalJSON(data []byte) error {
// Create a new type with same structure as ChangeRow but without its method set
// to avoid an infinite `UnmarshalJSON` call stack
type ChangeRow16 ChangeRow
changeRow := struct {
ChangeRow16
Seq json.Number `json:"seq"`
}{ChangeRow16: ChangeRow16(*c)}
if err := json.Unmarshal(data, &changeRow); err != nil {
return err
}
*c = ChangeRow(changeRow.ChangeRow16)
c.Seq = changeRow.Seq.String()
return nil
}
// ChangeRowChanges represents a part returned by _changes
type ChangeRowChanges struct {
Rev string `json:"rev"`
}
// Database holds a reference to an authenticated client connection and the
// name of a remote database
type Database struct {
client *CouchClient
Name string
URL *url.URL
}
// DocumentMeta is a CouchDB id/rev pair
type DocumentMeta struct {
ID string `json:"id"`
Rev string `json:"rev"`
}
// Info represents the account meta-data
type Info struct {
IsCompactRunning bool `json:"compact_running"`
DataSize int `json:"data_size"`
DocDelCount int `json:"doc_del_count"`
DocCount int `json:"doc_count"`
DiskSize int `json:"disk_size"`
UpdateSeq string `json:"update_seq"`
}
// All returns a channel in which AllRow types can be received.
func (d *Database) All(args *allDocsQuery) (<-chan *AllRow, error) {
verb := "GET"
var body []byte
var err error
if len(args.Keys) > 0 {
// If we're given a "Keys" argument, we're better off with a POST
body, err = json.Marshal(map[string][]string{"keys": args.Keys})
if err != nil {
return nil, err
}
verb = "POST"
args.Keys = nil
}
params, err := args.GetQuery()
if err != nil {
return nil, err
}
urlStr, err := Endpoint(*d.URL, "/_all_docs", params)
if err != nil {
return nil, err
}
job, err := d.client.request(verb, urlStr, bytes.NewReader(body))
if err != nil {
if job != nil {
job.done() // close the body reader to avoid leakage
}
return nil, err
}
err = expectedReturnCodes(job, 200)
if err != nil {
job.done() // close the body reader to avoid leakage
return nil, err
}
results := make(chan *AllRow, 1000)
go func(job *Job, results chan<- *AllRow) {
defer job.Close()
reader := bufio.NewReader(job.response.Body)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
close(results)
return
}
lineStr := string(line)
lineStr = strings.TrimSpace(lineStr) // remove whitespace
lineStr = strings.TrimRight(lineStr, ",") // remove trailing comma
if len(lineStr) > 7 && lineStr[0:7] == "{\"id\":\"" {
var result = new(AllRow)
err := json.Unmarshal([]byte(lineStr), result)
if err == nil {
results <- result
}
}
}
}(job, results)
return results, nil
}
// Bulk returns a new bulk document uploader.
func (d *Database) Bulk(batchSize int, batchMaxBytes int, flushSecs int) *Uploader {
return newUploader(d, batchSize, batchMaxBytes, bulkUploadBuffer, flushSecs)
}
// Changes returns a channel in which Change types can be received.
// See: https://console.bluemix.net/docs/services/Cloudant/api/database.html#get-changes
func (d *Database) Changes(args *changesQuery) (<-chan *Change, error) {
verb := "GET"
var body []byte
var err error
if len(args.DocIDs) > 0 {
// If we're given a "doc_ids" argument, we're better off with a POST
body, err = json.Marshal(map[string][]string{"doc_ids": args.DocIDs})
if err != nil {
return nil, err
}
verb = "POST"
args.DocIDs = nil
}
params, err := args.GetQuery()
if err != nil {
return nil, err
}
urlStr, err := Endpoint(*d.URL, "/_changes", params)
if err != nil {
return nil, err
}
job, err := d.client.request(verb, urlStr, bytes.NewReader(body))
if err != nil {
job.done()
return nil, err
}
err = expectedReturnCodes(job, 200)
if err != nil {
job.done()
return nil, err
}
changes := make(chan *Change, 1000)
go func(job *Job, changes chan<- *Change) {
defer job.Close()
defer close(changes)
reader := bufio.NewReader(job.response.Body)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
return
}
lineStr := string(line)
lineStr = strings.TrimSpace(lineStr) // remove whitespace
lineStr = strings.TrimRight(lineStr, ",") // remove trailing comma
if len(lineStr) > 7 && lineStr[0:7] == "{\"seq\":" {
var change = new(ChangeRow)
err := json.Unmarshal([]byte(lineStr), change)
if err == nil && len(change.Changes) == 1 {
changes <- &Change{
ID: change.ID,
Rev: change.Changes[0].Rev,
Seq: change.Seq,
Doc: change.Doc,
Deleted: change.Deleted,
}
} else {
fmt.Println(err)
}
}
}
}(job, changes)
return changes, nil
}
// Info returns database information.
// See https://console.bluemix.net/docs/services/Cloudant/api/database.html#getting-database-details
func (d *Database) Info() (*Info, error) {
job, err := d.client.request("GET", d.URL.String(), nil)
defer job.Close()
if err != nil {
return nil, err
}
err = expectedReturnCodes(job, 200)
if err != nil {
return nil, err
}
info := &Info{}
err = json.NewDecoder(job.response.Body).Decode(info)
return info, err
}
// Get a document from the database.
// See: https://console.bluemix.net/docs/services/Cloudant/api/document.html#read
func (d *Database) Get(documentID string, args *getQuery, target interface{}) error {
params, err := args.GetQuery()
if err != nil {
return err
}
urlStr, err := Endpoint(*d.URL, documentID, params)
if err != nil {
return err
}
job, err := d.client.request("GET", urlStr, nil)
defer job.Close()
if err != nil {
return err
}
err = expectedReturnCodes(job, 200)
if err != nil {
return err
}
return json.NewDecoder(job.response.Body).Decode(target)
}
// Delete a document with a specified revision.
func (d *Database) Delete(documentID, rev string) error {
query := url.Values{}
query.Add("rev", rev)
urlStr, err := Endpoint(*d.URL, documentID, query)
if err != nil {
return err
}
job, err := d.client.request("DELETE", urlStr, nil)
defer job.Close()
if err != nil {
return err
}
return expectedReturnCodes(job, 200)
}
// Set a document. The specified type may have a json attributes '_id' and '_rev'.
// If no '_id' is given the database will generate one for you.
func (d *Database) Set(document interface{}) (*DocumentMeta, error) {
jsonDocument, err := json.Marshal(document)
if err != nil {
return nil, err
}
job, err := d.client.request("POST", d.URL.String(), bytes.NewReader(jsonDocument))
defer job.Close()
if err != nil {
return nil, err
}
err = expectedReturnCodes(job, 201, 202)
if err != nil {
return nil, err
}
resp := &DocumentMeta{}
err = json.NewDecoder(job.response.Body).Decode(resp)
return resp, err
}