From 29fac6170a308c1ed765d4f7edee94985959225c Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 27 Mar 2009 12:14:35 +0100 Subject: [PATCH] Ruby client library updated. Important changes in this new version! --- client-libraries/ruby/.gitignore | 3 + client-libraries/ruby/README.rdoc | 12 - client-libraries/ruby/Rakefile | 2 +- client-libraries/ruby/examples/test_server.rb | 13 + client-libraries/ruby/fill.rb | 11 - client-libraries/ruby/lib/better_timeout.rb | 3 + client-libraries/ruby/lib/dist_redis.rb | 12 +- client-libraries/ruby/lib/hash_ring.rb | 88 +- client-libraries/ruby/lib/redis.rb | 1060 ++++++----------- client-libraries/ruby/lib/server.rb | 131 ++ client-libraries/ruby/profile.rb | 22 + client-libraries/ruby/spec/redis_spec.rb | 116 +- client-libraries/ruby/speed.rb | 16 + client-libraries/ruby/tasks/redis.tasks.rb | 20 +- 14 files changed, 699 insertions(+), 810 deletions(-) create mode 100644 client-libraries/ruby/.gitignore delete mode 100644 client-libraries/ruby/README.rdoc create mode 100644 client-libraries/ruby/examples/test_server.rb delete mode 100644 client-libraries/ruby/fill.rb create mode 100644 client-libraries/ruby/lib/server.rb create mode 100644 client-libraries/ruby/profile.rb create mode 100644 client-libraries/ruby/speed.rb diff --git a/client-libraries/ruby/.gitignore b/client-libraries/ruby/.gitignore new file mode 100644 index 00000000..b33f14f7 --- /dev/null +++ b/client-libraries/ruby/.gitignore @@ -0,0 +1,3 @@ +nohup.out +redis/* +rdsrv diff --git a/client-libraries/ruby/README.rdoc b/client-libraries/ruby/README.rdoc deleted file mode 100644 index 484fbc3c..00000000 --- a/client-libraries/ruby/README.rdoc +++ /dev/null @@ -1,12 +0,0 @@ -== redis - -A ruby client library for the redis key value storage system: -http://code.google.com/p/redis/wiki/README - -redis is a key value store with some interesting features: - -1. fast -2. keys are strings but values can have types of "NONE","STRING","LIST","SET" - list's can be atomicaly push'd, pop'd and lpush'd, lpop'd and indexed so you - can store things like lists of comments under one key and still be able to - append comments without reading and putting back the whole list. diff --git a/client-libraries/ruby/Rakefile b/client-libraries/ruby/Rakefile index 569b654f..74b7d15b 100644 --- a/client-libraries/ruby/Rakefile +++ b/client-libraries/ruby/Rakefile @@ -7,7 +7,7 @@ require 'tasks/redis.tasks' GEM = 'redis' -GEM_VERSION = '0.0.2' +GEM_VERSION = '0.0.3' AUTHORS = ['Ezra Zygmuntowicz', 'Taylor Weibley'] EMAIL = "ez@engineyard.com" HOMEPAGE = "http://github.com/ezmobius/redis-rb" diff --git a/client-libraries/ruby/examples/test_server.rb b/client-libraries/ruby/examples/test_server.rb new file mode 100644 index 00000000..fa388d15 --- /dev/null +++ b/client-libraries/ruby/examples/test_server.rb @@ -0,0 +1,13 @@ +require 'socket' +require 'pp' +require File.join(File.dirname(__FILE__), '../lib/redis') + +#require File.join(File.dirname(__FILE__), '../lib/server') + + +#r = Redis.new +#loop do + +# puts "--------------------------------------" +# sleep 12 +#end \ No newline at end of file diff --git a/client-libraries/ruby/fill.rb b/client-libraries/ruby/fill.rb deleted file mode 100644 index ed95f3a9..00000000 --- a/client-libraries/ruby/fill.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'benchmark' -$:.push File.join(File.dirname(__FILE__), 'lib') -require 'redis' - -times = 20000 - -@r = Redis.new -(0..1000000).each{|x| - @r[x] = "Hello World" - puts x if (x > 0 and x % 10000) == 0 -} diff --git a/client-libraries/ruby/lib/better_timeout.rb b/client-libraries/ruby/lib/better_timeout.rb index ac12c6eb..818909a8 100644 --- a/client-libraries/ruby/lib/better_timeout.rb +++ b/client-libraries/ruby/lib/better_timeout.rb @@ -163,6 +163,9 @@ end # Another name for Timeout::Error, defined for backwards compatibility with # earlier versions of timeout.rb. +class Object + remove_const(:TimeoutError) if const_defined?(:TimeoutError) +end TimeoutError = Timeout::Error # :nodoc: if __FILE__ == $0 diff --git a/client-libraries/ruby/lib/dist_redis.rb b/client-libraries/ruby/lib/dist_redis.rb index 04505c20..e79af472 100644 --- a/client-libraries/ruby/lib/dist_redis.rb +++ b/client-libraries/ruby/lib/dist_redis.rb @@ -24,7 +24,7 @@ class DistRedis end def method_missing(sym, *args, &blk) - if redis = node_for_key(args.first) + if redis = node_for_key(args.first.to_s) redis.send sym, *args, &blk else super @@ -94,11 +94,11 @@ r = DistRedis.new 'localhost:6379', 'localhost:6380', 'localhost:6381', 'localho r.push_tail 'listor', 'foo4' r.push_tail 'listor', 'foo5' - p r.pop_tail 'listor' - p r.pop_tail 'listor' - p r.pop_tail 'listor' - p r.pop_tail 'listor' - p r.pop_tail 'listor' + p r.pop_tail('listor') + p r.pop_tail('listor') + p r.pop_tail('listor') + p r.pop_tail('listor') + p r.pop_tail('listor') puts "key distribution:" diff --git a/client-libraries/ruby/lib/hash_ring.rb b/client-libraries/ruby/lib/hash_ring.rb index 403f7cf5..bed86601 100644 --- a/client-libraries/ruby/lib/hash_ring.rb +++ b/client-libraries/ruby/lib/hash_ring.rb @@ -1,10 +1,15 @@ -require 'digest/md5' +require 'zlib' + class HashRing + + POINTS_PER_SERVER = 160 # this is the default in libmemcached + attr_reader :ring, :sorted_keys, :replicas, :nodes + # nodes is a list of objects that have a proper to_s representation. # replicas indicates how many virtual points should be used pr. node, # replicas are required to improve the distribution. - def initialize(nodes=[], replicas=3) + def initialize(nodes=[], replicas=POINTS_PER_SERVER) @replicas = replicas @ring = {} @nodes = [] @@ -18,7 +23,7 @@ class HashRing def add_node(node) @nodes << node @replicas.times do |i| - key = gen_key("#{node}:#{i}") + key = Zlib.crc32("#{node}:#{i}") @ring[key] = node @sorted_keys << key end @@ -27,7 +32,7 @@ class HashRing def remove_node(node) @replicas.times do |i| - key = gen_key("#{node}:#{count}") + key = Zlib.crc32("#{node}:#{count}") @ring.delete(key) @sorted_keys.reject! {|k| k == key} end @@ -40,15 +45,9 @@ class HashRing def get_node_pos(key) return [nil,nil] if @ring.size == 0 - key = gen_key(key) - nodes = @sorted_keys - nodes.size.times do |i| - node = nodes[i] - if key <= node - return [@ring[node], i] - end - end - [@ring[nodes[0]], 0] + crc = Zlib.crc32(key) + idx = HashRing.binary_search(@sorted_keys, crc) + return [@ring[@sorted_keys[idx]], idx] end def iter_nodes(key) @@ -59,11 +58,66 @@ class HashRing end end - def gen_key(key) - key = Digest::MD5.hexdigest(key) - ((key[3] << 24) | (key[2] << 16) | (key[1] << 8) | key[0]) + class << self + + # gem install RubyInline to use this code + # Native extension to perform the binary search within the hashring. + # There's a pure ruby version below so this is purely optional + # for performance. In testing 20k gets and sets, the native + # binary search shaved about 12% off the runtime (9sec -> 8sec). + begin + require 'inline' + inline do |builder| + builder.c <<-EOM + int binary_search(VALUE ary, unsigned int r) { + int upper = RARRAY_LEN(ary) - 1; + int lower = 0; + int idx = 0; + + while (lower <= upper) { + idx = (lower + upper) / 2; + + VALUE continuumValue = RARRAY_PTR(ary)[idx]; + unsigned int l = NUM2UINT(continuumValue); + if (l == r) { + return idx; + } + else if (l > r) { + upper = idx - 1; + } + else { + lower = idx + 1; + } + } + return upper; + } + EOM + end + rescue Exception => e + # Find the closest index in HashRing with value <= the given value + def binary_search(ary, value, &block) + upper = ary.size - 1 + lower = 0 + idx = 0 + + while(lower <= upper) do + idx = (lower + upper) / 2 + comp = ary[idx] <=> value + + if comp == 0 + return idx + elsif comp > 0 + upper = idx - 1 + else + lower = idx + 1 + end + end + return upper + end + + end end - + end # ring = HashRing.new ['server1', 'server2', 'server3'] diff --git a/client-libraries/ruby/lib/redis.rb b/client-libraries/ruby/lib/redis.rb index f54910c1..2b0dad5f 100644 --- a/client-libraries/ruby/lib/redis.rb +++ b/client-libraries/ruby/lib/redis.rb @@ -1,15 +1,28 @@ require 'socket' -require File.join(File.dirname(__FILE__),'better_timeout') require 'set' +require File.join(File.dirname(__FILE__),'server') + class RedisError < StandardError end - +class RedisRenameError < StandardError +end class Redis - OK = "+OK".freeze - ERRCODE = "-".freeze - NIL = 'nil'.freeze - CTRLF = "\r\n".freeze + ERR = "-".freeze + OK = 'OK'.freeze + SINGLE = '+'.freeze + BULK = '$'.freeze + MULTI = '*'.freeze + INT = ':'.freeze + + attr_reader :server + + + def initialize(opts={}) + @opts = {:host => 'localhost', :port => '6379'}.merge(opts) + $debug = @opts[:debug] + @server = Server.new(@opts[:host], @opts[:port]) + end def to_s "#{host}:#{port}" @@ -23,569 +36,302 @@ class Redis @opts[:host] end - def initialize(opts={}) - @opts = {:host => 'localhost', :port => '6379'}.merge(opts) + def with_socket_management(server, &block) + begin + block.call(server.socket) + #Timeout or server down + rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED => e + server.close + puts "Client (#{server.inspect}) disconnected from server: #{e.inspect}\n" if $debug + retry + #Server down + rescue NoMethodError => e + puts "Client (#{server.inspect}) tryin server that is down: #{e.inspect}\n Dying!" if $debug + exit + end + end + + def quit + write "QUIT\r\n" end - # SET key value - # Time complexity: O(1) - # Set the string value as value of the key. The string can't be longer - # than 1073741824 bytes (1 GB). - # - # Return value: status code reply - def []=(key, val) - val = redis_marshal(val) - timeout_retry(3, 3){ - write "SET #{key} #{val.to_s.size}\r\n#{val}\r\n" - status_code_reply - } + def select_db(index) + write "SELECT #{index}\r\n" + get_response end - # SETNX key value - # - # Time complexity: O(1) - # SETNX works exactly like SET with the only difference that if the key - # already exists no operation is performed. SETNX actually means "SET if Not eXists". - # - # *Return value: integer reply, specifically: - # - # 1 if the key was set 0 if the key was not set - def set_unless_exists(key, val) - val = redis_marshal(val) - timeout_retry(3, 3){ - write "SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n" - integer_reply == 1 - } + def flush_db + write "FLUSHDB\r\n" + get_response == OK + end + + def last_save + write "LASTSAVE\r\n" + get_response.to_i end - # GET key - # Time complexity: O(1) - # Get the value of the specified key. If the key does not exist the special value - # 'nil' is returned. If the value stored at key is not a string an error is - # returned because GET can only handle string values. - # - # Return value: bulk reply - def [](key) - timeout_retry(3, 3){ - write "GET #{key}\r\n" - redis_unmarshal(bulk_reply) - } + def bgsave + write "BGSAVE\r\n" + get_response == OK + end + + def info + info = {} + write("INFO\r\n") + x = get_response + x.each do |kv| + k,v = kv.split(':', 2) + k,v = k.chomp, v = v.chomp + info[k.to_sym] = v + end + info end - # INCR key - # INCRBY key value - # Time complexity: O(1) - # Increment the number stored at key by one. If the key does not exist or contains - # a value of a wrong type, set the key to the value of "1" (like if the previous - # value was zero). - # - # INCRBY works just like INCR but instead to increment by 1 the increment is value. - # - # Return value: integer reply - def incr(key, increment=nil) - timeout_retry(3, 3){ - if increment - write "INCRBY #{key} #{increment}\r\n" - else - write "INCR #{key}\r\n" - end - integer_reply - } + + def bulk_reply + begin + x = read.chomp + puts "bulk_reply read value is #{x.inspect}" if $debug + return x + rescue => e + puts "error in bulk_reply #{e}" if $debug + nil + end end - - # DECR key - # - # DECRBY key value - # - # Time complexity: O(1) Like INCR/INCRBY but decrementing instead of incrementing. - def decr(key, increment=nil) - timeout_retry(3, 3){ - if increment - write "DECRBY #{key} #{increment}\r\n" - else - write "DECR #{key}\r\n" - end - integer_reply - } + def write(data) + with_socket_management(@server) do |socket| + puts "writing: #{data}" if $debug + socket.write(data) + end end - # RANDOMKEY - # Time complexity: O(1) - # Returns a random key from the currently seleted DB. - # - # Return value: single line reply - def randkey - timeout_retry(3, 3){ - write "RANDOMKEY\r\n" - single_line_reply - } + def fetch(len) + with_socket_management(@server) do |socket| + len = [0, len.to_i].max + res = socket.read(len + 2) + res = res.chomp if res + res + end + end + + def read(length = read_proto) + with_socket_management(@server) do |socket| + res = socket.read(length) + puts "read is #{res.inspect}" if $debug + res + end end - # RENAME oldkey newkey - # - # Atomically renames the key oldkey to newkey. If the source and destination - # name are the same an error is returned. If newkey already exists it is - # overwritten. - # - # Return value: status code reply - def rename!(oldkey, newkey) - timeout_retry(3, 3){ - write "RENAME #{oldkey} #{newkey}\r\n" - status_code_reply - } + def keys(glob) + write "KEYS #{glob}\r\n" + get_response.split(' ') end + + def rename!(oldkey, newkey) + write "RENAME #{oldkey} #{newkey}\r\n" + get_response + end - # RENAMENX oldkey newkey - # Just like RENAME but fails if the destination key newkey already exists. - # - # *Return value: integer reply, specifically: - # - # 1 if the key was renamed 0 if the target key already exist -1 if the - # source key does not exist -3 if source and destination keys are the same def rename(oldkey, newkey) - timeout_retry(3, 3){ - write "RENAMENX #{oldkey} #{newkey}\r\n" - case integer_reply - when -1 - raise RedisError, "source key: #{oldkey} does not exist" - when 0 - raise RedisError, "target key: #{oldkey} already exists" - when -3 - raise RedisError, "source and destination keys are the same" - when 1 - true - end - } - end + write "RENAMENX #{oldkey} #{newkey}\r\n" + case get_response + when -1 + raise RedisRenameError, "source key: #{oldkey} does not exist" + when 0 + raise RedisRenameError, "target key: #{oldkey} already exists" + when -3 + raise RedisRenameError, "source and destination keys are the same" + when 1 + true + end + end - # EXISTS key - # Time complexity: O(1) - # Test if the specified key exists. The command returns "0" if the key - # exists, otherwise "1" is returned. Note that even keys set with an empty - # string as value will return "1". - # - # *Return value: integer reply, specifically: - # - # 1 if the key exists 0 if the key does not exist def key?(key) - timeout_retry(3, 3){ - write "EXISTS #{key}\r\n" - integer_reply == 1 - } - end + write "EXISTS #{key}\r\n" + get_response == 1 + end - # DEL key - # Time complexity: O(1) - # Remove the specified key. If the key does not exist no operation is - # performed. The command always returns success. - # - # *Return value: integer reply, specifically: - # - # 1 if the key was removed 0 if the key does not exist def delete(key) - timeout_retry(3, 3){ - write "DEL #{key}\r\n" - integer_reply == 1 - } + write "DEL #{key}\r\n" + get_response == 1 + end + + def [](key) + get(key) + end + + def get(key) + write "GET #{key}\r\n" + get_response end - # KEYS pattern - # Time complexity: O(n) (with n being the number of keys in the DB) - # Returns all the keys matching the glob-style pattern as space separated strings. - # For example if you have in the database the keys "foo" and "foobar" the command - # "KEYS foo*" will return "foo foobar". - # - # Note that while the time complexity for this operation is O(n) the constant times - # are pretty low. For example Redis running on an entry level laptop can scan a 1 - # million keys database in 40 milliseconds. Still it's better to consider this one - # of the slow commands that may ruin the DB performance if not used with care. - # - # Return value: bulk reply - def keys(glob) - timeout_retry(3, 3){ - write "KEYS #{glob}\r\n" - bulk_reply.split(' ') - } + def mget(*keys) + write "MGET #{keys.join(' ')}\r\n" + get_response + end + + def incr(key, increment=nil) + if increment + write "INCRBY #{key} #{increment}\r\n" + else + write "INCR #{key}\r\n" + end + get_response + end + + def decr(key, decrement=nil) + if decrement + write "DECRRBY #{key} #{decrement}\r\n" + else + write "DECR #{key}\r\n" + end + get_response end - # TYPE key - # - # Time complexity: O(1) Return the type of the value stored at key in form of - # a string. The type can be one of "none", "string", "list", "set". "none" is - # returned if the key does not exist. - # - # Return value: single line reply + def randkey + write "RANDOMKEY\r\n" + get_response + end + + def list_length(key) + write "LLEN #{key}\r\n" + case i = get_response + when -2 + raise RedisError, "key: #{key} does not hold a list value" + else + i + end + end + def type?(key) - timeout_retry(3, 3){ - write "TYPE #{key}\r\n" - single_line_reply - } + write "TYPE #{key}\r\n" + get_response end - # RPUSH key string - # - # Time complexity: O(1) - # Add the given string to the tail of the list contained at key. If the key - # does not exist an empty list is created just before the append operation. - # If the key exists but is not a List an error is returned. - # - # Return value: status code reply def push_tail(key, string) - timeout_retry(3, 3){ - write "RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n" - status_code_reply - } - end - - # LPUSH key string - # Time complexity: O(1) - # Add the given string to the head of the list contained at key. If the - # key does not exist an empty list is created just before the append operation. - # If the key exists but is not a List an error is returned. - # - # Return value: status code reply + write "RPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n" + get_response + end + def push_head(key, string) - timeout_retry(3, 3){ - write "LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n" - status_code_reply - } + write "LPUSH #{key} #{string.to_s.size}\r\n#{string.to_s}\r\n" + get_response end - # LPOP key - # - # Time complexity: O(1) - # Atomically return and remove the first element of the list. For example if - # the list contains the elements "a","b","c" LPOP will return "a" and the - # list will become "b","c". - # - # If the key does not exist or the list is already empty the special value - # 'nil' is returned. - # - # Return value: bulk reply def pop_head(key) - timeout_retry(3, 3){ - write "LPOP #{key}\r\n" - bulk_reply - } + write "LPOP #{key}\r\n" + get_response end - - # RPOP key - # This command works exactly like LPOP, but the last element instead - # of the first element of the list is returned/deleted. + def pop_tail(key) - timeout_retry(3, 3){ - write "RPOP #{key}\r\n" - bulk_reply - } - end - - # LSET key index value - # Time complexity: O(N) (with N being the length of the list) - # Set the list element at index (see LINDEX for information about the index argument) with the new value. Out of range indexes will generate an error. Note that setting the first or last elements of the list is O(1). - # - # Return value: status code reply + write "RPOP #{key}\r\n" + get_response + end + def list_set(key, index, val) - timeout_retry(3, 3){ - write "LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n" - status_code_reply - } + write "LSET #{key} #{index} #{val.to_s.size}\r\n#{val}\r\n" + get_response == OK end - - - # LLEN key - # Time complexity: O(1) - # Return the length of the list stored at the specified key. If the key does not - # exist zero is returned (the same behaviour as for empty lists). If the value - # stored at key is not a list the special value -1 is returned. Note: client - # library should raise an exception when -1 is returned instead to pass the - # value back to the caller like a normal list length value. - # - # *Return value: integer reply, specifically: - # - # the length of the list as an integer - # >= - # 0 if the operation succeeded -2 if the specified key does not hold a list valu + def list_length(key) - timeout_retry(3, 3){ - write "LLEN #{key}\r\n" - case i = integer_reply - when -2 - raise RedisError, "key: #{key} does not hold a list value" - else - i - end - } + write "LLEN #{key}\r\n" + case i = get_response + when -2 + raise RedisError, "key: #{key} does not hold a list value" + else + i + end end - - # LRANGE key start end - # Time complexity: O(n) (with n being the length of the range) - # Return the specified elements of the list stored at the specified key. Start - # and end are zero-based indexes. 0 is the first element of the list (the list head), - # 1 the next element and so on. - # - # For example LRANGE foobar 0 2 will return the first three elements of the list. - # - # start and end can also be negative numbers indicating offsets from the end of the list. - # For example -1 is the last element of the list, -2 the penultimate element and so on. - # - # Indexes out of range will not produce an error: if start is over the end of the list, - # or start > end, an empty list is returned. If end is over the end of the list Redis - # will threat it just like the last element of the list. - # - # Return value: multi bulk reply + def list_range(key, start, ending) - timeout_retry(3, 3){ - write "LRANGE #{key} #{start} #{ending}\r\n" - multi_bulk_reply - } + write "LRANGE #{key} #{start} #{ending}\r\n" + get_response end - - # LTRIM key start end - # Time complexity: O(n) (with n being len of list - len of range) - # Trim an existing list so that it will contain only the specified range of - # elements specified. Start and end are zero-based indexes. 0 is the first - # element of the list (the list head), 1 the next element and so on. - # - # For example LTRIM foobar 0 2 will modify the list stored at foobar key so that - # only the first three elements of the list will remain. - # - # start and end can also be negative numbers indicating offsets from the end of - # the list. For example -1 is the last element of the list, -2 the penultimate - # element and so on. - # - # Indexes out of range will not produce an error: if start is over the end of - # the list, or start > end, an empty list is left as value. If end over the - # end of the list Redis will threat it just like the last element of the list. - # - # Hint: the obvious use of LTRIM is together with LPUSH/RPUSH. For example: - # - # LPUSH mylist LTRIM mylist 0 99 - # The above two commands will push elements in the list taking care that the - # list will not grow without limits. This is very useful when using Redis - # to store logs for example. It is important to note that when used in this - # way LTRIM is an O(1) operation because in the average case just one element - # is removed from the tail of the list. - # - # Return value: status code reply def list_trim(key, start, ending) - timeout_retry(3, 3){ - write "LTRIM #{key} #{start} #{ending}\r\n" - status_code_reply - } + write "LTRIM #{key} #{start} #{ending}\r\n" + get_response end - - # LINDEX key index - # Time complexity: O(n) (with n being the length of the list) - # Return the specified element of the list stored at the specified key. 0 is - # the first element, 1 the second and so on. Negative indexes are supported, - # for example -1 is the last element, -2 the penultimate and so on. - # - # If the value stored at key is not of list type an error is returned. If - # the index is out of range an empty string is returned. - # - # Note that even if the average time complexity is O(n) asking for the first - # or the last element of the list is O(1). - # - # Return value: bulk reply + def list_index(key, index) - timeout_retry(3, 3){ - write "LINDEX #{key} #{index}\r\n" - bulk_reply - } + write "LINDEX #{key} #{index}\r\n" + get_response end - - # SADD key member - # Time complexity O(1) - # Add the specified member to the set value stored at key. If member is - # already a member of the set no operation is performed. If key does not - # exist a new set with the specified member as sole member is crated. If - # the key exists but does not hold a set value an error is returned. - # - # *Return value: integer reply, specifically: - # - # 1 if the new element was added 0 if the new element was already a member - # of the set -2 if the key contains a non set value + + def list_rm(key, count, value) + write "LREM #{key} #{count} #{value.to_s.size}\r\n#{value}\r\n" + case num = get_response + when -1 + raise RedisError, "key: #{key} does not exist" + when -2 + raise RedisError, "key: #{key} does not hold a list value" + else + num + end + end + def set_add(key, member) - timeout_retry(3, 3){ - write "SADD #{key} #{member.to_s.size}\r\n#{member}\r\n" - case integer_reply - when 1 - true - when 0 - false - when -2 - raise RedisError, "key: #{key} contains a non set value" - end - } + write "SADD #{key} #{member.to_s.size}\r\n#{member}\r\n" + case get_response + when 1 + true + when 0 + false + when -2 + raise RedisError, "key: #{key} contains a non set value" + end end - - # SREM key member - # - # Time complexity O(1) - # Remove the specified member from the set value stored at key. If member - # was not a member of the set no operation is performed. If key does not - # exist or does not hold a set value an error is returned. - # - # *Return value: integer reply, specifically: - # - # 1 if the new element was removed 0 if the new element was not a member - # of the set -2 if the key does not hold a set value + def set_delete(key, member) - timeout_retry(3, 3){ - write "SREM #{key} #{member.to_s.size}\r\n#{member}\r\n" - case integer_reply - when 1 - true - when 0 - false - when -2 - raise RedisError, "key: #{key} contains a non set value" - end - } + write "SREM #{key} #{member.to_s.size}\r\n#{member}\r\n" + case get_response + when 1 + true + when 0 + false + when -2 + raise RedisError, "key: #{key} contains a non set value" + end end - - # SCARD key - # Time complexity O(1) - # Return the set cardinality (number of elements). If the key does not - # exist 0 is returned, like for empty sets. If the key does not hold a - # set value -1 is returned. Client libraries should raise an error when -1 - # is returned instead to pass the value to the caller. - # - # *Return value: integer reply, specifically: - # - # the cardinality (number of elements) of the set as an integer - # >= - # 0 if the operation succeeded -2 if the specified key does not hold a set value + def set_count(key) - timeout_retry(3, 3){ - write "SCARD #{key}\r\n" - case i = integer_reply - when -2 - raise RedisError, "key: #{key} contains a non set value" - else - i - end - } + write "SCARD #{key}\r\n" + case i = get_response + when -2 + raise RedisError, "key: #{key} contains a non set value" + else + i + end end - - # SISMEMBER key member - # - # Time complexity O(1) - # Return 1 if member is a member of the set stored at key, otherwise 0 is - # returned. On error a negative value is returned. Client libraries should - # raise an error when a negative value is returned instead to pass the value - # to the caller. - # - # *Return value: integer reply, specifically: - # - # 1 if the element is a member of the set 0 if the element is not a member of - # the set OR if the key does not exist -2 if the key does not hold a set value + def set_member?(key, member) - timeout_retry(3, 3){ - write "SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n" - case integer_reply - when 1 - true - when 0 - false - when -2 - raise RedisError, "key: #{key} contains a non set value" - end - } + write "SISMEMBER #{key} #{member.to_s.size}\r\n#{member}\r\n" + case get_response + when 1 + true + when 0 + false + when -2 + raise RedisError, "key: #{key} contains a non set value" + end end - - # SINTER key1 key2 ... keyN - # Time complexity O(N*M) worst case where N is the cardinality of the smallest - # set and M the number of sets - # Return the members of a set resulting from the intersection of all the sets - # hold at the specified keys. Like in LRANGE the result is sent to the client - # as a multi-bulk reply (see the protocol specification for more information). - # If just a single key is specified, then this command produces the same - # result as SELEMENTS. Actually SELEMENTS is just syntax sugar for SINTERSECT. - # - # If at least one of the specified keys does not exist or does not hold a set - # value an error is returned. - # - # Return value: multi bulk reply + + def set_members(key) + write "SMEMBERS #{key}\r\n" + Set.new(get_response) + end + def set_intersect(*keys) - timeout_retry(3, 3){ - write "SINTER #{keys.join(' ')}\r\n" - Set.new(multi_bulk_reply) - } + write "SINTER #{keys.join(' ')}\r\n" + Set.new(get_response) end - - # SINTERSTORE dstkey key1 key2 ... keyN - # - # Time complexity O(N*M) worst case where N is the cardinality of the smallest set and M the number of sets - # This commnad works exactly like SINTER but instead of being returned the resulting set is sotred as dstkey. - # - # Return value: status code reply + def set_inter_store(destkey, *keys) - timeout_retry(3, 3){ - write "SINTERSTORE #{destkey} #{keys.join(' ')}\r\n" - status_code_reply - } + write "SINTERSTORE #{destkey} #{keys.join(' ')}\r\n" + get_response end - - # SMEMBERS key - # - # Time complexity O(N) - # Return all the members (elements) of the set value stored at key. - # This is just syntax glue for SINTERSECT. - def set_members(key) - timeout_retry(3, 3){ - write "SMEMBERS #{key}\r\n" - Set.new(multi_bulk_reply) - } - end - - - # SORT key [BY pattern] [GET|DEL|INCR|DECR pattern] [ASC|DESC] [LIMIT start count] - # Sort the elements contained in the List or Set value at key. By default sorting is - # numeric with elements being compared as double precision floating point numbers. - # This is the simplest form of SORT. - # SORT mylist - # - # Assuming mylist contains a list of numbers, the return value will be the list of - # numbers ordered from the smallest to the bigger number. In order to get the sorting - # in reverse order use DESC: - # SORT mylist DESC - # - # ASC is also supported but it's the default so you don't really need it. If you - # want to sort lexicographically use ALPHA. Note that Redis is utf-8 aware - # assuming you set the right value for the LC_COLLATE environment variable. - # - # Sort is able to limit the number of results using the LIMIT option: - # SORT mylist LIMIT 0 10 - # In the above example SORT will return only 10 elements, starting from the first one - # (star is zero-based). Almost all the sort options can be mixed together. For example: - # SORT mylist LIMIT 0 10 ALPHA DESC - # Will sort mylist lexicographically, in descending order, returning only the first - # 10 elements. - # Sometimes you want to sort elements using external keys as weights to compare - # instead to compare the actual List or Set elements. For example the list mylist - # may contain the elements 1, 2, 3, 4, that are just the unique IDs of objects - # stored at object_1, object_2, object_3 and object_4, while the keys weight_1, - # weight_2, weight_3 and weight_4 can contain weights we want to use to sort the - # list of objects identifiers. We can use the following command: - # SORT mylist BY weight_* - # the BY option takes a pattern (weight_* in our example) that is used in order to - # generate the key names of the weights used for sorting. Weight key names are obtained - # substituting the first occurrence of * with the actual value of the elements on the - # list (1,2,3,4 in our example). - # Still our previous example will return just the sorted IDs. Often it is needed to - # get the actual objects sorted (object_1, ..., object_4 in the example). We can do - # it with the following command: - # SORT mylist BY weight_* GET object_* - # Note that GET can be used multiple times in order to get more key for every - # element of the original List or Set sorted. - - # redis.sort 'index', :by => 'weight_*', - # :order => 'DESC ALPHA', - # :limit => [0,10], - # :get => 'obj_*' + def sort(key, opts={}) cmd = "SORT #{key}" cmd << " BY #{opts[:by]}" if opts[:by] @@ -596,241 +342,105 @@ class Redis cmd << " #{opts[:order]}" if opts[:order] cmd << " LIMIT #{opts[:limit].join(' ')}" if opts[:limit] cmd << "\r\n" - write cmd - multi_bulk_reply + write(cmd) + get_response end - - # ADMIN functions for redis - - # SELECT index - # - # Select the DB with having the specified zero-based numeric index. - # For default every new client connection is automatically selected to DB 0. - # Return value: status code reply - def select_db(index) - timeout_retry(3, 3){ - write "SELECT #{index}\r\n" - status_code_reply - } - end - - # MOVE key dbindex - # - # Move the specified key from the currently selected DB to the specified - # destination DB. Note that this command returns 1 only if the key was - # successfully moved, and 0 if the target key was already there or if - # the source key was not found at all, so it is possible to use MOVE - # as a locking primitive. - # - # *Return value: integer reply, specifically: - # - # 1 if the key was moved 0 if the key was not moved because already - # present on the target DB or was not found in the current DB. -3 - # if the destination DB is the same as the source DB -4 if the database - # index if out of range - def move(key, index) - timeout_retry(3, 3){ - write "MOVE #{index}\r\n" - case integer_reply - when 1 - true - when 0 - false - when -3 - raise RedisError, "destination db same as source db" - when -4 - raise RedisError, "db index if out of range" - end - } - end - - # SAVE - # - # Save the DB on disk. The server hangs while the saving is not completed, - # no connection is served in the meanwhile. An OK code is returned when - # the DB was fully stored in disk. - # Return value: status code reply - def save - timeout_retry(3, 3){ - write "SAVE\r\n" - status_code_reply - } - end - - # BGSAVE - # - # Save the DB in background. The OK code is immediately returned. Redis - # forks, the parent continues to server the clients, the child saves - # the DB on disk then exit. A client my be able to check if the operation - # succeeded using the LASTSAVE command. - # Return value: status code reply - def bgsave - timeout_retry(3, 3){ - write "BGSAVE\r\n" - status_code_reply - } - end - - # LASTSAVE - # - # Return the UNIX TIME of the last DB save executed with success. A client - # may check if a BGSAVE command succeeded reading the LASTSAVE value, then - # issuing a BGSAVE command and checking at regular intervals every N seconds - # if LASTSAVE changed. - # - # Return value: integer reply (UNIX timestamp) - def lastsave - timeout_retry(3, 3){ - write "LASTSAVE\r\n" - integer_reply - } - end - - def quit - timeout_retry(3, 3){ - write "QUIT\r\n" - status_code_reply - } - end - - private - - def redis_unmarshal(obj) - if obj[0] == 4 - Marshal.load(obj) - else - obj + + def multi_bulk + res = read_proto + puts "mb res is #{res.inspect}" if $debug + list = [] + Integer(res).times do + vf = get_response + puts "curren vf is #{vf.inspect}" if $debug + list << vf + puts "current list is #{list.inspect}" if $debug end - end - - def redis_marshal(obj) - case obj - when String, Integer - obj - else - Marshal.dump(obj) + list + end + + def get_reply + begin + r = read(1) + raise RedisError if (r == "\r" || r == "\n") + rescue RedisError + retry end + r end - - def close - socket.close unless socket.closed? - end - - def timeout_retry(time, retries, &block) - timeout(time, &block) - rescue TimeoutError - retries -= 1 - retry unless retries < 0 - end - - def socket - connect if (!@socket or @socket.closed?) - @socket + + def []=(key, val) + set(key,val) end - def connect - @socket = TCPSocket.new(@opts[:host], @opts[:port]) - @socket.sync = true - @socket + + def set(key, val, expiry=nil) + write("SET #{key} #{val.to_s.size}\r\n#{val}\r\n") + get_response == OK end + + def set_unless_exists(key, val) + write "SETNX #{key} #{val.to_s.size}\r\n#{val}\r\n" + get_response == 1 + end - def read(length, nodebug=true) - retries = 3 - res = socket.read(length) - puts "read: #{res}" if @opts[:debug] && nodebug - res - rescue - retries -= 1 - if retries > 0 - connect - retry + def status_code_reply + begin + res = read_proto + if res.index('-') == 0 + raise RedisError, res + else + true + end + rescue RedisError + raise RedisError end end - def write(data) - puts "write: #{data}" if @opts[:debug] - retries = 3 - socket.write(data) - rescue - retries -= 1 - if retries > 0 - connect - retry + def get_response + begin + rtype = get_reply + rescue => e + raise RedisError, e.inspect + end + puts "reply_type is #{rtype.inspect}" if $debug + case rtype + when SINGLE + single_line + when BULK + bulk_reply + when MULTI + multi_bulk + when INT + integer_reply + when ERR + raise RedisError, single_line + else + raise RedisError, "Unknown response.." end end - def nibble_end - read(2) + def integer_reply + Integer(read_proto) end - def read_proto - print "read proto: " if @opts[:debug] + def single_line buff = "" - while (char = read(1, false)) - print char if @opts[:debug] - buff << char - break if buff[-2..-1] == CTRLF + while buff[-2..-1] != "\r\n" + buff << read(1) end - puts if @opts[:debug] + puts "single_line value is #{buff[0..-3].inspect}" if $debug buff[0..-3] end - - def status_code_reply - res = read_proto - if res.index(ERRCODE) == 0 - raise RedisError, res - else - true - end - end - - def bulk_reply - res = read_proto - if res.index(ERRCODE) == 0 - err = read(res.to_i.abs) - nibble_end - raise RedisError, err - elsif res != NIL - val = read(res.to_i.abs) - nibble_end - val - else - nil - end - end - - - def multi_bulk_reply - res = read_proto - if res.index(ERRCODE) == 0 - err = read(res.to_i.abs) - nibble_end - raise RedisError, err - elsif res == NIL - nil - else - items = Integer(res) - list = [] - items.times do - len = Integer(read_proto) - if len == -1 - nil - else - list << read(len) - end - nibble_end - end - list + def read_proto + with_socket_management(@server) do |socket| + if res = socket.gets + x = res.chomp + puts "read_proto is #{x.inspect}\n\n" if $debug + x.to_i + end end end - def single_line_reply - read_proto - end - - def integer_reply - Integer(read_proto) - end - -end +end \ No newline at end of file diff --git a/client-libraries/ruby/lib/server.rb b/client-libraries/ruby/lib/server.rb new file mode 100644 index 00000000..a52fbcbf --- /dev/null +++ b/client-libraries/ruby/lib/server.rb @@ -0,0 +1,131 @@ +## +# This class represents a redis server instance. + +class Server + + ## + # The amount of time to wait before attempting to re-establish a + # connection with a server that is marked dead. + + RETRY_DELAY = 30.0 + + ## + # The host the redis server is running on. + + attr_reader :host + + ## + # The port the redis server is listening on. + + attr_reader :port + + ## + # + + attr_reader :replica + + ## + # The time of next retry if the connection is dead. + + attr_reader :retry + + ## + # A text status string describing the state of the server. + + attr_reader :status + + ## + # Create a new Redis::Server object for the redis instance + # listening on the given host and port. + + def initialize(host, port = DEFAULT_PORT) + raise ArgumentError, "No host specified" if host.nil? or host.empty? + raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero? + + @host = host + @port = port.to_i + + @sock = nil + @retry = nil + @status = 'NOT CONNECTED' + @timeout = 1 + end + + ## + # Return a string representation of the server object. + def inspect + "" % [@host, @port, @status] + end + + ## + # Try to connect to the redis server targeted by this object. + # Returns the connected socket object on success or nil on failure. + + def socket + return @sock if @sock and not @sock.closed? + + @sock = nil + + # If the host was dead, don't retry for a while. + return if @retry and @retry > Time.now + + # Attempt to connect if not already connected. + begin + @sock = connect_to(@host, @port, @timeout) + @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 + @retry = nil + @status = 'CONNECTED' + rescue Errno::EPIPE, Errno::ECONNREFUSED => e + puts "Socket died... socket: #{@sock.inspect}\n" if $debug + @sock.close + retry + rescue SocketError, SystemCallError, IOError => err + puts "Unable to open socket: #{err.class.name}, #{err.message}" if $debug + mark_dead err + end + + return @sock + end + + def connect_to(host, port, timeout=nil) + addrs = Socket.getaddrinfo('localhost', nil) + addr = addrs.detect { |ad| ad[0] == 'AF_INET' } + sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) + #addr = Socket.getaddrinfo(host, nil) + #sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0) + + if timeout + secs = Integer(timeout) + usecs = Integer((timeout - secs) * 1_000_000) + optval = [secs, usecs].pack("l_2") + sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval + sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval + end + sock.connect(Socket.pack_sockaddr_in('6379', addr[3])) + sock + end + + ## + # Close the connection to the redis server targeted by this + # object. The server is not considered dead. + + def close + @sock.close if @sock && !@sock.closed? + @sock = nil + @retry = nil + @status = "NOT CONNECTED" + end + + ## + # Mark the server as dead and close its socket. + def mark_dead(error) + @sock.close if @sock && !@sock.closed? + @sock = nil + @retry = Time.now #+ RETRY_DELAY + + reason = "#{error.class.name}: #{error.message}" + @status = sprintf "%s:%s DEAD (%s), will retry at %s", @host, @port, reason, @retry + puts @status + end + +end diff --git a/client-libraries/ruby/profile.rb b/client-libraries/ruby/profile.rb new file mode 100644 index 00000000..e5dc8c03 --- /dev/null +++ b/client-libraries/ruby/profile.rb @@ -0,0 +1,22 @@ +require 'rubygems' +require 'ruby-prof' +require "#{File.dirname(__FILE__)}/lib/redis" + + +mode = ARGV.shift || 'process_time' +n = (ARGV.shift || 200).to_i + +r = Redis.new +RubyProf.measure_mode = RubyProf.const_get(mode.upcase) +RubyProf.start + +n.times do |i| + key = "foo#{i}" + r[key] = key * 10 + r[key] +end + +results = RubyProf.stop +File.open("profile.#{mode}", 'w') do |out| + RubyProf::CallTreePrinter.new(results).print(out) +end diff --git a/client-libraries/ruby/spec/redis_spec.rb b/client-libraries/ruby/spec/redis_spec.rb index 88c46f79..8fc471dd 100644 --- a/client-libraries/ruby/spec/redis_spec.rb +++ b/client-libraries/ruby/spec/redis_spec.rb @@ -12,7 +12,7 @@ class Foo end describe "redis" do - before do + before(:each) do @r = Redis.new @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data @r['foo'] = 'bar' @@ -20,15 +20,9 @@ describe "redis" do after do @r.keys('*').each {|k| @r.delete k } + @r.quit end - it "should properly marshall objects" do - class MyFail; def fail; 'it will' end; end - - @r['fail'] = MyFail.new - @r['fail'].fail.should == 'it will' - - end it "should be able to GET a key" do @r['foo'].should == 'bar' @@ -45,14 +39,14 @@ describe "redis" do @r.set_unless_exists 'foo', 'bar' @r['foo'].should == 'nik' end - + # it "should be able to INCR(increment) a key" do @r.delete('counter') @r.incr('counter').should == 1 @r.incr('counter').should == 2 @r.incr('counter').should == 3 end - + # it "should be able to DECR(decrement) a key" do @r.delete('counter') @r.incr('counter').should == 1 @@ -62,11 +56,11 @@ describe "redis" do @r.decr('counter').should == 1 @r.decr('counter').should == 0 end - + # it "should be able to RANDKEY(return a random key)" do @r.randkey.should_not be_nil end - + # it "should be able to RENAME a key" do @r.delete 'foo' @r.delete 'bar' @@ -74,23 +68,23 @@ describe "redis" do @r.rename! 'foo', 'bar' @r['bar'].should == 'hi' end - + # it "should be able to RENAMENX(rename unless the new key already exists) a key" do @r.delete 'foo' @r.delete 'bar' @r['foo'] = 'hi' @r['bar'] = 'ohai' - lambda {@r.rename 'foo', 'bar'}.should raise_error(RedisError) + lambda {@r.rename 'foo', 'bar'}.should raise_error(RedisRenameError) @r['bar'].should == 'ohai' end - + # it "should be able to EXISTS(check if key exists)" do @r['foo'] = 'nik' @r.key?('foo').should be_true @r.delete 'foo' @r.key?('foo').should be_false end - + # it "should be able to KEYS(glob for keys)" do @r.keys("f*").each do |key| @r.delete key @@ -100,14 +94,14 @@ describe "redis" do @r['foo'] = 'qux' @r.keys("f*").sort.should == ['f','fo', 'foo'].sort end - + # it "should be able to check the TYPE of a key" do @r['foo'] = 'nik' @r.type?('foo').should == "string" @r.delete 'foo' @r.type?('foo').should == "none" end - + # it "should be able to push to the head of a list" do @r.push_head "list", 'hello' @r.push_head "list", 42 @@ -116,14 +110,14 @@ describe "redis" do @r.pop_head('list').should == '42' @r.delete('list') end - + # it "should be able to push to the tail of a list" do @r.push_tail "list", 'hello' @r.type?('list').should == "list" @r.list_length('list').should == 1 @r.delete('list') end - + # it "should be able to pop the tail of a list" do @r.push_tail "list", 'hello' @r.push_tail "list", 'goodbye' @@ -132,7 +126,7 @@ describe "redis" do @r.pop_tail('list').should == 'goodbye' @r.delete('list') end - + # it "should be able to pop the head of a list" do @r.push_tail "list", 'hello' @r.push_tail "list", 'goodbye' @@ -141,7 +135,7 @@ describe "redis" do @r.pop_head('list').should == 'hello' @r.delete('list') end - + # it "should be able to get the length of a list" do @r.push_tail "list", 'hello' @r.push_tail "list", 'goodbye' @@ -149,7 +143,7 @@ describe "redis" do @r.list_length('list').should == 2 @r.delete('list') end - + # it "should be able to get a range of values from a list" do @r.push_tail "list", 'hello' @r.push_tail "list", 'goodbye' @@ -161,7 +155,7 @@ describe "redis" do @r.list_range('list', 2, -1).should == ['1', '2', '3'] @r.delete('list') end - + # it "should be able to trim a list" do @r.push_tail "list", 'hello' @r.push_tail "list", 'goodbye' @@ -175,7 +169,7 @@ describe "redis" do @r.list_range('list', 0, -1).should == ['hello', 'goodbye'] @r.delete('list') end - + # it "should be able to get a value by indexing into a list" do @r.push_tail "list", 'hello' @r.push_tail "list", 'goodbye' @@ -184,7 +178,7 @@ describe "redis" do @r.list_index('list', 1).should == 'goodbye' @r.delete('list') end - + # it "should be able to set a value by indexing into a list" do @r.push_tail "list", 'hello' @r.push_tail "list", 'hello' @@ -194,7 +188,17 @@ describe "redis" do @r.list_index('list', 1).should == 'goodbye' @r.delete('list') end - + # + it "should be able to remove values from a list LREM" do + @r.push_tail "list", 'hello' + @r.push_tail "list", 'goodbye' + @r.type?('list').should == "list" + @r.list_length('list').should == 2 + @r.list_rm('list', 1, 'hello').should == 1 + @r.list_range('list', 0, -1).should == ['goodbye'] + @r.delete('list') + end + # it "should be able add members to a set" do @r.set_add "set", 'key1' @r.set_add "set", 'key2' @@ -203,7 +207,7 @@ describe "redis" do @r.set_members('set').sort.should == ['key1', 'key2'].sort @r.delete('set') end - + # it "should be able delete members to a set" do @r.set_add "set", 'key1' @r.set_add "set", 'key2' @@ -215,7 +219,7 @@ describe "redis" do @r.set_members('set').should == Set.new(['key2']) @r.delete('set') end - + # it "should be able count the members of a set" do @r.set_add "set", 'key1' @r.set_add "set", 'key2' @@ -223,7 +227,7 @@ describe "redis" do @r.set_count('set').should == 2 @r.delete('set') end - + # it "should be able test for set membership" do @r.set_add "set", 'key1' @r.set_add "set", 'key2' @@ -234,7 +238,7 @@ describe "redis" do @r.set_member?('set', 'notthere').should be_false @r.delete('set') end - + # it "should be able to do set intersection" do @r.set_add "set", 'key1' @r.set_add "set", 'key2' @@ -242,7 +246,7 @@ describe "redis" do @r.set_intersect('set', 'set2').should == Set.new(['key2']) @r.delete('set') end - + # it "should be able to do set intersection and store the results in a key" do @r.set_add "set", 'key1' @r.set_add "set", 'key2' @@ -251,7 +255,7 @@ describe "redis" do @r.set_members('newone').should == Set.new(['key2']) @r.delete('set') end - + # it "should be able to do crazy SORT queries" do @r['dog_1'] = 'louie' @r.push_tail 'dogs', 1 @@ -264,4 +268,50 @@ describe "redis" do @r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should == ['louie'] @r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should == ['taj'] end + # + it "should provide info" do + [:last_save_time, :redis_version, :total_connections_received, :connected_clients, :total_commands_processed, :connected_slaves, :uptime_in_seconds, :used_memory, :uptime_in_days, :changes_since_last_save].each do |x| + @r.info.keys.should include(x) + end + end + # + it "should be able to flush the database" do + @r['key1'] = 'keyone' + @r['key2'] = 'keytwo' + @r.keys('*').sort.should == ['foo', 'key1', 'key2'] #foo from before + @r.flush_db + @r.keys('*').should == [] + end + # + it "should be able to provide the last save time" do + savetime = @r.last_save + Time.at(savetime).class.should == Time + Time.at(savetime).should <= Time.now + end + + it "should be able to MGET keys" do + @r['foo'] = 1000 + @r['bar'] = 2000 + @r.mget('foo', 'bar').should == ['1000', '2000'] + @r.mget('foo', 'bar', 'baz').should == ['1000', '2000', nil] + end + + it "should bgsave" do + lambda {@r.bgsave}.should_not raise_error(RedisError) + end + + it "should handle multiple servers" do + require 'dist_redis' + @r = DistRedis.new('localhost:6379', '127.0.0.1:6379') + @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data + + 100.times do |idx| + @r[idx] = "foo#{idx}" + end + + 100.times do |idx| + @r[idx].should == "foo#{idx}" + end + end + end \ No newline at end of file diff --git a/client-libraries/ruby/speed.rb b/client-libraries/ruby/speed.rb new file mode 100644 index 00000000..51e58110 --- /dev/null +++ b/client-libraries/ruby/speed.rb @@ -0,0 +1,16 @@ +require 'benchmark' +require "#{File.dirname(__FILE__)}/lib/redis" + +r = Redis.new +n = (ARGV.shift || 20000).to_i + +elapsed = Benchmark.realtime do + # n sets, n gets + n.times do |i| + key = "foo#{i}" + r[key] = key * 10 + r[key] + end +end + +puts '%.2f Kops' % (2 * n / 1000 / elapsed) diff --git a/client-libraries/ruby/tasks/redis.tasks.rb b/client-libraries/ruby/tasks/redis.tasks.rb index 67b9499f..b22c70e9 100644 --- a/client-libraries/ruby/tasks/redis.tasks.rb +++ b/client-libraries/ruby/tasks/redis.tasks.rb @@ -52,6 +52,12 @@ namespace :redis do task :stop do RedisRunner.stop end + + desc 'Restart redis' + task :restart do + RedisRunner.stop + RedisRunner.start + end desc 'Attach to redis dtach socket' task :attach do @@ -60,9 +66,12 @@ namespace :redis do desc 'Install the lastest redis from svn' task :install => [:about, :download, :make] do - sh 'sudo cp /tmp/redis/redis-server /usr/bin/' - sh 'sudo cp /tmp/redis/redis-benchmark /usr/bin/' - puts 'Installed redis-server and redis-benchmark to /usr/bin/' + %w(redis-benchmark redis-cli redis-server).each do |bin| + sh "sudo cp /tmp/redis/#{bin} /usr/bin/" + end + + puts "Installed redis-benchmark, redis-cli and redis-server to /usr/bin/" + unless File.exists?('/etc/redis.conf') sh 'sudo cp /tmp/redis/redis.conf /etc/' puts "Installed redis.conf to /etc/ \n You should look at this file!" @@ -76,8 +85,9 @@ namespace :redis do desc "Download package" task :download do - system 'svn checkout http://redis.googlecode.com/svn/trunk /tmp/redis' unless File.exists?(RedisRunner.redisdir) - system 'svn up' if File.exists?("#{RedisRunner.redisdir}/.svn") + sh 'rm -rf /tmp/redis/' if File.exists?("#{RedisRunner.redisdir}/.svn") + sh 'git clone git://github.com/antirez/redis.git /tmp/redis' unless File.exists?(RedisRunner.redisdir) + sh "cd #{RedisRunner.redisdir} && git pull" if File.exists?("#{RedisRunner.redisdir}/.git") end end -- 2.45.2