riakpb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,227 @@
1
+ require 'riak'
2
+
3
+ module Riak
4
+ # A client connection to Riak.
5
+ class Client
6
+ include Util::Translation
7
+ include Util::MessageCode
8
+
9
+ autoload :Rpc, 'riak/client/rpc'
10
+
11
+ # When using integer client IDs, the exclusive upper-bound of valid values.
12
+ MAX_CLIENT_ID = 4294967296
13
+
14
+ # Regexp for validating hostnames
15
+ # http://rubular.com/r/N2HOgxFkN3
16
+ HOST_REGEX = /^([[:alnum:]]+(-*[[:alnum:]]+)*(\.{0,1}(([[:alnum:]]-*)*[[:alnum:]]+)+)*)+$/
17
+
18
+ attr_reader :host
19
+ attr_reader :port
20
+ attr_reader :buckets
21
+ attr_reader :bucket_cache
22
+ attr_reader :node
23
+ attr_reader :server_version
24
+ attr_reader :client_id
25
+
26
+ # Creates a client connection to Riak's Protobuf Listener
27
+ # @param [String] options configuration options for the client
28
+ # @param [String] host ('127.0.0.1') The host or IP address for the Riak endpoint
29
+ # @param [Fixnum] port (8087) The port of the Riak protobuf listener endpoint
30
+ def initialize(options={})
31
+ self.host = options[:host] || "127.0.0.1"
32
+ self.port = options[:port] || 8087
33
+ self.client_id = options[:client_id] unless options[:client_id].nil?
34
+ @w = options[:w]
35
+ @dw = options[:dw]
36
+ @buckets = []
37
+ @bucket_cache = Hash.new{|k,v| k[v] = Riak::Bucket.new(self, v)}
38
+ end
39
+
40
+ # Set the hostname of the Riak endpoint. Must be an IPv4, IPv6, or valid hostname
41
+ # @param [String] value The host or IP address for the Riak endpoint
42
+ # @raise [ArgumentError] if an invalid hostname is given
43
+ # @return [String] the assigned hostname
44
+ def host=(value)
45
+ raise ArgumentError, t("hostname_invalid") unless value.is_a?(String) && value =~ HOST_REGEX
46
+ @host = value
47
+ end
48
+
49
+ # Set the port number of the Riak endpoint. This must be an integer between 0 and 65535.
50
+ # @param [Fixnum] value The port number of the Riak endpoint
51
+ # @raise [ArgumentError] if an invalid port number is given
52
+ # @return [Fixnum] the assigned port number
53
+ def port=(value)
54
+ raise ArgumentError, t("port_invalid") unless (0..65535).include?(value)
55
+ @port = value
56
+ end
57
+
58
+ # Set the client ID for this client. Must be a string or Fixnum value 0 =< value < MAX_CLIENT_ID.
59
+ # @param [String, Fixnum] value The internal client ID used by Riak to route responses
60
+ # @raise [ArgumentError] when an invalid client ID is given
61
+ # @return [String] the assigned client ID
62
+ def client_id=(value)
63
+ @client_id = case value
64
+ when 0...MAX_CLIENT_ID
65
+ b64encode(value)
66
+ when String
67
+ value
68
+ else
69
+ raise ArgumentError, t("invalid_client_id", :max_id => MAX_CLIENT_ID)
70
+ end
71
+ end
72
+
73
+ # Establish a connection to the riak node, and store the Rpc instance
74
+ # @return [Riak::Client::Rpc] the Rpc instance that handles connections to the riak node
75
+ def rpc(options={})
76
+ options[:client_id] ||= @client_id if @client_id
77
+ @rpc ||= Rpc.new(self)
78
+ end
79
+
80
+ # Tests connectivity with the Riak host.
81
+ # @return [Boolean] Successful returned as 'true', failed connection returned as 'false'
82
+ def ping?
83
+ rpc.request Util::MessageCode::PING_REQUEST
84
+
85
+ return rpc.status
86
+ end
87
+
88
+ # Retrieves basic information from the riak node.
89
+ # @return [Hash] Returns the name of the node and its software release number
90
+ def info
91
+ response = rpc.request Riak::Util::MessageCode::GET_SERVER_INFO_REQUEST
92
+
93
+ @node = response.node
94
+ @server_version = response.server_version
95
+
96
+ {:node => @node, :server_version => @server_version}
97
+ end
98
+
99
+ # I need bucket! Bring me bucket! (Retrieves a bucket from Riak. Eating disorder not included.)
100
+ # @param [String] bucket the bucket to retrieve
101
+ # @return [Bucket] the requested bucket
102
+ def bring_me_bucket(bucket)
103
+ request = Riak::RpbGetBucketReq.new(:bucket => bucket)
104
+ response = rpc.request(
105
+ Util::MessageCode::GET_BUCKET_REQUEST,
106
+ request
107
+ )
108
+
109
+ @bucket_cache[bucket].load(response)
110
+ end
111
+ alias :[] :bring_me_bucket
112
+ alias :bucket :bring_me_bucket
113
+
114
+ # Retrieves a key, using RpbGetReq, from within a given bucket, from Riak.
115
+ # @param [String] bucket the bucket from which to retrieve the key
116
+ # @param [String] key the name of the key to be received
117
+ # @param [Fixnum] quorum read quorum- num of replicas need to agree when retrieving the object
118
+ # @return [RpbGetResp] the response in which the given Key is stored
119
+ def get_request(bucket, key, quorum=nil)
120
+ request = Riak::RpbGetReq.new({:bucket => bucket, :key => key})
121
+ request.r = quorum if quorum.is_a?(Fixnum)
122
+
123
+ response = rpc.request(
124
+ Util::MessageCode::GET_REQUEST,
125
+ request
126
+ )
127
+
128
+ return(response)
129
+ end
130
+ alias :req :get_request
131
+ alias :get :get_request
132
+
133
+ # Inserts a key into riak, using RpbPutReq.
134
+ # @option options [Fixnum] :w (write quorum) how many replicas to write to before returning a successful response.
135
+ # @option options [Fixnum :dw how many replicas to commit to durable storage before returning a successful response.
136
+ # @option options [Boolean] :return_body whether to return the contents of the stored object.
137
+ # @return [RpbPutResp] the response confirming Key storage and (optionally) the Key's updated/new data.
138
+ def put_request(options)
139
+ raise ArgumentError, t('invalid_bucket') if options[:bucket].empty?
140
+ raise ArgumentError, t('empty_content') if options[:content].nil?
141
+
142
+ options[:w] ||= @w unless @w.nil?
143
+ options[:dw] ||= @dw unless @dw.nil?
144
+ options[:return_body] ||= true
145
+
146
+ request = Riak::RpbPutReq.new(options)
147
+
148
+ response = rpc.request(
149
+ Util::MessageCode::PUT_REQUEST,
150
+ request
151
+ )
152
+
153
+ return(response)
154
+ end
155
+
156
+ # Deletes a key, using RpbDelReq, from within a given bucket, from Riak.
157
+ # @param [String] bucket the bucket from which to delete the key
158
+ # @param [String] key the name of the key to be deleted
159
+ # @param [Fixnum] rw how many replicas to delete before returning a successful response
160
+ # @return [RpbGetResp] the response confirming deletion
161
+ def del_request(bucket, key, rw=nil)
162
+ request = Riak::RpbDelReq.new
163
+ request.bucket = bucket
164
+ request.key = key
165
+ request.rw ||= rw
166
+
167
+ response = rpc.request(
168
+ Util::MessageCode::DEL_REQUEST,
169
+ request
170
+ )
171
+ end
172
+
173
+ # Sends a MapReduce operation to riak, using RpbMapRedReq, and returns the Response/phases.
174
+ # @param [String] mr_request map/reduce job, encoded/stringified
175
+ # @param [String] content_type encoding for map/reduce job
176
+ # @return [RpbMapRedResp] the response, encoded in the same format that was sent
177
+ def map_reduce_request(mr_request, content_type)
178
+ request = Riak::RpbMapRedReq.new
179
+ request.request = mr_request
180
+ request.content_type = content_type
181
+
182
+ response = rpc.request(
183
+ Util::MessageCode::MAP_REDUCE_REQUEST,
184
+ request
185
+ )
186
+
187
+ return(response)
188
+ end
189
+ alias :mapred :map_reduce_request
190
+ alias :mr :map_reduce_request
191
+
192
+ # Lists the buckets found in the Riak database
193
+ # @raise [ReturnRespError] if the message response does not correlate with the message requested
194
+ # @return [Array] list of buckets (String)
195
+ def buckets
196
+ response = rpc.request Util::MessageCode::LIST_BUCKETS_REQUEST
197
+
198
+ # iterate through each of the Strings in the Bucket list, returning an array of String(s)
199
+ @buckets = response.buckets.each{|b| b}
200
+ end
201
+
202
+ # Lists the keys within their respective buckets, that are found in the Riak database
203
+ # @param [String] bucket the bucket from which to retrieve the list of keys
204
+ # @raise [ReturnRespError] if the message response does not correlate with the message requested
205
+ # @return [Hash] Mapping of the buckets (String) to their keys (Array of Strings)
206
+ def keys_in(bucket)
207
+
208
+ list_keys_request = RpbListKeysReq.new(:bucket => bucket)
209
+
210
+ response = rpc.request Util::MessageCode::LIST_KEYS_REQUEST, list_keys_request
211
+
212
+ return(response.keys.each{|k| k})
213
+ end
214
+
215
+ # @return [String] A representation suitable for IRB and debugging output.
216
+ # def inspect
217
+ # "#<Client >"
218
+ # end
219
+
220
+ private
221
+ def b64encode(n)
222
+ Base64.encode64([n].pack("N")).chomp
223
+ end
224
+
225
+ end # class Client
226
+ end # module Riak
227
+
@@ -0,0 +1,372 @@
1
+ ### Generated by rprotoc. DO NOT EDIT!
2
+ ### <proto file: riakclient.proto>
3
+ # /* -------------------------------------------------------------------
4
+ # **
5
+ # ** riakclient.proto: Protocol buffers for riak
6
+ # **
7
+ # ** Copyright (c) 2007-2010 Basho Technologies, Inc. All Rights Reserved.
8
+ # **
9
+ # ** This file is provided to you under the Apache License,
10
+ # ** Version 2.0 (the "License"); you may not use this file
11
+ # ** except in compliance with the License. You may obtain
12
+ # ** a copy of the License at
13
+ # **
14
+ # ** http://www.apache.org/licenses/LICENSE-2.0
15
+ # **
16
+ # ** Unless required by applicable law or agreed to in writing,
17
+ # ** software distributed under the License is distributed on an
18
+ # ** "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19
+ # ** KIND, either express or implied. See the License for the
20
+ # ** specific language governing permissions and limitations
21
+ # ** under the License.
22
+ # **
23
+ # ** -------------------------------------------------------------------
24
+ # */
25
+ # /*
26
+ # ** Revision: 1.1
27
+ # **
28
+ # ** Lowest Common Denominator Protocol Buffers Client
29
+ # ** - no ENUM (protobuffs_erlang does not support)
30
+ # **
31
+ # ** Protocol
32
+ # **
33
+ # ** The protocol encodes requests and responses as protocol buffer messages.
34
+ # ** Each request message results in one or more response messages.
35
+ # ** As message type and length are not encoded by PB they are sent
36
+ # ** on the wire as
37
+ # **
38
+ # ** <length:32> <msg_code:8> <pbmsg>
39
+ # **
40
+ # ** length is the length of msg_code (1 byte) plus the message length
41
+ # ** in bytes encoded in network order (big endian)
42
+ # **
43
+ # ** msg_code indicates what is encoded as pbmsg
44
+ # **
45
+ # ** pbmsg is the encoded protocol buffer message
46
+ # **
47
+ # ** On connect, the client sends RpbHello with protocol/node information.
48
+ # ** After that client can make requests and will receive responses.
49
+ # ** For each request message there is a correspondign response message,
50
+ # ** or the server will respond with an error message if something has
51
+ # ** gone wrong.
52
+ # **
53
+ # ** The client should be prepared to handle messages without any pbmsg
54
+ # ** (i.e. length==1) for requests like ping or a put without return_body set.
55
+ # **
56
+ # ** RpbGetClientIdReq -> RpbGetClientIdResp
57
+ # ** RpbSetClientIdReq -> RpbSetClientIdResp
58
+ # ** RpbGetServerInfoReq -> RpbGetServerInfoResp
59
+ # ** RpbPingReq -> RpbPingResp
60
+ # ** RpbGetReq -> RpbErrorResp | RbpGetResp
61
+ # ** RpbPutReq -> RpbErrorResp | RpbPutResp
62
+ # ** RpbDelReq -> RpbErrorResp | RpbDelResp
63
+ # ** RpbListBucketsReq -> RpbErrorResp | RpbListBucketsResp
64
+ # ** RpbListKeysReq -> RpbErrorResp | RpbListKeysResp{1,}
65
+ # ** RpbGetBucketReq -> RpbErrorResp | RpbGetBucketResp
66
+ # **
67
+ # **
68
+ # ** Message Codes
69
+ # ** 0 - RpbErrorResp
70
+ # ** 1 - RpbPingReq - 0 length
71
+ # ** 2 - RpbPingResp (pong) - 0 length
72
+ # ** 3 - RpbGetClientIdReq
73
+ # ** 4 - RpbGetClientIdResp
74
+ # ** 5 - RpbSetClientIdReq
75
+ # ** 6 - RpbSetClientIdResp
76
+ # ** 7 - RpbGetServerInfoReq
77
+ # ** 8 - RpbGetServerInfoResp
78
+ # ** 9 - RpbGetReq
79
+ # ** 10 - RpbGetResp
80
+ # ** 11 - RpbPutReq
81
+ # ** 12 - RpbPutResp - 0 length
82
+ # ** 13 - RpbDelReq
83
+ # ** 14 - RpbDelResp
84
+ # ** 15 - RpbListBucketsReq
85
+ # ** 16 - RpbListBucketsResp{1,}
86
+ # ** 17 - RpbListKeysReq
87
+ # ** 18 - RpbListKeysResp{1,}
88
+ # ** 19 - RpbGetBucketReq
89
+ # ** 20 - RpbGetBucketResp
90
+ # ** 21 - RpbSetBucketReq
91
+ # ** 22 - RpbSetBucketResp
92
+ # ** 23 - RpbMapRedReq
93
+ # ** 24 - RpbMapRedResp
94
+ # **
95
+ # */
96
+ #
97
+ # // Error response - may be generated for any Req
98
+ # message RpbErrorResp {
99
+ # required bytes errmsg = 1;
100
+ # required uint32 errcode = 2;
101
+ # }
102
+ #
103
+ # // Get ClientId Request - no message defined, just send RpbGetClientIdReq message code
104
+ # message RpbGetClientIdResp {
105
+ # required bytes client_id = 1; // Client id in use for this connection
106
+ # }
107
+ #
108
+ # message RpbSetClientIdReq {
109
+ # required bytes client_id = 1; // Client id to use for this connection
110
+ # }
111
+ # // Set ClientId Request - no message defined, just send RpbSetClientIdReq message code
112
+ #
113
+ # // Get server info request - no message defined, just send RpbGetServerInfoReq message code
114
+ #
115
+ # message RpbGetServerInfoResp {
116
+ # optional bytes node = 1;
117
+ # optional bytes server_version = 2;
118
+ # }
119
+ #
120
+ #
121
+ # // Get Request - retrieve bucket/key
122
+ # message RpbGetReq {
123
+ # required bytes bucket = 1;
124
+ # required bytes key = 2;
125
+ # optional uint32 r = 3;
126
+ # }
127
+ #
128
+ # // Get Response - if the record was not found there will be no content/vclock
129
+ # message RpbGetResp {
130
+ # repeated RpbContent content = 1;
131
+ # optional bytes vclock = 2; // the opaque vector clock for the object
132
+ # }
133
+ #
134
+ #
135
+ # // Put request - if options.return_body is set then the updated metadata/data for
136
+ # // the key will be returned.
137
+ # message RpbPutReq {
138
+ # required bytes bucket = 1;
139
+ # required bytes key = 2;
140
+ # optional bytes vclock = 3;
141
+ # required RpbContent content = 4;
142
+ # optional uint32 w = 5;
143
+ # optional uint32 dw = 6;
144
+ # optional bool return_body = 7;
145
+ # }
146
+ #
147
+ # // Put response - same as get response
148
+ # message RpbPutResp {
149
+ # repeated RpbContent contents = 1;
150
+ # optional bytes vclock = 2; // the opaque vector clock for the object
151
+ # }
152
+ #
153
+ #
154
+ # // Delete request
155
+ # message RpbDelReq {
156
+ # required bytes bucket = 1;
157
+ # required bytes key = 2;
158
+ # optional uint32 rw = 3;
159
+ # }
160
+ #
161
+ # // Delete response - not defined, will return a RpbDelResp on success or RpbErrorResp on failure
162
+ #
163
+ # // List buckets request - no message defined, just send RpbListBucketsReq
164
+ #
165
+ # // List buckets response
166
+ # message RpbListBucketsResp {
167
+ # repeated bytes buckets = 1;
168
+ # }
169
+ #
170
+ #
171
+ # // List keys in bucket request
172
+ # message RpbListKeysReq {
173
+ # required bytes bucket = 1;
174
+ # }
175
+ #
176
+ # // List keys in bucket response - one or more of these packets will be sent
177
+ # // the last one will have done set true (and may not have any keys in it)
178
+ # message RpbListKeysResp {
179
+ # repeated bytes keys = 1;
180
+ # optional bool done = 2;
181
+ # }
182
+ #
183
+ # // Get bucket properties request
184
+ # message RpbGetBucketReq {
185
+ # required bytes bucket = 1;
186
+ # }
187
+ #
188
+ # // Get bucket properties response
189
+ # message RpbGetBucketResp {
190
+ # required RpbBucketProps props = 1;
191
+ # }
192
+ #
193
+ # // Set bucket properties request
194
+ # message RpbSetBucketReq {
195
+ # required bytes bucket = 1;
196
+ # required RpbBucketProps props = 2;
197
+ # }
198
+ #
199
+ #
200
+ # // Set bucket properties response - no message defined, just send RpbSetBucketResp
201
+ #
202
+ #
203
+ # // Map/Reduce request
204
+ # message RpbMapRedReq {
205
+ # required bytes request = 1;
206
+ # required bytes content_type = 2;
207
+ # }
208
+ #
209
+ # // Map/Reduce response
210
+ # // one or more of these packets will be sent the last one will have done set
211
+ # // true (and may not have phase/data in it)
212
+ # message RpbMapRedResp {
213
+ # optional uint32 phase = 1;
214
+ # optional bytes response = 2;
215
+ # optional bool done = 3;
216
+ # }
217
+ #
218
+ # // Content message included in get/put responses
219
+ # // Holds the value and associated metadata
220
+ # message RpbContent {
221
+ # required bytes value = 1;
222
+ # optional bytes content_type = 2; // the media type/format
223
+ # optional bytes charset = 3;
224
+ # optional bytes content_encoding = 4;
225
+ # optional bytes vtag = 5;
226
+ # repeated RpbLink links = 6; // links to other resources
227
+ # optional uint32 last_mod = 7;
228
+ # optional uint32 last_mod_usecs = 8;
229
+ # repeated RpbPair usermeta = 9; // user metadata stored with the object
230
+ # }
231
+ #
232
+ # // Key/value pair - used for user metadata
233
+ # message RpbPair {
234
+ # required bytes key = 1;
235
+ # optional bytes value = 2;
236
+ # }
237
+ #
238
+ # // Link metadata
239
+ # message RpbLink {
240
+ # optional bytes bucket = 1;
241
+ # optional bytes key = 2;
242
+ # optional bytes tag = 3;
243
+ # }
244
+ #
245
+ # // Bucket properties
246
+ # message RpbBucketProps {
247
+ # optional uint32 n_val = 1;
248
+ # optional bool allow_mult = 2;
249
+ # }
250
+
251
+ require 'protobuf/message/message'
252
+ require 'protobuf/message/enum'
253
+ require 'protobuf/message/service'
254
+ require 'protobuf/message/extend'
255
+
256
+ module Riak
257
+ class RpbErrorResp < ::Protobuf::Message
258
+ defined_in __FILE__
259
+ required :bytes, :errmsg, 1
260
+ required :uint32, :errcode, 2
261
+ end
262
+ class RpbGetClientIdResp < ::Protobuf::Message
263
+ defined_in __FILE__
264
+ required :bytes, :client_id, 1
265
+ end
266
+ class RpbSetClientIdReq < ::Protobuf::Message
267
+ defined_in __FILE__
268
+ required :bytes, :client_id, 1
269
+ end
270
+ class RpbGetServerInfoResp < ::Protobuf::Message
271
+ defined_in __FILE__
272
+ optional :bytes, :node, 1
273
+ optional :bytes, :server_version, 2
274
+ end
275
+ class RpbGetReq < ::Protobuf::Message
276
+ defined_in __FILE__
277
+ required :bytes, :bucket, 1
278
+ required :bytes, :key, 2
279
+ optional :uint32, :r, 3
280
+ end
281
+ class RpbGetResp < ::Protobuf::Message
282
+ defined_in __FILE__
283
+ repeated :RpbContent, :content, 1
284
+ optional :bytes, :vclock, 2
285
+ end
286
+ class RpbPutReq < ::Protobuf::Message
287
+ defined_in __FILE__
288
+ required :bytes, :bucket, 1
289
+ required :bytes, :key, 2
290
+ optional :bytes, :vclock, 3
291
+ required :RpbContent, :content, 4
292
+ optional :uint32, :w, 5
293
+ optional :uint32, :dw, 6
294
+ optional :bool, :return_body, 7
295
+ end
296
+ class RpbPutResp < ::Protobuf::Message
297
+ defined_in __FILE__
298
+ repeated :RpbContent, :content, 1
299
+ optional :bytes, :vclock, 2
300
+ end
301
+ class RpbDelReq < ::Protobuf::Message
302
+ defined_in __FILE__
303
+ required :bytes, :bucket, 1
304
+ required :bytes, :key, 2
305
+ optional :uint32, :rw, 3
306
+ end
307
+ class RpbListBucketsResp < ::Protobuf::Message
308
+ defined_in __FILE__
309
+ repeated :bytes, :buckets, 1
310
+ end
311
+ class RpbListKeysReq < ::Protobuf::Message
312
+ defined_in __FILE__
313
+ required :bytes, :bucket, 1
314
+ end
315
+ class RpbListKeysResp < ::Protobuf::Message
316
+ defined_in __FILE__
317
+ repeated :bytes, :keys, 1
318
+ optional :bool, :done, 2
319
+ end
320
+ class RpbGetBucketReq < ::Protobuf::Message
321
+ defined_in __FILE__
322
+ required :bytes, :bucket, 1
323
+ end
324
+ class RpbGetBucketResp < ::Protobuf::Message
325
+ defined_in __FILE__
326
+ required :RpbBucketProps, :props, 1
327
+ end
328
+ class RpbSetBucketReq < ::Protobuf::Message
329
+ defined_in __FILE__
330
+ required :bytes, :bucket, 1
331
+ required :RpbBucketProps, :props, 2
332
+ end
333
+ class RpbMapRedReq < ::Protobuf::Message
334
+ defined_in __FILE__
335
+ required :bytes, :request, 1
336
+ required :bytes, :content_type, 2
337
+ end
338
+ class RpbMapRedResp < ::Protobuf::Message
339
+ defined_in __FILE__
340
+ repeated :uint32, :phase, 1
341
+ repeated :bytes, :response, 2
342
+ optional :bool, :done, 3
343
+ end
344
+ class RpbContent < ::Protobuf::Message
345
+ defined_in __FILE__
346
+ required :bytes, :value, 1
347
+ optional :bytes, :content_type, 2
348
+ optional :bytes, :charset, 3
349
+ optional :bytes, :content_encoding, 4
350
+ optional :bytes, :vtag, 5
351
+ repeated :RpbLink, :links, 6
352
+ optional :uint32, :last_mod, 7
353
+ optional :uint32, :last_mod_usecs, 8
354
+ repeated :RpbPair, :usermeta, 9
355
+ end
356
+ class RpbPair < ::Protobuf::Message
357
+ defined_in __FILE__
358
+ required :bytes, :key, 1
359
+ optional :bytes, :value, 2
360
+ end
361
+ class RpbLink < ::Protobuf::Message
362
+ defined_in __FILE__
363
+ optional :bytes, :bucket, 1
364
+ optional :bytes, :key, 2
365
+ optional :bytes, :tag, 3
366
+ end
367
+ class RpbBucketProps < ::Protobuf::Message
368
+ defined_in __FILE__
369
+ optional :uint32, :n_val, 1
370
+ optional :bool, :allow_mult, 2
371
+ end
372
+ end
@@ -0,0 +1,21 @@
1
+ require 'riak'
2
+
3
+ module Riak
4
+ # Exception raised when the expected response code from Riak
5
+ # fails to match the actual response code.
6
+ class FailedExchange < StandardError
7
+ include Util::Translation
8
+
9
+ attr_reader :expected
10
+ attr_reader :actual
11
+ attr_reader :output
12
+ attr_reader :stub
13
+
14
+ def initialize(expected, actual, output, stub)
15
+ @expected, @actual, @output, @stub = expected, actual, output, stub
16
+ super t("failed_rx", :failure =>
17
+ t(@stub, :expected => @expected, :actual => @actual, :output => @output.inspect)
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'riak'
2
+
3
+ module Riak
4
+ # Exception raised when the expected response code from Riak
5
+ # fails to match the actual response code.
6
+ class FailedRequest < StandardError
7
+ include Riak::Util::Translation
8
+
9
+ attr_reader :expected
10
+ attr_reader :actual
11
+ attr_reader :output
12
+ attr_reader :message
13
+
14
+ def initialize(expected=nil, actual=nil, output=nil, message=nil)
15
+ @expected = expected
16
+ @actual = actual
17
+ @output = output
18
+ @message = message || "failed_request"
19
+ super t(@message, :expected => @expected, :actual => @actual, :output => @output.inspect)
20
+ end
21
+ end
22
+ end
data/lib/riak/i18n.rb ADDED
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'active_support/i18n'
3
+ rescue LoadError
4
+ require 'i18n' # support ActiveSupport < 3
5
+ end
6
+
7
+ I18n.load_path << File.expand_path("../locale/en.yml", __FILE__)