U
    ccG                     @   s  d dl mZ d dlmZ d dlmZ dZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(d'Z)d(Z*d)Z+d*Z,d+Z-d,Z.d-Z/d.Z0G d/d0 d0Z1G d1d2 d2Z2G d3d4 d4Z3G d5d6 d6Z4G d7d8 d8Z5d9S ):    )NEVER_DECODE)ModuleError)HIREDIS_AVAILABLEz
BF.RESERVEzBF.ADDzBF.MADDz	BF.INSERTz	BF.EXISTSz
BF.MEXISTSzBF.SCANDUMPzBF.LOADCHUNKzBF.INFOz
CF.RESERVEzCF.ADDzCF.ADDNXz	CF.INSERTzCF.INSERTNXz	CF.EXISTSz
CF.MEXISTSzCF.DELzCF.COUNTzCF.SCANDUMPzCF.LOADCHUNKzCF.INFOzCMS.INITBYDIMzCMS.INITBYPROBz
CMS.INCRBYz	CMS.QUERYz	CMS.MERGEzCMS.INFOzTOPK.RESERVEzTOPK.ADDzTOPK.INCRBYz
TOPK.QUERYz
TOPK.COUNTz	TOPK.LISTz	TOPK.INFOzTDIGEST.CREATEzTDIGEST.RESETzTDIGEST.ADDzTDIGEST.MERGEzTDIGEST.CDFzTDIGEST.QUANTILEzTDIGEST.MINzTDIGEST.MAXzTDIGEST.INFOc                   @   s\   e Zd ZdZdddZdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dd Zdd ZdS )
BFCommandszBloom Filter commands.Nc                 C   s2   |||g}|  || | || | jtf| S )aA  
        Create a new Bloom Filter `key` with desired probability of false positives
        `errorRate` expected entries to be inserted as `capacity`.
        Default expansion value is 2. By default, filter is auto-scaling.
        For more information see `BF.RESERVE <https://redis.io/commands/bf.reserve>`_.
        )append_expansionappend_no_scaleexecute_command
BF_RESERVE)selfkeyZ	errorRatecapacity	expansionnoScaleparams r   T/var/www/html/project/venv/lib/python3.8/site-packages/redis/commands/bf/commands.pycreate:   s    
zBFCommands.createc                 C   s   |  t||S )z
        Add to a Bloom Filter `key` an `item`.
        For more information see `BF.ADD <https://redis.io/commands/bf.add>`_.
        )r   BF_ADDr
   r   itemr   r   r   addF   s    zBFCommands.addc                 G   s   | j t|f| S )z
        Add to a Bloom Filter `key` multiple `items`.
        For more information see `BF.MADD <https://redis.io/commands/bf.madd>`_.
        )r   BF_MADDr
   r   itemsr   r   r   maddM   s    zBFCommands.maddc           	      C   s^   |g}|  || | || | || | || | || | || | jtf| S )as  
        Add to a Bloom Filter `key` multiple `items`.

        If `nocreate` remain `None` and `key` does not exist, a new Bloom Filter
        `key` will be created with desired probability of false positives `errorRate`
        and expected entries to be inserted as `size`.
        For more information see `BF.INSERT <https://redis.io/commands/bf.insert>`_.
        )append_capacityZappend_errorr   append_no_creater   append_itemsr   	BF_INSERT)	r
   r   r   r   errorZnoCreater   r   r   r   r   r   insertT   s    zBFCommands.insertc                 C   s   |  t||S )z
        Check whether an `item` exists in Bloom Filter `key`.
        For more information see `BF.EXISTS <https://redis.io/commands/bf.exists>`_.
        )r   	BF_EXISTSr   r   r   r   existsp   s    zBFCommands.existsc                 G   s   | j t|f| S )z
        Check whether `items` exist in Bloom Filter `key`.
        For more information see `BF.MEXISTS <https://redis.io/commands/bf.mexists>`_.
        )r   
