
    q&fI                     j    d dl Z ddlmZ ddlmZmZ dZ ed      Zd Z G d d	      Z	 G d
 d      Z
y)    N   )_)errorutili   sC   partial read of revlog %s; expected %d bytes from offset %d, got %dc                 &    | | dz
  z  dk(  xr | dk7  S )N   r    )ns    H/usr/lib/python3/dist-packages/mercurial/revlogutils/randomaccessfile.py_is_power_of_twor      s    QK1(!q&(    c                   `    e Zd ZdZd Zd Zd Zd Zed        Z	d Z
ddZdd	Zd
 Zd Zd Zy)appenderz`the changelog index must be updated last on disk, so we use this class
    to delay writes to itc                     || _          |||      }|| _        |j                         | _        |j	                  |      j
                  | _        | j                  | _        y N)datafptelloffsetfstatst_sizesize_end)selfvfsnamemodebufr   s         r   __init__zappender.__init__   sH    	t_ggiIIbM))	II	r   c                     | j                   S r   )r   r   s    r   endzappender.end&   s    yyr   c                     | j                   S r   )r   r!   s    r   r   zappender.tell)   s    {{r   c                      y r   r	   r!   s    r   flushzappender.flush,   s    r   c                 .    | j                   j                  S r   )r   closedr!   s    r   r'   zappender.closed/   s    ww~~r   c                 8    | j                   j                          y r   )r   closer!   s    r   r)   zappender.close3   s    r   c                    |dk(  r|| _         n8|dk(  r| xj                   |z  c_         n|dk(  r| j                         |z   | _         | j                   | j                  k  r&| j                  j	                  | j                          yy)z,virtual file offset spans real file and datar   r   r   N)r   r"   r   r   seek)r   r   whences      r   r+   zappender.seek6   sg    Q; DKq[KK6!Kq[((*v-DK;;"GGLL% #r   c                    d}| j                   | j                  k  rN| j                  j                  |      }|}| xj                   t	        |      z  c_         |dkD  r|t	        |      z  }|dk7  r| j                   | j                  z
  }| j
                  j                  ddj                  | j
                               | j
                  dd= | j
                  d   |||z    }| xj                   t	        |      z  c_         ||z  }|S )z5only trick here is reads that span real file and datar   r   r   N)r   r   r   readlenr   insertjoin)r   countretsdoffs        r   r.   zappender.readA   s    ;;"U#ACKK3q6!KqyQA:;;*DIIQ 34		!"		!TD5L1AKK3q6!K1HC
r   c                     | j                   j                  t        |             | xj                  t	        |      z  c_        | xj
                  t	        |      z  c_        y r   )r   appendbytesr   r/   r   )r   r4   s     r   writezappender.writeS   s<    		q"s1v		SV	r   c                 :    | j                   j                          | S r   )r   	__enter__r!   s    r   r;   zappender.__enter__X   s    r   c                 4     | j                   j                  | S r   )r   __exit__)r   argss     r   r=   zappender.__exit__\   s    tww&&r   N)r   ))__name__
