U
    h                  	   @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZ erd dlZd dlmZ edZnedZed	Zed
Z ee!e!ge"f Z#G dd dZ$G dd de$Z%G dd de$Z&G dd de$Z'G dd de$Z(G dd de$Z)G dd de$Z*G dd de$Z+G dd deeef Z,G dd de$Z-de$iZ.de/d < d(d"d#d$d%d&d'Z0e$e%e)e&e(e'e*e+e-f	D ]Z1e0e1 qdS ))    )annotationsN)FutureThreadPoolExecutor)groupby)
itemgetter)	TYPE_CHECKINGAnyCallableClassVarGeneric
NamedTupleOptionalOrderedDictTypeVar)	ParamSpecPTZfsspecc                   @  sn   e Zd ZU dZdZded< dddddd	d
ZddddddZddddZddddZ	ddddZ
dS )	BaseCacheag  Pass-though cache: doesn't keep anything, calls every time

    Acts as base class for other cachers

    Parameters
    ----------
    blocksize: int
        How far to read ahead in numbers of bytes
    fetcher: func
        Function of the form f(start, end) which gets bytes from remote as
        specified
    size: int
        How big this file is
    noneClassVar[str]nameintFetcherNone	blocksizefetchersizereturnc                 C  s.   || _ d| _|| _|| _d| _d| _d| _d S Nr   )r   nblocksr   r   	hit_count
miss_counttotal_requested_bytesselfr   r   r    r&   @/var/www/html/venv/lib/python3.8/site-packages/fsspec/caching.py__init__<   s    zBaseCache.__init__
int | Nonebytesstartstopr   c                 C  s<   |d krd}|d kr| j }|| j ks,||kr0dS | ||S )Nr       )r   r   r%   r,   r-   r&   r&   r'   _fetchF   s    zBaseCache._fetchr   c                 C  s   d| _ d| _d| _dS )zAReset hit and miss counts for a more ganular report e.g. by file.r   N)r!   r"   r#   r%   r&   r&   r'   _reset_statsO   s    zBaseCache._reset_statsstrc              	   C  s>   | j dkr| jdkrdS d| j d| j  d| j d| j d	S )z2Return a formatted string of the cache statistics.r    z , z: z hits, z	 misses, z total requested bytes)r!   r"   r   r#   r2   r&   r&   r'   
_log_statsU   s    zBaseCache._log_statsc                 C  s@   d| j j d| j d| j d| j d| j d| j d| j dS )	Nz

        <z:
            block size  :   z
            block count :   z
            file size   :   z
            cache hits  :   z
            cache misses:   z$
            total requested bytes: z
>
        )	__class____name__r   r    r   r!   r"   r#   r2   r&   r&   r'   __repr__\   s    zBaseCache.__repr__N)r8   
__module____qualname____doc__r   __annotations__r(   r0   r3   r6   r9   r&   r&   r&   r'   r   *   s   

	r   c                      st   e Zd ZdZdZdddddddd	 fd
dZddddZddddddZddddZdddddZ	  Z
S )	MMapCachezmemory-mapped sparse file cache

    Opens temporary file, which is filled blocks-wise when data is requested.
    Ensure there is enough disc space in the temporary location.

    This cache method might only work on posix
    mmapNr   r   z
str | Nonezset[int] | Noner   )r   r   r   locationblocksr   c                   s8   t  ||| |d krt n|| _|| _|  | _d S N)superr(   setrA   r@   	_makefilecache)r%   r   r   r   r@   rA   r7   r&   r'   r(   t   s    zMMapCache.__init__zmmap.mmap | bytearrayr1   c                 C  s   dd l }dd l}| jdkr t S | jd ks8tj| js| jd krT| }t	 | _
nt| jd}|| jd  |d |  nt| jd}| | | jS )Nr   zwb+      1zr+b)r?   tempfiler   	bytearrayr@   ospathexistsTemporaryFilerD   rA   openseekwriteflushfileno)r%   r?   rJ   fdr&   r&   r'   rE      s    