BF_MEXISTSr   r   r   r   mexistsw   s    zBFCommands.mexistsc                 C   s2   t rtd||g}i }g |t< | jtf||S )a  
        Begin an incremental save of the bloom filter `key`.

        This is useful for large bloom filters which cannot fit into the normal SAVE and RESTORE model.
        The first time this command is called, the value of `iter` should be 0.
        This command will return successive (iter, data) pairs until (0, NULL) to indicate completion.
        For more information see `BF.SCANDUMP <https://redis.io/commands/bf.scandump>`_.
        z6This command cannot be used when hiredis is available.)r   r   r   r   BF_SCANDUMP)r
   r   iterr   optionsr   r   r   scandump~   s    	zBFCommands.scandumpc                 C   s   |  t|||S )ai  
        Restore a filter previously saved using SCANDUMP.

        See the SCANDUMP command for example usage.
        This command will overwrite any bloom filter stored under key.
        Ensure that the bloom filter will not be modified between invocations.
        For more information see `BF.LOADCHUNK <https://redis.io/commands/bf.loadchunk>`_.
        )r   BF_LOADCHUNKr
   r   r&   datar   r   r   	loadchunk   s    	zBFCommands.loadchunkc                 C   s   |  t|S )z
        Return capacity, size, number of filters, number of items inserted, and expansion rate.
        For more information see `BF.INFO <https://redis.io/commands/bf.info>`_.
        )r   BF_INFOr
   r   r   r   r   info   s    zBFCommands.info)NN)NNNNN)__name__
__module____qualname____doc__r   r   r   r    r"   r$   r(   r,   r/   r   r   r   r   r   6   s   
     
r   c                   @   sv   e Zd ZdZdddZdd Zdd Zdd	d
ZdddZdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )
CFCommandszCuckoo Filter commands.Nc                 C   s<   ||g}|  || | || | || | jtf| S )z
        Create a new Cuckoo Filter `key` an initial `capacity` items.
        For more information see `CF.RESERVE <https://redis.io/commands/cf.reserve>`_.
        )r   Zappend_bucket_sizeZappend_max_iterationsr   
CF_RESERVE)r
   r   r   r   Zbucket_sizeZmax_iterationsr   r   r   r   r      s
    zCFCommands.createc                 C   s   |  t||S )z
        Add an `item` to a Cuckoo Filter `key`.
        For more information see `CF.ADD <https://redis.io/commands/cf.add>`_.
        )r   CF_ADDr   r   r   r   r      s    zCFCommands.addc                 C   s   |  t||S )z
        Add an `item` to a Cuckoo Filter `key` only if item does not yet exist.
        Command might be slower that `add`.
        For more information see `CF.ADDNX <https://redis.io/commands/cf.addnx>`_.
        )r   CF_ADDNXr   r   r   r   addnx   s    zCFCommands.addnxc                 C   s:   |g}|  || | || | || | jtf| S )a  
        Add multiple `items` to a Cuckoo Filter `key`, allowing the filter
        to be created with a custom `capacity` if it does not yet exist.
        `items` must be provided as a list.
        For more information see `CF.INSERT <https://redis.io/commands/cf.insert>`_.
        )r   r   r   r   	CF_INSERTr
   r   r   r   Znocreater   r   r   r   r       s
    zCFCommands.insertc                 C   s:   |g}|  || | || | || | jtf| S )a@  
        Add multiple `items` to a Cuckoo Filter `key` only if they do not exist yet,
        allowing the filter to be created with a custom `capacity` if it does not yet exist.
        `items` must be provided as a list.
        For more information see `CF.INSERTNX <https://redis.io/commands/cf.insertnx>`_.
        )r   r   r   r   CF_INSERTNXr:   r   r   r   insertnx   s
    zCFCommands.insertnxc                 C   s   |  t||S )z
        Check whether an `item` exists in Cuckoo Filter `key`.
        For more information see `CF.EXISTS <https://redis.io/commands/cf.exists>`_.
        )r   	CF_EXISTSr   r   r   r   r"      s    zCFCommands.existsc                 G   s   | j t|f| S )z
        Check whether an `items` exist in Cuckoo Filter `key`.
        For more information see `CF.MEXISTS <https://redis.io/commands/cf.mexists>`_.
        )r   
