o
    reg                  	   @   s&  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 e	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ZG dd dZG dd deZdeiZdddZeeeeeeeeef	D ]Zee qdS )    N)ThreadPoolExecutorZfsspecc                   @   s$   e Zd ZdZdZdd Z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
    nonec                 C   s   || _ || _|| _d S N)	blocksizefetchersizeselfr   r   r    r   >D:\Projects\ConvertPro\env\Lib\site-packages\fsspec/caching.py__init__   s   
zBaseCache.__init__c                 C   s<   |d u rd}|d u r| j }|| j ks||krdS | ||S Nr       )r   r   )r
   startstopr   r   r   _fetch$   s   zBaseCache._fetchN)__name__
__module____qualname____doc__namer   r   r   r   r   r   r      s
    r   c                       sF   e Zd ZdZdZd fdd	Zdd Zdd	 Zd
d Z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
    mmapNc                    s8   t  ||| |d u rt n|| _|| _|  | _d S r   )superr   setblockslocation	_makefilecache)r
   r   r   r   r   r   	__class__r   r   r   9   s   zMMapCache.__init__c                 C   s   dd l }dd l}| jdkrt S | jd u stj| jsB| jd u r*| }t	 | _
nt| jd}|| jd  |d |  nt| jd}| | | jS )Nr   zwb+      1zrb+)r   tempfiler   	bytearrayr   ospathexistsTemporaryFiler   r   openseekwriteflushfileno)r
   r   r$   fdr   r   r   r   ?   s   




zMMapCache._makefilec           	         s   t d| d|  |d u rd}|d u r j}| jks!||kr#dS | j }| j } fddt||d D }|rq|d}| j }t| j  j}t d| d	| d|   || j||<  j	
| |s= j|| S )
NzMMap cache fetching -r   r   c                    s   g | ]	}| j vr|qS r   )r   ).0ir
   r   r   
<listcomp>_   s    z$MMapCache._fetch.<locals>.<listcomp>r"   zMMap get block #z ()loggerdebugr   r   rangepopminr   r   r   add)	r
   r   endZstart_blockZ	end_blockZneedr2   Zsstartsendr   r3   r   r   U   s&   




zMMapCache._fetchc                 C   s   | j  }|d= |S )Nr   )__dict__copyr
   stater   r   r   __getstate__l   s   
zMMapCache.__getstate__c                 C   s   | j | |  | _d S r   )r=   updater   r   r?   r   r   r   __setstate__r   s   zMMapCache.__setstate__)NN)r   r   r   r   r   r   r   r   rA   rC   __classcell__r   r   r    r   r   .   s    r   c                       ,   e Zd ZdZdZ fddZ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	readaheadc                    s&   t  ||| d| _d| _d| _d S )Nr   r   )r   r   r   r   r;   r	   r    r   r   r      s   
zReadAheadCache.__init__c                 C   s  |d u rd}|d u s|| j kr| j }|| j ks||krdS || }|| jkr8|| jkr8| j|| j || j  S | j|  krD| jk rZn n| j|| j d  }|t|8 }| j}nd}t| j || j }| ||| _|| _| jt| j | _|| jd |  S r   )r   r   r;   r   lenr9   r   r   )r
   r   r;   lpartr   r   r   r      s&   zReadAheadCache._fetchr   r   r   r   r   r   r   rD   r   r   r    r   rF   x   s
    rF   c                       rE   )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.
    firstc                    s   t  ||| d | _d S r   )r   r   r   r	   r    r   r   r      s   
zFirstChunkCache.__init__c                 C   s   |pd}|p| j }|| jk rK| jd u r4|| jkr,| d|}|d | j | _||d  S | d| j| _| j|| }|| jkrI|| | j|7 }|S | ||S Nr   )r   r   r   r   )r
   r   r;   datarI   r   r   r   r      s   




zFirstChunkCache._fetchrJ   r   r   r    r   rK      s
    rK   c                       sb   e Zd ZdZdZd fdd	Zdd Zdd	 Zd
d Zdd Z	dd Z
 fddZ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    c                    s<   t  ||| t|| | _|| _t|| j| _	d S r   )
r   r   mathceilnblocks	maxblocks	functools	lru_cache_fetch_block_fetch_block_cachedr
   r   r   r   rT   r    r   r   r      s   zBlockCache.__init__c                 C      d | j| j| jS )Nz.<BlockCache blocksize={}, size={}, nblocks={}>formatr   r   rS   r3   r   r   r   __repr__      zBlockCache.__repr__c                 C   
   | j  S z
        The statistics on the block cache.

        Returns
        -------
        NamedTuple
            Returned directly from the LRU Cache used internally.
        rX   