zMMapCache._makefiler)   r*   r,   endr   c                   st  t d| d|  |d kr"d}|d kr0 j}| jksB||krFdS | j }| j }t||d } fdd|D }  jt fdd|D 7  _tt|d	d
 dD ]\}}t	t
td|}|d  j }	t|d  j  j  j}
  j|
|	 7  _t d|d  d|d  d|	 d|
 d	  |	|
 j|	|
<  j|   jt|7  _q j|| S )NzMMap cache fetching -r   r.   rH   c                 3  s   | ]}| j kr|V  qd S rB   rA   .0ir2   r&   r'   	<genexpr>   s     
 z#MMapCache._fetch.<locals>.<genexpr>c                 3  s   | ]}| j krd V  qdS )rH   NrY   rZ   r2   r&   r'   r]      s     
 c                 S  s   | d | d  S )Nr   rH   r&   )xr&   r&   r'   <lambda>   r.   z"MMapCache._fetch.<locals>.<lambda>)keyzMMap get blocks z ())loggerdebugr   r   ranger!   sumr   	enumeratetuplemapr   minr#   r   rF   rA   updater"   len)r%   r,   rW   Zstart_blockZ	end_blockZblock_rangeZneed_Z_blocksZsstartsendr&   r2   r'   r0      s0    

 
$zMMapCache._fetchdict[str, Any]c                 C  s   | j  }|d= |S )NrF   )__dict__copyr%   stater&   r&   r'   __getstate__   s    
zMMapCache.__getstate__rs   r   c                 C  s   | j | |  | _d S rB   )rp   rk   rE   rF   rr   r&   r&   r'   __setstate__   s    zMMapCache.__setstate__)NN)r8   r:   r;   r<   r   r(   rE   r0   rt   rv   __classcell__r&   r&   rG   r'   r>   i   s     .r>   c                      sB   e Zd ZdZdZddddd fddZd	d	d
dddZ  ZS )ReadAheadCachea!  Cache which reads only when we get beyond a block of data

    This is a much simpler version of BytesCache, and does not attempt to
    fill holes in the cache or keep fragments alive. It is best suited to
    many small reads in a sequential order (e.g., reading lines from a file).
    Z	readaheadr   r   r   r   c                   s&   t  ||| d| _d| _d| _d S )Nr.   r   )rC   r(   rF   r,   rW   r$   rG   r&   r'   r(      s    zReadAheadCache.__init__r)   r*   rV   c                 C  s>  |d krd}|d ks|| j kr$| j }|| j ks6||kr:dS || }|| jkr~|| jkr~|  jd7  _| j|| j || j  S | j|  kr| jk rn n6|  jd7  _| j|| j d  }|t|8 }| j}n|  jd7  _d}t| j || j }|  j	|| 7  _	| 
||| _|| _| jt| j | _|| jd |  S Nr   r.   rH   )r   r,   rW   r!   rF   r"   rl   rj   r   r#   r   )r%   r,   rW   lpartr&   r&   r'   r0      s.    zReadAheadCache._fetchr8   r:   r;   r<   r   r(   r0   rw   r&   r&   rG   r'   rx      s   rx   c                      sB   e Zd ZdZdZddddd fddZd	d	d
dddZ  ZS )FirstChunkCachezCaches the first block of a file only

    This may be useful for file types where the metadata is stored in the header,
    but is randomly accessed.
    firstr   r   r   r   c                   s&   ||kr|}t  ||| d | _d S rB   )rC   r(   rF   r$   rG   r&   r'   r(     s    zFirstChunkCache.__init__r)   r*   rV   c                 C  s0  |pd}|| j kr td dS t|| j }|| jk r | jd kr|  jd7  _|| jkr|  j|7  _| d|}|d | j | _||d  S | d| j| _|  j| j7  _| j|| }|| jkr|  j|| j 7  _|| | j|7 }|  j	d7  _	|S |  jd7  _|  j|| 7  _| ||S d S )Nr   z,FirstChunkCache: requested start > file sizer.   rH   )
r   rc   rd   rj   r   rF   r"   r#   r   r!   )r%   r,   rW   datar{   r&   r&   r'   r0     s0    




zFirstChunkCache._fetchr|   r&   r&   rG   r'   r}      s   r}   c                      s   e Zd ZdZdZddddddd fdd	Zd
d ZddddZdddddZddddddZ	ddd fddZ
ddddddddZ  ZS )
BlockCachea  
    Cache holding memory as a set of blocks.

    Requests are only ever made ``blocksize`` at a time, and are
    stored in an LRU cache. The least recently accessed block is
    discarded when more than ``maxblocks`` are stored.

    Parameters
    ----------
    blocksize : int
        The number of bytes to store in each block.
        Requests are only ever made for ``blocksize``, so this
        should balance the overhead of making a request against
        the granularity of the blocks.
    fetcher : Callable
    size : int
        The total size of the file being cached.
    maxblocks : int
        The maximum number of blocks to cache for. The maximum memory
        use for this cache is then ``blocksize * maxblocks``.
    Z
