
    Mh                     D   d dl Z d dlZd dlZd dlZd dlmZ dZ ej        d          j        Z	 ej        d          j        Z
 ej        d          j        Z ej        d          j        Zd Zd ZddZ G d	 d
          Zdededee         dee         dedefdZdS )    N)Listi      is   ddc                 6    t          ||          | ||dz   <   d S )N   )_pack_two_doubles_func)dataposvalue	timestamps       [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prometheus_client/mmap_dict.py_pack_two_doublesr      s$    /yAADS2X    c                 4    t          |          | ||dz   <   d S )N   )_pack_integer_func)r   r	   r
   s      r   _pack_integerr      s"    *511DS1Wr   c              #   n  K   |dk    rt          | d          d         }d}||k     rt          | |          d         }||z   |k    rt          d          |dz  }| |||z            }|d|dz   dz  z
  z   }||z  }t          | |          \  }}|                    d          |||fV  |dz  }||k     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   s           r   _read_all_valuesr      s       qyytQ''*
C
**%dC003t##STTTq3s[001 AqA(=$=>
z.tS99y  ))5)S@@@@r	 ******r   c                   T    e Zd ZdZddZed             Zd Zd Zd Z	d Z
d	 Z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                    t          ||rdnd          | _        || _        t          j        | j                                                  j        }|dk    r&| j                            t                     t          }|| _	        t          j
        | j                                        | j	        |rt          j        nt          j                  | _        i | _        t          | j        d          d         | _        | j        dk    r$d| _        t#          | j        d| j                   d S |s&|                                 D ]\  }}}}|| j        |<   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	   s          r   __init__zMmapedDict.__init__?   sJ   x!=>>8DGNN,,--5q==G/000)H!)DGNN,,dn7@#W4#3#3dFWY Y Y $TWa003
:??DJ$'1dj11111 /&*&;&;&=&= / /NCAs+.DOC((/ // /r   c                 R   t          | d          5 }|                    t          j                  }t	          |d          d         }|t          |          k    r(||                    |t          |          z
            z  }d d d            n# 1 swxY w Y   t          ||          S )Nr"   r   )r$   readr.   PAGESIZEr   lenr   )r5   infpr   r   s       r   read_all_values_from_filez$MmapedDict.read_all_values_from_fileT   s    (D!! 	4T 99T]++D"4++A.Dc$ii		$T"2333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4  d+++s   A1BBBc                 &   |                     d          }|ddt          |          dz   dz  z
  z  z   }t          j        dt          |           d                                 t          |          |dd          }| j        t          |          z   | j        k    r| xj        dz  c_        | j                            | j                   t          j        | j        	                                | j                  | _
        | j        t          |          z   | j        k    || j
        | j        | j        t          |          z   <   | xj        t          |          z  c_        t          | j
        d	| j                   | j        d
z
  | j        |<   dS )z0Initialize a value. Lock must be held by caller.r       r   r   isddg           r   r   N)encoder>   structpackr3   r-   r%   r+   r.   r)   r1   r   r2   )r4   r8   encodedpaddedr
   s        r   _init_valuezMmapedDict._init_value_   s[   **W%%DAW)9Q(>$>?@0F00077993w<<QTVYZZj3u::%66NNaNNGT^,,,i 0 0$.AADG j3u::%66 7<
4:E

223 	

c%jj 

dgq$*---#zBr   c                 8    t          | j        | j                  S )z1Yield (key, value, pos). No locking is performed.)r   r   )r   r1   r3   r4   s    r   r   zMmapedDict._read_all_valuesp   s    TW4:>>>>r   c              #   P   K   |                                  D ]\  }}}}|||fV  dS )z7Yield (key, value, timestamp). No locking is performed.N)r   )r4   kvtsr9   s        r   read_all_valueszMmapedDict.read_all_valuest   sD      0022 	 	KAq"aQ(NNNN	 	r   c                     || j         vr|                     |           | j         |         }t          | j        |          S N)r2   rK   r   r1   )r4   r8   r	   s      r   
read_valuezMmapedDict.read_valuey   sA    do%%S!!!oc""47C000r   c                     || j         vr|                     |           | j         |         }t          | j        |||           d S rT   )r2   rK   r   r1   )r4   r8   r
   r   r	   s        r   write_valuezMmapedDict.write_value   sK    do%%S!!!oc"$'3y99999r   c                     | j         rB| j                                         d | _        | j                                          d | _         d S d S rT   )r%   r1   closerM   s    r   rY   zMmapedDict.close   sF    7 	GMMOOODGGMMOOODGGG		 	r   N)F)__name__
__module____qualname____doc__r:   staticmethodr@   rK   r   rR   rU   rW   rY    r   r   r    r    2   s        
 
/ / / /* , , \,/ / /"? ? ?  
1 1 1: : :    r   r    metric_namename
labelnameslabelvalues	help_textreturnc                 p    t          t          ||                    }t          j        | |||gd          S )z&Format a key for use in the mmap file.T)	sort_keys)dictzipjsondumps)r`   ra   rb   rc   rd   labelss         r   mmap_keyrm      s9     #j+..//F:{D&)<MMMMr   )r   )rj   r.   r'   rG   typingr   r,   StructrH   r   r   unpack_fromr   r   r   r   r   r    strrm   r_   r   r   <module>rr      sY     				        "V]4((- &u--2 &-%%1#fmE**6 B B B2 2 2   .X X X X X X X XvN# NS Nd3i NdSVi Ndg Nlo N N N N N Nr   