
    Dh$                         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y)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  }|| |   kD  r|}n|dz   }||k  r| j                  ||       y)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        ^/var/www/html/civilisation/venv/lib/python3.12/site-packages/mysql/connector/fabric/caching.pyinsort_right_revr   '   sm     Qw344|5z
**"v&D1*C * 
LLk"    c                   L    e Zd ZdZddefdZedd       Zed        Z	d Z
d Zy)	
CacheEntryz)Base class for MySQL Fabric cache entriesNc                 `    || _         || _        t        j                         | _        || _        y N)versionfabric_uuidr   utcnowlast_updated_ttl)selfr   r   ttls       r   __init__zCacheEntry.__init__?   s'    &$OO-	r   c                     t         )Create hash for indexing)NotImplementedErrorclspart1part2s      r   
hash_indexzCacheEntry.hash_indexE   s
     "!r   c                     | j                   sy| j                   t        | j                        z   }t        j                         |kD  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   s;       !!Idii$@@ 5((r   c                 6    t        j                         | _        y)zReset the Time to LiveN)r   r   r   r   s    r   	reset_ttlzCacheEntry.reset_ttlW   s    $OO-r   c                     d| _         y)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   ;   sC    3#:  " " 
) 
).!r   r   c                   H     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 tablec                    t        |t              st        d      t        t        |   ||       i | _        || _        g | _        g | _	        |j                  r4|j                  r'| j                  |j                  |j                         y y y )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}    %-LMMot-g:E 	. 	G	99uyy%++6 %9r   c                 .    t        | j                  |      S r   )getattrrB   )r   attrs     r   __getattr__zCacheShardTable.__getattr__q   s    t{{D))r   c                 N   | j                   dk(  rt        |      }n| j                   dk(  rA	 d|v rt        j                  |d      }n$t        j                  |d      j	                         }nD| j                   dk(  rn4| j                   dk(  rn$t        d	j                  | j                               d
|i| j                  |<   | j                          t        j                  | j                  |       t        | j                  |       y#  t        dj                  |            xY w)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(C__ 00	#:"++C1DEC"++C<AACC __.__&<CC   U"
# 	DIIs+++S1% FMM s   ?D D$c                 v    t        |j                  d      |j                  d      z         j                         S r#   zutf-8r   encode	hexdigestr%   s      r   r)   zCacheShardTable.hash_index   s/     ELL)ELL,AABLLNNr   c                 |    dj                  | 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__   s6    6==>>]]**;;	 > 
 	
r   NNr   )r3   r4   r5   r6   r!   rM   rG   r8   r)   re   __classcell__rI   s   @r   r<   r<   `   s0    07*2< 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                 L    t         t        |   d d        || _        || _        y )Nr>   )r@   rj   r!   
group_nameservers)r   rl   rm   rI   s      r   r!   zCacheGroup.__init__   s%    j$(4(H$r   c                 R    t        |j                  d            j                         S r\   r]   r%   s      r   r)   zCacheGroup.hash_index   s!     ELL)*4466r   c                 P    dj                  | 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      s$    (
 7 7
r   rj   c                   F    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y)FabricCachezfSingleton class for caching Fabric data

    Only one instance of this class can exists globally.
    c                     || _         i | _        i | _        t        j                         | _        t        j                         | _        y r   )r   	_sharding_groups	threadingLock_FabricCache__sharding_lock_FabricCache__groups_lock)r   r    s     r   r!   zFabricCache.__init__   s5    	(~~/&^^-r   c                     | j                   5  	 | j                  |= t        j                  d       ddd       y# t        $ r Y w xY w# 1 sw Y   yxY w)zRemove cache entry for groupzGroup removed from cacheN)rx   rt   _LOGGERdebugKeyErrorr   
entry_hashs     r   remove_groupzFabricCache.remove_group   sY     	::LL,
 89	: 	:  	: 	:s+   A	:A		AA	AA		Ac                     | j                   5  	 | j                  |= ddd       y# t        $ r Y w xY w# 1 sw Y   yxY w)zRemove cache entry for shardN)rw   rs   r|   r}   s     r   remove_shardtablezFabricCache.remove_shardtable   sJ    !! 	NN:.	 	  	 	s   4%	1414=Nc                 ^   t         j                  |j                  |j                        }| j                  5  	 | j
                  |   }|j                  |j                  |j                         ddd       y# t        $ r  t        |||      }|| j
                  |<   Y 1w xY w# 1 sw Y   yxY w)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
!! 	33z2##EIIu{{;	3 	3  3'w4?A-2z*	3		3 	3s(   B#5A77&B B#B  B##B,c                    t         j                  |      }| j                  5  	 | j                  |   }||_        |j                          t        j                  dj                  ||             ddd       y# t        $ rC t        ||      }|| j                  |<   t        j                  dj                  ||             Y Tw xY w# 1 sw Y   yxY w)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   s    **:6
 	**Z0 '!<CC) *	* 	*  *":w7+0Z(:AA) *	*	* 	*s*   CAA88A	CCCCCc                     t         j                  ||      }d}	 | j                  |   }|j                  r6t        j                  dj                  |             | j                  |       y	 |S # t        $ r Y yw xY w)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|    $//%@
	NN:.E}}/66u=>&&z2  	  		s   AA. .	A:9A:c                     t         j                  |      }d}	 | j                  |   }|j                  r6t        j                  dj                  |             | j                  |       y	 |S # t        $ r Y yw xY w)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  sz    **:6
	LL,E}}/66u=>!!*-  	  		s   AA- -	A98A9c                     dj                  | 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      s4     & .	:3*$""
r   rq   )r   N)r6   rY   r   r   hashlibr   loggingru    r   	getLoggerrz   r7   r   objectr   r<   rj   rq   r:   r   r   <module>r      so   0 7  (    
'

-
.
#("! "!J=
j =
@
 
$g
& g
r   