forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
120 lines (104 loc) · 2.66 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
package gocb
import (
"github.com/couchbaselabs/gocb/gocbcore"
"math/rand"
"sync"
"time"
)
// An interface representing a single bucket within a cluster.
type Bucket struct {
name string
password string
client *gocbcore.Agent
transcoder Transcoder
opTimeout time.Duration
duraTimeout time.Duration
duraPollTimeout time.Duration
}
func createBucket(config *gocbcore.AgentConfig) (*Bucket, error) {
cli, err := gocbcore.CreateAgent(config)
if err != nil {
return nil, err
}
return &Bucket{
name: config.BucketName,
password: config.Password,
client: cli,
transcoder: &DefaultTranscoder{},
opTimeout: 2500 * time.Millisecond,
duraTimeout: 40000 * time.Millisecond,
duraPollTimeout: 100 * time.Millisecond,
}, 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) SetTranscoder(transcoder Transcoder) {
b.transcoder = transcoder
}
var timerPool = sync.Pool{}
func acquireTimer(d time.Duration) *time.Timer {
tmrMaybe := timerPool.Get()
if tmrMaybe == nil {
return time.NewTimer(d)
}
tmr := tmrMaybe.(*time.Timer)
tmr.Reset(d)
return tmr
}
func releaseTimer(t *time.Timer, wasRead bool) {
stopped := t.Stop()
if !wasRead && !stopped {
<-t.C
}
timerPool.Put(t)
}
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) IoRouter() *gocbcore.Agent {
return b.client
}
func (b *Bucket) Manager(username, password string) *BucketManager {
return &BucketManager{
bucket: b,
username: username,
password: password,
}
}