a
    ö£Be  ã                   @   s¬   d dl Z d dlZd dlZd dlZd dlmZ dZe d¡jZ	e d¡jZ
e d¡jZe d¡jZdd„ Zdd	„ Zdd
d„ZG dd„ dƒZeeee ee eedœdd„ZdS )é    N)ÚListi   ó   is   ddc                 C   s   t ||ƒ| ||d …< d S )Né   )Ú_pack_two_doubles_func)ÚdataÚposÚvalueÚ	timestamp© r
   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prometheus_client/mmap_dict.pyÚ_pack_two_doubles   s    r   c                 C   s   t |ƒ| ||d …< d S )Né   )Ú_pack_integer_func)r   r   r   r
   r
   r   Ú_pack_integer   s    r   c                 c   s¨   |dkrt | dƒd }d}||k r¤t | |ƒd }|| |krDtdƒ‚|d7 }| ||| … }|d|d d   }||7 }t| |ƒ\}}| d¡|||fV  |d7 }qdS )z<Yield (key, value, timestamp, pos). No locking is performed.r   é   z2Read beyond file size detected, file is corrupted.r   úutf-8r   N)Ú_unpack_integerÚRuntimeErrorÚ_unpack_two_doublesÚdecode)r   Úusedr   Zencoded_lenZencoded_keyZ
padded_lenr   r	   r
   r
   r   Ú_read_all_values   s    r   c                   @   sV   e Zd ZdZddd„Zedd„ ƒZdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )Ú
MmapedDictaÁ  A dict of doubles, backed by an mmapped file.

    The file starts with a 4 byte int, indicating how much of it is used.
    Then 4 bytes of padding.
    There's then a number of entries, consisting of a 4 byte int which is the
    size of the next field, a utf-8 encoded string key, padding to a 8 byte
    alignment, and then a 8 byte float which is the value and a 8 byte float
    which is a UNIX timestamp in seconds.

    Not thread safe.
    Fc                 C   sÒ   t ||rdndƒ| _|| _t | j ¡ ¡j}|dkrD| j t¡ t}|| _	t
j
| j ¡ | j	|rdt
jnt
jd�| _i | _t| jdƒd | _| jdkrªd| _t| jd| jƒ n$|sÎ|  ¡ D ]\}}}}|| j|< q¶d S )NÚrbza+br   )Úaccessr   )ÚopenÚ_fZ_fnameÚosÚfstatÚfilenoÚst_sizeÚtruncateÚ_INITIAL_MMAP_SIZEÚ	_capacityÚmmapÚACCESS_READÚACCESS_WRITEÚ_mÚ
_positionsr   Ú_usedr   r   )ÚselfÚfilenameZ	read_modeÚcapacityÚkeyÚ_r   r
   r
   r   Ú__init__?   s$    ÿ
zMmapedDict.__init__c                 C   sp   t | dƒ�L}| tj¡}t|dƒd }|t|ƒkrH|| |t|ƒ ¡7 }W d   ƒ n1 s\0    Y  t||ƒS )Nr   r   )r   Úreadr$   ÚPAGESIZEr   Úlenr   )r+   Úinfpr   r   r
   r
   r   Úread_all_values_from_fileT   s    4z$MmapedDict.read_all_values_from_filec                 C   sä   |  d¡}|ddt|ƒd d    }t dt|ƒ› d�  ¡ t|ƒ|dd¡}| jt|ƒ | jkr”|  jd9  _| j | j¡ t | j 	¡ | j¡| _
qL|| j
| j| jt|ƒ …< |  jt|ƒ7  _t| j
d	| jƒ | jd
 | j|< dS )z0Initialize a value. Lock must be held by caller.r   ó    r   r   ÚiZsddg        é   r   r   N)Úencoder2   ÚstructÚpackr)   r#   r   r!   r$   r   r'   r   r(   )r*   r-   ÚencodedÚpaddedr   r
   r
   r   Ú_init_value_   s    
&zMmapedDict._init_valuec                 C   s   t | j| jd�S )z1Yield (key, value, pos). No locking is performed.)r   r   )r   r'   r)   ©r*   r
   r
   r   r   p   s    zMmapedDict._read_all_valuesc                 c   s&   |   ¡ D ]\}}}}|||fV  qdS )z7Yield (key, value, timestamp). No locking is performed.N)r   )r*   ÚkÚvÚtsr.   r
   r
   r   Úread_all_valuest   s    zMmapedDict.read_all_valuesc                 C   s*   || j vr|  |¡ | j | }t| j|ƒS ©N)r(   r=   r   r'   )r*   r-   r   r
   r
   r   Ú
read_valuey   s    


zMmapedDict.read_valuec                 C   s2   || j vr|  |¡ | j | }t| j|||ƒ d S rC   )r(   r=   r   r'   )r*   r-   r   r	   r   r
   r
   r   Úwrite_value   s    


zMmapedDict.write_valuec                 C   s*   | j r&| j ¡  d | _| j  ¡  d | _ d S rC   )r   r'   Úcloser>   r
   r
   r   rF   …   s
    

zMmapedDict.closeN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   Ústaticmethodr4   r=   r   rB   rD   rE   rF   r
   r
   r
   r   r   2   s   


r   )Úmetric_nameÚnameÚ
labelnamesÚlabelvaluesÚ	help_textÚreturnc                 C   s$   t t||ƒƒ}tj| |||gdd�S )z&Format a key for use in the mmap file.T)Ú	sort_keys)ÚdictÚzipÚjsonÚdumps)rL   rM   rN   rO   rP   Úlabelsr
   r
   r   Úmmap_key�   s    rX   )r   )rU   r$   r   r9   Útypingr   r"   ÚStructr:   r   r   Úunpack_fromr   r   r   r   r   r   ÚstrrX   r
   r
   r
   r   Ú<module>   s   
[