Skip to content

Commit 42101fc

Browse files
Integrate RedisBloom support (#1683)
Co-authored-by: Chayim I. Kirshen <[email protected]>
1 parent d425227 commit 42101fc

File tree

7 files changed

+1208
-0
lines changed

7 files changed

+1208
-0
lines changed

redis/commands/bf/__init__.py

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
from redis.client import bool_ok
2+
3+
from ..helpers import parse_to_list
4+
from .commands import * # noqa
5+
from .info import BFInfo, CFInfo, CMSInfo, TDigestInfo, TopKInfo
6+
7+
8+
class AbstractBloom(object):
9+
"""
10+
The client allows to interact with RedisBloom and use all of
11+
it's functionality.
12+
13+
- BF for Bloom Filter
14+
- CF for Cuckoo Filter
15+
- CMS for Count-Min Sketch
16+
- TOPK for TopK Data Structure
17+
- TDIGEST for estimate rank statistics
18+
"""
19+
20+
@staticmethod
21+
def appendItems(params, items):
22+
"""Append ITEMS to params."""
23+
params.extend(["ITEMS"])
24+
params += items
25+
26+
@staticmethod
27+
def appendError(params, error):
28+
"""Append ERROR to params."""
29+
if error is not None:
30+
params.extend(["ERROR", error])
31+
32+
@staticmethod
33+
def appendCapacity(params, capacity):
34+
"""Append CAPACITY to params."""
35+
if capacity is not None:
36+
params.extend(["CAPACITY", capacity])
37+
38+
@staticmethod
39+
def appendExpansion(params, expansion):
40+
"""Append EXPANSION to params."""
41+
if expansion is not None:
42+
params.extend(["EXPANSION", expansion])
43+
44+
@staticmethod
45+
def appendNoScale(params, noScale):
46+
"""Append NONSCALING tag to params."""
47+
if noScale is not None:
48+
params.extend(["NONSCALING"])
49+
50+
@staticmethod
51+
def appendWeights(params, weights):
52+
"""Append WEIGHTS to params."""
53+
if len(weights) > 0:
54+
params.append("WEIGHTS")
55+
params += weights
56+
57+
@staticmethod
58+
def appendNoCreate(params, noCreate):
59+
"""Append NOCREATE tag to params."""
60+
if noCreate is not None:
61+
params.extend(["NOCREATE"])
62+
63+
@staticmethod
64+
def appendItemsAndIncrements(params, items, increments):
65+
"""Append pairs of items and increments to params."""
66+
for i in range(len(items)):
67+
params.append(items[i])
68+
params.append(increments[i])
69+
70+
@staticmethod
71+
def appendValuesAndWeights(params, items, weights):
72+
"""Append pairs of items and weights to params."""
73+
for i in range(len(items)):
74+
params.append(items[i])
75+
params.append(weights[i])
76+
77+
@staticmethod
78+
def appendMaxIterations(params, max_iterations):
79+
"""Append MAXITERATIONS to params."""
80+
if max_iterations is not None:
81+
params.extend(["MAXITERATIONS", max_iterations])
82+
83+
@staticmethod
84+
def appendBucketSize(params, bucket_size):
85+
"""Append BUCKETSIZE to params."""
86+
if bucket_size is not None:
87+
params.extend(["BUCKETSIZE", bucket_size])
88+
89+
90+
class CMSBloom(CMSCommands, AbstractBloom):
91+
def __init__(self, client, **kwargs):
92+
"""Create a new RedisBloom client."""
93+
# Set the module commands' callbacks
94+
MODULE_CALLBACKS = {
95+
CMS_INITBYDIM: bool_ok,
96+
CMS_INITBYPROB: bool_ok,
97+
# CMS_INCRBY: spaceHolder,
98+
# CMS_QUERY: spaceHolder,
99+
CMS_MERGE: bool_ok,
100+
CMS_INFO: CMSInfo,
101+
}
102+
103+
self.client = client
104+
self.commandmixin = CMSCommands
105+
self.execute_command = client.execute_command
106+
107+
for k, v in MODULE_CALLBACKS.items():
108+
self.client.set_response_callback(k, v)
109+
110+
111+
class TOPKBloom(TOPKCommands, AbstractBloom):
112+
def __init__(self, client, **kwargs):
113+
"""Create a new RedisBloom client."""
114+
# Set the module commands' callbacks
115+
MODULE_CALLBACKS = {
116+
TOPK_RESERVE: bool_ok,
117+
TOPK_ADD: parse_to_list,
118+
TOPK_INCRBY: parse_to_list,
119+
# TOPK_QUERY: spaceHolder,
120+
# TOPK_COUNT: spaceHolder,
121+
TOPK_LIST: parse_to_list,
122+
TOPK_INFO: TopKInfo,
123+
}
124+
125+
self.client = client
126+
self.commandmixin = TOPKCommands
127+
self.execute_command = client.execute_command
128+
129+
for k, v in MODULE_CALLBACKS.items():
130+
self.client.set_response_callback(k, v)
131+
132+
133+
class CFBloom(CFCommands, AbstractBloom):
134+
def __init__(self, client, **kwargs):
135+
"""Create a new RedisBloom client."""
136+
# Set the module commands' callbacks
137+
MODULE_CALLBACKS = {
138+
CF_RESERVE: bool_ok,
139+
# CF_ADD: spaceHolder,
140+
# CF_ADDNX: spaceHolder,
141+
# CF_INSERT: spaceHolder,
142+
# CF_INSERTNX: spaceHolder,
143+
# CF_EXISTS: spaceHolder,
144+
# CF_DEL: spaceHolder,
145+
# CF_COUNT: spaceHolder,
146+
# CF_SCANDUMP: spaceHolder,
147+
# CF_LOADCHUNK: spaceHolder,
148+
CF_INFO: CFInfo,
149+
}
150+
151+
self.client = client
152+
self.commandmixin = CFCommands
153+
self.execute_command = client.execute_command
154+
155+
for k, v in MODULE_CALLBACKS.items():
156+
self.client.set_response_callback(k, v)
157+
158+
159+
class TDigestBloom(TDigestCommands, AbstractBloom):
160+
def __init__(self, client, **kwargs):
161+
"""Create a new RedisBloom client."""
162+
# Set the module commands' callbacks
163+
MODULE_CALLBACKS = {
164+
TDIGEST_CREATE: bool_ok,
165+
# TDIGEST_RESET: bool_ok,
166+
# TDIGEST_ADD: spaceHolder,
167+
# TDIGEST_MERGE: spaceHolder,
168+
TDIGEST_CDF: float,
169+
TDIGEST_QUANTILE: float,
170+
TDIGEST_MIN: float,
171+
TDIGEST_MAX: float,
172+
TDIGEST_INFO: TDigestInfo,
173+
}
174+
175+
self.client = client
176+
self.commandmixin = TDigestCommands
177+
self.execute_command = client.execute_command
178+
179+
for k, v in MODULE_CALLBACKS.items():
180+
self.client.set_response_callback(k, v)
181+
182+
183+
class BFBloom(BFCommands, AbstractBloom):
184+
def __init__(self, client, **kwargs):
185+
"""Create a new RedisBloom client."""
186+
# Set the module commands' callbacks
187+
MODULE_CALLBACKS = {
188+
BF_RESERVE: bool_ok,
189+
# BF_ADD: spaceHolder,
190+
# BF_MADD: spaceHolder,
191+
# BF_INSERT: spaceHolder,
192+
# BF_EXISTS: spaceHolder,
193+
# BF_MEXISTS: spaceHolder,
194+
# BF_SCANDUMP: spaceHolder,
195+
# BF_LOADCHUNK: spaceHolder,
196+
BF_INFO: BFInfo,
197+
}
198+
199+
self.client = client
200+
self.commandmixin = BFCommands
201+
self.execute_command = client.execute_command
202+
203+
for k, v in MODULE_CALLBACKS.items():
204+
self.client.set_response_callback(k, v)

0 commit comments

Comments
 (0)