
    T8                     X    S r \" 5       r " S S\5      r " S S\5      r " S S\5      rg)z)A simple least-recently-used (LRU) cache.c                   2    \ rS rSrSrSrS	S jrS rS rSr	g)
_LRUNode   z:This maintains the linked-list which is the lru internals.)prevnext_keykeyvaluecleanupsizeNc                 Z    S U l         [        U l        Xl        X l        X0l        S U l        g N)r   	_null_keyr   r   r   r	   r
   )selfr   r   r	   s       $lib/third_party/dulwich/lru_cache.py__init___LRUNode.__init__   s(    	!
 	    c                     U R                   c  S nOU R                   R                  nU R                  R                  < SU R                  < SU R                  < SU< S3$ )N(z n:z p:))r   r   	__class____name__r   )r   prev_keys     r   __repr___LRUNode.__repr__*   sF    99Hyy}}HNN##HHMM	
 	
r   c                     U R                   b&  U R                  U R                  U R                  5        S U l         S U l        g r   )r	   r   r   r   s    r   run_cleanup_LRUNode.run_cleanup6   s0    <<#LL4::.
r   )r	   r   r   r   r
   r   r   )
r   
__module____qualname____firstlineno____doc__	__slots__r   r   r   __static_attributes__ r   r   r   r      s    DGI	

