a
    8öja	  ã                   @  sf   d Z ddlmZ ddlZddlmZ zddlmZ W n eyR   ddl	mZ Y n0 G dd„ dƒZ
dS )z<For internal use only. It provides a slice-like file reader.é    )ÚannotationsN)Úoverload)ÚLockc                   @  s¶   e Zd ZdZdddœdd„Zedddœd	d
„ƒZedddœdd
„ƒZdddœdd
„Zddddœdd„Zddœdd„Zddœdd„Z	e
edƒr ddddœdd„Znddddœdd„ZdS )Ú
FileBufferzA slice-able file reader.ÚstrÚNone)ÚdatabaseÚreturnc                 C  s6   t |dƒ| _t | j ¡ ¡j| _ttdƒs2tƒ | _	dS )zCreate FileBuffer.ÚrbÚpreadN)
ÚopenÚ_handleÚosÚfstatÚfilenoÚst_sizeÚ_sizeÚhasattrr   Ú_lock)Úselfr   © r   úM/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/maxminddb/file.pyÚ__init__   s    
zFileBuffer.__init__Úint)Úindexr	   c                 C  s   d S ©Nr   ©r   r   r   r   r   Ú__getitem__   s    zFileBuffer.__getitem__ÚsliceÚbytesc                 C  s   d S r   r   r   r   r   r   r      s    zslice | intzbytes | intc                 C  sJ   t |tƒr |  |j|j |j¡S t |tƒr:|  d|¡d S d}t|ƒ‚dS )zGet item by index.é   r   zInvalid argument type.N)Ú
isinstancer   Ú_readÚstopÚstartr   Ú	TypeError)r   r   Úmsgr   r   r   r      s    

)Úneedler$   r	   c                 C  s0   |   | j| d |¡ |¡}|dkr(|S || S )zReverse find needle from start.r    éÿÿÿÿ)r"   r   Úrfind)r   r'   r$   Úposr   r   r   r)   '   s    zFileBuffer.rfind)r	   c                 C  s   | j S )zSize of file.)r   ©r   r   r   r   Úsize.   s    zFileBuffer.sizec                 C  s   | j  ¡  dS )zClose file.N)r   Úcloser+   r   r   r   r-   2   s    zFileBuffer.closer   )Ú
buffersizeÚoffsetr	   c                 C  s   t  | j ¡ ||¡S )zRead that uses pread.)r   r   r   r   ©r   r.   r/   r   r   r   r"   8   s    zFileBuffer._readc                 C  sB   | j �( | j |¡ | j |¡W  d  ƒ S 1 s40    Y  dS )a°  Read with a lock.

            This lock is necessary as after a fork, the different processes
            will share the same file table entry, even if we dup the fd, and
            as such the same offsets. There does not appear to be a way to
            duplicate the file table entry and we cannot re-open based on the
            original path as that file may have replaced with another or
            unlinked.
            N)r   r   ÚseekÚreadr0   r   r   r   r"   >   s    
N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r)   r,   r-   r   r   r"   r   r   r   r   r      s   	
r   )r6   Ú
__future__r   r   Útypingr   Úmultiprocessingr   ÚImportErrorÚ	threadingr   r   r   r   r   Ú<module>   s   