-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
227 lines (191 loc) · 6.32 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
package sofa
import (
"bytes"
"encoding/json"
"net/url"
)
// DatabaseMetadata contains information about the database in CouchDB.
type DatabaseMetadata struct {
CompactRunning bool `json:"compact_running"`
Name string `json:"db_name"`
DocCount int `json:"doc_count"`
DelCount int `json:"doc_del_count"`
InstanceStartTime string `json:"instance_start_time"`
DataSize int `json:"data_size"`
DiskSize int `json:"disk_size"`
DiskFormatVersion int `json:"disk_format_version"`
PurgeSeq AlwaysString `json:"purge_seq"`
UpdateSeq AlwaysString `json:"update_seq"`
CommittedUpdateSeq AlwaysString `json:"committed_update_seq"`
}
// Database represents a CouchDB database & provides methods to access documents in the database.
type Database struct {
name string
metadata *DatabaseMetadata
con *Connection
}
// Get retrieves a single document from the database and unmarshals it into the
// provided interface.
func (d *Database) Get(document Document, id, rev string) (string, error) {
path := d.DocumentPath(id)
var opts = NewURLOptions()
if rev != "" {
if err := opts.Add("rev", rev); err != nil {
return "", err
}
}
resp, err := d.con.unmarshalRequest("GET", path, opts, nil, document)
if err != nil {
return "", err
}
return responseEtag(resp)
}
// Put marshals the provided document into JSON and the sends it to the CouchDB server
// with a PUT request. This allows modification of the document on the server.
func (d *Database) Put(document Document) (string, error) {
docMeta := document.Metadata()
path := d.DocumentPath(docMeta.ID)
var opts = NewURLOptions()
if docMeta.Rev != "" {
if err := opts.Add("rev", docMeta.Rev); err != nil {
return "", err
}
}
b, err := json.Marshal(document)
if err != nil {
return "", err
}
buf := bytes.NewBuffer(b)
res := ServerResponse{}
resp, err := d.con.unmarshalRequest("PUT", path, opts, buf, &res)
if err != nil {
return "", err
}
return responseEtag(resp)
}
// Delete removed a document from the Database.
func (d *Database) Delete(document Document) (string, error) {
docMeta := document.Metadata()
path := d.DocumentPath(docMeta.ID)
var opts = NewURLOptions()
if docMeta.Rev != "" {
if err := opts.Add("rev", docMeta.Rev); err != nil {
return "", err
}
}
res := ServerResponse{}
resp, err := d.con.unmarshalRequest("DELETE", path, opts, nil, &res)
if err != nil {
return "", err
}
return responseEtag(resp)
}
// Name returns the name of the Database.
func (d *Database) Name() string {
return d.name
}
// Path returns the path for this database. The value (as with the other *Path functions)
// is in the correct for to be passed to Connection.URL()
func (d *Database) Path() string {
return "/" + d.name
}
// ViewPath returns the path to a view in this database
func (d *Database) ViewPath(view string) string {
return urlConcat(d.Path(), view)
}
// DocumentPath returns the path to a document in this database
func (d *Database) DocumentPath(id string) string {
return urlConcat(d.Path(), id)
}
// ViewCleanup cleans old data from the database. From the CouchDB API documentation:
// "Old view output remains on disk until you explicitly run cleanup."
func (d *Database) ViewCleanup() error {
path := urlConcat(d.Path(), "_view_cleanup")
_, err := d.con.Post(path, NewURLOptions(), nil)
return err
}
// CompactView compacts the stored data for a view, meaning that the view uses less space on
// the disk.
func (d *Database) CompactView(name string) error {
path := urlConcat(urlConcat(d.Path(), "_compact"), name)
_, err := d.con.Post(path, NewURLOptions(), nil)
return err
}
// Metadata downloads the Metadata for the Database and saves it to the Database object. If
// there is already metadata stored then that will be returned without contacting the server.
func (d *Database) Metadata() (DatabaseMetadata, error) {
if d.metadata != nil {
return *d.metadata, nil
}
var metadata DatabaseMetadata
if _, err := d.con.unmarshalRequest("GET", d.Path(), NewURLOptions(), nil, &metadata); err != nil {
return DatabaseMetadata{}, err
}
d.metadata = &metadata
return metadata, nil
}
// AllDocuments gets all documents from a database. All document content is included for each row.
func (d *Database) AllDocuments() (DocumentList, error) {
resp, err := d.con.Get(d.ViewPath("_all_docs"), URLOptions{url.Values{"include_docs": []string{"true"}}})
if err != nil {
return DocumentList{}, err
}
var docs DocumentList
err = json.NewDecoder(resp.Body).Decode(&docs)
if err != nil {
return DocumentList{}, err
}
return docs, nil
}
// ListDocuments gets all rows from the Database but does not include the content of the documents.
func (d *Database) ListDocuments() (DocumentList, error) {
resp, err := d.con.Get(d.ViewPath("_all_docs"), NewURLOptions())
if err != nil {
return DocumentList{}, err
}
var docs DocumentList
err = json.NewDecoder(resp.Body).Decode(&docs)
if err != nil {
return DocumentList{}, err
}
return docs, nil
}
// Documents gets a set of Documents from a database. All of the IDs requested will be downloaded &
// all document content is included for each row.
func (d *Database) Documents(ids ...string) (DocumentList, error) {
body := map[string]interface{}{"keys": ids}
bodyBytes, err := json.Marshal(&body)
if err != nil {
return DocumentList{}, err
}
bodyBuf := bytes.NewBuffer(bodyBytes)
resp, err := d.con.Post(d.ViewPath("_all_docs"), URLOptions{url.Values{"include_docs": []string{"true"}}}, bodyBuf)
if err != nil {
return DocumentList{}, err
}
var docs DocumentList
err = json.NewDecoder(resp.Body).Decode(&docs)
if err != nil {
return DocumentList{}, err
}
return docs, nil
}
// PollingChangesFeed gets a changes feed which will poll the server for changes to documents.
func (d *Database) PollingChangesFeed(long bool) PollingChangesFeed {
var t = "normal"
if long {
t = "longpoll"
}
return PollingChangesFeed{
db: d,
feedType: t,
}
}
// ContinuousChangesFeed gets a changes feed with a continuous connection to the database. New
// changes are then pushed over the existing connection as they arrive.
func (d *Database) ContinuousChangesFeed(params ChangesFeedParams) ContinuousChangesFeed {
return ContinuousChangesFeed{
db: d,
params: params,
}
}