a
    yn„fp{  ã                	   @  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mZmZmZmZmZmZmZmZ er¤d 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%„Z,e e!e%e"e$e#e&e'e)f	D ]Z-e,e-ƒ �q®dS )'é    )ÚannotationsN)ÚFutureÚThreadPoolExecutor)	Ú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$   úQ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fsspec/caching.pyÚ__init__:   s    zBaseCache.__init__ú
int | NoneÚbytes©ÚstartÚstopr   c                 C  s<   |d u rd}|d u r| j }|| j ks,||kr0dS |  ||¡S )Nr   ó    )r   r   ©r#   r*   r+   r$   r$   r%   Ú_fetchD   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_statsM   s    zBaseCache._reset_statsÚstrc                 C  s0   | j dkr| jdkrdS d| j| j | j| jf S )z2Return a formatted string of the cache statistics.r   Ú z3 , %s: %d hits, %d misses, %d total requested bytes)r   r    r   r!   r0   r$   r$   r%   Ú
_log_statsS   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!   r0   r$   r$   r%   Ú__repr___   s    ÿþýüûúùzBaseCache.__repr__N)r6   Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r&   r.   r1   r4   r7   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 )Ú	MMapCachezämemory-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 u rtƒ n|| _|| _|  ¡ | _d S ©N)Úsuperr&   Úsetr?   r>   Ú	_makefileÚcache)r#   r   r   r   r>   r?   ©r5   r$   r%   r&   w   s    zMMapCache.__init__zmmap.mmap | bytearrayr/   c                 C  s¢   dd l }dd l}| jdkr tƒ S | jd u s8tj | j¡s„| jd u 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ÚTemporaryFilerB   r?   ÚopenÚseekÚwriteÚflushÚfileno)r#   r=   rH   Úfdr$   r$   r%   rC   „   s    




zMMapCache._makefiler'   r(   ©r*   Úendr   c           
   	     sD  t  d|› d|› �¡ |d u r"d}|d u r0ˆ j}|ˆ jksB||krFdS |ˆ j }|ˆ j }‡ fdd„t||d ƒD ƒ}‡ fdd„t||d ƒD ƒ}ˆ  jt|ƒ7  _ˆ  jt|ƒ7  _|�r6| d¡}|ˆ j }t	|ˆ j ˆ jƒ}	ˆ  j
|	| 7  _
t  d	|› d
|› d|	› d�¡ ˆ  ||	¡ˆ j||	…< ˆ j |¡ q¶ˆ j||… S )NzMMap cache fetching ú-r   r,   c                   s   g | ]}|ˆ j vr|‘qS r$   ©r?   ©Ú.0Úir0   r$   r%   Ú
<listcomp>¤   r,   z$MMapCache._fetch.<locals>.<listcomp>rF   c                   s   g | ]}|ˆ j v r|‘qS r$   rW   rX   r0   r$   r%   r[   ¥   r,   zMMap get block #z (ú))ÚloggerÚdebugr   r   Úranger    Úlenr   ÚpopÚminr!   r   rD   r?   Úadd)
r#   r*   rU   Zstart_blockZ	end_blockZneedÚhitsrZ   ZsstartÚsendr$   r0   r%   r.   š   s,    



zMMapCache._fetchúdict[str, Any]c                 C  s   | j  ¡ }|d= |S )NrD   )Ú__dict__Úcopy©r#   Ústater$   r$   r%   Ú__getstate__¶   s    
zMMapCache.__getstate__©rj   r   c                 C  s   | j  |¡ |  ¡ | _d S r@   )rg   ÚupdaterC   rD   ri   r$   r$   r%   Ú__setstate__¼   s    zMMapCache.__setstate__)NN)r6   r8   r9   r:   r   r&   rC   r.   rk   rn   Ú__classcell__r$   r$   rE   r%   r<   l   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   )rA   r&   rD   r*   rU   r"   rE   r$   r%   r&   Ì   s    zReadAheadCache.__init__r'   r(   rT   c                 C  s>  |d u rd}|d u 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 rÐn 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,   rF   )r   r*   rU   r   rD   r    r`   rb   r   r!   r   )r#   r*   rU   ÚlÚpartr$   r$   r%   r.   Ò   s.    zReadAheadCache._fetch©r6   r8   r9   r:   r   r&   r.   ro   r$   r$   rE   r%   rp   Â   s   rp   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 )ÚFirstChunkCachez›Caches 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 r@   )rA   r&   rD   r"   rE   r$   r%   r&   ù   s    zFirstChunkCache.__init__r'   r(   rT   c                 C  s0  |pd}|| j kr t d¡ dS t|| j ƒ}|| jk �r | jd u 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,   rF   )
r   r]   r^   rb   r   rD   r    r!   r   r   )r#   r*   rU   Údatars   r$   r$   r%   r.      s0    