cache_infor3   r   r   r   rb         
	zBlockCache.cache_infoc                 C   s   | j }|d= |S )NrX   r=   r?   r   r   r   rA      s   zBlockCache.__getstate__c                 C   s&   | j | t|d | j| _d S )NrT   )r=   rB   rU   rV   rW   rX   r?   r   r   r   rC      s   
zBlockCache.__setstate__c                 C   st   |d u rd}|d u r| j }|| j ks||krdS || j }|| j }t||d D ]}| | q)| j||||dS )Nr   r   r"   start_block_numberend_block_number)r   r   r7   rX   _read_cache)r
   r   r;   rf   rg   block_numberr   r   r   r      s    

zBlockCache._fetchc                    sN   || j krtd|| j || j }|| j }td| t ||}|S )=
        Fetch the block of data for `block_number`.
        ;'block_number={}' is greater than the number of blocks ({})zBlockCache fetching block %drS   
ValueErrorr\   r   r5   infor   r   )r
   ri   r   r;   block_contentsr    r   r   rW     s   


zBlockCache._fetch_blockc           
      C      || j  }|| j  }||kr| |}||| S g }|| ||d  t|d |D ]
}	|| |	 q.|| |d|  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.
        Nr"   r   r   rX   appendr7   join
r
   r   r;   rf   rg   	start_posend_posblockoutri   r   r   r   rh   #     



zBlockCache._read_cacherP   r   r   r   r   r   r   r]   rb   rA   rC   r   rW   rh   rD   r   r   r    r   rO      s    rO   c                       s6   e Zd ZdZdZd
 fdd	Zdd Z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.
    bytesTc                    s,   t  ||| d| _d | _d | _|| _d S )Nr   )r   r   r   r   r;   trim)r
   r   r   r   r   r    r   r   r   U  s
   