blockcache    r   r   r   r   r   r   	maxblocksr   c                   s<   t  ||| t|| | _|| _t|| j| _	d S rB   )
rC   r(   mathceilr    r   	functools	lru_cache_fetch_block_fetch_block_cachedr%   r   r   r   r   rG   r&   r'   r(   F  s    zBlockCache.__init__c                 C  s
   | j  S z
        The statistics on the block cache.

        Returns
        -------
        NamedTuple
            Returned directly from the LRU Cache used internally.
        r   
cache_infor2   r&   r&   r'   r   N  s    	zBlockCache.cache_inforo   r1   c                 C  s   | j }|d= |S )Nr   rp   rr   r&   r&   r'   rt   Y  s    zBlockCache.__getstate__ru   c                 C  s&   | j | t|d | j| _d S )Nr   )rp   rk   r   r   r   r   rr   r&   r&   r'   rv   ^  s    zBlockCache.__setstate__r)   r*   rV   c                 C  st   |d krd}|d kr| j }|| j ks,||kr0dS || j }|| j }t||d D ]}| | qR| j||||dS )Nr   r.   rH   start_block_numberend_block_number)r   r   re   r   _read_cache)r%   r,   rW   r   r   block_numberr&   r&   r'   r0   d  s     

zBlockCache._fetch)r   r   c                   st   || j kr"td| d| j  d|| j }|| j }|  j|| 7  _|  jd7  _td| t ||}|S )=
        Fetch the block of data for `block_number`.
        'block_number=(' is greater than the number of blocks (rb   rH   zBlockCache fetching block %d)	r    
ValueErrorr   r#   r"   rc   inforC   r0   )r%   r   r,   rW   block_contentsrG   r&   r'   r   {  s    


zBlockCache._fetch_blockr,   rW   r   r   r   c           	      C  s   || j  }|| j  }|  jd7  _||kr@| |}||| S | ||d g}|t| jt|d | || |d|  d|S dS z
        Read from our block cache.

        Parameters
        ----------
        start, end : int
            The start and end byte positions.
        start_block_number, end_block_number : int
            The start and end block numbers.
        rH   Nr.   r   r!   r   extendri   re   appendjoin	r%   r,   rW   r   r   	start_posend_posblockoutr&   r&   r'   r     s    


zBlockCache._read_cache)r   )r8   r:   r;   r<   r   r(   r   rt   rv   r0   r   r   rw   r&   r&   rG   r'   r   -  s    r   c                      s^   e Zd ZU dZdZded< dddddd	d
 fddZddddddZddddZ  Z	S )
