Skip to content

Commit 8add9cc

Browse files
committed
Allow multiple arguments for pfcount
It now supports both an array of keys and multiple arguments. For distributed access, it will now also ensure that all keys are on the same node. The tests were changed to do correctly work both for both scenarios (keys were extended to use tags, so they land on the same node) Additionally, a test for this behaviour was added. Closes #450.
1 parent b8bfb62 commit 8add9cc

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

lib/redis.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,9 +2483,9 @@ def pfadd(key, member)
24832483
#
24842484
# @param [String] key
24852485
# @return [Fixnum]
2486-
def pfcount(key)
2486+
def pfcount(*keys)
24872487
synchronize do |client|
2488-
client.call([:pfcount, key])
2488+
client.call([:pfcount] + keys.flatten(1))
24892489
end
24902490
end
24912491

lib/redis/distributed.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,10 @@ def pfadd(key, member)
792792
end
793793

794794
# Get the approximate cardinality of members added to HyperLogLog structure.
795-
def pfcount(key)
796-
node_for(key).pfcount(key)
795+
def pfcount(*keys)
796+
ensure_same_node(:pfcount, keys.flatten(1)) do |node|
797+
node.pfcount(keys)
798+
end
797799
end
798800

799801
# Merge multiple HyperLogLog values into an unique value that will approximate the cardinality of the union of

test/distributed_commands_on_hyper_log_log_test.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ def test_pfmerge
1919
end
2020
end
2121

22-
end
22+
def test_pfcount_multiple_keys_diff_nodes
23+
target_version "2.8.9" do
24+
assert_raise Redis::Distributed::CannotDistribute do
25+
r.pfadd "foo", "s1"
26+
r.pfadd "bar", "s2"
27+
28+
assert r.pfcount("res", "foo", "bar")
29+
end
30+
end
31+
end
32+
33+
end

test/lint/hyper_log_log.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,28 @@ def test_pfcount
3333

3434
def test_variadic_pfcount
3535
target_version "2.8.9" do
36-
assert_equal 0, r.pfcount(["foo", "bar"])
36+
assert_equal 0, r.pfcount(["{1}foo", "{1}bar"])
3737

38-
assert_equal true, r.pfadd("foo", "s1")
39-
assert_equal true, r.pfadd("bar", "s1")
40-
assert_equal true, r.pfadd("bar", "s2")
38+
assert_equal true, r.pfadd("{1}foo", "s1")
39+
assert_equal true, r.pfadd("{1}bar", "s1")
40+
assert_equal true, r.pfadd("{1}bar", "s2")
41+
42+
assert_equal 2, r.pfcount(["{1}foo", "{1}bar"])
43+
end
44+
end
45+
46+
def test_variadic_pfcount_expanded
47+
target_version "2.8.9" do
48+
assert_equal 0, r.pfcount("{1}foo", "{1}bar")
49+
50+
assert_equal true, r.pfadd("{1}foo", "s1")
51+
assert_equal true, r.pfadd("{1}bar", "s1")
52+
assert_equal true, r.pfadd("{1}bar", "s2")
4153

42-
assert_equal 2, r.pfcount(["foo", "bar"])
54+
assert_equal 2, r.pfcount("{1}foo", "{1}bar")
4355
end
4456
end
4557

4658
end
4759

48-
end
60+
end

0 commit comments

Comments
 (0)