
    	^c9                     d    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	dS )
    N   )_)errorutili   sC   partial read of revlog %s; expected %d bytes from offset %d, got %dc                 &    | | dz
  z  dk    o| dk    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dZd ZddZej        dd            Z	ej        d             Z
dd	Zdd
Zd ZdS )randomaccessfilezDAccessing arbitrary chuncks of data within a file, with some cachingNc                     t          |          sJ || _        || _        || _        d | _        d | _        d| _        d| _        |r|\  | _        | _        d S d S Nr   r   )r   openerfilenamedefault_cached_chunk_sizewriting_handlereading_handle_cached_chunk_cached_chunk_position)selfr   r   r   initial_caches        r   __init__zrandomaccessfile.__init__   sz       9::::: )B&"" &'# 	L>K;D');););	L 	Lr   c                 "    d| _         d| _        d S r   )r   r   )r   s    r   clear_cachezrandomaccessfile.clear_cache1   s     &'###r      rc                 :    |                      | j        |          S )zReturn a file object)mode)r   r   )r   r    s     r   _openzrandomaccessfile._open5   s    {{4=t{444r   c              #      K   ||V  dS | j         r| j         V  dS | j        r| j        V  dS |                                 5 }|V  ddd           dS # 1 swxY w Y   dS )z%File object suitable for reading dataN)r   r   r!   )r   existing_file_objfps      r   
_open_readzrandomaccessfile._open_read9   s       (######   		%%%%%%  	%%%%%%                   s   AAAc              #      K   | j         ^| j        W| j        P|                                 5 }|| _         	 dV  d| _         n# d| _         w xY w	 ddd           dS # 1 swxY w Y   dS dV  dS )z4Context manager that keeps the file open for readingN)r   r   r   r!   )r   r$   s     r   readingzrandomaccessfile.readingO   s       '#+) /&(#/EEE*.D''$D'....'/ / / / / / / / / / / / / / / / / / EEEEEs'   AAA	A

AA"Ac                     ||z   }| j         }|t          | j                  z   }||k    r9||k    r3||k    r||k    r| j        S ||z
  }t          j        | j        ||          S |                     |||          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.
        )r   lenr   r   buffer_read_and_update_cache)r   offsetlengthr#   endcache_start	cache_endrelative_starts           r   
read_chunkzrandomaccessfile.read_chunk`   s     vo1#d&8"9"99	&  SI%5%5f$$	)9)9))#k1N;t1>6JJJ**66;LMMMr   c                     || j         dz
   z  }||z   | j         z   | j         dz
   z  |z
  }|                     |          5 }|                    |           |                    |          }d d d            n# 1 swxY w Y   |                     ||           ||z
  }t          |          |z
  }	|	|k     r't          | j        |||	fz  }
t          j	        |
          ||k    s||k    rt          j        |||          S |S )Nr   )r   r%   seekread_add_cached_chunkr)   PARTIAL_READ_MSGr   r   RevlogErrorr   r*   )r   r,   r-   r#   real_offsetreal_lengthfile_objdatarelative_offsetgotmessages              r   r+   z'randomaccessfile._read_and_update_cachey   s^    !?!!CDDf_t==.234 __.// 	.8MM+&&&==--D	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	{D111 ;.$ii/)<<&$-)MMG#G,,,[  K6$9$9;t_f===s    +A77A;>A;c                     | j         t          | j                  z   |k    r?t          | j                  t          |          z   t          k     r| xj        |z  c_        dS || _        || _         dS )z|Add to or replace the cached data chunk.

        Accepts an absolute offset and the data that is at that location.
        N)r   r)   r   _MAX_CACHED_CHUNK_SIZE)r   r,   r<   s      r   r6   z"randomaccessfile._add_cached_chunk   sv     '#d.@*A*AAVKKD&''#d))36LLL $&!%D*0D'''r   )N)r   )__name__
__module____qualname____doc__r   r   r!   
contextlibcontextmanagerr%   r'   r2   r+   r6   r	   r   r   r   r      s        NN L L L L(( ( (5 5 5 5    *    N N N N2   21 1 1 1 1r   r   )
rF   i18nr    r   r   rA   r7   r   r   r	   r   r   <module>rJ      s                     ! 1J  
) ) )E1 E1 E1 E1 E1 E1 E1 E1 E1 E1r   