-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdatabase.go
380 lines (349 loc) · 10.5 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package couchdb
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"github.com/google/go-querystring/query"
)
// DatabaseService is an interface for dealing with a single CouchDB database.
type DatabaseService interface {
AllDocs(params *QueryParameters) (*ViewResponse, error)
AllDesignDocs() ([]DesignDocument, error)
Head(id string) (*http.Response, error)
Get(doc CouchDoc, id string) error
Put(doc CouchDoc) (*DocumentResponse, error)
Post(doc CouchDoc) (*DocumentResponse, error)
Delete(doc CouchDoc) (*DocumentResponse, error)
PutAttachment(doc CouchDoc, path string) (*DocumentResponse, error)
Bulk(docs []CouchDoc) ([]DocumentResponse, error)
Purge(req map[string][]string) (*PurgeResponse, error)
GetSecurity() (*SecurityDocument, error)
PutSecurity(secDoc SecurityDocument) (*DatabaseResponse, error)
View(name string) ViewService
Seed([]DesignDocument) error
}
// Database performs actions on certain database
type Database struct {
Client *Client
Name string
}
// AllDesignDocs returns all design documents from database.
// http://stackoverflow.com/questions/2814352/get-all-design-documents-in-couchdb
func (db *Database) AllDesignDocs() ([]DesignDocument, error) {
startKey := fmt.Sprintf("%q", "_design/")
endKey := fmt.Sprintf("%q", "_design0")
includeDocs := true
q := QueryParameters{
StartKey: &startKey,
EndKey: &endKey,
IncludeDocs: &includeDocs,
}
res, err := db.AllDocs(&q)
if err != nil {
return nil, err
}
docs := make([]interface{}, len(res.Rows))
for index, row := range res.Rows {
docs[index] = row.Doc
}
designDocs := make([]DesignDocument, len(docs))
b, err := json.Marshal(docs)
if err != nil {
return nil, err
}
return designDocs, json.Unmarshal(b, &designDocs)
}
// AllDocs returns all documents in selected database.
// http://docs.couchdb.org/en/latest/api/database/bulk-api.html
func (db *Database) AllDocs(params *QueryParameters) (*ViewResponse, error) {
q, err := query.Values(params)
if err != nil {
return nil, err
}
u := fmt.Sprintf("%s/_all_docs?%s", url.PathEscape(db.Name), q.Encode())
res, err := db.Client.Request(http.MethodGet, u, nil, "")
if err != nil {
return nil, err
}
defer res.Body.Close()
var response ViewResponse
return &response, json.NewDecoder(res.Body).Decode(&response)
}
// Head request.
func (db *Database) Head(id string) (*http.Response, error) {
u := fmt.Sprintf("%s/%s", url.PathEscape(db.Name), url.PathEscape(id))
body, err := db.Client.Request(http.MethodHead, u, nil, "")
if err != nil {
return nil, err
}
return body, nil
}
// Get document.
func (db *Database) Get(doc CouchDoc, id string) error {
u := fmt.Sprintf("%s/%s", url.PathEscape(db.Name), url.PathEscape(id))
res, err := db.Client.Request(http.MethodGet, u, nil, "application/json")
if err != nil {
return err
}
defer res.Body.Close()
return json.NewDecoder(res.Body).Decode(doc)
}
// Put document.
func (db *Database) Put(doc CouchDoc) (*DocumentResponse, error) {
u := fmt.Sprintf("%s/%s", url.PathEscape(db.Name), url.PathEscape(doc.GetID()))
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(doc); err != nil {
return nil, err
}
res, err := db.Client.Request(http.MethodPut, u, &b, "application/json")
if err != nil {
return nil, err
}
defer res.Body.Close()
var response DocumentResponse
return &response, json.NewDecoder(res.Body).Decode(&response)
}
// Post document.
func (db *Database) Post(doc CouchDoc) (*DocumentResponse, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(doc); err != nil {
return nil, err
}
res, err := db.Client.Request(http.MethodPost, url.PathEscape(db.Name), &b, "application/json")
if err != nil {
return nil, err
}
defer res.Body.Close()
var response DocumentResponse
return &response, json.NewDecoder(res.Body).Decode(&response)
}
// Delete document.
func (db *Database) Delete(doc CouchDoc) (*DocumentResponse, error) {
u := fmt.Sprintf("%s/%s?rev=%s", url.PathEscape(db.Name), url.PathEscape(doc.GetID()), doc.GetRev())
res, err := db.Client.Request(http.MethodDelete, u, nil, "application/json")
if err != nil {
return nil, err
}
defer res.Body.Close()
var response DocumentResponse
return &response, json.NewDecoder(res.Body).Decode(&response)
}
// PutAttachment adds attachment to document
func (db *Database) PutAttachment(doc CouchDoc, path string) (*DocumentResponse, error) {
// target url
u := fmt.Sprintf("%s/%s", url.PathEscape(db.Name), url.PathEscape(doc.GetID()))
// get file from disk
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
// create new writer
buffer := bytes.Buffer{}
writer := multipart.NewWriter(&buffer)
// create first "application/json" document part
document := Document{
ID: doc.GetID(),
Rev: doc.GetRev(),
}
err = writeJSON(&document, writer, file)
if err != nil {
return nil, err
}
// write actual file content to multipart message
err = writeMultipart(writer, file)
if err != nil {
return nil, err
}
// finish multipart message and write trailing boundary
err = writer.Close()
if err != nil {
return nil, err
}
// create http request
contentType := fmt.Sprintf("multipart/related; boundary=%q", writer.Boundary())
res, err := db.Client.Request(http.MethodPut, u, &buffer, contentType)
if err != nil {
return nil, err
}
defer res.Body.Close()
var response DocumentResponse
return &response, json.NewDecoder(res.Body).Decode(&response)
}
// Bulk allows to create and update multiple documents
// at the same time within a single request. The basic operation is similar to
// creating or updating a single document, except that you batch
// the document structure and information.
func (db *Database) Bulk(docs []CouchDoc) ([]DocumentResponse, error) {
bulk := BulkDoc{
Docs: docs,
}
u := fmt.Sprintf("%s/_bulk_docs", url.PathEscape(db.Name))
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(bulk); err != nil {
return nil, err
}
res, err := db.Client.Request(http.MethodPost, u, &b, "application/json")
if err != nil {
return nil, err
}
defer res.Body.Close()
response := []DocumentResponse{}
return response, json.NewDecoder(res.Body).Decode(&response)
}
// View returns view for given name.
func (db *Database) View(name string) ViewService {
u := fmt.Sprintf("%s/_design/%s/", url.PathEscape(db.Name), url.PathEscape(name))
return &View{
URL: u,
Client: db.Client,
}
}
// PurgeResponse is response from POST request to the _purge URL.
type PurgeResponse struct {
PurgeSeq float64 `json:"purge_seq"`
Purged map[string][]string
}
// Purge permanently removes the references to deleted documents from the database.
//
// http://docs.couchdb.org/en/1.6.1/api/database/misc.html
func (db *Database) Purge(req map[string][]string) (*PurgeResponse, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(req); err != nil {
return nil, err
}
res, err := db.Client.Request(http.MethodPost, url.PathEscape(db.Name)+"/_purge", &b, "application/json")
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &PurgeResponse{}
return response, json.NewDecoder(res.Body).Decode(&response)
}
// Element is single element inside Admins/Members in security document.
type Element struct {
Names []string `json:"names"`
Roles []string `json:"roles"`
}
// SecurityDocument describes document _security document.
type SecurityDocument struct {
Admins Element `json:"admins"`
Members Element `json:"members"`
}
// GetSecurity returns security document.
// http://docs.couchdb.org/en/latest/api/database/security.html
func (db *Database) GetSecurity() (*SecurityDocument, error) {
u := fmt.Sprintf("%s/_security", url.PathEscape(db.Name))
res, err := db.Client.Request(http.MethodGet, u, nil, "application/json")
if err != nil {
return nil, err
}
defer res.Body.Close()
var secDoc SecurityDocument
return &secDoc, json.NewDecoder(res.Body).Decode(&secDoc)
}
// PutSecurity sets the security object for the given database.
// http://docs.couchdb.org/en/latest/api/database/security.html#put--db-_security
func (db *Database) PutSecurity(secDoc SecurityDocument) (*DatabaseResponse, error) {
u := fmt.Sprintf("%s/_security", url.PathEscape(db.Name))
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(secDoc); err != nil {
return nil, err
}
res, err := db.Client.Request(http.MethodPut, u, &b, "application/json")
if err != nil {
return nil, err
}
defer res.Body.Close()
r := new(DatabaseResponse)
return r, json.NewDecoder(res.Body).Decode(r)
}
// Seed makes sure all your design documents are up to date.
func (db *Database) Seed(cache []DesignDocument) error {
// query all docs to get all design documents
designDocs, err := db.AllDesignDocs()
if err != nil {
return err
}
difference := diff(cache, designDocs)
// remove all deletions
for _, doc := range difference.deletions {
if _, err := db.Delete(&doc); err != nil {
return err
}
}
// update all changes
for _, doc := range difference.changes {
// get design document first to get current revision
var old DesignDocument
if err := db.Get(&old, doc.ID); err != nil {
return err
}
// update document with new version
doc.Rev = old.Rev
if _, err := db.Put(&doc); err != nil {
return err
}
}
// add all additions
for _, doc := range difference.additions {
if _, err := db.Put(&doc); err != nil {
return err
}
}
return nil
}
type difference struct {
additions []DesignDocument
changes []DesignDocument
deletions []DesignDocument
}
func diff(cache, db []DesignDocument) difference {
di := difference{
additions: []DesignDocument{},
changes: []DesignDocument{},
deletions: []DesignDocument{},
}
// check for additions changes
// design document is in cache but not in db
for _, c := range cache {
exists := false
existsButDifferent := false
for _, d := range db {
if d.ID == c.ID {
exists = true
// check for different map/reduce and language
// do not check for different revision
if !reflect.DeepEqual(c.Views, d.Views) {
existsButDifferent = true
}
}
}
if !exists {
di.additions = append(di.additions, c)
} else if existsButDifferent {
di.changes = append(di.changes, c)
}
}
// check for deletions
// design document is in db but not in cache
for _, d := range db {
exists := false
for _, c := range cache {
if d.ID == c.ID {
exists = true
}
}
// do not delete internal design documents like _auth
if !exists && !strings.HasPrefix(d.Name(), "_") {
di.deletions = append(di.deletions, d)
}
}
return di
}