BytesCacheaK  Cache which holds data in a in-memory bytes object

    Implements read-ahead by the block size, for semi-random reads progressing
    through the file.

    Parameters
    ----------
    trim: bool
        As we read more data, whether to discard the start of the buffer when
        we are more than a blocksize ahead of it.
    r*   r   r   Tr   r   boolr   )r   r   r   trimr   c                   s,   t  ||| d| _d | _d | _|| _d S )Nr.   )rC   r(   rF   r,   rW   r   )r%   r   r   r   r   rG   r&   r'   r(     s
    zBytesCache.__init__r)   rV   c                 C  s  |d krd}|d kr| j }|| j ks,||kr0dS | jd k	r|| jkr| jd k	r|| jk r|| j }|  jd7  _| j||| |  S | jrt| j || j }n|}||ks|| j krdS | jd ks|| jk r| jd ks|| jkr|  j|| 7  _|  jd7  _| 	||| _|| _nF| jd k	s.t
| jd k	s>t
|  jd7  _|| jk r| jd ksv| j| | jkr|  j|| 7  _| 	||| _|| _n4|  j| j| 7  _| 	|| j}|| _|| j | _n| jd k	rd|| jkrd| j| j krnh|| j | jkr6|  j|| 7  _| 	||| _|| _n.|  j|| j 7  _| 	| j|}| j| | _| jt| j | _|| j }| j||| |  }| jr| j| j | jd  }|dkr|  j| j| 7  _| j| j| d  | _|S ry   )r   r,   rW   r!   rF   r   rj   r#   r"   r   AssertionErrorrl   r   )r%   r,   rW   offsetZbendnewr   numr&   r&   r'   r0     s|    



zBytesCache._fetchr1   c                 C  s
   t | jS rB   )rl   rF   r2   r&   r&   r'   __len__  s    zBytesCache.__len__)T)
r8   r:   r;   r<   r   r=   r(   r0   r   rw   r&   r&   rG   r'   r     s   
 	Ir   c                      sP   e Zd ZU dZdZded< dddddd	d
 fddZddddddZ  ZS )AllBytesz!Cache entire contents of the fileallr   r   Nr)   zFetcher | Nonezbytes | Noner   )r   r   r   r   r   c                   sN   t  ||| |d krD|  jd7  _|  j| j7  _| d| j}|| _d S )NrH   r   )rC   r(   r"   r#   r   r   r   )r%   r   r   r   r   rG   r&   r'   r(      s    zAllBytes.__init__r*   r+   c                 C  s   |  j d7  _ | j|| S )NrH   )r!   r   r/   r&   r&   r'   r0   .  s    zAllBytes._fetch)NNNN	r8   r:   r;   r<   r   r=   r(   r0   rw   r&   r&   rG   r'   r     s   
    r   c                      sV   e Zd ZU dZdZded< ddddd	d
dd fddZdddd fddZ  ZS )KnownPartsOfAFilea  
    Cache holding known file parts.

    Parameters
    ----------
    blocksize: int
        How far to read ahead in numbers of bytes
    fetcher: func
        Function of the form f(start, end) which gets bytes from remote as
        specified
    size: int
        How big this file is
    data: dict
        A dictionary mapping explicit `(start, stop)` file-offset tuples
        with known bytes.
    strict: bool, default True
        Whether to fetch reads that go beyond a known byte-range boundary.
        If `False`, any read that ends outside a known part will be zero
        padded. Note that zero padding will not be used for reads that
        begin outside a known byte-range.
    partsr   r   NTr   r   z&Optional[dict[tuple[int, int], bytes]]r   r   )r   r   r   r   strictrm   c                   s   t  ||| || _|rt| }|d g}||d g}	|dd  D ]f\}
}|d \}}|
|kr||f|d< |	d  ||
|f7  < qL||
|f |	||
|f qLtt||	| _	ni | _	d S )Nr   rH   ra   )
rC   r(   r   sortedkeyspopr   dictzipr   )r%   r   r   r   r   r   rm   Zold_offsetsoffsetsrA   r,   r-   Zstart0Zstop0rG   r&   r'   r(   L  s    	
zKnownPartsOfAFile.__init__r)   r*   r+   c                   s:  |d krd}|d kr| j }d}| j D ]\\}}}||  krH|k r(n q(|| }|||| |  }| jr||  kr|krn n.|d|| t|  7 }|  jd7  _|  S |} qq(| jd krtd||f dt	d||f d t
d| d	|  |  j|| 7  _|  jd7  _|t || S )
Nr   r.       rH   z&Read is outside the known file parts: z. z%. IO/caching performance may be poor!z!KnownPartsOfAFile cache fetching rX   )r   r   itemsr   rl   r!   r   r   warningswarnrc   rd   r#   r"   rC   r0   )r%   r,   r-   r   Zloc0Zloc1r   offrG   r&   r'   r0   j  s0    
zKnownPartsOfAFile._fetch)NTr   r&   r&   rG   r'   r   3  s   
  r   c                   @  sv   e Zd ZdZG dd deZdddddd	d
