Skip to content

Commit 5bb0b2f

Browse files
committed
Merge pull request redis#453 from pepijndevos/master
Implement HyperLogLog commands
2 parents 5633e55 + 2b51217 commit 5bb0b2f

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

redis/client.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ class StrictRedis(object):
276276
),
277277
string_keys_to_dict(
278278
'BITCOUNT DECRBY DEL GETBIT HDEL HLEN INCRBY LINSERT LLEN LPUSHX '
279-
'RPUSHX SADD SCARD SDIFFSTORE SETBIT SETRANGE SINTERSTORE SREM '
280-
'STRLEN SUNIONSTORE ZADD ZCARD ZREM ZREMRANGEBYRANK '
281-
'ZREMRANGEBYSCORE',
279+
'PFADD PFCOUNT RPUSHX SADD SCARD SDIFFSTORE SETBIT SETRANGE '
280+
'SINTERSTORE SREM STRLEN SUNIONSTORE ZADD ZCARD ZREM '
281+
'ZREMRANGEBYRANK ZREMRANGEBYSCORE',
282282
int
283283
),
284284
string_keys_to_dict('INCRBYFLOAT HINCRBYFLOAT', float),
@@ -290,7 +290,7 @@ class StrictRedis(object):
290290
string_keys_to_dict('SORT', sort_return_tuples),
291291
string_keys_to_dict('ZSCORE ZINCRBY', float_or_none),
292292
string_keys_to_dict(
293-
'FLUSHALL FLUSHDB LSET LTRIM MSET RENAME '
293+
'FLUSHALL FLUSHDB LSET LTRIM MSET PFMERGE RENAME '
294294
'SAVE SELECT SHUTDOWN SLAVEOF WATCH UNWATCH',
295295
lambda r: nativestr(r) == 'OK'
296296
),
@@ -1527,6 +1527,22 @@ def _zaggregate(self, command, dest, keys, aggregate=None):
15271527
pieces.append(aggregate)
15281528
return self.execute_command(*pieces)
15291529

1530+
# HYPERLOGLOG COMMANDS
1531+
def pfadd(self, name, *values):
1532+
"Adds the specified elements to the specified HyperLogLog."
1533+
return self.execute_command('PFADD', name, *values)
1534+
1535+
def pfcount(self, name):
1536+
"""
1537+
Return the approximated cardinality of
1538+
the set observed by the HyperLogLog at key.
1539+
"""
1540+
return self.execute_command('PFCOUNT', name)
1541+
1542+
def pfmerge(self, dest, *sources):
1543+
"Merge N different HyperLogLogs into a single one."
1544+
return self.execute_command('PFMERGE', dest, *sources)
1545+
15301546
# HASH COMMANDS
15311547
def hdel(self, name, *keys):
15321548
"Delete ``keys`` from hash ``name``"

tests/test_commands.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,33 @@ def test_zunionstore_with_weight(self, r):
951951
assert r.zrange('d', 0, -1, withscores=True) == \
952952
[(b('a2'), 5), (b('a4'), 12), (b('a3'), 20), (b('a1'), 23)]
953953

954+
# HYPERLOGLOG TESTS
955+
@skip_if_server_version_lt('2.8.9')
956+
def test_pfadd(self, r):
957+
members = set([b('1'), b('2'), b('3')])
958+
assert r.pfadd('a', *members) == 1
959+
assert r.pfadd('a', *members) == 0
960+
assert r.pfcount('a') == len(members)
961+
962+
@skip_if_server_version_lt('2.8.9')
963+
def test_pfcount(self, r):
964+
members = set([b('1'), b('2'), b('3')])
965+
r.pfadd('a', *members)
966+
assert r.pfcount('a') == len(members)
967+
968+
@skip_if_server_version_lt('2.8.9')
969+
def test_pfmerge(self, r):
970+
mema = set([b('1'), b('2'), b('3')])
971+
memb = set([b('2'), b('3'), b('4')])
972+
memc = set([b('5'), b('6'), b('7')])
973+
r.pfadd('a', *mema)
974+
r.pfadd('b', *memb)
975+
r.pfadd('c', *memc)
976+
r.pfmerge('d', 'c', 'a')
977+
assert r.pfcount('d') == 6
978+
r.pfmerge('d', 'b')
979+
assert r.pfcount('d') == 7
980+
954981
# HASH COMMANDS
955982
def test_hget_and_hset(self, r):
956983
r.hmset('a', {'1': 1, '2': 2, '3': 3})

0 commit comments

Comments
 (0)