/
db.go
409 lines (335 loc) · 8.61 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package gaia
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"github.com/elos/data"
"github.com/elos/data/builtin/mem"
"github.com/elos/data/builtin/mongo"
"github.com/elos/data/transfer"
"github.com/elos/gaia/routes"
"github.com/elos/models"
"golang.org/x/net/websocket"
)
type ChangeTransport struct {
ChangeKind data.ChangeKind `json:"change_kind"`
RecordKind data.Kind `json:"record_kind"`
Record map[string]interface{} `json:"record"`
}
func recordEndpoint(host string) string {
return host + routes.Record
}
func recordQueryEndpoint(host string) string {
return host + routes.RecordQuery
}
func recordChangesEndpoint(host string) string {
return host + routes.RecordChanges
}
// DB implements the data.DB interface, and communicates over HTTP
// with the gaia server to complete it's actions
type DB struct {
URL, Username, Password string
*http.Client
}
func (db *DB) recordURL(v url.Values) string {
return recordEndpoint(db.URL) + "?" + v.Encode()
}
func (db *DB) recordQueryURL(v url.Values) string {
return recordQueryEndpoint(db.URL) + "?" + v.Encode()
}
func (db *DB) recordChangesURL(v url.Values) string {
return strings.Replace(recordChangesEndpoint(db.URL), "http", "ws", 1) + "?" + v.Encode()
}
func (db *DB) get(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return db.do(req)
}
func (db *DB) deleteReq(url string) (*http.Response, error) {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
return db.do(req)
}
func (db *DB) postJSON(url string, v interface{}) (*http.Response, error) {
requestBody, err := json.Marshal(v)
if err != nil {
return nil, err
}
return db.post(url, bytes.NewBuffer(requestBody))
}
func (db *DB) post(url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
return db.do(req)
}
func (db *DB) do(req *http.Request) (*http.Response, error) {
req.SetBasicAuth(db.Username, db.Password)
return db.Client.Do(req)
}
func (db *DB) save(r data.Record) error {
// setup Params
url := db.recordURL(url.Values{
"kind": []string{r.Kind().String()},
"id": []string{r.ID().String()},
})
resp, err := db.postJSON(url, r)
if err != nil {
log.Printf("gaia.(*DB).save Error: while making request: %s", err)
return data.ErrNoConnection
}
switch resp.StatusCode {
case http.StatusBadRequest:
log.Print("gaia.(*DB).save Error: malformed request")
return data.ErrNoConnection
case http.StatusInternalServerError:
return data.ErrNoConnection
case http.StatusUnauthorized:
return data.ErrAccessDenial
case http.StatusCreated:
fallthrough
case http.StatusOK:
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("gaia.(*DB).save Error: reading response body: %s", err)
return data.ErrNoConnection
}
if err := json.Unmarshal(body, r); err != nil {
log.Printf("gaia.(*DB).save Error: unmarshalling JSON into record: %s", err)
return data.ErrNoConnection
}
default:
log.Printf("gaia.(*DB).save Error: unexpected status code: %d", resp.StatusCode)
return data.ErrNoConnection
}
return nil
}
func (db *DB) deleteRecord(r data.Record) error {
url := db.recordURL(url.Values{
"kind": []string{r.Kind().String()},
"id": []string{r.ID().String()},
})
resp, err := db.deleteReq(url)
if err != nil {
log.Printf("gaia.(*DB).save Error: while making request: %s", err)
return data.ErrNoConnection
}
switch resp.StatusCode {
case http.StatusBadRequest:
log.Print("gaia.(*DB).deleteRecord Error: malformed request")
return data.ErrNoConnection
case http.StatusInternalServerError:
return data.ErrNoConnection
case http.StatusUnauthorized:
return data.ErrAccessDenial
case http.StatusNotFound:
return data.ErrNotFound
case http.StatusNoContent:
// pass, there is nothing to do, the delete has succeeded
default:
log.Printf("gaia.(*DB).deleteRecord Error: unexpected status code: %d", resp.StatusCode)
return data.ErrNoConnection
}
return nil
}
func (db *DB) query(q *query) (data.Iterator, error) {
url := db.recordQueryURL(url.Values{
"kind": []string{q.kind.String()},
"skip": []string{fmt.Sprintf("%d", q.skip)},
"limit": []string{fmt.Sprintf("%d", q.limit)},
"batch": []string{fmt.Sprintf("%d", q.batch)},
"order": q.order,
})
resp, err := db.postJSON(url, q.attrs)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusBadRequest:
log.Print("gaia db bad request")
fallthrough
case http.StatusInternalServerError:
return nil, data.ErrNoConnection
case http.StatusUnauthorized:
return nil, data.ErrAccessDenial
case http.StatusOK:
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var resultsUntyped []*data.AttrMap
if err := json.Unmarshal(body, &resultsUntyped); err != nil {
return nil, err
}
var results []data.Record
for _, attrs := range resultsUntyped {
r := models.ModelFor(q.kind)
if err := transfer.TransferAttrs(attrs, r); err != nil {
return nil, err
}
results = append(results, r)
}
out := make(chan data.Record)
// cache results in another goroutine
go func() {
// read them off
for _, r := range results {
out <- r
}
close(out)
}()
return mem.Iter(out), nil
default:
log.Printf("Unexpected status code: %d", resp.StatusCode)
return nil, data.ErrNoConnection
}
return nil, nil
}
// --- Exported Interface {{{
func (db *DB) NewID() data.ID {
return data.ID(mongo.NewObjectID().Hex())
}
func (db *DB) ParseID(s string) (data.ID, error) {
oid, err := mongo.ParseObjectID(s)
return data.ID(oid.Hex()), err
}
func (db *DB) Save(r data.Record) error {
return db.save(r)
}
func (db *DB) Delete(r data.Record) error {
return db.deleteRecord(r)
}
func (db *DB) PopulateByID(r data.Record) error {
params := url.Values{}
params.Set("kind", r.Kind().String())
params.Set("id", r.ID().String())
url := db.recordURL(params)
resp, err := db.get(url)
if err != nil {
return err
}
switch resp.StatusCode {
case http.StatusBadRequest:
log.Print("gaia.(*DB).PopulateByID Bad Request Error")
fallthrough
case http.StatusInternalServerError:
log.Print("gaia.(*DB).PopulateByID Internal Server Error")
fallthrough
case http.StatusUnauthorized:
log.Print("gaia.(*DB).PopulateByID Unauthorized Server Error")
fallthrough
case -1: // catch
return data.ErrAccessDenial
case http.StatusNotFound:
return data.ErrNotFound
case http.StatusCreated:
case http.StatusOK:
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(body, r)
default:
log.Printf("Unexpected status code: %d", resp.StatusCode)
return data.ErrNoConnection
}
return nil
}
func (db *DB) PopulateByField(field string, value interface{}, r data.Record) error {
iter, err := db.query(&query{kind: r.Kind(), attrs: data.AttrMap{
field: value,
}})
if err != nil {
return err
}
if !iter.Next(r) {
return data.ErrNotFound
}
return iter.Close()
}
func (db *DB) Query(k data.Kind) data.Query {
return &query{
kind: k,
attrs: make(data.AttrMap),
db: db,
}
}
type query struct {
kind data.Kind
db *DB
attrs data.AttrMap
skip, limit, batch int
order []string
}
func (q *query) Execute() (data.Iterator, error) {
return q.db.query(q)
}
func (q *query) Skip(i int) data.Query {
q.skip = i
return q
}
func (q *query) Limit(i int) data.Query {
q.limit = i
return q
}
func (q *query) Batch(i int) data.Query {
q.batch = i
return q
}
func (q *query) Order(fields ...string) data.Query {
q.order = fields
return q
}
func (q *query) Select(attrs data.AttrMap) data.Query {
for k, v := range attrs {
q.attrs[k] = v
}
return q
}
func (db *DB) Changes() *chan *data.Change {
ch := make(chan *data.Change)
go func() {
// setup Params
wsURL := db.recordChangesURL(url.Values{
"public": []string{db.Username},
"private": []string{db.Password},
})
ws, err := websocket.Dial(wsURL, "", db.URL)
if err != nil {
log.Print("FAILED TO CONNECT TO GAIA")
close(ch)
return
}
defer ws.Close()
var change transfer.ChangeTransport
for {
if err := websocket.JSON.Receive(ws, &change); err != nil {
if err == io.EOF {
close(ch)
return
}
close(ch)
return
}
m := models.ModelFor(change.RecordKind)
transfer.TransferAttrs(change.Record, m)
ch <- data.NewChange(change.ChangeKind, m)
}
}()
return &ch
}
// --- }}}