o
    UhZ=                     @   s   d Z ddlmZmZ ddlmZmZmZmZ e	 Z
edZedZG dd deeef ZG dd	 d	eeef ZG d
d deeef ZdS )z)A simple least-recently-used (LRU) cache.    )IterableIterator)CallableGenericOptionalTypeVarKVc                   @   sf   e Zd ZU dZdZed ed< eed< ee ed< dded	e	d
dfddZ
d
efddZdddZdS )_LRUNodez:This maintains the linked-list which is the lru internals.)cleanupkeynext_keyprevsizevaluez_LRUNode[K, V]r   r   r   Nr   r   returnc                 C   s(   d | _ t| _|| _|| _|| _d | _d S N)r   	_null_keyr   r   r   r   r   )selfr   r   r    r   e/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/lru_cache.py__init__,   s   
z_LRUNode.__init__c                 C   s<   | j d u rd }n| j j}| jj d| jd| jd|dS )N(z n:z p:))r   r   	__class____name__r   )r   prev_keyr   r   r   __repr__7   s   
$z_LRUNode.__repr__c                 C   s(   | j d ur|  | j| j d | _ | `d S r   )r   r   r   r   r   r   r   run_cleanup>   s   
z_LRUNode.run_cleanupr   r   N)r   
__module____qualname____doc__	__slots__r   __annotations__r   intr	   r   strr   r   r   r   r   r   r
   #   s   
 r
   c                
   @   s  e Zd ZU dZeeeef  ed< eeeef  ed< 	d2de	dee	 ddfd	d
Z
dedefddZdedefddZde	fddZdeeeef  fddZ	d3dededeeeegdf  ddfddZde	fddZd3dedee dee fddZdee fddZdeeef fdd Zd4d!d"Zdededdfd#d$Zd%eeef ddfd&d'Zd%eeef ddfd(d)Zd4d*d+Zd4d,d-Zd3de	dee	 ddfd.d/Zd3d4d0d1Z dS )5LRUCachez?A class which manages a cache of entries, removing unused ones._least_recently_used_most_recently_usedd   N	max_cacheafter_cleanup_countr   c                 C   s"   i | _ d | _d | _| || d S r   )_cacher*   r)   _update_max_cacher   r,   r-   r   r   r   r   L   s   zLRUCache.__init__r   c                 C   s
   || j v S r   )r.   )r   r   r   r   r   __contains__V      
zLRUCache.__contains__c                 C   s   | j }|| }| j}||u r|jS |j}|j}|tu r|| _n|| }||_|s*J |s.J ||_|j|_||_|| _d |_|jS r   )r.   r*   r   r   r   r   r)   r   )r   r   cachenodemru	node_prevr   	node_nextr   r   r   __getitem__Y   s&   zLRUCache.__getitem__c                 C   s
   t | jS r   )lenr.   r   r   r   r   __len__z   r2   zLRUCache.__len__c                 c   s    | j }|dur|jdurtd| |duri|jtu r,|| jur)td| d}n| j|j }|j|ur>td| |jdu rP|| j urOtd| n|jj|jkr^td| |V  |}|dusdS dS )z2Walk the LRU list, only meant to be used in tests.NzGthe _most_recently_used entry is not supposed to have a previous entry z.only the last node should have no next value: z-inconsistency found, node.next.prev != node: z>only the _most_recently_used should not have a previous node: z-inconsistency found, node.prev.next != node: )r*   r   AssertionErrorr   r   r)   r.   r   r   r4   r7   r   r   r   	_walk_lru}   sH   





zLRUCache._walk_lrur   r   c                 C   s|   |t u rtd|| jv r| j| }|  ||_||_nt|||d}|| j|< | | t| j| j	kr<|   dS dS )]  Add a new value to the cache.

        Also, if the entry is ever removed from the cache, call
        cleanup(key, value).

        Args:
          key: The key to store it under
          value: The object to store
          cleanup: None or a function taking (key, value) to indicate
                        'value' should be cleaned up.
        cannot use _null_key as a keyr   N)
r   
ValueErrorr.   r   r   r   r
   _record_accessr9   
_max_cache)r   r   r   r   r4   r   r   r   add   s   



zLRUCache.addc                 C   s   | j S )z(Get the number of entries we will cache.)rC   r   r   r   r   
cache_size   s   zLRUCache.cache_sizedefaultc                 C   s*   | j |d }|d u r|S | | |jS r   )r.   getrB   r   )r   r   rF   r4   r   r   r   rG      s
   
zLRUCache.getc                 C   s
   | j  S )a  Get the list of keys currently cached.

        Note that values returned here may not be available by the time you
        request them later. This is simply meant as a peak into the current
        state.

        Returns: An unordered list of keys that are currently cached.
        )r.   keysr   r   r   r   rH      s   
	zLRUCache.keysc                 C   s   dd | j  D S )z"Get the key:value pairs as a dict.c                 S   s   i | ]\}}||j qS r   )r   ).0knr   r   r   
<dictcomp>   s    z"LRUCache.items.<locals>.<dictcomp>)r.   itemsr   r   r   r   rM         zLRUCache.itemsc                 C   s0   t | j| jkr|   t | j| jksdS dS )zClear the cache until it shrinks to the requested size.

        This does not completely wipe the cache, just makes sure it is under
        the after_cleanup_count.
        N)r9   r.   _after_cleanup_count_remove_lrur   r   r   r   r      s   zLRUCache.cleanupc                 C   s   | j ||dd dS )z<Add a value to the cache, there will be no cleanup function.Nr@   )rD   )r   r   r   r   r   r   __setitem__   rN   zLRUCache.__setitem__r4   c                 C   s   | j du r|| _ || _dS || j u rdS || ju r|j| _|jdur'|j|j_|jtur6| j|j }|j|_| j j|_|| j _|| _ d|_dS )zRecord that key was accessed.N)r*   r)   r   r   r   r.   r   r<   r   r   r   rB      s"   







zLRUCache._record_accessc                 C   sz   || j u r	|j| _ | j|j | j d u rd | _|  |jd ur&|j|j_|jtur5| j|j }|j|_d |_t|_d S r   )	r)   r   r.   popr   r*   r   r   r   r<   r   r   r   _remove_node  s   





zLRUCache._remove_nodec                 C   s   | j sJ | | j  dS )zRemove one entry from the lru, and handle consequences.

        If there are no more references to the lru, then this entry should be
        removed from the cache.
        N)r)   rS   r   r   r   r   rP     s   
zLRUCache._remove_lruc                 C   s   | j r|   | j sdS dS )zClear out all of the cache.N)r.   rP   r   r   r   r   clear  s   zLRUCache.clearc                 C   s   | j ||d dS )z1Change the number of entries that will be cached.)r-   N)r/   r0   r   r   r   resize#  s   zLRUCache.resizec                 C   s:   || _ |d u r| j d d | _nt|| j | _|   d S N   
   )rC   rO   minr   r0   r   r   r   r/   '  s
   zLRUCache._update_max_cache)r+   Nr   r    )!r   r!   r"   r#   r   r
   r   r	   r%   r&   r   boolr1   r8   r:   r   r=   r   rD   rE   rG   r   rH   dictrM   r   rQ   rB   rS   rP   rT   rU   r/   r   r   r   r   r(   F   sL   
 

!&
 



	r(   c                
   @   s   e Zd ZU dZeegef ed< 			ddedee deeegef  ddfd	d
Z		dde
dedeee
egdf  ddfddZdddZdee
ef ddfddZddedee ddfddZ	ddedee ddfddZdS )LRUSizeCacheaU  An LRUCache that removes things based on the size of the values.

    This differs in that it doesn't care how many actual items there are,
    it just restricts the cache to be cleaned up after so much data is stored.

    The size of items added will be computed using compute_size(value), which
    defaults to len() if not supplied.
    _compute_size   Nmax_sizeafter_cleanup_sizecompute_sizer   c                 C   sJ   d| _ |du rt| _n|| _| j||d tj| tt|d dd dS )a  Create a new LRUSizeCache.

        Args:
          max_size: The max number of bytes to store before we start
            clearing out entries.
          after_cleanup_size: After cleaning up, shrink everything to this
            size.
          compute_size: A function to compute the size of the values. We
            use a function here, so that you can pass 'len' if you are just
            using simple strings, or a more complex function if you are using
            something like a list of strings, or even a custom object.
            The function should take the form "compute_size(value) => integer".
            If not supplied, it defaults to 'len()'
        r   Nr`         )r,   )_value_sizer9   r]   _update_max_sizer(   r   maxr&   )r   r_   r`   ra   r   r   r   r   <  s    zLRUSizeCache.__init__r   r   r   c                 C   s   |t u rtd| j|d}| |}|| jkr-|dur"| | |dur+||| dS |du r>t|||d}|| j|< n|jdusEJ |  j	|j8  _	||_|  j	|7  _	| 
| | j	| jkrh|   dS dS )r>   r?   Nr@   )r   rA   r.   rG   r]   _after_cleanup_sizerS   r
   r   re   rB   	_max_sizer   )r   r   r   r   r4   	value_lenr   r   r   rD   X  s*   




zLRUSizeCache.addc                 C   s(   | j | jkr|   | j | jksdS dS )zClear the cache until it shrinks to the requested size.

        This does not completely wipe the cache, just makes sure it is under
        the after_cleanup_size.
        N)re   rh   rP   r   r   r   r   r     s   zLRUSizeCache.cleanupr4   c                 C   s.   |j d usJ |  j|j 8  _t| | d S r   )r   re   r(   rS   )r   r4   r   r   r   rS     s   zLRUSizeCache._remove_nodec                 C   s.   | j ||d tt|d d}| | dS )z/Change the number of bytes that will be cached.rb   rc   rd   N)rf   rg   r&   r/   )r   r_   r`   r,   r   r   r   rU     s   zLRUSizeCache.resizec                 C   s4   || _ |d u r| j d d | _d S t|| j | _d S rV   )ri   rh   rY   )r   r_   r`   r   r   r   rf     s   zLRUSizeCache._update_max_size)r^   NNr   r    )r   r!   r"   r#   r   r	   r&   r%   r   r   r   rD   r   r
   rS   rU   rf   r   r   r   r   r\   0  sH   
 	


)
r\   N)r#   collections.abcr   r   typingr   r   r   r   objectr   r   r	   r
   r(   r\   r   r   r   r   <module>   s   # k