Zd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S )UpdatableLRUzg
    Custom implementation of LRU cache that allows updating keys

    Used by BackgroudBlockCache
    c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )UpdatableLRU.CacheInfor   hitsmissesmaxsizecurrsizeN)r8   r:   r;   r=   r&   r&   r&   r'   	CacheInfo  s   
r      zCallable[P, T]r   r   )funcmax_sizer   c                 C  s0   t  | _|| _|| _d| _d| _t | _	d S r   )
collectionsr   _cache_func	_max_size_hits_misses	threadingLock_lock)r%   r   r   r&   r&   r'   r(     s    
zUpdatableLRU.__init__zP.argszP.kwargsr   )argskwargsr   c              
   O  s   |rt d|  | j@ || jkrX| j| |  jd7  _| j| W  5 Q R  S W 5 Q R X | j||}| j< || j|< |  jd7  _t| j| j	kr| jj
dd W 5 Q R X |S )Nz Got unexpected keyword argument rH   Flast)	TypeErrorr   r   r   move_to_endr   r   r   rl   r   popitem)r%   r   r   resultr&   r&   r'   __call__  s    
 
zUpdatableLRU.__call__r   r   )r   r   c              
   G  s(   | j  || jkW  5 Q R  S Q R X d S rB   )r   r   )r%   r   r&   r&   r'   is_key_cached  s    zUpdatableLRU.is_key_cached)r   r   r   c              	   G  s>   | j . || j|< t| j| jkr0| jjdd W 5 Q R X d S )NFr   )r   r   rl   r   r   )r%   r   r   r&   r&   r'   add_key  s    
zUpdatableLRU.add_keyr   r1   c              
   C  s<   | j , | j| jt| j| j| jdW  5 Q R  S Q R X d S )N)r   r   r   r   )r   r   r   rl   r   r   r   r2   r&   r&   r'   r     s    zUpdatableLRU.cache_infoN)r   )r8   r:   r;   r<   r   r   r(   r   r   r   r   r&   r&   r&   r'   r     s   r   c                      s   e Zd ZU dZdZded< d"dddddd	 fd
dZddddZddddZddddZ	ddddddZ
d#dddd fddZddddddd d!Z  ZS )$BackgroundBlockCachea  
    Cache holding memory as a set of blocks with pre-loading of
    the next block in the background.

    Requests are only ever made ``blocksize`` at a time, and are
    stored in an LRU cache. The least recently accessed block is
    discarded when more than ``maxblocks`` are stored. If the
    next block is not in cache, it is loaded in a separate thread
    in non-blocking way.

    Parameters
    ----------
    blocksize : int
        The number of bytes to store in each block.
        Requests are only ever made for ``blocksize``, so this
        should balance the overhead of making a request against
        the granularity of the blocks.
    fetcher : Callable
    size : int
        The total size of the file being cached.
    maxblocks : int
        The maximum number of blocks to cache for. The maximum memory
        use for this cache is then ``blocksize * maxblocks``.
    
