forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
325 lines (294 loc) · 8.62 KB
/
db.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
package couchdb
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/flimzy/kivik"
"github.com/flimzy/kivik/driver"
"github.com/flimzy/kivik/errors"
"github.com/go-kivik/couchdb/chttp"
)
type db struct {
*client
dbName string
fullCommit bool
}
func (d *db) path(path string, query url.Values) string {
url, _ := url.Parse(d.dbName + "/" + strings.TrimPrefix(path, "/"))
if query != nil {
url.RawQuery = query.Encode()
}
return url.String()
}
func optionsToParams(opts ...map[string]interface{}) (url.Values, error) {
params := url.Values{}
for _, optsSet := range opts {
for key, i := range optsSet {
var values []string
switch v := i.(type) {
case string:
values = []string{v}
case []string:
values = v
case bool:
values = []string{fmt.Sprintf("%t", v)}
case int, uint, uint8, uint16, uint32, uint64, int8, int16, int32, int64:
values = []string{fmt.Sprintf("%d", v)}
default:
return nil, errors.Statusf(kivik.StatusBadRequest, "kivik: invalid type %T for options", i)
}
for _, value := range values {
params.Add(key, value)
}
}
}
return params, nil
}
// rowsQuery performs a query that returns a rows iterator.
func (d *db) rowsQuery(ctx context.Context, path string, opts map[string]interface{}) (driver.Rows, error) {
options, err := optionsToParams(opts)
if err != nil {
return nil, err
}
resp, err := d.Client.DoReq(ctx, kivik.MethodGet, d.path(path, options), nil)
if err != nil {
return nil, err
}
if err = chttp.ResponseError(resp); err != nil {
return nil, err
}
return newRows(resp.Body), nil
}
// AllDocs returns all of the documents in the database.
func (d *db) AllDocs(ctx context.Context, opts map[string]interface{}) (driver.Rows, error) {
return d.rowsQuery(ctx, "_all_docs", opts)
}
// Query queries a view.
func (d *db) Query(ctx context.Context, ddoc, view string, opts map[string]interface{}) (driver.Rows, error) {
return d.rowsQuery(ctx, fmt.Sprintf("_design/%s/_view/%s", chttp.EncodeDocID(ddoc), chttp.EncodeDocID(view)), opts)
}
// Get fetches the requested document.
func (d *db) Get(ctx context.Context, docID string, options map[string]interface{}) (json.RawMessage, error) {
if docID == "" {
return nil, missingArg("docID")
}
inm, err := ifNoneMatch(options)
if err != nil {
return nil, err
}
params, err := optionsToParams(options)
if err != nil {
return nil, err
}
opts := &chttp.Options{
Accept: "application/json; multipart/mixed",
IfNoneMatch: inm,
}
resp, err := d.Client.DoReq(ctx, http.MethodGet, d.path(chttp.EncodeDocID(docID), params), opts)
if err != nil {
return nil, err
}
if respErr := chttp.ResponseError(resp); respErr != nil {
return nil, respErr
}
defer func() { _ = resp.Body.Close() }()
doc := &bytes.Buffer{}
if _, err := doc.ReadFrom(resp.Body); err != nil {
return nil, errors.WrapStatus(kivik.StatusUnknownError, err)
}
return doc.Bytes(), nil
}
func (d *db) CreateDoc(ctx context.Context, doc interface{}) (docID, rev string, err error) {
return d.CreateDocOpts(ctx, doc, nil)
}
func (d *db) CreateDocOpts(ctx context.Context, doc interface{}, options map[string]interface{}) (docID, rev string, err error) {
result := struct {
ID string `json:"id"`
Rev string `json:"rev"`
}{}
fullCommit, err := fullCommit(d.fullCommit, options)
if err != nil {
return "", "", err
}
path := d.dbName
if len(options) > 0 {
params, e := optionsToParams(options)
if e != nil {
return "", "", e
}
path += "?" + params.Encode()
}
opts := &chttp.Options{
Body: chttp.EncodeBody(doc),
FullCommit: fullCommit,
}
_, err = d.Client.DoJSON(ctx, kivik.MethodPost, path, opts, &result)
return result.ID, result.Rev, err
}
func (d *db) Put(ctx context.Context, docID string, doc interface{}) (rev string, err error) {
return d.PutOpts(ctx, docID, doc, nil)
}
func (d *db) PutOpts(ctx context.Context, docID string, doc interface{}, options map[string]interface{}) (rev string, err error) {
if docID == "" {
return "", missingArg("docID")
}
fullCommit, err := fullCommit(d.fullCommit, options)
if err != nil {
return "", err
}
opts := &chttp.Options{
Body: chttp.EncodeBody(doc),
FullCommit: fullCommit,
}
var result struct {
ID string `json:"id"`
Rev string `json:"rev"`
}
_, err = d.Client.DoJSON(ctx, kivik.MethodPut, d.path(chttp.EncodeDocID(docID), nil), opts, &result)
if err != nil {
return "", err
}
if result.ID != docID {
// This should never happen; this is mostly for debugging and internal use
return result.Rev, errors.Statusf(kivik.StatusBadResponse, "modified document ID (%s) does not match that requested (%s)", result.ID, docID)
}
return result.Rev, nil
}
func (d *db) Delete(ctx context.Context, docID, rev string) (string, error) {
return d.DeleteOpts(ctx, docID, rev, nil)
}
func (d *db) DeleteOpts(ctx context.Context, docID, rev string, options map[string]interface{}) (string, error) {
if docID == "" {
return "", missingArg("docID")
}
if rev == "" {
return "", missingArg("rev")
}
fullCommit, err := fullCommit(d.fullCommit, options)
if err != nil {
return "", err
}
query, err := optionsToParams(options)
if err != nil {
return "", err
}
query.Add("rev", rev)
opts := &chttp.Options{
FullCommit: fullCommit,
}
resp, err := d.Client.DoReq(ctx, kivik.MethodDelete, d.path(chttp.EncodeDocID(docID), query), opts)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
return chttp.GetRev(resp)
}
func (d *db) Flush(ctx context.Context) error {
_, err := d.Client.DoError(ctx, kivik.MethodPost, d.path("/_ensure_full_commit", nil), nil)
return err
}
func (d *db) Stats(ctx context.Context) (*driver.DBStats, error) {
result := struct {
driver.DBStats
Sizes struct {
File int64 `json:"file"`
External int64 `json:"external"`
Active int64 `json:"active"`
} `json:"sizes"`
UpdateSeq json.RawMessage `json:"update_seq"`
}{}
_, err := d.Client.DoJSON(ctx, kivik.MethodGet, d.dbName, nil, &result)
stats := result.DBStats
if result.Sizes.File > 0 {
stats.DiskSize = result.Sizes.File
}
if result.Sizes.External > 0 {
stats.ExternalSize = result.Sizes.External
}
if result.Sizes.Active > 0 {
stats.ActiveSize = result.Sizes.Active
}
stats.UpdateSeq = string(bytes.Trim(result.UpdateSeq, `"`))
return &stats, err
}
func (d *db) Compact(ctx context.Context) error {
res, err := d.Client.DoReq(ctx, kivik.MethodPost, d.path("/_compact", nil), nil)
if err != nil {
return err
}
return chttp.ResponseError(res)
}
func (d *db) CompactView(ctx context.Context, ddocID string) error {
if ddocID == "" {
return missingArg("ddocID")
}
res, err := d.Client.DoReq(ctx, kivik.MethodPost, d.path("/_compact/"+ddocID, nil), nil)
if err != nil {
return err
}
return chttp.ResponseError(res)
}
func (d *db) ViewCleanup(ctx context.Context) error {
res, err := d.Client.DoReq(ctx, kivik.MethodPost, d.path("/_view_cleanup", nil), nil)
if err != nil {
return err
}
return chttp.ResponseError(res)
}
func (d *db) Security(ctx context.Context) (*driver.Security, error) {
var sec *driver.Security
_, err := d.Client.DoJSON(ctx, kivik.MethodGet, d.path("/_security", nil), nil, &sec)
return sec, err
}
func (d *db) SetSecurity(ctx context.Context, security *driver.Security) error {
opts := &chttp.Options{
Body: chttp.EncodeBody(security),
}
res, err := d.Client.DoReq(ctx, kivik.MethodPut, d.path("/_security", nil), opts)
if err != nil {
return err
}
defer func() { _ = res.Body.Close() }()
return chttp.ResponseError(res)
}
// Rev returns the most current rev of the requested document.
func (d *db) Rev(ctx context.Context, docID string) (rev string, err error) {
if docID == "" {
return "", missingArg("docID")
}
res, err := d.Client.DoError(ctx, http.MethodHead, d.path(chttp.EncodeDocID(docID), nil), nil)
if err != nil {
return "", err
}
return chttp.GetRev(res)
}
func (d *db) Copy(ctx context.Context, targetID, sourceID string, options map[string]interface{}) (targetRev string, err error) {
if sourceID == "" {
return "", errors.Status(kivik.StatusBadRequest, "kivik: sourceID required")
}
if targetID == "" {
return "", errors.Status(kivik.StatusBadRequest, "kivik: targetID required")
}
fullCommit, err := fullCommit(d.fullCommit, options)
if err != nil {
return "", err
}
params, err := optionsToParams(options)
if err != nil {
return "", err
}
opts := &chttp.Options{
FullCommit: fullCommit,
Destination: targetID,
}
resp, err := d.Client.DoReq(ctx, kivik.MethodCopy, d.path(chttp.EncodeDocID(sourceID), params), opts)
if err != nil {
return "", err
}
defer resp.Body.Close() // nolint: errcheck
return chttp.GetRev(resp)
}