]> git.saurik.com Git - redis.git/blob - client-libraries/ruby/lib/redis.rb
ignore gcc warning about write() return code not checked. It is esplicitily this...
[redis.git] / client-libraries / ruby / lib / redis.rb
1 require 'socket'
2 require 'set'
3 require File.join(File.dirname(__FILE__),'server')
4 require File.join(File.dirname(__FILE__),'pipeline')
5
6
7 class RedisError < StandardError
8 end
9 class RedisRenameError < StandardError
10 end
11 class Redis
12 ERR = "-".freeze
13 OK = 'OK'.freeze
14 SINGLE = '+'.freeze
15 BULK = '$'.freeze
16 MULTI = '*'.freeze
17 INT = ':'.freeze
18
19 attr_reader :server
20
21
22 def initialize(opts={})
23 @opts = {:host => 'localhost', :port => '6379', :db => 0}.merge(opts)
24 $debug = @opts[:debug]
25 @db = @opts[:db]
26 @server = Server.new(@opts[:host], @opts[:port], (@opts[:timeout]||10))
27 end
28
29 def pipelined
30 pipeline = Pipeline.new(self)
31 yield pipeline
32 pipeline.finish
33 end
34
35 def to_s
36 "#{host}:#{port}"
37 end
38
39 def port
40 @opts[:port]
41 end
42
43 def host
44 @opts[:host]
45 end
46
47 def with_socket_management(server, &block)
48 begin
49 socket = server.socket
50 block.call(socket)
51 #Timeout or server down
52 rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED, Timeout::Error => e
53 server.close
54 puts "Client (#{server.inspect}) disconnected from server: #{e.inspect}\n" if $debug
55 retry
56 #Server down
57 rescue NoMethodError => e
58 puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
59 raise Errno::ECONNREFUSED
60 #exit
61 end
62 end
63
64 def monitor
65 with_socket_management(@server) do |socket|
66 trap("INT") { puts "\nGot ^C! Dying!"; exit }
67 write "MONITOR\r\n"
68 puts "Now Monitoring..."
69 socket.read(12)
70 loop do
71 x = socket.gets
72 puts x unless x.nil?
73 end
74 end
75 end
76
77 def quit
78 write "QUIT\r\n"
79 end
80
81 def select_db(index)
82 @db = index
83 write "SELECT #{index}\r\n"
84 get_response
85 end
86
87 def flush_db
88 write "FLUSHDB\r\n"
89 get_response == OK
90 end
91
92 def flush_all
93 ensure_retry do
94 puts "Warning!\nFlushing *ALL* databases!\n5 Seconds to Hit ^C!"
95 trap('INT') {quit; return false}
96 sleep 5
97 write "FLUSHALL\r\n"
98 get_response == OK
99 end
100 end
101
102 def last_save
103 write "LASTSAVE\r\n"
104 get_response.to_i
105 end
106
107 def bgsave
108 write "BGSAVE\r\n"
109 get_response == OK
110 end
111
112 def info
113 info = {}
114 write("INFO\r\n")
115 x = get_response
116 x.each do |kv|
117 k,v = kv.split(':', 2)
118 k,v = k.chomp, v = v.chomp
119 info[k.to_sym] = v
120 end
121 info
122 end
123
124
125 def bulk_reply
126 begin
127 x = read
128 puts "bulk_reply read value is #{x.inspect}" if $debug
129 return x
130 rescue => e
131 puts "error in bulk_reply #{e}" if $debug
132 nil
133 end
134 end
135
136 def write(data)
137 with_socket_management(@server) do |socket|
138 puts "writing: #{data}" if $debug
139 socket.write(data)
140 end
141 end
142
143 def fetch(len)
144 with_socket_management(@server) do |socket|
145 len = [0, len.to_i].max
146 res = socket.read(len + 2)
147 res = res.chomp if res
148 res
149 end
150 end
151
152 def read(length = read_proto)
153 with_socket_management(@server) do |socket|
154 res = socket.read(length)
155 puts "read is #{res.inspect}" if $debug
156 res
157 end
158 end
159
160 def keys(glob)
161 write "KEYS #{glob}\r\n"
162 get_response.split(' ')
163 end
164
165 def rename!(oldkey, newkey)
166 write "RENAME #{oldkey} #{newkey}\r\n"
167 get_response
168 end
169
170 def rename(oldkey, newkey)
171 write "RENAMENX #{oldkey} #{newkey}\r\n"
172 case get_response
173 when -1
174 raise RedisRenameError, "source key: #{oldkey} does not exist"
175 when 0
176 raise RedisRenameError, "target key: #{oldkey} already exists"
177 when -3
178 raise RedisRenameError, "source and destination keys are the same"
179 when 1
180 true
181 end
182 end
183
184 def key?(key)
185 write "EXISTS #{key}\r\n"
186 get_response == 1
187 end
188
189 def delete(key)
190 write "DEL #{key}\r\n"
191 get_response == 1
192 end
193
194 def [](key)
195 get(key)
196 end
197
198 def get(key)
199 write "GET #{key}\r\n"
200 get_response
201 end
202
203 def mget(*keys)
204 write "MGET #{keys.join(' ')}\r\n"
205 get_response
206 end
207
208 def incr(key, increment=nil)
209 if increment
210 write "INCRBY #{key} #{increment}\r\n"
211 else
212 write "INCR #{key}\r\n"
213 end
214 get_response
215 end
216
217 def decr(key, decrement=nil)
218 if decrement
219 write "DECRBY #{key} #{decrement}\r\n"
220 else
221 write "DECR #{key}\r\n"
222 end
223 get_response
224 end
225
226 def randkey
227 write "RANDOMKEY\r\n"
228 get_response
229 end
230
231 def list_length(key)
232 write "LLEN #{key}\r\n"
233 case i = get_response
234 when -2
235 raise RedisError, "key: #{key} does not hold a list value"
236 else
237 i
238 end
239 end
240
241 def type?(key)
242 write "TYPE #{key}\r\n"
243 get_response
244 end
245
246 def push_tail(key, string)
247 write "RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
248 get_response
249 end
250
251 def push_head(key, string)
252 write "LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n"
253 get_response
254 end
255
256 def pop_head(key)
257 write "LPOP #{key}\r\n"
258 get_response
259 end
260
261 def pop_tail(key)
262 write "RPOP #{key}\r\n"
263 get_response
264 end
265
266 def list_set(key, index, val)
267 write "LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n"
268 get_response == OK
269 end
270
271 def list_range(key, start, ending)
272 write "LRANGE #{key} #{start} #{ending}\r\n"
273 get_response
274 end
275
276 def list_trim(key, start, ending)
277 write "LTRIM #{key} #{start} #{ending}\r\n"
278 get_response
279 end
280
281 def list_index(key, index)
282 write "LINDEX #{key} #{index}\r\n"
283 get_response
284 end
285
286 def list_rm(key, count, value)
287 write "LREM #{key} #{count} #{value.to_s.size}\r\n#{value}\r\n"
288 case num = get_response
289 when -1
290 raise RedisError, "key: #{key} does not exist"
291 when -2
292 raise RedisError, "key: #{key} does not hold a list value"
293 else
294 num
295 end
296 end
297
298 def set_add(key, member)
299 write "SADD #{key} #{member.to_s.size}\r\n#{member}\r\n"
300 case get_response
301 when 1
302 true
303 when 0
304 false
305 when -2
306 raise RedisError, "key: #{key} contains a non set value"
307 end
308 end
309
310 def set_delete(key, member)
311 write "SREM #{key} #{member.to_s.size}\r\n#{member}\r\n"
312 case get_response
313 when 1
314 true
315 when 0
316 false
317 when -2
318 raise RedisError, "key: #{key} contains a non set value"
319 end
320 end
321
322 def set_count(key)
323 write "SCARD #{key}\r\n"
324 case i = get_response
325 when -2
326 raise RedisError, "key: #{key} contains a non set value"
327 else
328 i
329 end
330 end
331
332 def set_member?(key, member)
333 write "SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n"
334 case get_response
335 when 1
336 true
337 when 0
338 false
339 when -2
340 raise RedisError, "key: #{key} contains a non set value"
341 end
342 end
343
344 def set_members(key)
345 write "SMEMBERS #{key}\r\n"
346 Set.new(get_response)
347 end
348
349 def set_intersect(*keys)
350 write "SINTER #{keys.join(' ')}\r\n"
351 Set.new(get_response)
352 end
353
354 def set_inter_store(destkey, *keys)
355 write "SINTERSTORE #{destkey} #{keys.join(' ')}\r\n"
356 get_response
357 end
358
359 def set_union(*keys)
360 write "SUNION #{keys.join(' ')}\r\n"
361 Set.new(get_response)
362 end
363
364 def set_union_store(destkey, *keys)
365 write "SUNIONSTORE #{destkey} #{keys.join(' ')}\r\n"
366 get_response
367 end
368
369 def set_diff(*keys)
370 write "SDIFF #{keys.join(' ')}\r\n"
371 Set.new(get_response)
372 end
373
374 def set_diff_store(destkey, *keys)
375 write "SDIFFSTORE #{destkey} #{keys.join(' ')}\r\n"
376 get_response
377 end
378
379 def set_move(srckey, destkey, member)
380 write "SMOVE #{srckey} #{destkey} #{member.to_s.size}\r\n#{member}\r\n"
381 get_response == 1
382 end
383
384 def sort(key, opts={})
385 cmd = "SORT #{key}"
386 cmd << " BY #{opts[:by]}" if opts[:by]
387 cmd << " GET #{opts[:get]}" if opts[:get]
388 cmd << " INCR #{opts[:incr]}" if opts[:incr]
389 cmd << " DEL #{opts[:del]}" if opts[:del]
390 cmd << " DECR #{opts[:decr]}" if opts[:decr]
391 cmd << " #{opts[:order]}" if opts[:order]
392 cmd << " LIMIT #{opts[:limit].join(' ')}" if opts[:limit]
393 cmd << "\r\n"
394 write(cmd)
395 get_response
396 end
397
398 def multi_bulk
399 res = read_proto
400 puts "mb res is #{res.inspect}" if $debug
401 list = []
402 Integer(res).times do
403 vf = get_response
404 puts "curren vf is #{vf.inspect}" if $debug
405 list << vf
406 puts "current list is #{list.inspect}" if $debug
407 end
408 list
409 end
410
411 def get_reply
412 begin
413 r = read(1)
414 raise RedisError if (r == "\r" || r == "\n")
415 rescue RedisError
416 retry
417 end
418 r
419 end
420
421 def []=(key, val)
422 set(key,val)
423 end
424
425
426 def set(key, val, expiry=nil)
427 write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n")
428 s = get_response == OK
429 return expire(key, expiry) if s && expiry
430 s
431 end
432
433 def expire(key, expiry=nil)
434 write("EXPIRE #{key} #{expiry}\r\n")
435 get_response == 1
436 end
437
438 def set_unless_exists(key, val)
439 write "SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n"
440 get_response == 1
441 end
442
443 def status_code_reply
444 begin
445 res = read_proto
446 if res.index('-') == 0
447 raise RedisError, res
448 else
449 true
450 end
451 rescue RedisError
452 raise RedisError
453 end
454 end
455
456 def get_response
457 begin
458 rtype = get_reply
459 rescue => e
460 raise RedisError, e.inspect
461 end
462 puts "reply_type is #{rtype.inspect}" if $debug
463 case rtype
464 when SINGLE
465 single_line
466 when BULK
467 bulk_reply
468 when MULTI
469 multi_bulk
470 when INT
471 integer_reply
472 when ERR
473 raise RedisError, single_line
474 else
475 raise RedisError, "Unknown response.."
476 end
477 end
478
479 def integer_reply
480 Integer(read_proto)
481 end
482
483 def single_line
484 buff = ""
485 while buff[-2..-1] != "\r\n"
486 buff << read(1)
487 end
488 puts "single_line value is #{buff[0..-3].inspect}" if $debug
489 buff[0..-3]
490 end
491
492 def read_socket
493 begin
494 socket = @server.socket
495 while res = socket.read(8096)
496 break if res.size != 8096
497 end
498 #Timeout or server down
499 rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED => e
500 server.close
501 puts "Client (#{server.inspect}) disconnected from server: #{e.inspect}\n" if $debug
502 retry
503 rescue Timeout::Error => e
504 #BTM - Ignore this error so we don't go into an endless loop
505 puts "Client (#{server.inspect}) Timeout\n" if $debug
506 #Server down
507 rescue NoMethodError => e
508 puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug
509 raise Errno::ECONNREFUSED
510 #exit
511 end
512 end
513
514 def read_proto
515 with_socket_management(@server) do |socket|
516 if res = socket.gets
517 x = res.chomp
518 puts "read_proto is #{x.inspect}\n\n" if $debug
519 x.to_i
520 end
521 end
522 end
523
524 end