CF_MEXISTSr   r   r   r   r$      s    zCFCommands.mexistsc                 C   s   |  t||S )zz
        Delete `item` from `key`.
        For more information see `CF.DEL <https://redis.io/commands/cf.del>`_.
        )r   CF_DELr   r   r   r   delete   s    zCFCommands.deletec                 C   s   |  t||S )z
        Return the number of times an `item` may be in the `key`.
        For more information see `CF.COUNT <https://redis.io/commands/cf.count>`_.
        )r   CF_COUNTr   r   r   r   count   s    zCFCommands.countc                 C   s   |  t||S )a  
        Begin an incremental save of the Cuckoo filter `key`.

        This is useful for large Cuckoo filters which cannot fit into the normal
        SAVE and RESTORE model.
        The first time this command is called, the value of `iter` should be 0.
        This command will return successive (iter, data) pairs until
        (0, NULL) to indicate completion.
        For more information see `CF.SCANDUMP <https://redis.io/commands/cf.scandump>`_.
        )r   CF_SCANDUMP)r
   r   r&   r   r   r   r(      s    zCFCommands.scandumpc                 C   s   |  t|||S )ac  
        Restore a filter previously saved using SCANDUMP. See the SCANDUMP command for example usage.

        This command will overwrite any Cuckoo filter stored under key.
        Ensure that the Cuckoo filter will not be modified between invocations.
        For more information see `CF.LOADCHUNK <https://redis.io/commands/cf.loadchunk>`_.
        )r   CF_LOADCHUNKr*   r   r   r   r,     s    zCFCommands.loadchunkc                 C   s   |  t|S )z
        Return size, number of buckets, number of filter, number of items inserted,
        number of items deleted, bucket size, expansion rate, and max iteration.
        For more information see `CF.INFO <https://redis.io/commands/cf.info>`_.
        )r   CF_INFOr.   r   r   r   r/     s    zCFCommands.info)NNN)NN)NN)r0   r1   r2   r3   r   r   r8   r    r<   r"   r$   r@   rB   r(   r,   r/   r   r   r   r   r4      s         



r4   c                   @   sJ   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdddZ	dd Z
dS )TOPKCommandszTOP-k Filter commands.c                 C   s   |  t|||||S )z
        Create a new Top-K Filter `key` with desired probability of false
        positives `errorRate` expected entries to be inserted as `size`.
        For more information see `TOPK.RESERVE <https://redis.io/commands/topk.reserve>`_.
        )r   TOPK_RESERVE)r
   r   kwidthdepthZdecayr   r   r   reserve  s    zTOPKCommands.reservec                 G   s   | j t|f| S )z
        Add one `item` or more to a Top-K Filter `key`.
        For more information see `TOPK.ADD <https://redis.io/commands/topk.add>`_.
        )r   TOPK_ADDr   r   r   r   r   #  s    zTOPKCommands.addc                 C   s$   |g}|  ||| | jtf| S )a  
        Add/increase `items` to a Top-K Sketch `key` by ''increments''.
        Both `items` and `increments` are lists.
        For more information see `TOPK.INCRBY <https://redis.io/commands/topk.incrby>`_.

        Example:

        >>> topkincrby('A', ['foo'], [1])
        )append_items_and_incrementsr   TOPK_INCRBYr
   r   r   Z