__module____qualname____doc__r   r"   r   r%   propertyr'   r)   r+   r.   r9   r;   r=   r	   r   r   r   r      sM      	&$
'r   r   c                       e Zd ZdZ	 ddZd Zed        ZddZe	j                  d        Ze	j                  d        Zd	 Zd
 Zd Zy)randomaccessfilezDAccessing arbitrary chuncks of data within a file, with some cachingNc                     t        |      sJ || _        || _        || _        d | _        d | _        d| _        d| _        |r|\  | _        | _        y y Nr   r   )r   openerfilenamedefault_cached_chunk_sizewriting_handlereading_handle_cached_chunk_cached_chunk_position)r   rI   rJ   rK   initial_caches        r   r   zrandomaccessfile.__init__c   sa       9::: )B&"" &'#>K;D'); r   c                      d| _         d| _        y rH   )rN   rO   r!   s    r   clear_cachezrandomaccessfile.clear_cachew   s     &'#r   c                 >    | j                   duxs | j                  duS )z[True if any file handle is being held

        Used for assert and debug in the python codeN)rM   rL   r!   s    r   is_openzrandomaccessfile.is_open{   s'     t+Nt/B/B$/N	
r   c                 <    | j                  | j                  |      S )zReturn a file object)r   )rI   rJ   )r   r   s     r   _openzrandomaccessfile._open   s    {{4==t{44r   c              #      K   | j                   r| j                    y| j                  r| j                   y| j                         5 }| ddd       y# 1 sw Y   yxY ww)z%File object suitable for reading dataN)rL   rM   rV   r   r   s     r   _read_handlezrandomaccessfile._read_handle   sX      %%%  %%%    s   AA#	A	A#A A#c              #      K   | j                   F| j                  :| j                  .| j                         5 }|| _         	 d d| _         	 ddd       yd y# d| _         w xY w# 1 sw Y   yxY ww)z4Context manager that keeps the file open for readingN)rM   rL   rJ   rV   rX   s     r   readingzrandomaccessfile.reading   sw      '##+) /&(#/*.D'/ /  +/D'/ /s4   5A2A& AA&A2	A##A&&A/+A2c                    ||z   }| j                   }|t        | j                        z   }||k  rA||k  r<||k(  r||k(  r| j                  S ||z
  }t        j                  | j                  ||      S | j                  ||      S )a  Read a chunk of bytes from the file.

        Accepts an absolute offset, length to read, and an optional existing
        file handle to read from.

        If an existing file handle is passed, it will be seeked and the
        original seek position will NOT be restored.

        Returns a str or buffer of raw byte data.

        Raises if the requested number of bytes could not be read.
        )rO   r/   rN   r   buffer_read_and_update_cache)r   r   lengthr"   cache_start	cache_endrelative_starts          r   
read_chunkzrandomaccessfile.read_chunk   s     vo11#d&8&8"99	& SI%5f$	)9)))#k1N;;t11>6JJ**66::r   c                    || j                   dz
   z  }||z   | j                   z   | j                   dz
   z  |z
  }| j                         5 }|j                  |       |j                  |      }d d d        | j	                  |       ||z
  }t        |      |z
  }||k  r,t        | j                  |||fz  }	t        j                  |	      ||k7  s||k7  rt        j                  |||      S |S # 1 sw Y   xY w)Nr   )rK   rY   r+   r.   _add_cached_chunkr/   PARTIAL_READ_MSGrJ   r   RevlogErrorr   r]   )
r   r   r_   real_offsetreal_lengthfile_objr   relative_offsetgotmessages
             r   r^   z'randomaccessfile._read_and_update_cache   s    !?!?!!CDDf_t===..234   	.HMM+&==-D	. 	{D1 ;.$i/)<&$--)MMG##G,,[ K6$9;;t_f==	. 	.s   
#C..C7c                     | j                   t        | j                        z   |k(  r>t        | j                        t        |      z   t        k  r| xj                  |z  c_        y|| _        || _         y)z|Add to or replace the cached data chunk.

        Accepts an absolute offset and the data that is at that location.
        N)rO   r/   rN   _MAX_CACHED_CHUNK_SIZE)r   r   r   s      r   re   z"randomaccessfile._add_cached_chunk   sb     ''#d.@.@*AAVKD&&'#d)36LL $&!%D*0D'r   r   )   r)r@   rA   rB   rC   r   rR   rD   rT   rV   
contextlibcontextmanagerrY   r[   rc   r^   re   r	   r   r   rF   rF   `   sq    N L(( 
 
5  "   ;221r   rF   )rq   i18nr    r   r   ro   rf   r   r   rF   r	   r   r   <module>ru      sF      ! J 
)C' C'LJ1 J1r   