
    .Phu                         d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlZddlZdZ G d	 d
eeee          ZdS )z/Module containing a database to deal with packs    )
FileDBBase	ObjectDBR	CachingDB)	LazyMixin)	BadObjectUnsupportedOperationAmbiguousObjectName)
PackEntity)reduceN)PackedDBc                   p     e Zd ZdZdZ fdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZddZd Zd Z xZS )r   z-A database operating on a set of object packsi  c                 f    t                                          |           d| _        d| _        d S Nr   )super__init__
_hit_count	_st_mtime)self	root_path	__class__s     M/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/gitdb/db/pack.pyr   zPackedDB.__init__)   s/    ###     c                 h    |dk    r+t                      | _        |                     d           d S d S )N	_entitiesT)force)listr   update_cache)r   attrs     r   _set_cache_zPackedDB._set_cache_3   s=    ;!VVDND))))) r   c                 @    | j                             d d           d S )Nc                     | d         S r    )ls    r   <lambda>z)PackedDB._sort_entities.<locals>.<lambda>:   s
    !A$ r   T)keyreverse)r   sortr   s    r   _sort_entitieszPackedDB._sort_entities9   s%    =====r   c                     | j         | j        z  dk    r|                                  | j        D ]A} |d         |          }|,|dxx         dz  cc<   | xj         dz  c_         |d         |fc S Bt	          |          )a  :return: tuple(entity, index) for an item at the given sha
        :param sha: 20 or 40 byte sha
        :raise BadObject:
        **Note:** This method is not thread-safe, but may be hit in multi-threaded
            operation. The worst thing that can happen though is a counter that
            was not incremented, or the list being in wrong order. So we safe
            the time for locking here, lets see how that goesr      N   )r   _sort_intervalr)   r   r   )r   shaitemindexs       r   
_pack_infozPackedDB._pack_info<   s     ?T00A55!!! N 	( 	(DDGCLLE Q11$Q'''' ! nnr   c                 T    	 |                      |           dS # t          $ r Y dS w xY w)NTF)r1   r   )r   r.   s     r   
has_objectzPackedDB.has_objectY   s@    	OOC   4 	 	 	55	s    
''c                 \    |                      |          \  }}|                    |          S N)r1   info_at_indexr   r.   entityr0   s       r   infozPackedDB.infoa   s+    ,,##E***r   c                 \    |                      |          \  }}|                    |          S r5   )r1   stream_at_indexr7   s       r   streamzPackedDB.streame   s+    ,,%%e,,,r   c              #      K   |                                  D ]N}|                                }|j        }t          |                                          D ]} ||          V  Od S r5   )entitiesr0   r.   rangesize)r   r8   r0   sha_by_indexs       r   sha_iterzPackedDB.sha_iteri   sx      mmoo 	* 	*FLLNNE 9Luzz||,, * *"l5))))))*	* 	*r   c                 H    d | j         D             }t          d |d          S )Nc                 f    g | ].}|d                                                                           /S r,   )r0   r@   .0r/   s     r   
<listcomp>z!PackedDB.size.<locals>.<listcomp>s   s0    CCCDa%%''CCCr   c                     | |z   S r5   r"   )xys     r   r$   zPackedDB.size.<locals>.<lambda>t   s
    1q5 r   r   )r   r   )r   sizess     r   r@   zPackedDB.sizer   s-    CCDNCCC((%333r   c                     t                      )zStoring individual objects is not feasible as a pack is designed to
        hold multiple objects. Writing or rewriting packs for single objects is
        inefficient)r   )r   istreams     r   storezPackedDB.storez   s     #$$$r   Fc                 ,   t          j        |                                           }|s|j        | j        k    rdS |j        | _        t          t          j        t           j                            |                                 d                              }d | j	        D             }||z
  D ]i}t          |          }| j	                            |                                                                ||                                j        g           j||z
  D ]b}d}t!          | j	                  D ]9\  }}	|	d                                                                         |k    r|} n:|dk    sJ | j	        |= c|                                  dS )a  
        Update our cache with the actually existing packs on disk. Add new ones,
        and remove deleted ones. We keep the unchanged ones

        :param force: If True, the cache will be updated even though the directory
            does not appear to have changed according to its modification timestamp.
        :return: True if the packs have been updated so there is new information,
            False if there was no change to the pack databaseFzpack-*.packc                 f    h | ].}|d                                                                           /S rE   )packpathrF   s     r   	<setcomp>z(PackedDB.update_cache.<locals>.<setcomp>   s0    KKKD$q',,..--//KKKr   r,   T)osstatr   st_mtimer   setglobrS   joinr   r
   appendrR   r@   r0   sha_to_index	enumerater)   )
r   r   rW   
pack_filesour_pack_files	pack_filer8   	del_indexir/   s
             r   r   zPackedDB.update_cache   s    wt~~''(( 	$.885 27<<0@0@-#P#PQQRR
KKDNKKK %~5 	_ 	_I  	**FN!!6;;==#5#5#7#7A\"]^^^^ ):5 		+ 		+II$T^44  47<<>>&&((I55 !IE 6
 ????y)) 	tr   c                 $    d | j         D             S )z=:return: list of pack entities operated upon by this databasec                     g | ]
}|d          S rE   r"   rF   s     r   rH   z%PackedDB.entities.<locals>.<listcomp>   s    333DQ333r   )r   r(   s    r   r>   zPackedDB.entities   s    33DN3333r   c                 ,   d}| j         D ]x}|d                                                             ||          }|F|d                                                             |          }|r||k    rt	          |          |}y|r|S t          |          )a  :return: 20 byte sha as inferred by the given partial binary sha
        :param partial_binsha: binary sha with less than 20 bytes
        :param canonical_length: length of the corresponding canonical representation.
            It is required as binary sha's cannot display whether the original hex sha
            had an odd or even number of characters
        :raise AmbiguousObjectName:
        :raise BadObject: Nr,   )r   r0   partial_sha_to_indexr.   r	   r   )r   partial_binshacanonical_length	candidater/   
item_indexr.   s          r   partial_to_complete_shaz PackedDB.partial_to_complete_sha   s     	N 	  	 Da==nN^__J%1gmmoo))*55 >c!1!1-n===	  	 '''r   )F)__name__
__module____qualname____doc__r-   r   r   r)   r1   r3   r9   r<   rB   r@   rO   r   r>   rl   __classcell__)r   s   @r   r   r       s        77
 N    * * *> > >  :  + + +- - -* * *4 4 4% % %+ + + +Z4 4 4( ( ( ( ( ( (r   r   )rp   gitdb.db.baser   r   r   
gitdb.utilr   	gitdb.excr   r   r	   
gitdb.packr
   	functoolsr   rV   rZ   __all__r   r"   r   r   <module>rx      s  
 6 5          !                    " ! ! ! ! !       				 

l( l( l( l( l(z9i l( l( l( l( l(r   