
    vd$                         d Z ddlZddlmZmZ ddlmZ ddlZddlZddlm	Z	  ej
        d          ZdZdd	Z G d
 de          Z G d de          Z G d de          Z G d de          ZdS )z0Implementing caching mechanisms for MySQL Fabric    N)datetime	timedelta)sha1   )FabricShardzmyconnpy-fabric<   c                     |dk     rt          d          |t          |           }||k     r"||z   dz  }|| |         k    r|}n|dz   }||k     "|                     ||           dS )zSimilar to bisect.insort_right but for reverse sorted lists

    This code is similar to the Python code found in Lib/bisect.py.
    We simply change the comparison from 'less than' to 'greater than'.
    r   zlow must be non-negativeN   r   )
ValueErrorleninsert)alistnew_elementlowhighmiddles        Z/var/www/html/t/fyr/venv311/lib/python3.11/site-packages/mysql/connector/fabric/caching.pyinsort_right_revr   '   s     Qww3444|5zz
***"v&&DD1*C ** 
LLk"""""    c                   Z    e Zd ZdZddefdZedd            Zed             Z	d Z
d ZdS )	
CacheEntryz)Base class for MySQL Fabric cache entriesNc                 `    || _         || _        t          j                    | _        || _        d S N)versionfabric_uuidr   utcnowlast_updated_ttl)selfr   r   ttls       r   __init__zCacheEntry.__init__?   s,    &$O--			r   c                     t           )Create hash for indexing)NotImplementedErrorclspart1part2s      r   
hash_indexzCacheEntry.hash_indexE   s
     "!r   c                 |    | j         sdS | j         t          | j                  z   }t          j                    |k    S )zReturns True if entry is not valid any longer

        This property returns True when the entry is not valid any longer.
        The entry is valid when now > (last updated + ttl), where ttl is
        in seconds.
        F)seconds)r   r   r   r   r   )r   atimes     r   invalidzCacheEntry.invalidJ   sA       	5!Idi$@$@$@@  5((r   c                 6    t          j                    | _        dS )zReset the Time to LiveN)r   r   r   r   s    r   	reset_ttlzCacheEntry.reset_ttlW   s    $O--r   c                     d| _         dS )zInvalidates the cache entryN)r   r/   s    r   
invalidatezCacheEntry.invalidate[   s     r   r   )__name__
__module____qualname____doc__
_CACHE_TTLr!   classmethodr)   propertyr-   r0   r2    r   r   r   r   ;   s        33#:     " " " [" 
) 
) X
). . .! ! ! ! !r   r   c                   N     e Zd ZdZd fd	Zd Zd Zed	d            Zd Z	 xZ
S )
CacheShardTablez&Cache entry for a Fabric sharded tableNc                 @   t          |t                    st          d          t          t          |                               ||           i | _        || _        g | _        g | _	        |j
        r)|j        r$|                     |j
        |j                   d S d S d S )Nz-shard argument must be a FabricShard instancer   r   )
isinstancer   r   superr<   r!   partitioning_shardkeyskeys_reversedkeygroupadd_partition)r   shardr   r   	__class__s       r   r!   zCacheShardTable.__init__d   s    %-- 	NLMMMot$$--g:E 	. 	G 	G 	G	9 	7 	7uy%+66666	7 	7 	7 	7r   c                 ,    t          | j        |          S r   )getattrrB   )r   attrs     r   __getattr__zCacheShardTable.__getattr__q   s    t{D)))r   c                 B   | j         dk    rt          |          }n| j         dk    rj	 d|v rt          j        |d          }n't          j        |d                                          }nf#  t          d                    |                    xY w| j         dk    rn3| j         dk    rn't          d	                    | j                             d
|i| j        |<   |                                  t          j
        | j        |           t          | j        |           dS )z$Add sharding information for a groupRANGERANGE_DATETIME:z%Y-%m-%d %H:%M:%Sz%Y-%m-%dz0RANGE_DATETIME key could not be parsed, was: {0}RANGE_STRINGHASHzUnsupported sharding type {0}rF   N)
shard_typeintr   strptimedater   formatrA   r0   bisectinsort_rightrC   r   rD   )r   rE   rF   s      r   rG   zCacheShardTable.add_partitiont   s?   ?g%%c((CC_ 000	#::"+C1DEECC"+C<<AACCC FMM    _.._&&<CC     U"
# 	DIs++++S11111s   AA* *$Bc                     t          |                    d          |                    d          z                                             S r#   zutf-8r   encode	hexdigestr%   s      r   r)   zCacheShardTable.hash_index   s;     ELL))ELL,A,AABBLLNNNr   c                 \    d                     | j        | j        | j        | j                  S )Nz%{class_}({database}.{table}.{column}))class_databasetablecolumn)rX   rI   rb   rc   rd   r/   s    r   __repr__zCacheShardTable.__repr__   s3    6==>]*;	 > 
 
 	
r   NNr   )r3   r4   r5   r6   r!   rM   rG   r8   r)   re   __classcell__rI   s   @r   r<   r<   `   s        007 7 7 7 7 7* * *2 2 2< O O O [O
 
 
 
 
 
 
r   r<   c                   @     e Zd ZdZ fdZedd            Zd Z xZS )
CacheGroupzCache entry for a Fabric groupc                 x    t          t          |                               d d            || _        || _        d S )Nr>   )r@   rj   r!   
group_nameservers)r   rl   rm   rI   s      r   r!   zCacheGroup.__init__   s8    j$((4(HHH$r   Nc                 j    t          |                    d                                                    S r\   r]   r%   s      r   r)   zCacheGroup.hash_index   s*     ELL))**44666r   c                 D    d                     | j        | j                  S )Nz{class_}({group}))ra   rF   )rX   rI   rl   r/   s    r   re   zCacheGroup.__repr__   s)    "))>/ * 
 
 	
r   r   )	r3   r4   r5   r6   r!   r8   r)   re   rg   rh   s   @r   rj   rj      sm        ((    
 7 7 7 [7
 
 
 
 
 
 
r   rj   c                   H    e Zd ZdZefdZd Zd ZddZd Z	d Z
d	 Zd
 ZdS )FabricCachezfSingleton class for caching Fabric data

    Only one instance of this class can exists globally.
    c                     || _         i | _        i | _        t          j                    | _        t          j                    | _        d S r   )r   	_sharding_groups	threadingLock_FabricCache__sharding_lock_FabricCache__groups_lock)r   r    s     r   r!   zFabricCache.__init__   s=    	(~//&^--r   c                     | j         5  	 | j        |= t                              d           n# t          $ r Y nw xY wddd           dS # 1 swxY w Y   dS )zRemove cache entry for groupzGroup removed from cacheN)rx   rt   _LOGGERdebugKeyErrorr   
entry_hashs     r   remove_groupzFabricCache.remove_group   s     	: 	::L,
 89999	    	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s,   A
-A

:A
:A

AAc                 z    | j         5  	 | j        |= n# t          $ r Y nw xY wddd           dS # 1 swxY w Y   dS )zRemove cache entry for shardN)rw   rs   r|   r}   s     r   remove_shardtablezFabricCache.remove_shardtable   s    ! 	 	N:..   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s$   00
 0 044Nc                 F   t                               |j        |j                  }| j        5  	 | j        |         }|                    |j        |j                   n,# t          $ r t          |||          }|| j        |<   Y nw xY wddd           dS # 1 swxY w Y   dS )zCache information about a shardr>   N)
r<   r)   rb   rc   rw   rs   rG   rE   rF   r|   )r   rH   r   r   r~   entrys         r   sharding_cache_tablez FabricCache.sharding_cache_table   s   $//LL
! 	3 	33z2##EIu{;;;; 3 3 3'w4?A A A-2z***	3		3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s4   B-AB&BBBBBBc                    t                               |          }| j        5  	 | j        |         }||_        |                                 t                              d                    ||                     nX# t          $ rK t          ||          }|| j        |<   t                              d                    ||                     Y nw xY wddd           dS # 1 swxY w Y   dS )zCache information about a groupzRecaching group {0} with {1}zCaching group {0} with {1}N)
rj   r)   rx   rt   rm   r0   rz   r{   rX   r|   )r   rl   rm   r~   r   s        r   cache_groupzFabricCache.cache_group   sK   **:66
 	* 	**Z0 '!!!<CC) ) * * * * * * *":w77+0Z(:AA) ) * * * * *	*	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s6   C AA;:C ;ACC CC  C$'C$c                    t                               ||          }d}	 | j        |         }|j        rDt                              d                    |                     |                     |           dS n# t          $ r Y dS w xY w|S )z4Search cache for a shard based on database and tableN{0} invalidated)	r<   r)   rs   r-   rz   r{   rX   r   r|   )r   rb   rc   r~   r   s        r   sharding_searchzFabricCache.sharding_search   s    $//%@@
	N:.E} /66u==>>>&&z222t  	 	 	44	 s   AA8 8
BBc                    t                               |          }d}	 | j        |         }|j        rDt                              d                    |                     |                     |           dS n# t          $ r Y dS w xY w|S )z*Search cache for a group based on its nameNr   )	rj   r)   rt   r-   rz   r{   rX   r   r|   )r   rl   r~   r   s       r   group_searchzFabricCache.group_search  s    **:66
	L,E} /66u==>>>!!*---t  	 	 	44	 s   AA7 7
BBc                     d                     | j        t          | j                  t          | j                            S )Nz-{class_}(groups={nrgroups},shards={nrshards}))ra   nrgroupsnrshards)rX   rI   r   rt   rs   r/   s    r   re   zFabricCache.__repr__  s?    >EE>&&(( F 
 
 	
r   rf   )r3   r4   r5   r6   r7   r!   r   r   r   r   r   r   re   r:   r   r   rq   rq      s          & . . . .	: 	: 	:  3 3 3 3* * *$  "  "
 
 
 
 
r   rq   )r   N)r6   rY   r   r   hashlibr   loggingru    r   	getLoggerrz   r7   r   objectr   r<   rj   rq   r:   r   r   <module>r      sS  0 7 6  ( ( ( ( ( ( ( (                 
'
-
.
.
# # # #("! "! "! "! "! "! "! "!J=
 =
 =
 =
 =
j =
 =
 =
@
 
 
 
 
 
 
 
$g
 g
 g
 g
 g
& g
 g
 g
 g
 g
r   