incrementsr   r   r   r   incrby*  s    
zTOPKCommands.incrbyc                 G   s   | j t|f| S )z
        Check whether one `item` or more is a Top-K item at `key`.
        For more information see `TOPK.QUERY <https://redis.io/commands/topk.query>`_.
        )r   
TOPK_QUERYr   r   r   r   query8  s    zTOPKCommands.queryc                 G   s   | j t|f| S )z
        Return count for one `item` or more from `key`.
        For more information see `TOPK.COUNT <https://redis.io/commands/topk.count>`_.
        )r   
TOPK_COUNTr   r   r   r   rB   ?  s    zTOPKCommands.countFc                 C   s$   |g}|r| d | jtf| S )a  
        Return full list of items in Top-K list of `key`.
        If `withcount` set to True, return full list of items
        with probabilistic count in Top-K list of `key`.
        For more information see `TOPK.LIST <https://redis.io/commands/topk.list>`_.
        Z	WITHCOUNT)appendr   	TOPK_LIST)r
   r   Z	withcountr   r   r   r   listF  s    
zTOPKCommands.listc                 C   s   |  t|S )z
        Return k, width, depth and decay values of `key`.
        For more information see `TOPK.INFO <https://redis.io/commands/topk.info>`_.
        )r   	TOPK_INFOr.   r   r   r   r/   R  s    zTOPKCommands.infoN)F)r0   r1   r2   r3   rK   r   rP   rR   rB   rV   r/   r   r   r   r   rF     s   
rF   c                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )TDigestCommandsc                 C   s   |  t||S )z
        Allocate the memory and initialize the t-digest.
        For more information see `TDIGEST.CREATE <https://redis.io/commands/tdigest.create>`_.
        )r   TDIGEST_CREATE)r
   r   compressionr   r   r   r   [  s    zTDigestCommands.createc                 C   s   |  t|S )z
        Reset the sketch `key` to zero - empty out the sketch and re-initialize it.
        For more information see `TDIGEST.RESET <https://redis.io/commands/tdigest.reset>`_.
        )r   TDIGEST_RESETr.   r   r   r   resetb  s    zTDigestCommands.resetc                 C   s$   |g}|  ||| | jtf| S )a  
        Add one or more samples (value with weight) to a sketch `key`.
        Both `values` and `weights` are lists.
        For more information see `TDIGEST.ADD <https://redis.io/commands/tdigest.add>`_.

        Example:

        >>> tdigestadd('A', [1500.0], [1.0])
        )Zappend_values_and_weightsr   TDIGEST_ADD)r
   r   valuesweightsr   r   r   r   r   i  s    
zTDigestCommands.addc                 C   s   |  t||S )z
        Merge all of the values from 'fromKey' to 'toKey' sketch.
        For more information see `TDIGEST.MERGE <https://redis.io/commands/tdigest.merge>`_.
        )r   TDIGEST_MERGE)r
   ZtoKeyZfromKeyr   r   r   mergew  s    zTDigestCommands.mergec                 C   s   |  t|S )z
        Return minimum value from the sketch `key`. Will return DBL_MAX if the sketch is empty.
        For more information see `TDIGEST.MIN <https://redis.io/commands/tdigest.min>`_.
        )r   TDIGEST_MINr.   r   r   r   min~  s    zTDigestCommands.minc                 C   s   |  t|S )z
        Return maximum value from the sketch `key`. Will return DBL_MIN if the sketch is empty.
        For more information see `TDIGEST.MAX <https://redis.io/commands/tdigest.max>`_.
        )r   TDIGEST_MAXr.   r   r   r   max  s    zTDigestCommands.maxc                 C   s   |  t||S )a  
        Return double value estimate of the cutoff such that a specified fraction of the data
        added to this TDigest would be less than or equal to the cutoff.
        For more information see `TDIGEST.QUANTILE <https://redis.io/commands/tdigest.quantile>`_.
        )r   TDIGEST_QUANTILE)r
   r   quantiler   r   r   rg     s    zTDigestCommands.quantilec                 C   s   |  t||S )z
        Return double fraction of all points added which are <= value.
        For more information see `TDIGEST.CDF <https://redis.io/commands/tdigest.cdf>`_.
        )r   TDIGEST_CDF)r
   r   valuer   r   r   cdf  s    zTDigestCommands.cdfc                 C   s   |  t|S )z
        Return Compression, Capacity, Merged Nodes, Unmerged Nodes, Merged Weight, Unmerged Weight
        and Total Compressions.
        For more information see `TDIGEST.INFO <https://redis.io/commands/tdigest.info>`_.
        )r   TDIGEST_INFOr.   r   r   r   r/     s    zTDigestCommands.infoN)r0   r1   r2   r   r\   r   ra   rc   re   rg   rj   r/   r   r   r   r   rX   Z  s   rX   c                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zg fd
