B
    Ñš[†$  ã               @   s    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 
d¡ZdZdd	d
„ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z0Implementing caching mechanisms for MySQL Fabricé    N)ÚdatetimeÚ	timedelta)Úsha1é   )ÚFabricShardzmyconnpy-fabricé<   c             C   sd   |dk rt dƒ‚|dkr t| ƒ}x2||k rR|| d }|| | krH|}q"|d }q"W |  ||¡ dS )zÍSimilar 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)ÚalistZnew_elementÚlowÚhighZmiddle© r   ú¢C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-ioykiiwb\mysql-connector-python-rf\mysql_connector_python_rf-2.2.2.data\purelib\mysql\connector\fabric\caching.pyÚinsort_right_rev'   s    
r   c               @   sJ   e Zd ZdZddefdd„Zeddd„ƒZedd„ ƒZ	d	d
„ Z
dd„ ZdS )Ú
CacheEntryz)Base class for MySQL Fabric cache entriesNc             C   s    || _ || _t ¡ | _|| _d S )N)ÚversionÚfabric_uuidr   ÚutcnowÚlast_updatedÚ_ttl)Úselfr   r   Úttlr   r   r   Ú__init__?   s    
zCacheEntry.__init__c             C   s   t ‚dS )zCreate hash for indexingN)ÚNotImplementedError)ÚclsÚpart1Úpart2r   r   r   Ú
hash_indexE   s    zCacheEntry.hash_indexc             C   s(   | j s
dS | j t| jd� }t ¡ |kS )zßReturns 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   Úatimer   r   r   ÚinvalidJ   s    zCacheEntry.invalidc             C   s   t  ¡ | _dS )zReset the Time to LiveN)r   r   r   )r   r   r   r   Ú	reset_ttlW   s    zCacheEntry.reset_ttlc             C   s
   d| _ dS )zInvalidates the cache entryN)r   )r   r   r   r   Ú
invalidate[   s    zCacheEntry.invalidate)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
_CACHE_TTLr   Úclassmethodr   Úpropertyr"   r#   r$   r   r   r   r   r   ;   s   r   c                   sH   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zedd	d
„ƒZdd„ Z	‡  Z
S )ÚCacheShardTablez&Cache entry for a Fabric sharded tableNc                s^   t |tƒstdƒ‚tt| ƒj||d� i | _|| _g | _g | _	|j
rZ|jrZ|  |j
|j¡ 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__r   r   r   d   s    
zCacheShardTable.__init__c             C   s   t | j|ƒS )N)Úgetattrr0   )r   Úattrr   r   r   Ú__getattr__q   s    zCacheShardTable.__getattr__c             C   sÂ   | j dkrt|ƒ}nz| j dkrfy*d|kr6t |d¡}nt |d¡ ¡ }W qŽ   td |¡ƒ‚Y qŽX n(| j dkrrn| j dkr~ntd	 | j ¡ƒ‚d
|i| j|< |  ¡  t	 
| j|¡ t| j|ƒ dS )z$Add sharding information for a groupÚRANGEZRANGE_DATETIMEú:z%Y-%m-%d %H:%M:%Sz%Y-%m-%dz0RANGE_DATETIME key could not be parsed, was: {0}ZRANGE_STRINGÚHASHzUnsupported sharding type {0}r4   N)Z
shard_typeÚintr   ÚstrptimeÚdater	   Úformatr/   r#   ÚbisectÚinsort_rightr1   r   r2   )r   r3   r4   r   r   r   r5   t   s*    





zCacheShardTable.add_partitionc             C   s   t | d¡| d¡ ƒ ¡ S )zCreate hash for indexingzutf-8)r   ÚencodeÚ	hexdigest)r   r   r   r   r   r   r   ’   s    zCacheShardTable.hash_indexc             C   s   dj | j| j| j| jd�S )Nz%{class_}({database}.{table}.{column}))Úclass_ÚdatabaseÚtableÚcolumn)rA   r7   rG   rH   rI   )r   r   r   r   Ú__repr__—   s
    zCacheShardTable.__repr__)NN)N)r%   r&   r'   r(   r   r:   r5   r*   r   rJ   Ú__classcell__r   r   )r7   r   r,   `   s   r,   c                   s6   e Zd ZdZ‡ fdd„Zed	dd„ƒZdd„ Z‡  ZS )
