forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
216 lines (181 loc) · 6.13 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
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
package gocb
import (
"math/rand"
"time"
"github.com/opentracing/opentracing-go"
"gopkg.in/couchbase/gocbcore.v7"
)
// Bucket is an interface representing a single bucket within a cluster.
type Bucket struct {
cluster *Cluster
name string
password string
client *gocbcore.Agent
mtEnabled bool
tracer opentracing.Tracer
transcoder Transcoder
opTimeout time.Duration
bulkOpTimeout time.Duration
duraTimeout time.Duration
duraPollTimeout time.Duration
viewTimeout time.Duration
n1qlTimeout time.Duration
ftsTimeout time.Duration
internal *BucketInternal
}
func (b *Bucket) startKvOpTrace(operationName string) opentracing.Span {
return b.tracer.StartSpan(operationName,
opentracing.Tag{Key: "couchbase.bucket", Value: b.name},
opentracing.Tag{Key: "couchbase.service", Value: "kv"})
}
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,
mtEnabled: config.UseMutationTokens,
transcoder: &DefaultTranscoder{},
tracer: config.Tracer,
opTimeout: 2500 * time.Millisecond,
bulkOpTimeout: 10000 * time.Millisecond,
duraTimeout: 40000 * time.Millisecond,
duraPollTimeout: 100 * time.Millisecond,
viewTimeout: 75 * time.Second,
n1qlTimeout: 75 * time.Second,
ftsTimeout: 75 * time.Second,
}
bucket.internal = &BucketInternal{
b: bucket,
}
return bucket, nil
}
// Name returns the name of the bucket we are connected to.
func (b *Bucket) Name() string {
return b.name
}
// UUID returns the uuid of the bucket we are connected to.
func (b *Bucket) UUID() string {
return b.client.BucketUUID()
}
// OperationTimeout returns the maximum amount of time to wait for an operation to succeed.
func (b *Bucket) OperationTimeout() time.Duration {
return b.opTimeout
}
// SetOperationTimeout sets the maximum amount of time to wait for an operation to succeed.
func (b *Bucket) SetOperationTimeout(timeout time.Duration) {
b.opTimeout = timeout
}
// BulkOperationTimeout returns the maximum amount of time to wait for a bulk op to succeed.
func (b *Bucket) BulkOperationTimeout() time.Duration {
return b.bulkOpTimeout
}
// SetBulkOperationTimeout sets the maxium amount of time to wait for a bulk op to succeed.
func (b *Bucket) SetBulkOperationTimeout(timeout time.Duration) {
b.bulkOpTimeout = timeout
}
// DurabilityTimeout returns the maximum amount of time to wait for durability to succeed.
func (b *Bucket) DurabilityTimeout() time.Duration {
return b.duraTimeout
}
// SetDurabilityTimeout sets the maximum amount of time to wait for durability to succeed.
func (b *Bucket) SetDurabilityTimeout(timeout time.Duration) {
b.duraTimeout = timeout
}
// DurabilityPollTimeout returns the amount of time waiting between durability polls.
func (b *Bucket) DurabilityPollTimeout() time.Duration {
return b.duraPollTimeout
}
// SetDurabilityPollTimeout sets the amount of time waiting between durability polls.
func (b *Bucket) SetDurabilityPollTimeout(timeout time.Duration) {
b.duraPollTimeout = timeout
}
// ViewTimeout returns the maximum amount of time to wait for a view query to complete.
func (b *Bucket) ViewTimeout() time.Duration {
return b.viewTimeout
}
// SetViewTimeout sets the maximum amount of time to wait for a view query to complete.
func (b *Bucket) SetViewTimeout(timeout time.Duration) {
b.viewTimeout = timeout
}
// N1qlTimeout returns the maximum amount of time to wait for a N1QL query to complete.
func (b *Bucket) N1qlTimeout() time.Duration {
return b.n1qlTimeout
}
// SetN1qlTimeout sets the maximum amount of time to wait for a N1QL query to complete.
func (b *Bucket) SetN1qlTimeout(timeout time.Duration) {
b.n1qlTimeout = timeout
}
// SetTranscoder specifies a Transcoder to use when translating documents from their
// raw byte format to Go types and back.
func (b *Bucket) SetTranscoder(transcoder Transcoder) {
b.transcoder = transcoder
}
// InvalidateQueryCache forces the internal cache of prepared queries to be cleared.
// Queries to be cached are controlled by the Adhoc() method of N1qlQuery.
func (b *Bucket) InvalidateQueryCache() {
b.cluster.InvalidateQueryCache()
}
// Cas represents the specific state of a document on the cluster.
type Cas gocbcore.Cas
type pendingOp gocbcore.PendingOp
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) getCbasEp() (string, error) {
cbasEps := b.cluster.analyticsHosts
if len(cbasEps) == 0 {
return "", &clientError{"No available Analytics nodes."}
}
return cbasEps[rand.Intn(len(cbasEps))], nil
}
func (b *Bucket) getFtsEp() (string, error) {
ftsEps := b.client.FtsEps()
if len(ftsEps) == 0 {
return "", &clientError{"No available FTS nodes."}
}
return ftsEps[rand.Intn(len(ftsEps))], nil
}
// Close the instance’s underlying socket resources. Note that operations pending on the connection may fail.
func (b *Bucket) Close() error {
b.cluster.closeBucket(b)
return b.client.Close()
}
// IoRouter returns the underlying gocb agent managing connections.
func (b *Bucket) IoRouter() *gocbcore.Agent {
return b.client
}
// Internal methods, not safe to be consumed by third parties.
func (b *Bucket) Internal() *BucketInternal {
return b.internal
}
// Manager returns a BucketManager for performing management operations on this bucket.
func (b *Bucket) Manager(username, password string) *BucketManager {
return &BucketManager{
bucket: b,
username: username,
password: password,
}
}