o
    Ò);c}  ã                   @   sˆ   d dl Z d dlZd dlZd dl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d„ ZdS )é    Ni   ó   ió   dc                 C   ó   t |ƒ| ||d …< d S )Né   )Ú_pack_double_func©ÚdataÚposÚvalue© r   úf/Users/cmarchbanks/go/src/github.com/prometheus/client_python/build/lib/prometheus_client/mmap_dict.pyÚ_pack_double   ó   r   c                 C   r   )Né   )Ú_pack_integer_funcr   r   r   r   Ú_pack_integer   r   r   c                 c   s²    |dkrt | dƒd }d}||k rWt | |ƒd }|| |kr#tdƒ‚|d7 }| ||| … }|d|d d   }||7 }t| |ƒd }| d¡||fV  |d7 }||k sdS 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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 |sg|  ¡ 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ÚmmapZACCESS_READZACCESS_WRITEÚ_mÚ
_positionsr   Ú_usedr   r   )ÚselfÚfilenameZ	read_modeZ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&   ZPAGESIZEr   Úlenr   )r+   Úinfpr   r   r   r   r   Úread_all_values_from_fileQ   s   €ú
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rR|  jd9  _| j | j¡ t | j 	¡ | j¡| _
| jt|ƒ | jks/|| 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)Úencoder0   ÚstructÚpackr)   r%   r   r#   r&   r!   r'   r   r(   )r*   r,   ÚencodedZ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   m   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_valuesq   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_valuev   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 d S rA   )r   r'   Úcloser<   r   r   r   rC   „   s   


üzMmapedDict.closeN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   Ústaticmethodr2   r;   r   r?   r@   rB   rC   r   r   r   r   r   0   s    


r   c                 C   s"   t t||ƒƒ}tj| ||gddS )z&Format a key for use in the mmap file.T)Z	sort_keys)ÚdictÚzipÚjsonÚdumps)Úmetric_nameÚnameÚ
labelnamesÚlabelvaluesÚlabelsr   r   r   Úmmap_keyŒ   s   rR   )r   )rK   r&   r   r8   r$   ÚStructr9   r   r   Úunpack_fromr   r   r   r   r   r   rR   r   r   r   r   Ú<module>   s    
\