zBytesCache.__init__c                 C   s  |d u rd}|d u r| j }|| j ks||krdS | jd ur<|| jkr<| jd ur<|| jk r<|| j }| j||| |  S | jrIt| j || j }n|}||ksT|| j krVdS | jd u s`|| jk ru| jd u sj|| jkru| ||| _|| _nU|| jk r| j| | jkr| ||| _|| _n=| || j}|| _|| j | _n,|| jkr| j| j krn || j | jkr| ||| _|| _n| | j|}| j| | _| jt| j | _|| j }| j||| |  }| jr| j| j | jd  }|dkr|  j| j| 7  _| j| j| d  | _|S )Nr   r   r"   )	r   r   r;   r   r   r9   r   rG   r   )r
   r   r;   offsetZbendnewry   numr   r   r   r   \  sZ   








zBytesCache._fetchc                 C   s
   t | jS r   )rG   r   r3   r   r   r   __len__  s   
zBytesCache.__len__)T)	r   r   r   r   r   r   r   r   rD   r   r   r    r   r}   F  s    =r}   c                       s.   e Zd ZdZdZd fdd	Zdd Z  ZS )	AllBytesz!Cache entire contents of the fileallNc                    s0   t  ||| |d u r| d| j}|| _d S rM   )r   r   r   r   rN   )r
   r   r   r   rN   r    r   r   r     s   
zAllBytes.__init__c                 C   s   | j || S r   )rN   )r
   r   r;   r   r   r   r     s   zAllBytes._fetch)NNNNrJ   r   r   r    r   r     s
    r   c                       s6   e Zd ZdZdZi df fdd	Z 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.
    partsTc                    s   t t| ||| || _|rhtt| }|d g}||d g}	|dd  D ]3\}
}|d \}}|
|krL||f|d< |	d  ||
|f7  < q*||
|f |	||
|f q*t	t
||	| _d S || _d S )Nr   r"   )r   r   r   strictsortedlistkeysr8   rs   dictziprN   )r
   r   r   r   rN   r   _Zold_offsetsoffsetsr   r   r   Zstart0Zstop0r    r   r   r     s   

zKnownPartsOfAFile.__init__c                    s   d}| j  D ]C\\}}}||  kr|k rJn q|| }|||| |  }| jr6||  kr4|krFn n|d|| t|  7 }|  S |} nq| jd u rZtd||f dtd||f d t	d| d|  |t
 || S )Nr       z&Read is outside the known file parts: z. z%. IO/caching performance may be poor!z!KnownPartsOfAFile cache fetching r0   )rN   itemsr   rG   r   rm   warningswarnr5   r6   r   r   )r
   r   r   ry   Zloc0loc1rN   offr    r   r   r     s$   
zKnownPartsOfAFile._fetchrJ   r   r   r    r   r     s
    r   c                   @   sJ   e Zd ZdZedg dZdddZdd Zd	d
 Z	dd Z
dd ZdS )UpdatableLRUzg
    Custom implementation of LRU cache that allows updating keys

    Used by BackgroudBlockCache
    	CacheInfo)hitsmissesmaxsizecurrsize   c                 C   s0   t  | _|| _|| _d| _d| _t | _	d S rM   )
collectionsOrderedDict_cache_func	_max_size_hits_misses	threadingLock_lock)r
   funcmax_sizer   r   r   r     s   
zUpdatableLRU.__init__c                 G   s   | j & || jv r"| j| |  jd7  _| j| W  d    S W d    n1 s,w   Y  | j| }| j , || j|< |  jd7  _t| j| jkr]| jjdd W d    |S W d    |S 1 shw   Y  |S )Nr"   Flast)	r   r   move_to_endr   r   r   rG   r   popitem)r
   argsresultr   r   r   __call__  s*   




zUpdatableLRU.__call__c                 G   s4   | j  || jv W  d    S 1 sw   Y  d S r   )r   r   )r
   r   r   r   r   is_key_cached%  s   $zUpdatableLRU.is_key_cachedc                 G   sd   | j % || j|< t| j| jkr | jjdd W d    d S W d    d S 1 s+w   Y  d S )NFr   )r   r   rG   r   r   )r
   r   r   r   r   r   add_key)  s   
"zUpdatableLRU.add_keyc                 C   sH   | j  | j| jt| j| j| jdW  d    S 1 sw   Y  d S )N)r   r   r   r   )r   r   r   rG   r   r   r   r3   r   r   r   rb   /  s   $zUpdatableLRU.cache_infoN)r   )r   r   r   r   r   
namedtupler   r   r   r   r   rb   r   r   r   r   r     s    
r   c                       sd   e Zd ZdZdZd fdd	Zdd Zdd	 Zd
d Zdd Z	dd Z
d fdd	Z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``.
    
backgroundrP   c                    sZ   t  ||| t|| | _|| _t| j|| _t	dd| _
d | _d | _t | _d S )Nr"   max_workers)r   r   rQ   rR   rS   rT   r   rW   rX   r   _thread_executor_fetch_future_block_number_fetch_futurer   r   _fetch_future_lockrY   r    r   r   r   U  s   zBackgroundBlockCache.__init__c                 C   rZ   )Nz8<BackgroundBlockCache blocksize={}, size={}, nblocks={}>r[   r3   r   r   r   r]   `  r^   zBackgroundBlockCache.__repr__c                 C   r_   r`   ra   r3   r   r   r   rb   e  rc   zBackgroundBlockCache.cache_infoc                 C   s(   | j }|d= |d= |d= |d= |d= |S )NrX   r   r   r   r   rd   r?   r   r   r   rA   p  s   z!BackgroundBlockCache.__getstate__c                 C   sD   | j | t| j|d | _tdd| _d | _d | _t	
 | _d S )NrT   r"   r   )r=   rB   r   rW   rX   r   r   r   r   r   r   r   r?   r   r   r   rC   y  s   z!BackgroundBlockCache.__setstate__c           
      C   s  |d u rd}|d u r| j }|| j ks||krdS || j }|| j }d }d }| jF | jd urh| j rKtd | j| j	 | j
 d | _
d | _nt|| j
  koV|kn  }|rh| j
}| j}d | _
d | _W d    n1 srw   Y  |d urtd | j|	 | t||d D ]}| | q|d }	| j% | jd u r|	| jkr| j|	s|	| _
| j| j|	d| _W d    n1 sw   Y  | j||||dS )Nr   r   z3BlockCache joined background fetch without waiting.z(BlockCache waiting for background fetch.r"   asyncre   )r   r   r   r   doner5   rn   rX   r   r   r   boolr7   rS   r   r   submitrW   rh   )
r
   r   r;   rf   rg   Zfetch_future_block_numberZfetch_futureZ	must_joinri   Zend_block_plus_1r   r   r   r     st   








zBackgroundBlockCache._fetchsyncc                    sP   || j krtd|| j || j }|| j }td|| t ||}|S )rj   rk   z!BlockCache fetching block (%s) %drl   )r
   ri   Zlog_infor   r;   ro   r    r   r   rW     s   


z!BackgroundBlockCache._fetch_blockc           
      C   rp   rq   rr   ru   r   r   r   rh     rz   z BackgroundBlockCache._read_cacher{   )r   r|   r   r   r    r   r   9  s    	Kr   Fc                 C   s6   | j }|s|tv 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   cachesrm   )clsclobberr   r   r   r   register_cache  s   r   )F)r   rU   loggingrQ   r&   r   r   concurrent.futuresr   	getLoggerr5   objectr   r   rF   rK   rO   r}   r   r   r   r   r   r   cr   r   r   r   <module>   sH    
!J* WU8 J

