o
    װi                     @   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
   O/home/ubuntu/.local/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   encoded_lenencoded_key
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_f_fnameosfstatfilenost_sizetruncate_INITIAL_MMAP_SIZE	_capacitymmapACCESS_READACCESS_WRITE_m
_positionsr   _usedr   r   )selffilename	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   isddg           r   r   N)encoder7   structpackr-   r'   r   r%   r(   r#   r+   r   r,   )r.   r2   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tsr3   r
   r
   r   read_all_valuest   s   zMmapedDict.read_all_valuesc                 C   s*   || j vr
| | | j | }t| j|S N)r,   rC   r   r+   )r.   r2   r   r
   r
   r   
read_valuey   s   


zMmapedDict.read_valuec                 C   s2   || j vr
| | | j | }t| j||| d S rI   )r,   rC   r   r+   )r.   r2   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 rI   )r   r+   closerD   r
   r
   r   rL      s   


zMmapedDict.closeN)F)__name__
__module____qualname____doc__r4   staticmethodr9   rC   r   rH   rJ   rK   rL   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)rR   rS   rT   rU   rV   labelsr
   r
   r   mmap_key   s   r^   )r   )r[   r(   r!   r?   typingr   r&   Structr@   r   r   unpack_fromr   r   r   r   r   r   strr^   r
   r
   r
   r   <module>   s    
.[