o
    Lj                      @   s   d dl mZ d dlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZ G d	d
 d
eZG dd deeZG dd deeZG dd deeeZdS )    )Literal)Pipeline   )check_protocol_version   )get_legacy_responsesget_protocol_version   )AGGREGATE_CMD
CURSOR_CMDAsyncSearchCommandsSearchCommandsc                   @   s2   e Zd ZdZG dd dZdddZdd	d
ZdS )Searchzy
    Create a client for talking to search.
    It abstracts the API of the module and lets you just use the engine.
    c                   @   sJ   e Zd ZdZdddZdd Z							dd
dZdddZdd Zd	S )zSearch.BatchIndexer
        A batch indexer allows you to automatically batch
        document indexing in pipelines, flushing it every N documents.
          c                 C   s4   || _ |j| _|jdd d| _d| _|| _d| _d S )NF)transaction
shard_hintr   )clientexecute_commandpipeline	_pipelinetotal
chunk_sizecurrent_chunk)selfr   r    r   \/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/redis/commands/search/__init__.py__init__   s   
zSearch.BatchIndexer.__init__c                 C   s   | j r	|   d S d S N)r   commitr   r   r   r   __del__$   s   zSearch.BatchIndexer.__del__F      ?Nc           	   
   K   s^   | j j|f| j||||||d| |  jd7  _|  jd7  _| j| jkr-|   dS dS z;
            Add a document to the batch query
            )connnosavescorepayloadreplacepartial	no_creater	   Nr   _add_documentr   r   r   r   r   	r   doc_idr%   r&   r'   r(   r)   r*   fieldsr   r   r   add_document(   s$   	z Search.BatchIndexer.add_documentc                 C   sN   | j j|| j||d |  jd7  _|  jd7  _| j| jkr%|   dS dS )z7
            Add a hash to the batch query
            )r$   r&   r(   r	   N)r   _add_document_hashr   r   r   r   r   )r   r.   r&   r(   r   r   r   add_document_hashF   s   
z%Search.BatchIndexer.add_document_hashc                 C   s   | j   d| _dS )L
            Manually commit and flush the batch indexing query
            r   Nr   executer   r    r   r   r   r   R   s   

zSearch.BatchIndexer.commit)r   Fr"   NFFF)r"   F)	__name__
__module____qualname____doc__r   r!   r0   r2   r   r   r   r   r   BatchIndexer   s    


r;   idxc                 C   s(   || _ || _|j| _|j| _|   dS )z
        Create a new Client for the given index_name.
        The default name is `idx`

        If conn is not None, we employ an already existing redis connection
        N)r   
index_namer   r   r   _init_module_callbacks)r   r   r=   r   r   r   r   Y   s
   zSearch.__init__TNc                 C   $   t | jj| jj||d}| j|_|S zCreates a pipeline for the SEARCH module, that can be used for executing
        SEARCH commands, as well as classic core commands.
        )connection_poolresponse_callbacksr   r   )r   r   rA   rB   r=   r   r   r   pr   r   r   r   f      zSearch.pipeline)r<   TN)r7   r8   r9   r:   r;   r   r   r   r   r   r   r      s
    
Cr   c                   @   s(   e Zd ZG dd dejZdddZdS )AsyncSearchc                   @   s.   e Zd ZdZ						d	ddZdd ZdS )
zAsyncSearch.BatchIndexerr   Fr"   Nc           	   
      sf   | j j|f| j||||||d| |  jd7  _|  jd7  _| j| jkr1|  I dH  dS dS r#   r+   r-   r   r   r   r0   {   s&   	z%AsyncSearch.BatchIndexer.add_documentc                    s   | j  I dH  d| _dS )r3   Nr   r4   r    r   r   r   r      s   
zAsyncSearch.BatchIndexer.commitr6   )r7   r8   r9   r:   r0   r   r   r   r   r   r;   u   s    
r;   TNc                 C   r?   r@   )AsyncPipeliner   rA   rB   r=   rC   r   r   r   r      rE   zAsyncSearch.pipelinerF   )r7   r8   r9   r   r;   r   r   r   r   r   rG   t   s    +rG   c                       sF   e Zd ZU dZdZed ed<  fddZdd Ze	dd	 Z
  ZS )
r   zPipeline for the module.F_is_async_clientc                    *   t  |t||| |   |   d S r   superr   dictr>   _register_module_callbacksr   rA   rB   r   r   	__class__r   r   r      
   zPipeline.__init__c                 C   s   t | }t| r|d u r| j}nt|dr| j}n| j}nt|dr&| j}n| j}| D ]	\}}|| j	|< q-|
t}|d urG|| j	t< d S d S )Nr   )r   r   )_RESP3_TO_RESP2_LEGACY_PIPELINE_CALLBACKSr   _RESP3_MODULE_CALLBACKS _RESP2_LEGACY_PIPELINE_CALLBACKS_RESP3_UNIFIED_MODULE_CALLBACKS_RESP2_UNIFIED_MODULE_CALLBACKSitemsrB   getr
   r   )r   protocolcmd_callbackscmdcbagg_cbr   r   r   rN      s    


z#Pipeline._register_module_callbacksc                 C      | S )zAReturn self so ``get_protocol_version`` can read connection_pool.r   r    r   r   r   r      s   zPipeline.client)r7   r8   r9   r:   rI   r   __annotations__r   rN   propertyr   __classcell__r   r   rP   r   r      s   
 	r   c                       s>   e Zd ZU dZdZed ed<  fddZedd Z	  Z
S )rH   zAsyncPipeline for the module.TrI   c                    rJ   r   rK   rO   rP   r   r   r      rR   zAsyncPipeline.__init__c                 C   r_   )a  Return self so ``get_protocol_version`` can read connection_pool.

        Redefined here because ``redis.asyncio.client.Redis.client`` (a
        plain method) appears earlier in the MRO than ``Pipeline.client``
        (a property) and would otherwise shadow it.
        r   r    r   r   r   r      s   zAsyncPipeline.client)r7   r8   r9   r:   rI   r   r`   r   ra   r   rb   r   r   rP   r   rH      s   
 	rH   N)typingr   redis.clientr   RedisPipelineasyncio.clientAsyncioPipelineutilsr   helpersr   r   commandsr
   r   r   r   r   rG   rH   r   r   r   r   <module>   s    d:0