o
    RDiG,                     @  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	 d dl
mZmZ d dlmZ eeZeG dd	 d	ZG d
d dZdS )    )annotations)total_ordering)md5)	getLogger)Lock)AnyIterable)	SortedSetc                   @  s>   e Zd Zdd	d
ZdddZdddZdddZdddZdS )QueryContextElementidintread_timestampprioritycontextstrreturnNonec                 C  s   || _ || _|| _|| _d S Nr   r   r   r   )selfr   r   r   r    r   a/var/www/Datamplify/venv/lib/python3.10/site-packages/snowflake/connector/_query_context_cache.py__init__   s   
zQueryContextElement.__init__otherobjectboolc                 C  s>   t |tsdS | j|jko| j|jko| j|jko| j|jkS )NF)
isinstancer
   r   r   r   r   r   r   r   r   r   __eq__   s   



zQueryContextElement.__eq__r   c                 C  s(   t |tstdt| | j|jk S )Nz7cannot compare QueryContextElement with object of type )r   r
   	TypeErrortyper   r   r   r   r   __lt__%   s
   
zQueryContextElement.__lt__c                 C  sh   d}|d | j  }||d | j 7 }||d | j 7 }| jr2||d tt| jd d 7 }|S )N   zutf-8big)	r   r   r   r   r   
from_bytesr   encodedigest)r   _hashr   r   r   __hash__,   s   zQueryContextElement.__hash__c                 C  s   d| j  d| j d| j dS )N(z, ))r   r   r   r   r   r   r   __str__8   s   zQueryContextElement.__str__N
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   )r   r   )__name__
__module____qualname__r   r   r!   r(   r,   r   r   r   r   r
      s    




r
   c                   @  s   e Zd Zd.ddZd/d	d
Zd/ddZd0ddZdd Zd1ddZd2ddZ	d2ddZ
d3dd Zd4d!d"Zd5d$d%Zd6d'd(Zd2d)d*Zd7d+d,Zd-S )8QueryContextCachecapacityr   r   r   c                 C  s2   || _ i | _i | _i | _t | _t | _d | _d S r   )	r3   _id_map_priority_map_intermediate_priority_mapr	   	_tree_setr   _lock_data)r   r3   r   r   r   r   =   s   
zQueryContextCache.__init__qcer
   c                 C  s(   | j | || j|j< || j|j< dS )zAdds qce element in tree_set, id_map and intermediate_priority_map.
        We still need to add _sync_priority_map after all the new qce have been merged
        into the cache.
        N)r7   addr4   r   r6   r   r   r:   r   r   r   _add_qceI   s   zQueryContextCache._add_qcec                 C  s,   | j |j | j|j | j| d S r   )r4   popr   r5   r   r7   remover<   r   r   r   _remove_qceR   s   zQueryContextCache._remove_qceold_qcenew_qcec                 C  s   |  | | | dS )zSThis is just a convenience function to call a remove and add operation back-to-backN)r@   r=   )r   rA   rB   r   r   r   _replace_qceW   s   
zQueryContextCache._replace_qcec                 C  s>   t dt| j dt| j  | j| j | j  dS )zx
        Sync the _intermediate_priority_map with the _priority_map at the end of the current round of inserts.
        z-sync_priority_map called priority_map size = z, new_priority_map size = N)loggerdebuglenr5   r6   updateclearr+   r   r   r   _sync_priority_map^   s
   z$QueryContextCache._sync_priority_mapr   r   r   r   r   c                 C  s   || j v r,| j | }||jks||jkr(||jkr*t||||}| || d S d S d S t||||}|| jv rE| j| }| || d S | | d S r   )r4   r   r   r
   rC   r5   r=   )r   r   r   r   r   r:   rB   rA   r   r   r   insertj   s   




zQueryContextCache.insertc                 C  sn   t dt| j d| j  t| | jkr&|  }| | t| | jkst dt| j d| j  d S )Nz%trim_cache() called. treeSet size is z and cache capacity is z&trim_cache() returns. treeSet size is )rD   rE   rF   r7   r3   _lastr@   r<   r   r   r   
trim_cache{   s   
zQueryContextCache.trim_cachec                 C  s6   t d | j  | j  | j  | j  d S )Nzclear_cache() called)rD   rE   r4   rH   r5   r7   r6   r+   r   r   r   clear_cache   s
   



zQueryContextCache.clear_cacheIterable[QueryContextElement]c                 C  s   | j S r   r7   r+   r   r   r   _get_elements   s   zQueryContextCache._get_elementsc                 C  s
   | j d S )NrO   r+   r   r   r   rK         
zQueryContextCache._lastdictc                 C  s   | j Z td |   t| jdkri W  d    S zddd | jD i}td|  |W W  d    S  ty\ } ztd|  i W  Y d }~W  d    S d }~ww 1 s`w   Y  d S )Nzserialize_to_dict() calledr   entriesc                 S  s4   g | ]}|j |j|j|jd urd|jini dqS )N
base64Data)r   	timestampr   r   r   ).0r:   r   r   r   
<listcomp>   s    

z7QueryContextCache.serialize_to_dict.<locals>.<listcomp>z,serialize_to_dict(): data to send to server zserialize_to_dict(): Exception )r8   rD   rE   log_cache_entriesrF   r7   	Exception)r   dataer   r   r   serialize_to_dict   s*   
z#QueryContextCache.serialize_to_dictr[   c                 C  s  | j  td|  |   |d u st|dkr0|   td |   	 W d    d S z|dt }|D ]}td|  t|dt	s\td t
dt|d  t|d	t	sttd
 t
dt|d	  t|dt	std t
dt|d  |dd }|rt|dtstd t
dt|d  | |d|d	|d| q:|   W n ty } ztd|  |   W Y d }~nd }~ww |   td |   W d    d S 1 sw   Y  d S )Nz2deserialize_json_dict() called: data from server: r   zdeserialize_json_dict() returnsrT   zdeserialize r   zid type errorz/Invalid type for 'id' field: Expected int, got rV   ztimestamp type errorz6Invalid type for 'timestamp' field: Expected int, got r   zpriority type errorz5Invalid type for 'priority' field: Expected int, got r   zcontext type errorz4Invalid type for 'context' field: Expected str, got z#deserialize_json_dict: Exception = )r8   rD   rE   rY   rF   rM   getlistr   r   r   r    r   rJ   rI   rZ   rL   )r   r[   rT   entryr   r\   r   r   r   deserialize_json_dict   sd   







"z'QueryContextCache.deserialize_json_dictc                 C  s$   | j D ]}tdt|  qd S )NzCache Entry: )r7   rD   rE   r   r<   r   r   r   rY     s   
z#QueryContextCache.log_cache_entriesc                 C  s
   t | jS r   )rF   r7   r+   r   r   r   __len__  rR   zQueryContextCache.__len__N)r3   r   r   r   )r:   r
   r   r   )rA   r
   rB   r
   r   r   r-   )r   r   )r   rN   )r   r
   )r   rS   )r[   rS   r   r   r.   )r/   r0   r1   r   r=   r@   rC   rI   rJ   rL   rM   rP   rK   r]   ra   rY   rb   r   r   r   r   r2   <   s    


	







$
Ur2   N)
__future__r   	functoolsr   hashlibr   loggingr   	threadingr   typingr   r   sortedcontainersr	   r/   rD   r
   r2   r   r   r   r   <module>   s    -