-
Notifications
You must be signed in to change notification settings - Fork 1
/
couchbase.go
65 lines (52 loc) · 1.21 KB
/
couchbase.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
package main
import (
"gopkg.in/couchbase/gocb.v1"
)
type CouchbaseAdapter struct {
Cluster *gocb.Cluster
Bucket *gocb.Bucket
}
func NewCouchbaseAdapter(connStr, username, password, bucketName string) (*CouchbaseAdapter, error) {
cluster, err := gocb.Connect(connStr)
if err != nil {
return nil, err
}
err = cluster.Authenticate(gocb.PasswordAuthenticator{
Username: username,
Password: password,
})
if err != nil {
return nil, err
}
bucket, err := cluster.OpenBucket(bucketName, "")
if err != nil {
return nil, err
}
return &CouchbaseAdapter{
Cluster: cluster,
Bucket: bucket,
}, nil
}
func (ca *CouchbaseAdapter) Store(key string, sample Sample) error {
_, err := ca.Bucket.Upsert(key, sample, 0)
return err
}
func (ca *CouchbaseAdapter) Read(query string, params []interface{}) ([]Sample, error) {
q := gocb.NewAnalyticsQuery(query)
res, err := ca.Bucket.ExecuteAnalyticsQuery(q, params)
if err != nil {
return nil, err
}
var samples []Sample
var sample Sample
for res.Next(&sample) {
samples = append(samples, sample)
}
if err := res.Close(); err != nil {
return nil, err
}
return samples, nil
}
func (ca *CouchbaseAdapter) BucketName() string {
return ca.Bucket.Name()
}