zFirstChunkCache._fetchrt   r$   r$   rE   r%   ru   ð   s   ru   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 r@   )
rA   r&   ÚmathÚceilr   r{   Ú	functoolsÚ	lru_cacheÚ_fetch_blockÚ_fetch_block_cached©r#   r   r   r   r{   rE   r$   r%   r&   7  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_infor0   r$   r$   r%   r…   ?  s    	zBlockCache.cache_inforf   r/   c                 C  s   | j }|d= |S )Nr�   ©rg   ri   r$   r$   r%   rk   J  s    zBlockCache.__getstate__rl   c                 C  s&   | j  |¡ t |d ¡| jƒ| _d S )Nr{   )rg   rm   r~   r   r€   r�   ri   r$   r$   r%   rn   O  s    ÿzBlockCache.__setstate__r'   r(   rT   c                 C  st   |d u rd}|d u r| j }|| j ks,||kr0dS || j }|| j }t||d ƒD ]}|  |¡ qR| j||||d�S )Nr   r,   rF   ©Ústart_block_numberÚend_block_number)r   r   r_   r�   Ú_read_cache)r#   r*   rU   rˆ   r‰   Úblock_numberr$   r$   r%   r.   U  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 (r\   rF   zBlockCache fetching block %d)	r   Ú
ValueErrorr   r!   r    r]   ÚinforA   r.   )r#   r‹   r*   rU   Úblock_contentsrE   r$   r%   r€   l  s    
ÿÿ

zBlockCache._fetch_block©r*   rU   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.
        rF   Nr,   ©r   r   r�   ÚextendÚmapr_   ÚappendÚjoin©	r#   r*   rU   rˆ   r‰   Ú	start_posÚend_posÚblockÚoutr$   r$   r%   rŠ   ~  s    


þÿzBlockCache._read_cache)ry   )r6   r8   r9   r:   r   r&   r…   rk   rn   r.   r€   rŠ   ro   r$   r$   rE   r%   rx     s    ÿrx   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,   )rA   r&   rD   r*   rU   r    )r#   r   r   r   r    rE   r$   r%   r&   ¶  s
    zBytesCache.__init__r'   rT   c                 C  sì  |d u rd}|d u r| j }|| j ks,||kr0dS | jd ur†|| jkr†| jd ur†|| jk r†|| j }|  jd7  _| j||| | … S | jr t| j || j ƒ}n|}||ks¶|| j krºdS | jd u sÐ|| jk �r| jd u sæ|| jk�r|  j|| 7  _|  jd7  _|  	||¡| _|| _�nF| jd u�s.J ‚| jd u�s>J ‚|  jd7  _|| jk �rÔ| jd u �sv| j| | jk�rž|  j|| 7  _|  	||¡| _|| _n4|  j| j| 7  _|  	|| j¡}|| _|| j | _n�| jd u�rd|| jk�rd| j| j k�rünh|| 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 rq   )r   r*   rU   r   rD   r   rb   r!   r    r   r`   r    )r#   r*   rU   ÚoffsetZbendÚnewr�   Únumr$   r$   r%   r.   ¿  s|    ÿþýü
ÿÿ


