Skip to content

Commit

Permalink
Merge pull request resque#28 from justinweiss/zunionstore-support
Browse files Browse the repository at this point in the history
Add namespace support for the ZUNIONSTORE command.
  • Loading branch information
defunkt committed Aug 31, 2011
2 parents ef0bfe0 + fd5a691 commit fe53438
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/redis/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Namespace
# Add the namespace to all arguments but the last, e.g.
# BLPOP key1 key2 timeout =>
# BLPOP namespace:key1 namespace:key2 timeout
# :exclude_options
# Add the namespace to all arguments, except the last argument,
# if the last argument is a hash of options.
# ZUNIONSTORE key1 2 key2 key3 WEIGHTS 2 1 =>
# ZUNIONSTORE namespace:key1 2 namespace:key2 namespace:key3 WEIGHTS 2 1
# :alternate
# Add the namespace to every other argument, e.g.
# MSET key1 value1 key2 value2 =>
Expand Down Expand Up @@ -128,7 +133,7 @@ class Namespace
"zcard" => [ :first ],
"zcount" => [ :first ],
"zincrby" => [ :first ],
"zinterstore" => [ :all ],
"zinterstore" => [ :exclude_options ],
"zrange" => [ :first ],
"zrangebyscore" => [ :first ],
"zrank" => [ :first ],
Expand All @@ -139,6 +144,7 @@ class Namespace
"zrevrangebyscore" => [ :first ],
"zrevrank" => [ :first ],
"zscore" => [ :first ],
"zunionstore" => [ :exclude_options ],
"[]" => [ :first ],
"[]=" => [ :first ]
}
Expand Down Expand Up @@ -203,6 +209,14 @@ def method_missing(command, *args, &block)
last = args.pop
args = add_namespace(args)
args.push(last) if last
when :exclude_options
if args.last.is_a?(Hash)
last = args.pop
args = add_namespace(args)
args.push(last)
else
args = add_namespace(args)
end
when :alternate
args.each_with_index { |a, i| args[i] = add_namespace(a) if i.even? }
when :sort
Expand Down
20 changes: 20 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@
@namespaced.sunion('foo', 'bar').sort.should == %w( 1 2 3 4 )
end

it "should properly union two sorted sets with options" do
@namespaced.zadd('sort1', 1, 1)
@namespaced.zadd('sort1', 2, 2)
@namespaced.zadd('sort2', 2, 2)
@namespaced.zadd('sort2', 3, 3)
@namespaced.zadd('sort2', 4, 4)
@namespaced.zunionstore('union', ['sort1', 'sort2'], :weights => [2, 1])
@namespaced.zrevrange('union', 0, -1).should == %w( 2 4 3 1 )
end

it "should properly union two sorted sets without options" do
@namespaced.zadd('sort1', 1, 1)
@namespaced.zadd('sort1', 2, 2)
@namespaced.zadd('sort2', 2, 2)
@namespaced.zadd('sort2', 3, 3)
@namespaced.zadd('sort2', 4, 4)
@namespaced.zunionstore('union', ['sort1', 'sort2'])
@namespaced.zrevrange('union', 0, -1).should == %w( 4 2 3 1 )
end

it "should add namespace to sort" do
@namespaced.sadd('foo', 1)
@namespaced.sadd('foo', 2)
Expand Down

0 comments on commit fe53438

Please sign in to comment.