-
Notifications
You must be signed in to change notification settings - Fork 27
/
defaultclient.go
140 lines (122 loc) · 3.76 KB
/
defaultclient.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
package riak
import (
"errors"
)
var (
NoDefaultClientConnection = errors.New("No (default) client connection")
)
// Introduce a default client
var defaultClient *Client = nil
// Create the default client with a single connection to Riak.
func ConnectClient(addr string) (err error) {
if defaultClient != nil {
defaultClient.Close()
}
defaultClient = NewClient(addr)
return defaultClient.Connect()
}
// Create the default client, using a pool of connections. This is the recommended
// method to connect to Riak in an application. A single client instance can be
// used by multiple threads/goroutines and will not block operations if there are
// enough connections in the pool.
// NOTE: If an application needs connections to different Riak clusters it can use
// riak.NewClientPool or riak.NewClient.
func ConnectClientPool(addr string, count int) (err error) {
if defaultClient != nil {
defaultClient.Close()
}
defaultClient = NewClientPool(addr, count)
return defaultClient.Connect()
}
// Closes the connection of the default client.
func Close() {
defaultClient.Close()
}
// Ping the server
func Ping() (err error) {
if defaultClient == nil {
return NoDefaultClientConnection
}
return defaultClient.Ping()
}
// Get the client Id
func Id() (id string, err error) {
if defaultClient == nil {
err = NoDefaultClientConnection
return
}
return defaultClient.Id()
}
// Set the client Id
func SetId(id string) (err error) {
if defaultClient == nil {
return NoDefaultClientConnection
}
return defaultClient.SetId(id)
}
// Get the server version for the default client
func ServerVersion() (node string, version string, err error) {
if defaultClient == nil {
err = NoDefaultClientConnection
return
}
return defaultClient.ServerVersion()
}
// Return a new bucket using the client connection
func NewBucket(name string) (*Bucket, error) {
if defaultClient == nil {
return nil, NoDefaultClientConnection
}
return defaultClient.NewBucket(name)
}
// Return a new bucket using the client connection
func NewBucketType(btype, name string) (*Bucket, error) {
if defaultClient == nil {
return nil, NoDefaultClientConnection
}
return defaultClient.NewBucketType(btype, name)
}
// Create a MapReduce object that can be used to build a MR query
func NewMapReduce() *MapReduce {
if defaultClient == nil {
return nil
}
return defaultClient.MapReduce()
}
// Run a MapReduce query directly
func RunMapReduce(query string) (resp [][]byte, err error) {
if defaultClient == nil {
err = NoDefaultClientConnection
return
}
return defaultClient.RunMapReduce(query)
}
// Get directly from a bucket, without creating a bucket first
func GetFrom(bucketname string, key string, options ...map[string]uint32) (obj *RObject, err error) {
if defaultClient == nil {
err = NoDefaultClientConnection
return
}
return defaultClient.GetFrom(bucketname, key, options...)
}
// Delete directly from a bucket, without creating a bucket object first
func DeleteFrom(bucketname string, key string, options ...map[string]uint32) (err error) {
if defaultClient == nil {
return NoDefaultClientConnection
}
return defaultClient.DeleteFrom(bucketname, key, options...)
}
// Create a new RObject in a bucket directly, without creating a bucket object first
func NewObjectIn(bucketname string, key string, options ...map[string]uint32) (*RObject, error) {
if defaultClient == nil {
return nil, NoDefaultClientConnection
}
return defaultClient.NewObjectIn(bucketname, key, options...)
}
// Test if an object exists in a bucket directly, without creating a bucket object first
func ExistsIn(bucketname string, key string, options ...map[string]uint32) (exists bool, err error) {
if defaultClient == nil {
return false, NoDefaultClientConnection
}
return defaultClient.ExistsIn(bucketname, key, options...)
}