r   r   c                       \ rS rSrSrSS jrS rS rS rS r	SS	 jr
S
 rSS jrS rS rS rS rS rS rS rS rSS jrSS jrSrg)LRUCache>   z?A class which manages a cache of entries, removing unused ones.Nc                 P    0 U l         S U l        S U l        U R                  X5        g r   )_cache_most_recently_used_least_recently_used_update_max_cacher   	max_cacheafter_cleanup_counts      r   r   LRUCache.__init__A   s'    #' $(!y>r   c                     XR                   ;   $ r   )r*   )r   r   s     r   __contains__LRUCache.__contains__I   s    kk!!r   c                 *   U R                   nX!   nU R                  nX4L a  UR                  $ UR                  nUR                  nU[
        L a  XPl        O
X&   nXWl        Xel        UR                  Ul        X4l        X0l        S Ul        UR                  $ r   )r*   r+   r   r   r   r   r,   r   )r   r   cachenodemru	node_prevr   	node_nexts           r   __getitem__LRUCache.__getitem__L   s    z
 &&;::II	== y  )2%I&N%#' 	zzr   c                 ,    [        U R                  5      $ r   )lenr*   r   s    r   __len__LRUCache.__len__k   s    4;;r   c              #     #    U R                   nUb  UR                  b  [        SU< 35      eUb  UR                  [        L a   XR
                  La  [        SU< 35      eSnO7U R                  UR                     nUR                  ULa  [        SU< 35      eUR                  c  XR                   La  [        SU< 35      eO3UR                  R                  UR                  :w  a  [        SU< 35      eUv   UnUb  M  gg7f)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   r7   r:   s      r   	_walk_lruLRUCache._walk_lrun   s    ''yy$$ "$ 
 }}	)888(QUW  !	 KK6	>>-(PTV  yy 777(;?B  8 99%%1(PTV  JD3 s   D DDc                 \   U[         L a  [        S5      eXR                  ;   a,  U R                  U   nUR                  5         X$l        X4l        O[        XUS9nX@R                  U'   U R                  U5        [        U R                  5      U R                  :  a  U R                  5         gg)  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_accessr>   
_max_cache)r   r   r   r	   r7   s        r   addLRUCache.add   s     )<==++;;s#DJ"LC8D#KKD!t{{doo-LLN .r   c                     U R                   $ )z(Get the number of entries we will cache.)rL   r   s    r   
cache_sizeLRUCache.cache_size   s    r   c                 ~    U R                   R                  US 5      nUc  U$ U R                  U5        UR                  $ r   )r*   getrK   r   )r   r   defaultr7   s       r   rS   LRUCache.get   s8    {{sD)<ND!zzr   c                 6    U R                   R                  5       $ )zGet 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   s    r   rW   LRUCache.keys   s     {{!!r   c                 V    [        S U R                  R                  5        5       5      $ )z"Get the key:value pairs as a dict.c              3   @   #    U  H  u  pXR                   4v   M     g 7fr   )r   ).0kns      r   	<genexpr>!LRUCache.items.<locals>.<genexpr>   s     A-@TQQL-@s   )dictr*   itemsr   s    r   ra   LRUCache.items   s     AT[[->->-@AAAr   c                     [        U R                  5      U R                  :  a6  U R                  5         [        U R                  5      U R                  :  a  M5  gg)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)r>   r*   _after_cleanup_count_remove_lrur   s    r   r	   LRUCache.cleanup   s@     $++!:!:: $++!:!::r   c                 $    U R                  XSS9  g)z<Add a value to the cache, there will be no cleanup function.NrI   )rM   )r   r   r   s      r   __setitem__LRUCache.__setitem__   s    T*r   c                    U R                   c  Xl         Xl        gXR                   L a  gXR                  L a  UR                  U l        UR                  b  UR                  UR                  l        UR                  [        La*  U R
                  UR                     nUR                  Ul        U R                   R                  Ul        XR                   l        Xl         SUl        g)zRecord that key was accessed.N)r+   r,   r   r   r   r*   r   rC   s      r   rK   LRUCache._record_access   s     ##+'+$(,%--- ,,,(,		D%99 !%DII==	)DMM2I!YYIN0044(,  %#' 	r   c                    XR                   L a  UR                  U l         U R                  R                  UR                  5        U R                   c  S U l        UR                  5         UR                  b  UR                  UR                  l        UR                  [        La*  U R                  UR                     nUR                  Ul        S Ul        [        Ul        g r   )	r,   r   r*   popr   r+   r   r   r   rC   s      r   _remove_nodeLRUCache._remove_node   s    ,,,(,		D%!$$,'+D$99 !%DII==	)DMM2I!YYIN	!r   c                 :    U R                  U R                  5        g)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)rn   r,   r   s    r   re   LRUCache._remove_lru  s     	$334r   c                 n    U R                   (       a$  U R                  5         U R                   (       a  M#  gg)zClear out all of the cache.N)r*   re   r   s    r   clearLRUCache.clear  s"     kk kkkr   c                 "    U R                  XS9  g)z1Change the number of entries that will be cached.)r0   N)r-   r.   s      r   resizeLRUCache.resize  s    yRr   c                     Xl         Uc  U R                   S-  S-  U l        O[        X R                   5      U l        U R                  5         g N   
   )rL   rd   minr	   r.   s      r   r-   LRUCache._update_max_cache  s>    #&(,!(;b(@D%(+,?(QD%r   )rd   r*   r,   rL   r+   )d   Nr   )r   r   r    r!   r"   r   r3   r;   r?   rD   rM   rP   rS   rW   ra   r	   rh   rK   rn   re   rs   rv   r-   r$   r%   r   r   r'   r'   >   sb    I?"> #J8	"B+4"$5Sr   r'   c                   N    \ rS rSrSr SS jrSS jrS rS rSS jr	SS	 jr
S
rg)LRUSizeCachei  aA  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.
Nc           	          SU l         X0l        Uc  [        U l        U R                  XS9  [        R                  U [        [        US-  5      S5      S9  g)aD  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()'
    Nafter_cleanup_size      )r/   )_value_size_compute_sizer>   _update_max_sizer'   r   maxint)r   max_sizer   compute_sizes       r   r   LRUSizeCache.__init__(  sT    " )!$DhN$#c(S..A1*EFr   c                    U[         L a  [        S5      eU R                  R                  US5      nU R	                  U5      nXPR
                  :  a   Ub  U R                  U5        Ub  U" X5        gUc  [        XUS9nX@R                  U'   OU =R                  UR                  -  sl        XTl	        U =R                  U-  sl        U R                  U5        U R                  U R                  :  a  U R                  5         gg)rG   rH   NrI   )r   rJ   r*   rS   r   _after_cleanup_sizern   r   r   r
   rK   	_max_sizer	   )r   r   r   r	   r7   	value_lens         r   rM   LRUSizeCache.add@  s     )<=={{sD)&&u-	000 !!$'"#<C8D#KK		)	I%D!dnn,LLN -r   c                     U R                   U R                  :  a-  U R                  5         U R                   U R                  :  a  M,  gg)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)r   r   re   r   s    r   r	   LRUSizeCache.cleanupf  s:     !9!99 !9!99r   c                 l    U =R                   UR                  -  sl         [        R                  X5        g r   )r   r
   r'   rn   )r   r7   s     r   rn   LRUSizeCache._remove_nodep  s%    DII%d)r   c                 t    U R                  XS9  [        [        US-  5      S5      nU R                  U5        g)z/Change the number of bytes that will be cached.r   r   r   N)r   r   r   r-   )r   r   r   r/   s       r   rv   LRUSizeCache.resizet  s6    hNHsN+Q/	y)r   c                 z    Xl         Uc  U R                   S-  S-  U l        g [        X R                   5      U l        g ry   )r   r   r|   )r   r   r   s      r   r   LRUSizeCache._update_max_sizez  s5    !%'+~~'9R'?D$'*+=~~'ND$r   )r   r   r   r   )i   NNr   )r   r   r    r!   r"   r   rM   r	   rn   rv   r   r$   r%   r   r   r   r     s/     KOG0$L**Or   r   N)r"   objectr   r   r'   r   r%   r   r   <module>r      s=   * 0H	!v !H]v ]@aO8 aOr   