Skip to content
Browse files

Merge branch 'defunkt_master'

  • Loading branch information...
2 parents 22dab02 + 7852b8c commit 0b6adc4a1033aa4d7e5dfbcd86aed7da1e284393 @defunkt defunkt committed Sep 21, 2009
Showing with 64 additions and 18 deletions.
  1. +12 −2 lib/dist_redis.rb
  2. +23 −13 lib/redis.rb
  3. +2 −2 redis-rb.gemspec
  4. +27 −1 spec/redis_spec.rb
View
14 lib/dist_redis.rb
@@ -36,10 +36,20 @@ def method_missing(sym, *args, &blk)
end
end
+ # Ruby defines a now deprecated type method so we need to override it here
+ # since it will never hit method_missing
+ def type(key)
+ if redis = node_for_key(key)
+ redis.send :type, key
+ end
+ end
+
def keys(glob)
- @ring.nodes.map do |red|
- red.keys(glob)
+ keyz = []
+ @ring.nodes.each do |red|
+ keyz.concat red.keys(glob)
end
+ keyz
end
def save
View
36 lib/redis.rb
@@ -101,16 +101,16 @@ class Redis
}
def initialize(options = {})
- @host = options[:host] || '127.0.0.1'
- @port = (options[:port] || 6379).to_i
- @db = (options[:db] || 0).to_i
- @timeout = (options[:timeout] || 5).to_i
- @password = options[:password]
- @logger = options[:logger]
+ @host = options[:host] || '127.0.0.1'
+ @port = (options[:port] || 6379).to_i
+ @db = (options[:db] || 0).to_i
+ @timeout = (options[:timeout] || 5).to_i
+ @password = options[:password]
+ @logger = options[:logger]
+ @namespace = options[:namespace]
@thread_safe = options[:thread_safe]
@logger.info { self.to_s } if @logger
- connect_to_server
end
def to_s
@@ -160,7 +160,7 @@ def method_missing(*argv)
call_command(argv)
end
- def call_command(argv)
+ def call_command(argv, use_namespace = true)
@logger.debug { argv.inspect } if @logger
# this wrapper to raw_call_command handle reconnection on socket
@@ -169,7 +169,7 @@ def call_command(argv)
connect_to_server if !@sock
begin
- raw_call_command(argv.dup)
+ raw_call_command(argv.dup, use_namespace)
rescue Errno::ECONNRESET, Errno::EPIPE
@sock.close
@sock = nil
@@ -178,7 +178,7 @@ def call_command(argv)
end
end
- def raw_call_command(argvp)
+ def raw_call_command(argvp, use_namespace = true)
pipeline = argvp[0].is_a?(Array)
unless pipeline
@@ -198,10 +198,15 @@ def raw_call_command(argvp)
bulk = argv[-1].to_s
argv[-1] = bulk.respond_to?(:bytesize) ? bulk.bytesize : bulk.size
end
+
+ if @namespace && argv[1] && use_namespace
+ argv[1] = "#{@namespace}:#{argv[1]}"
+ end
+
command << "#{argv.join(' ')}\r\n"
command << "#{bulk}\r\n" if bulk
end
-
+
results = if @thread_safe
with_mutex { process_command(command, argvv) }
else
@@ -210,15 +215,15 @@ def raw_call_command(argvp)
return pipeline ? results : results[0]
end
-
+
def process_command(command, argvv)
@sock.write(command)
argvv.map do |argv|
processor = REPLY_PROCESSOR[argv[0]]
processor ? processor.call(read_reply) : read_reply
end
end
-
+
def with_mutex(&block)
@mutex ||= Mutex.new
@mutex.synchronize &block
@@ -271,6 +276,11 @@ def mapped_mget(*keys)
result
end
+ def mget(*keys)
+ keys = keys.map { |key| "#{@namespace}:#{key}"} if @namespace
+ call_command([:mget] + keys, false)
+ end
+
# Ruby defines a now deprecated type method so we need to override it here
# since it will never hit method_missing
def type(key)
View
4 redis-rb.gemspec
@@ -2,12 +2,12 @@
Gem::Specification.new do |s|
s.name = %q{redis}
- s.version = "0.1"
+ s.version = "0.2.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ezra Zygmuntowicz", "Taylor Weibley", "Matthew Clark", "Brian McKinney", "Salvatore Sanfilippo", "Luca Guidi"]
# s.autorequire = %q{redis-rb}
- s.date = %q{2009-06-23}
+ s.date = %q{2009-09-21}
s.description = %q{Ruby client library for redis key value storage server}
s.email = %q{ez@engineyard.com}
s.extra_rdoc_files = ["LICENSE"]
View
28 spec/redis_spec.rb
@@ -30,7 +30,7 @@ def ==(other)
end
after(:each) do
- @r.keys('*').each {|k| @r.del k}
+ @r.flushdb
end
after(:all) do
@@ -466,6 +466,8 @@ def ==(other)
100.times do |idx|
@r[idx].should == "foo#{idx}"
end
+
+ @r.keys('*').sort.uniq.should == ('0'...'100').to_a.sort + ['foo']
end
it "should be able to pipeline writes" do
@@ -486,4 +488,28 @@ def ==(other)
r.connect_to_server
end
+ it "should be able to use a namespace" do
+ r = Redis.new(:namespace => :ns, :db => 15)
+ r.flushdb
+
+ r['foo'].should == nil
+ r['foo'] = 'chris'
+ r['foo'].should == 'chris'
+ @r['foo'] = 'bob'
+ @r['foo'].should == 'bob'
+
+ r.incr('counter', 2)
+ r['counter'].to_i.should == 2
+ @r['counter'].should == nil
+ end
+
+ it "should be able to use a namespace with mget" do
+ r = Redis.new(:namespace => :ns, :db => 15)
+
+ r['foo'] = 1000
+ r['bar'] = 2000
+ r.mapped_mget('foo', 'bar').should == { 'foo' => '1000', 'bar' => '2000' }
+ r.mapped_mget('foo', 'baz', 'bar').should == { 'foo' => '1000', 'bar' => '2000' }
+ end
+
end

0 comments on commit 0b6adc4

Please sign in to comment.
Something went wrong with that request. Please try again.