a
    ܌xd                     @   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      i   dc                 C   s   t || ||d < d S )N   )_pack_double_funcdataposvalue r   S/var/www/html/Ranjet/env/lib/python3.9/site-packages/prometheus_client/mmap_dict.py_pack_double   s    r   c                 C   s   t || ||d < d S )N   )_pack_integer_funcr   r   r   r   _pack_integer   s    r   c                 c   s   |dkrt | dd }d}||k rt | |d }|| |krDtd|d7 }| |||  }|d|d d   }||7 }t| |d }|d||fV  |d7 }qdS )1Yield (key, value, pos). No locking is performed.r   r   z2Read beyond file size detected, file is corrupted.r   utf-8N)_unpack_integerRuntimeError_unpack_doubledecode)r   usedr	   Zencoded_lenZencoded_keyZ
padded_lenr
   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.

    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rd| _t| jd| j n"|s|  D ]\}}}|| j|< qd 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_fileR   s    4z$MmapedDict.read_all_values_from_filec                 C   s   | d}|ddt|d d    }tdt| d  t||d}| jt| | jkr|  jd9  _| j| j t| j	 | j| _
qJ|| 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   isdg           r   N)encoder3   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 )r   )r   r   )r   r(   r*   r+   r   r   r   r   n   s    zMmapedDict._read_all_valuesc                 c   s"   |   D ]\}}}||fV  qdS )z,Yield (key, value). No locking is performed.N)r   )r+   kvr/   r   r   r   read_all_valuesr   s    zMmapedDict.read_all_valuesc                 C   s.   || j vr| | | j | }t| j|d S )Nr   )r)   r?   r   r(   )r+   r.   r	   r   r   r   
read_valuew   s    


zMmapedDict.read_valuec                 C   s0   || j vr| | | j | }t| j|| d S N)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 rE   )r   r(   closer@   r   r   r   rG      s
    

zMmapedDict.closeN)F)__name__
__module____qualname____doc__r0   staticmethodr5   r?   r   rC   rD   rF   rG   r   r   r   r   r   1   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)rM   rN   rO   rP   rQ   labelsr   r   r   mmap_key   s    rY   )r   )rV   r%   r   r;   typingr   r#   Structr<   r   r   unpack_fromr   r   r   r   r   r   strrY   r   r   r   r   <module>   s   
\