Ú
CacheGroupzCache entry for a Fabric groupc                s$   t t| ƒjd d d� || _|| _d S )N)r   r   )r.   rL   r   Ú
group_nameÚservers)r   rM   rN   )r7   r   r   r   ¢   s    zCacheGroup.__init__Nc             C   s   t | d¡ƒ ¡ S )zCreate hash for indexingzutf-8)r   rD   rE   )r   r   r   r   r   r   r   §   s    zCacheGroup.hash_indexc             C   s   dj | j| jd�S )Nz{class_}({group}))rF   r4   )rA   r7   rM   )r   r   r   r   rJ   ¬   s    zCacheGroup.__repr__)N)	r%   r&   r'   r(   r   r*   r   rJ   rK   r   r   )r7   r   rL       s
   rL   c               @   sV   e Zd ZdZef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S )ÚFabricCachezfSingleton class for caching Fabric data

    Only one instance of this class can exists globally.
    c             C   s*   || _ i | _i | _t ¡ | _t ¡ | _d S )N)r   Ú	_shardingÚ_groupsÚ	threadingÚLockÚ_FabricCache__sharding_lockÚ_FabricCache__groups_lock)r   r   r   r   r   r   ·   s
    
zFabricCache.__init__c          	   C   sB   | j �2 y| j|= W n tk
r(   Y nX t d¡ W dQ R X dS )zRemove cache entry for groupzGroup removed from cacheN)rU   rQ   ÚKeyErrorÚ_LOGGERÚdebug)r   Ú
entry_hashr   r   r   Úremove_group¾   s    zFabricCache.remove_groupc          	   C   s8   | j �( y| j|= W n tk
r(   Y nX W dQ R X dS )zRemove cache entry for shardN)rT   rP   rV   )r   rY   r   r   r   Úremove_shardtableÉ   s
    zFabricCache.remove_shardtableNc          	   C   sr   t  |j|j¡}| j�R y| j| }| |j|j¡ W n, t	k
rb   t |||d�}|| j|< Y nX W dQ R X dS )zCache information about a shard)r   r   N)
r,   r   rG   rH   rT   rP   r5   r3   r4   rV   )r   r6   r   r   rY   Úentryr   r   r   Úsharding_cache_tableÒ   s    
z FabricCache.sharding_cache_tablec          
   C   sŠ   t  |¡}| j�p y.| j| }||_| ¡  t d ||¡¡ W n: t	k
rz   t ||ƒ}|| j|< t d ||¡¡ Y nX W dQ R X dS )zCache information about a groupzRecaching group {0} with {1}zCaching group {0} with {1}N)
rL   r   rU   rQ   rN   r#   rW   rX   rA   rV   )r   rM   rN   rY   r\   r   r   r   Úcache_groupà   s    



zFabricCache.cache_groupc             C   s\   t  ||¡}d}y2| j| }|jr@t d |¡¡ |  |¡ dS W n tk
rV   dS X |S )z4Search cache for a shard based on database and tableNz{0} invalidated)	r,   r   rP   r"   rW   rX   rA   r[   rV   )r   rG   rH   rY   r\   r   r   r   Úsharding_searchò   s    

zFabricCache.sharding_searchc             C   sZ   t  |¡}d}y2| j| }|jr>t d |¡¡ |  |¡ dS W n tk
rT   dS X |S )z*Search cache for a group based on its nameNz{0} invalidated)	rL   r   rQ   r"   rW   rX   rA   rZ   rV   )r   rM   rY   r\   r   r   r   Úgroup_search  s    


zFabricCache.group_searchc             C   s   dj | jt| jƒt| jƒd�S )Nz-{class_}(groups={nrgroups},shards={nrshards}))rF   ZnrgroupsZnrshards)rA   r7   r
   rQ   rP   )r   r   r   r   rJ     s    zFabricCache.__repr__)NN)r%   r&   r'   r(   r)   r   rZ   r[   r]   r^   r_   r`   rJ   r   r   r   r   rO   ²   s   	
rO   )r   N)r(   rB   r   r   Úhashlibr   ÚloggingrR   Ú r   Ú	getLoggerrW   r)   r   Úobjectr   r,   rL   rO   r   r   r   r   Ú<module>   s   

%@