backgroundr   r   r   r   r   r   r   c                   sZ   t  ||| t|| | _|| _t| j|| _t	dd| _
d | _d | _t | _d S )NrH   max_workers)rC   r(   r   r   r    r   r   r   r   r   _thread_executor_fetch_future_block_number_fetch_futurer   r   _fetch_future_lockr   rG   r&   r'   r(     s    zBackgroundBlockCache.__init__r   r1   c                 C  s
   | j  S r   r   r2   r&   r&   r'   r     s    	zBackgroundBlockCache.cache_inforo   c                 C  s(   | j }|d= |d= |d= |d= |d= |S )Nr   r   r   r   r   r   rr   r&   r&   r'   rt     s    z!BackgroundBlockCache.__getstate__c                 C  sD   | j | t| j|d | _tdd| _d | _d | _t	
 | _d S )Nr   rH   r   )rp   rk   r   r   r   r   r   r   r   r   r   r   rr   r&   r&   r'   rv     s    z!BackgroundBlockCache.__setstate__r)   r*   rV   c           
   	   C  s  |d krd}|d kr| j }|| j ks,||kr0dS || j }|| j }d }d }| j | jd k	r| jd k	slt| j rtd | j	
| j | j d | _d | _n:t|| j  ko|kn  }|r| j}| j}d | _d | _W 5 Q R X |d k	rtd | j	
| | t||d D ]}| 	| q|d }	| jF | jd kr~|	| jkr~| j	|	s~|	| _| j| j|	d| _W 5 Q R X | j||||dS )Nr   r.   z3BlockCache joined background fetch without waiting.z(BlockCache waiting for background fetch.rH   asyncr   )r   r   r   r   r   r   donerc   r   r   r   r   r   re   r    r   r   Zsubmitr   r   )
r%   r,   rW   r   r   Zfetch_future_block_numberZfetch_futureZ	must_joinr   Zend_block_plus_1r&   r&   r'   r0     sz    




 

 
  zBackgroundBlockCache._fetchsyncr4   )r   log_infor   c                   sv   || j kr"td| d| j  d|| j }|| j }td|| |  j|| 7  _|  jd7  _t ||}|S )r   r   r   rb   z!BlockCache fetching block (%s) %drH   )	r    r   r   rc   r   r#   r"   rC   r0   )r%   r   r   r,   rW   r   rG   r&   r'   r   e  s    


z!BackgroundBlockCache._fetch_blockr   c           	      C  s   || j  }|| j  }|  jd7  _||kr@| |}||| S | ||d g}|t| jt|d | || |d|  d|S dS r   r   r   r&   r&   r'   r   w  s    


z BackgroundBlockCache._read_cache)r   )r   )r8   r:   r;   r<   r   r=   r(   r   rt   rv   r0   r   r   rw   r&   r&   rG   r'   r     s   
 	Lr   z!dict[str | None, type[BaseCache]]cachesFztype[BaseCache]r   r   )clsclobberr   c                 C  s6   | j }|s*|tkr*td|dt|  | t|< dS )z'Register' cache implementation.

    Parameters
    ----------
    clobber: bool, optional
        If set to True (default is False) - allow to overwrite existing
        entry.

    Raises
    ------
    ValueError
    zCache with name z is already known: N)r   r   r   )r   r   r   r&   r&   r'   register_cache  s    r   )F)2
__future__r   r   r   loggingr   rL   r   r   concurrent.futuresr   r   	itertoolsr   operatorr   typingr   r   r	   r
   r   r   r   r   r   r?   typing_extensionsr   r   r   	getLoggerrc   r   r*   r   r   r>   rx   r}   r   r   r   r   r   r   r   r=   r   cr&   r&   r&   r'   <module>   s^    ,

?h.. 
ee< Q 