Skip to content

Commit 8f33f8a

Browse files
author
Ariel Shtul
authored
Add TOPK.LIST WITHCOUNT param (#36)
* Add TOPK.LIST WITHCOUNT param * docs * dox fix after PR 354 on RedisBloom * clean
1 parent 3462665 commit 8f33f8a

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ rb.topkAdd('topk', 'A', 'B', 'C', 'D', 'E', 'A', 'A', 'B',
5454
'C', 'G', 'D', 'B', 'D', 'A', 'E', 'E')
5555
rb.topkQuery('topk', 'A', 'B', 'C', 'D') # returns [1, 1, 0, 1]
5656
rb.topkCount('topk', 'A', 'B', 'C', 'D') # returns [4, 3, 2, 3]
57-
rb.topkList('topk') # returns ['D', 'A', 'B']
57+
rb.topkList('topk') # returns ['A', 'B', 'E']
58+
rb.topkListWithCount('topk') # returns ['A', 4, 'B', 3, 'E', 3]
5859
```
5960

6061
### API

redisbloom/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ def spaceHolder(response):
9898
def parseToList(response):
9999
res = []
100100
for item in response:
101-
if item is not None:
101+
if isinstance(item, int):
102+
res.append(item)
103+
elif item is not None:
102104
res.append(nativestr(item))
103105
else:
104106
res.append(None)
@@ -597,6 +599,13 @@ def topkList(self, key):
597599

598600
return self.execute_command(self.TOPK_LIST, key)
599601

602+
def topkListWithCount(self, key):
603+
"""
604+
Return full list of items with probabilistic count in Top-K list of ``key```.
605+
"""
606+
607+
return self.execute_command(self.TOPK_LIST, key, 'WITHCOUNT')
608+
600609
def topkInfo(self, key):
601610
"""
602611
Returns k, width, depth and decay values of ``key``.

test_commands.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ def testCMSMerge(self):
187187
def testTopK(self):
188188
# test list with empty buckets
189189
self.assertTrue(rb.topkReserve('topk', 3, 50, 4, 0.9))
190-
self.assertEqual([None, None, None, None, None, None, None, None,
191-
None, None, None, None, 'C', None, None, None, None],
190+
self.assertEqual([None, None, None, 'A', 'C', 'D', None, None, 'E',
191+
None, 'B', 'C', None, None, None, 'D', None],
192192
rb.topkAdd('topk', 'A', 'B', 'C', 'D', 'E', 'A', 'A', 'B', 'C',
193193
'G', 'D', 'B', 'D', 'A', 'E', 'E', 1))
194-
self.assertEqual([1, 1, 0, 1, 0, 0, 0],
194+
self.assertEqual([1, 1, 0, 0, 1, 0, 0],
195195
rb.topkQuery('topk', 'A', 'B', 'C', 'D', 'E', 'F', 'G'))
196196
self.assertEqual([4, 3, 2, 3, 3, 0, 1],
197197
rb.topkCount('topk', 'A', 'B', 'C', 'D', 'E', 'F', 'G'))
@@ -200,7 +200,8 @@ def testTopK(self):
200200
self.assertTrue(rb.topkReserve('topklist', 3, 50, 3, 0.9))
201201
self.assertTrue(rb.topkAdd('topklist', 'A', 'B', 'C', 'D', 'E','A', 'A', 'B', 'C',
202202
'G', 'D', 'B', 'D', 'A', 'E', 'E'))
203-
self.assertEqual(['A', 'D', 'B'], rb.topkList('topklist'))
203+
self.assertEqual(['A', 'B', 'E'], rb.topkList('topklist'))
204+
self.assertEqual(['A', 4, 'B', 3, 'E', 3], rb.topkListWithCount('topklist'))
204205
info = rb.topkInfo('topklist')
205206
self.assertEqual(3, info.k)
206207
self.assertEqual(50, info.width)

0 commit comments

Comments
 (0)