1
1
from redis .client import NEVER_DECODE
2
2
from redis .exceptions import ModuleError
3
- from redis .utils import HIREDIS_AVAILABLE
3
+ from redis .utils import HIREDIS_AVAILABLE , deprecated_function
4
4
5
5
BF_RESERVE = "BF.RESERVE"
6
6
BF_ADD = "BF.ADD"
49
49
TDIGEST_MIN = "TDIGEST.MIN"
50
50
TDIGEST_MAX = "TDIGEST.MAX"
51
51
TDIGEST_INFO = "TDIGEST.INFO"
52
+ TDIGEST_TRIMMED_MEAN = "TDIGEST.TRIMMED_MEAN"
53
+ TDIGEST_RANK = "TDIGEST.RANK"
54
+ TDIGEST_REVRANK = "TDIGEST.REVRANK"
55
+ TDIGEST_BYRANK = "TDIGEST.BYRANK"
56
+ TDIGEST_BYREVRANK = "TDIGEST.BYREVRANK"
52
57
53
58
54
59
class BFCommands :
@@ -67,6 +72,8 @@ def create(self, key, errorRate, capacity, expansion=None, noScale=None):
67
72
self .append_no_scale (params , noScale )
68
73
return self .execute_command (BF_RESERVE , * params )
69
74
75
+ reserve = create
76
+
70
77
def add (self , key , item ):
71
78
"""
72
79
Add to a Bloom Filter `key` an `item`.
@@ -176,6 +183,8 @@ def create(
176
183
self .append_max_iterations (params , max_iterations )
177
184
return self .execute_command (CF_RESERVE , * params )
178
185
186
+ reserve = create
187
+
179
188
def add (self , key , item ):
180
189
"""
181
190
Add an `item` to a Cuckoo Filter `key`.
@@ -316,6 +325,7 @@ def query(self, key, *items):
316
325
""" # noqa
317
326
return self .execute_command (TOPK_QUERY , key , * items )
318
327
328
+ @deprecated_function (version = "4.4.0" , reason = "deprecated since redisbloom 2.4.0" )
319
329
def count (self , key , * items ):
320
330
"""
321
331
Return count for one `item` or more from `key`.
@@ -344,12 +354,12 @@ def info(self, key):
344
354
345
355
346
356
class TDigestCommands :
347
- def create (self , key , compression ):
357
+ def create (self , key , compression = 100 ):
348
358
"""
349
359
Allocate the memory and initialize the t-digest.
350
360
For more information see `TDIGEST.CREATE <https://redis.io/commands/tdigest.create>`_.
351
361
""" # noqa
352
- return self .execute_command (TDIGEST_CREATE , key , compression )
362
+ return self .execute_command (TDIGEST_CREATE , key , "COMPRESSION" , compression )
353
363
354
364
def reset (self , key ):
355
365
"""
@@ -358,26 +368,30 @@ def reset(self, key):
358
368
""" # noqa
359
369
return self .execute_command (TDIGEST_RESET , key )
360
370
361
- def add (self , key , values , weights ):
371
+ def add (self , key , values ):
362
372
"""
363
- Add one or more samples (value with weight) to a sketch `key`.
364
- Both `values` and `weights` are lists.
365
- For more information see `TDIGEST.ADD <https://redis.io/commands/tdigest.add>`_.
373
+ Adds one or more observations to a t-digest sketch `key`.
366
374
367
- Example:
368
-
369
- >>> tdigestadd('A', [1500.0], [1.0])
375
+ For more information see `TDIGEST.ADD <https://redis.io/commands/tdigest.add>`_.
370
376
""" # noqa
371
- params = [key ]
372
- self .append_values_and_weights (params , values , weights )
373
- return self .execute_command (TDIGEST_ADD , * params )
377
+ return self .execute_command (TDIGEST_ADD , key , * values )
374
378
375
- def merge (self , toKey , fromKey ):
379
+ def merge (self , destination_key , num_keys , * keys , compression = None , override = False ):
376
380
"""
377
- Merge all of the values from 'fromKey' to 'toKey' sketch.
381
+ Merges all of the values from `keys` to 'destination-key' sketch.
382
+ It is mandatory to provide the `num_keys` before passing the input keys and
383
+ the other (optional) arguments.
384
+ If `destination_key` already exists its values are merged with the input keys.
385
+ If you wish to override the destination key contents use the `OVERRIDE` parameter.
386
+
378
387
For more information see `TDIGEST.MERGE <https://redis.io/commands/tdigest.merge>`_.
379
388
""" # noqa
380
- return self .execute_command (TDIGEST_MERGE , toKey , fromKey )
389
+ params = [destination_key , num_keys , * keys ]
390
+ if compression is not None :
391
+ params .extend (["COMPRESSION" , compression ])
392
+ if override :
393
+ params .append ("OVERRIDE" )
394
+ return self .execute_command (TDIGEST_MERGE , * params )
381
395
382
396
def min (self , key ):
383
397
"""
@@ -393,20 +407,21 @@ def max(self, key):
393
407
""" # noqa
394
408
return self .execute_command (TDIGEST_MAX , key )
395
409
396
- def quantile (self , key , quantile ):
410
+ def quantile (self , key , quantile , * quantiles ):
397
411
"""
398
- Return double value estimate of the cutoff such that a specified fraction of the data
399
- added to this TDigest would be less than or equal to the cutoff.
412
+ Returns estimates of one or more cutoffs such that a specified fraction of the
413
+ observations added to this t-digest would be less than or equal to each of the
414
+ specified cutoffs. (Multiple quantiles can be returned with one call)
400
415
For more information see `TDIGEST.QUANTILE <https://redis.io/commands/tdigest.quantile>`_.
401
416
""" # noqa
402
- return self .execute_command (TDIGEST_QUANTILE , key , quantile )
417
+ return self .execute_command (TDIGEST_QUANTILE , key , quantile , * quantiles )
403
418
404
- def cdf (self , key , value ):
419
+ def cdf (self , key , value , * values ):
405
420
"""
406
421
Return double fraction of all points added which are <= value.
407
422
For more information see `TDIGEST.CDF <https://redis.io/commands/tdigest.cdf>`_.
408
423
""" # noqa
409
- return self .execute_command (TDIGEST_CDF , key , value )
424
+ return self .execute_command (TDIGEST_CDF , key , value , * values )
410
425
411
426
def info (self , key ):
412
427
"""
@@ -416,6 +431,50 @@ def info(self, key):
416
431
""" # noqa
417
432
return self .execute_command (TDIGEST_INFO , key )
418
433
434
+ def trimmed_mean (self , key , low_cut_quantile , high_cut_quantile ):
435
+ """
436
+ Return mean value from the sketch, excluding observation values outside
437
+ the low and high cutoff quantiles.
438
+ For more information see `TDIGEST.TRIMMED_MEAN <https://redis.io/commands/tdigest.trimmed_mean>`_.
439
+ """ # noqa
440
+ return self .execute_command (
441
+ TDIGEST_TRIMMED_MEAN , key , low_cut_quantile , high_cut_quantile
442
+ )
443
+
444
+ def rank (self , key , value , * values ):
445
+ """
446
+ Retrieve the estimated rank of value (the number of observations in the sketch
447
+ that are smaller than value + half the number of observations that are equal to value).
448
+
449
+ For more information see `TDIGEST.RANK <https://redis.io/commands/tdigest.rank>`_.
450
+ """ # noqa
451
+ return self .execute_command (TDIGEST_RANK , key , value , * values )
452
+
453
+ def revrank (self , key , value , * values ):
454
+ """
455
+ Retrieve the estimated rank of value (the number of observations in the sketch
456
+ that are larger than value + half the number of observations that are equal to value).
457
+
458
+ For more information see `TDIGEST.REVRANK <https://redis.io/commands/tdigest.revrank>`_.
459
+ """ # noqa
460
+ return self .execute_command (TDIGEST_REVRANK , key , value , * values )
461
+
462
+ def byrank (self , key , rank , * ranks ):
463
+ """
464
+ Retrieve an estimation of the value with the given rank.
465
+
466
+ For more information see `TDIGEST.BY_RANK <https://redis.io/commands/tdigest.by_rank>`_.
467
+ """ # noqa
468
+ return self .execute_command (TDIGEST_BYRANK , key , rank , * ranks )
469
+
470
+ def byrevrank (self , key , rank , * ranks ):
471
+ """
472
+ Retrieve an estimation of the value with the given reverse rank.
473
+
474
+ For more information see `TDIGEST.BY_REVRANK <https://redis.io/commands/tdigest.by_revrank>`_.
475
+ """ # noqa
476
+ return self .execute_command (TDIGEST_BYREVRANK , key , rank , * ranks )
477
+
419
478
420
479
class CMSCommands :
421
480
"""Count-Min Sketch Commands"""
0 commit comments