forked from basho/riak-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
311 lines (255 loc) · 9.2 KB
/
client.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
Copyright 2010 Rusty Klophaus <rusty@basho.com>
Copyright 2010 Justin Sheehy <justin@basho.com>
Copyright 2009 Jay Baird <jay@mochimedia.com>
This file is provided to you under the Apache License,
Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
"""
# Use json as first choice, simplejson as second choice.
try:
import json
except ImportError:
import simplejson as json
from riak.transports import RiakHttpTransport
from riak.bucket import RiakBucket
from riak.mapreduce import RiakMapReduce
from riak.search import RiakSearch
class RiakClient(object):
"""
The ``RiakClient`` object holds information necessary to connect to
Riak. The Riak API uses HTTP, so there is no persistent
connection, and the ``RiakClient`` object is extremely lightweight.
"""
def __init__(self, host='127.0.0.1', port=8098, prefix='riak',
mapred_prefix='mapred', transport_class=None,
client_id=None, solr_transport_class=None):
"""
Construct a new ``RiakClient`` object.
:param host: Hostname or IP address
:type host: string
:param port: Port number
:type port: integer
:param prefix: Interface prefix
:type prefix: string
:param mapred_prefix: MapReduce prefix
:type mapred_prefix: string
:param transport_class: transport class to use
:type transport_class: :class:`RiakTransport`
:param solr_transport_class: HTTP-based transport class for Solr interface queries
:type transport_class: :class:`RiakHttpTransport`
"""
if not transport_class:
self._transport = RiakHttpTransport(host,
port,
prefix,
mapred_prefix,
client_id)
else:
self._transport = transport_class(host, port, client_id=client_id)
self._r = "default"
self._w = "default"
self._dw = "default"
self._rw = "default"
self._encoders = {'application/json':json.dumps,
'text/json':json.dumps}
self._decoders = {'application/json':json.loads,
'text/json':json.loads}
self._solr = None
self._host = host
self._port = port
def get_transport(self):
"""
Get the transport instance the client is using for it's connection.
"""
return self._transport
def get_r(self):
"""
Get the R-value setting for this ``RiakClient``. (default "quorum")
:rtype: integer
"""
return self._r
def set_r(self, r):
"""
Set the R-value for this ``RiakClient``. This value will be used
for any calls to :func:`RiakBucket.get <riak.bucket.RiakBucket.get>`
or :func:`RiakBucket.get_binary <riak.bucket.RiakBucket.get_binary>`
where 1) no R-value is specified in the method call and 2) no R-value has
been set in the :class:`RiakBucket <riak.bucket.RiakBucket>`.
:param r: The R value.
:type r: integer
:rtype: self
"""
self._r = r
return self
def get_w(self):
"""
Get the W-value setting for this ``RiakClient``. (default "quorum")
:rtype: integer
"""
return self._w
def set_w(self, w):
"""
Set the W-value for this ``RiakClient`` instance. See :func:`set_r` for a
description of how these values are used.
:param w: The W value.
:type w: integer
:rtype: self
"""
self._w = w
return self
def get_dw(self):
"""
Get the DW-value for this ``RiakClient`` instance. (default "quorum")
:rtype: integer
"""
return self._dw
def set_dw(self, dw):
"""
Set the DW-value for this ``RiakClient`` instance. See :func:`set_r` for a
description of how these values are used.
:param dw: The DW value.
:type dw: integer
:rtype: self
"""
self._dw = dw
return self
def get_rw(self):
"""
Get the RW-value for this ``RiakClient`` instance. (default "quorum")
:rtype: integer
"""
return self._rw
def set_rw(self, rw):
"""
Set the RW-value for this ``RiakClient`` instance. See :func:`set_r` for a
description of how these values are used.
:param rw: The RW value.
:type rw: integer
:rtype: self
"""
self._rw = rw
return self
def get_client_id(self):
"""
Get the ``client_id`` for this ``RiakClient`` instance.
:rtype: string
"""
return self._transport.get_client_id()
def set_client_id(self, client_id):
"""
Set the client_id for this ``RiakClient`` instance.
.. warning::
You should not call this method unless you know what you are doing.
:param client_id: The new client_id.
:type client_id: string
:rtype: self
"""
self._transport.set_client_id(client_id)
return self
def get_encoder(self, content_type):
"""
Get the encoding function for the provided content type.
"""
if content_type in self._encoders:
return self._encoders[content_type]
def set_encoder(self, content_type, encoder):
"""
Set the encoding function for the provided content type.
:param encoder:
:type encoder: function
"""
self._encoders[content_type] = encoder
return self
def get_decoder(self, content_type):
"""
Get the decoding function for the provided content type.
"""
if content_type in self._decoders:
return self._decoders[content_type]
def set_decoder(self, content_type, decoder):
"""
Set the decoding function for the provided content type.
:param decoder:
:type decoder: function
"""
self._decoders[content_type] = decoder
return self
def get_buckets(self):
"""
Get the list of buckets.
NOTE: Do not use this in production, as it requires traversing through
all keys stored in a cluster.
"""
return self._transport.get_buckets()
def bucket(self, name):
"""
Get the bucket by the specified name. Since buckets always exist,
this will always return a :class:`RiakBucket <riak.bucket.RiakBucket>`.
:rtype: :class:`RiakBucket <riak.bucket.RiakBucket>`
"""
return RiakBucket(self, name)
def is_alive(self):
"""
Check if the Riak server for this ``RiakClient`` instance is alive.
:rtype: boolean
"""
return self._transport.ping()
def add(self, *args):
"""
Start assembling a Map/Reduce operation. A shortcut for :func:`RiakMapReduce.add`.
:rtype: :class:`RiakMapReduce`
"""
mr = RiakMapReduce(self)
return apply(mr.add, args)
def search(self, *args):
"""
Start assembling a Map/Reduce operation based on search
results. This command will return an error unless executed
against a Riak Search cluster. A shortcut for :func:`RiakMapReduce.search`.
:rtype: :class:`RiakMapReduce`
"""
mr = RiakMapReduce(self)
return apply(mr.search, args)
def link(self, *args):
"""
Start assembling a Map/Reduce operation. A shortcut for :func:`RiakMapReduce.link`.
:rtype: :class:`RiakMapReduce`
"""
mr = RiakMapReduce(self)
return apply(mr.link, args)
def map(self, *args):
"""
Start assembling a Map/Reduce operation. A shortcut for :func:`RiakMapReduce.map`.
:rtype: :class:`RiakMapReduce`
"""
mr = RiakMapReduce(self)
return apply(mr.map, args)
def reduce(self, *args):
"""
Start assembling a Map/Reduce operation. A shortcut for :func:`RiakMapReduce.reduce`.
:rtype: :class:`RiakMapReduce`
"""
mr = RiakMapReduce(self)
return apply(mr.reduce, args)
def store_file(self, filename, data, content_type="application/octet-stream"):
"""
Store data in luwak using filename as the key
"""
self._transport.store_file(filename, content_type=content_type, content=data)
def get_file(self, filename):
return self._transport.get_file(filename)
def delete_file(self, filename):
self._transport.delete_file(filename)
def solr(self):
if self._solr is None:
self._solr = RiakSearch(self, host=self._host, port=self._port)
return self._solr