forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
146 lines (127 loc) · 3.37 KB
/
bucket.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
package gocb
import (
"github.com/couchbase/gocb/gocbcore"
"math/rand"
"time"
)
// An interface representing a single bucket within a cluster.
type Bucket struct {
cluster *Cluster
name string
password string
client *gocbcore.Agent
transcoder Transcoder
opTimeout time.Duration
duraTimeout time.Duration
duraPollTimeout time.Duration
viewTimeout time.Duration
n1qlTimeout time.Duration
internal *bucketInternal
}
func createBucket(cluster *Cluster, config *gocbcore.AgentConfig) (*Bucket, error) {
cli, err := gocbcore.CreateAgent(config)
if err != nil {
return nil, err
}
bucket := &Bucket{
cluster: cluster,
name: config.BucketName,
password: config.Password,
client: cli,
transcoder: &DefaultTranscoder{},
opTimeout: 2500 * time.Millisecond,
duraTimeout: 40000 * time.Millisecond,
duraPollTimeout: 100 * time.Millisecond,
viewTimeout: 75 * time.Second,
n1qlTimeout: 75 * time.Second,
}
bucket.internal = &bucketInternal{
b: bucket,
}
return bucket, nil
}
func (b *Bucket) OperationTimeout() time.Duration {
return b.opTimeout
}
func (b *Bucket) SetOperationTimeout(timeout time.Duration) {
b.opTimeout = timeout
}
func (b *Bucket) DurabilityTimeout() time.Duration {
return b.duraTimeout
}
func (b *Bucket) SetDurabilityTimeout(timeout time.Duration) {
b.duraTimeout = timeout
}
func (b *Bucket) DurabilityPollTimeout() time.Duration {
return b.duraPollTimeout
}
func (b *Bucket) SetDurabilityPollTimeout(timeout time.Duration) {
b.duraPollTimeout = timeout
}
func (b *Bucket) ViewTimeout() time.Duration {
return b.viewTimeout
}
func (b *Bucket) SetViewTimeout(timeout time.Duration) {
b.viewTimeout = timeout
}
func (b *Bucket) N1qlTimeout() time.Duration {
return b.n1qlTimeout
}
func (b *Bucket) SetN1qlTimeout(timeout time.Duration) {
b.n1qlTimeout = timeout
}
func (b *Bucket) SetTranscoder(transcoder Transcoder) {
b.transcoder = transcoder
}
func (b *Bucket) InvalidateQueryCache() {
b.cluster.InvalidateQueryCache()
}
type Cas gocbcore.Cas
type pendingOp gocbcore.PendingOp
// Returns a CAPI endpoint. Guarenteed to return something for now...
func (b *Bucket) getViewEp() (string, error) {
capiEps := b.client.CapiEps()
if len(capiEps) == 0 {
return "", &clientError{"No available view nodes."}
}
return capiEps[rand.Intn(len(capiEps))], nil
}
func (b *Bucket) getMgmtEp() (string, error) {
mgmtEps := b.client.MgmtEps()
if len(mgmtEps) == 0 {
return "", &clientError{"No available management nodes."}
}
return mgmtEps[rand.Intn(len(mgmtEps))], nil
}
func (b *Bucket) getN1qlEp() (string, error) {
n1qlEps := b.client.N1qlEps()
if len(n1qlEps) == 0 {
return "", &clientError{"No available N1QL nodes."}
}
return n1qlEps[rand.Intn(len(n1qlEps))], nil
}
func (b *Bucket) Close() {
b.cluster.closeBucket(b)
b.client.Close()
}
func (b *Bucket) IoRouter() *gocbcore.Agent {
return b.client
}
// *INTERNAL*
// Internal methods, not safe to be consumed by third parties.
func (b *Bucket) Internal() *bucketInternal {
return b.internal
}
func (b *Bucket) Manager(username, password string) *BucketManager {
userPass := userPassPair{username, password}
if username == "" || password == "" {
if b.cluster.auth != nil {
userPass = b.cluster.auth.bucketMgmt(b.name)
}
}
return &BucketManager{
bucket: b,
username: userPass.Username,
password: userPass.Password,
}
}