dZdd Z	dS )CMSCommandszCount-Min Sketch Commandsc                 C   s   |  t|||S )z
        Initialize a Count-Min Sketch `key` to dimensions (`width`, `depth`) specified by user.
        For more information see `CMS.INITBYDIM <https://redis.io/commands/cms.initbydim>`_.
        )r   CMS_INITBYDIM)r
   r   rI   rJ   r   r   r   	initbydim  s    zCMSCommands.initbydimc                 C   s   |  t|||S )z
        Initialize a Count-Min Sketch `key` to characteristics (`error`, `probability`) specified by user.
        For more information see `CMS.INITBYPROB <https://redis.io/commands/cms.initbyprob>`_.
        )r   CMS_INITBYPROB)r
   r   r   Zprobabilityr   r   r   
initbyprob  s    zCMSCommands.initbyprobc                 C   s$   |g}|  ||| | jtf| S )a  
        Add/increase `items` to a Count-Min Sketch `key` by ''increments''.
        Both `items` and `increments` are lists.
        For more information see `CMS.INCRBY <https://redis.io/commands/cms.incrby>`_.

        Example:

        >>> cmsincrby('A', ['foo'], [1])
        )rM   r   
CMS_INCRBYrO   r   r   r   rP     s    
zCMSCommands.incrbyc                 G   s   | j t|f| S )z
        Return count for an `item` from `key`. Multiple items can be queried with one call.
        For more information see `CMS.QUERY <https://redis.io/commands/cms.query>`_.
        )r   	CMS_QUERYr   r   r   r   rR     s    zCMSCommands.queryc                 C   s,   ||g}||7 }|  || | jtf| S )an  
        Merge `numKeys` of sketches into `destKey`. Sketches specified in `srcKeys`.
        All sketches must have identical width and depth.
        `Weights` can be used to multiply certain sketches. Default weight is 1.
        Both `srcKeys` and `weights` are lists.
        For more information see `CMS.MERGE <https://redis.io/commands/cms.merge>`_.
        )Zappend_weightsr   	CMS_MERGE)r
   ZdestKeyZnumKeysZsrcKeysr_   r   r   r   r   ra     s    zCMSCommands.mergec                 C   s   |  t|S )z
        Return width, depth and total count of the sketch.
        For more information see `CMS.INFO <https://redis.io/commands/cms.info>`_.
        )r   CMS_INFOr.   r   r   r   r/     s    zCMSCommands.infoN)
r0   r1   r2   r3   rn   rp   rP   rR   ra   r/   r   r   r   r   rl     s   rl   N)6Zredis.clientr   Zredis.exceptionsr   Zredis.utilsr   r	   r   r   r   r!   r#   r%   r)   r-   r5   r6   r7   r9   r;   r=   r>   r?   rA   rC   rD   rE   rm   ro   rq   rr   rs   rt   rG   rL   rN   rQ   rS   rU   rW   rY   r[   r]   r`   rh   rf   rb   rd   rk   r   r4   rF   rX   rl   r   r   r   r   <module>   sd   lvBJ