zBytesCache._fetchr/   c                 C  s
   t | jƒS r@   )r`   rD   r0   r$   r$   r%   Ú__len__  s    zBytesCache.__len__)T)
r6   r8   r9   r:   r   r;   r&   r.   r¤   ro   r$   r$   rE   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   rw   r   c                   sN   t ƒ  |||¡ |d u rD|  jd7  _|  j| j7  _|  d| j¡}|| _d S )NrF   r   )rA   r&   r    r!   r   r   rw   )r#   r   r   r   rw   rE   r$   r%   r&     s    zAllBytes.__init__r(   r)   c                 C  s   |  j d7  _ | j||… S )NrF   )r   rw   r-   r$   r$   r%   r.     s    zAllBytes._fetch)NNNN©	r6   r8   r9   r:   r   r;   r&   r.   ro   r$   r$   rE   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   rw   ÚstrictÚ_c                   sÐ   t ƒ  |||¡ || _|rÆt| ¡ ƒ}|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   rF   éÿÿÿÿ)
rA   r&   rª   ÚsortedÚkeysra   r—   ÚdictÚziprw   )r#   r   r   r   rw   rª   r«   Zold_offsetsÚoffsetsr?   r*   r+   Zstart0Zstop0rE   r$   r%   r&   =  s    	
zKnownPartsOfAFile.__init__r'   r(   r)   c                   s:  |d u rd}|d u r| j }d}| j ¡ D ]”\\}}}||  krH|k r(n q(|| }|||| | … }| jr†||  kr‚|kr´n n.|d|| t|ƒ  7 }|  jd7  _|  S |} q¾q(| jd u rÜtd||f› d�ƒ‚t 	d||f› d�¡ t
 d|› d	|› �¡ |  j|| 7  _|  jd7  _|tƒ  ||¡ S )
Nr   r,   ó    rF   z&Read is outside the known file parts: z. z%. IO/caching performance may be poor!z!KnownPartsOfAFile cache fetching rV   )r   rw   Úitemsrª   r`   r   r   r�   ÚwarningsÚwarnr]   r^   r!   r    rA   r.   )r#   r*   r+   r�   Zloc0Úloc1rw   ÚoffrE   r$   r%   r.   [  s0    
ÿzKnownPartsOfAFile._fetch)NTr§   r$   r$   rE   r%   r¨   $  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   rd   ÚmissesÚmaxsizeÚcurrsizeN)r6   r8   r9   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�L || jv rZ| j |¡ |  jd7  _| j| W  d   ƒ S W d   ƒ n1 sn0    Y  | j|i |¤Ž}| j�F || j|< |  jd7  _t| jƒ| j	krÆ| jj
dd� W d   ƒ n1 sÚ0    Y  |S )Nz Got unexpected keyword argument rF   F©Úlast)Ú	TypeErrorr®   rÉ   rÂ   Úmove_to_endrÅ   rÃ   rÆ   r`   rÄ   Úpopitem)r#   rÊ   rË   Úresultr$   r$   r%   Ú__call__ž  s    
6
,zUpdatableLRU.__call__r   rŸ   )rÊ   r   c                 G  s4   | j � || jv W  d   ƒ S 1 s&0    Y  d S r@   )rÉ   rÂ   )r#   rÊ   r$   r$   r%   Úis_key_cached±  s    zUpdatableLRU.is_key_cached)rÑ   rÊ   r   c                 G  sR   | j �8 || j|< t| jƒ| jkr0| jjdd� W d   ƒ n1 sD0    Y  d S )NFrÌ   )rÉ   rÂ   r`   rÄ   rÐ   )r#   rÑ   rÊ   r$   r$   r%   Úadd_keyµ  s    
zUpdatableLRU.add_keyr¹   r/   c                 C  sH   | j �. | j| jt| jƒ| j| jd�W  d   ƒ S 1 s:0    Y  d S )N)r»   r¼   rd   rº   )rÉ   r½   rÄ   r`   rÂ   rÅ   rÆ   r0   r$   r$   r%   r…   »  s    üzUpdatableLRU.cache_infoN)r¾   )r6   r8   r9   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   ry   r   r   r   rz   c                   sZ   t ƒ  |||¡ t || ¡| _|| _t| j|ƒ| _t	dd�| _
d | _d | _t ¡ | _d S )NrF   ©Úmax_workers)rA   r&   r|   r}   r   r{   r¸   r€   r�   r   Ú_thread_executorÚ_fetch_future_block_numberÚ_fetch_futurerÇ   rÈ   Ú_fetch_future_lockr‚   rE   r$   r%   r&   á  s    zBackgroundBlockCache.__init__r¹   r/   c                 C  s
   | j  ¡ S rƒ   r„   r0   r$   r$   r%   r…   î  s    	zBackgroundBlockCache.cache_inforf   c                 C  s(   | j }|d= |d= |d= |d= |d= |S )Nr�   rÙ   rÚ   rÛ   rÜ   r†   ri   r$   r$   r%   rk   ù  s    z!BackgroundBlockCache.__getstate__c                 C  sD   | j  |¡ t| j|d ƒ| _tdd�| _d | _d | _t	 
¡ | _d S )Nr{   rF   r×   )rg   rm   r¸   r€   r�   r   rÙ   rÚ   rÛ   rÇ   rÈ   rÜ   ri   r$   r$   r%   rn     s    z!BackgroundBlockCache.__setstate__r'   r(   rT   c           
      C  sÄ  |d u rd}|d u r| j }|| j ks,||kr0dS || j }|| j }d }d }| j�š | jd urÞ| jd uslJ ‚| j ¡ r¤t d¡ | j 	| j 
¡ | j¡ d | _d | _n:t|| j  koº|kn  ƒ}|rÞ| j}| j}d | _d | _W d   ƒ n1 sò0    Y  |d u�r"t d¡ | j 	| 
¡ |¡ t||d ƒD ]}|  |¡ �q0|d }	| j�P | jd u �r’|	| jk�r’| j |	¡�s’|	| _| j | j|	d¡| _W d   ƒ n1 �s¨0    Y  | j||||d�S )Nr   r,   z3BlockCache joined background fetch without waiting.z(BlockCache waiting for background fetch.rF   Úasyncr‡   )r   r   rÜ   rÛ   rÚ   Údoner]   r�   r�   rÔ   rÑ   rŸ   r_   r   rÓ   rÙ   Úsubmitr€   rŠ   )
r#   r*   rU   rˆ   r‰   Zfetch_future_block_numberZfetch_futureZ	must_joinr‹   Zend_block_plus_1r$   r$   r%   r.   
  sr    




ÿÿþÿ$

ÿÿþ
ýÿ&üzBackgroundBlockCache._fetchÚsyncr2   )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Ž   r\   z!BlockCache fetching block (%s) %drF   )	r   r�   r   r]   r�   r!   r    rA   r.   )r#   r‹   rá   r*   rU   r‘   rE   r$   r%   r€   V  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Š   h  s    


þÿz BackgroundBlockCache._read_cache)ry   )rà   )r6   r8   r9   r:   r   r;   r&   r…   rk   rn   r.   r€   rŠ   ro   r$   r$   rE   r%   rÕ   Å  s   
 ÿ	LrÕ   z!dict[str | None, type[BaseCache]]ÚcachesFztype[BaseCache]rŸ   r   )ÚclsÚclobberr   c                 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   râ   r�   )rã   rä   r   r$   r$   r%   Úregister_cache™  s    rå   )F).Ú
__future__r   rÁ   r~   Úloggingr|   rJ   rÇ   r´   Úconcurrent.futuresr   r   Útypingr   r   r   r   r	   r
   r   r   r   r=   Ztyping_extensionsr   r   r   Ú	getLoggerr]   r   r(   r   r   r<   rp   ru   rx   rž   r¥   r¨   r¸   rÕ   râ   r;   rå   Úcr$   r$   r$   r%   Ú<module>   sV   ,

DV.. 
ee< Qþ÷