图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
7.8 KiB

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