o
    tf                     @   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dededee dee dedef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
   d/var/www/html/software/conda/envs/catlas/lib/python3.10/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 rXt | |d }|| |kr#td|d7 }| |||  }|d|d d   }||7 }t| |\}}|d|||fV  |d7 }||k sdS 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r"| jt t}|| _	t
j
| j | j	|r2t
jnt
jd| _i | _t| jdd | _| jdkrVd| _t| jd| j d S |sh|  D ]\}}}}|| j|< q\d S 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&}|tj}t|dd }|t|kr$|||t| 7 }W d    n1 s.w   Y  t||S )Nr   r   )r   readr$   PAGESIZEr   lenr   )r+   infpr   r   r
   r
   r   read_all_values_from_fileT   s   
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rS|  jd9  _| j| j t| j	 | j| _
| jt| | jks0|| 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 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    
.[