§
    ÚÇÔiµ  ã                   ót   — d dl Z d dlZd dlZd dlZej         G d„ d¦  «        ¦   «         Z G d„ d¦  «        ZdS )é    Nc                   ó.   — e Zd ZU eed<   eed<   eed<   dS )Ú_BufferItemÚdataÚidÚ	timestampN)Ú__name__Ú
__module__Ú__qualname__ÚbytesÚ__annotations__ÚstrÚfloat© ó    ú]/root/projects/qq-shell/venv/lib/python3.11/site-packages/inngest/connect/_internal/buffer.pyr   r      s0   € € € € € € à
€K€KKØ€G€GGØÐÐÑÐÐr   r   c            	       óÖ   — e Zd ZdZ	 	 ddedej        egdf         dz  dej        egdf         dz  fd„Zdede	d	e
fd
„Zded	e	dz  fd„Zded	e
fd„Zded	eeee	f                  fd„ZdS )ÚSizeConstrainedBuffera  
    Buffer for storing execution replies awaiting server acknowledgment.

    If the WebSocket connection drops before acknowledgment, these messages can
    be flushed via HTTP as a fallback.

    Features:
        - Maximum size enforcement: Oldest items are evicted when full
        - Timestamp tracking: Items can be retrieved by age for TTL-based flushing
        - O(1) add/delete/get operations using OrderedDict

    Used by _ExecutionHandler to implement reliable message delivery even
    across connection interruptions.
    NÚmax_size_bytesÚon_evictÚ	on_rejectc                 ó¢   — d| _         t          j        ¦   «         | _        || _        || _        || _        | j        dk    rt          d¦  «        ‚dS )aB  
        Initialize the buffer with a maximum size constraint.

        Args:
            max_size_bytes: Maximum total size of all items' data.
            on_evict: Existing item is evicted to make room for a new one.
            on_reject: New item is rejected because it exceeds the max buffer size by itself.
        r   z%max_size_bytes must be greater than 0N)Ú_current_sizeÚcollectionsÚOrderedDictÚ_itemsÚ	_on_evictÚ
_on_rejectÚ_max_size_bytesÚ
ValueError)Úselfr   r   r   s       r   Ú__init__zSizeConstrainedBuffer.__init__   s\   € ð ˆÔåÔ#Ñ%Ô%ð 	Œð "ˆŒØ#ˆŒà-ˆÔØÔ 1Ò$Ð$ÝÐDÑEÔEÐEð %Ð$r   Úitem_idr   Úreturnc                 ó–  — t          |¦  «        }|| j        k    r| j        |                      |¦  «         dS || j        v r5| xj        t          | j        |         j        ¦  «        z  c_        | j        |= | j        |z   | j        k    r‚| j        r{| j                             d¬¦  «        \  }}| xj        t          |j        ¦  «        z  c_        | j        |                      |j        ¦  «         | j        |z   | j        k    r| j        °{t          ||t          j
        ¦   «         ¬¦  «        }|| j        |<   | xj        |z  c_        dS )a  
        Add item to buffer. If adding would exceed size limit, evicts oldest
        items until there is enough space. Returns True if item was added
        successfully. If the item is larger than the max size, it is not added
        to the buffer.
        NF)Úlast)r   r   r   T)Úlenr   r   r   r   r   Úpopitemr   r   r   Útime)r    r"   r   Ú	item_sizeÚ_Úoldest_itemÚitems          r   ÚaddzSizeConstrainedBuffer.add8   sf  € õ ˜‘I”Iˆ	àtÔ+Ò+Ð+ØŒÐ*Ø—’ Ñ(Ô(Ð(Ø5ð d”kÐ!Ð!ØÐÔ¥# d¤k°'Ô&:Ô&?Ñ"@Ô"@Ñ@ÐÔØ”˜GÐ$ð
 Ô Ñ*¨TÔ-AÒAÐAØ”ð Bð "œ[×0Ò0°eÐ0Ñ<Ô<‰NˆAˆ{ØÐÔ¥# kÔ&6Ñ"7Ô"7Ñ7ÐÔØŒ~Ð)Ø—’˜{œ~Ñ.Ô.Ð.ð Ô Ñ*¨TÔ-AÒAÐAØ”ð Bõ ØØÝ”i‘k”kð
ñ 
ô 
ˆð
  $ˆŒGÑØÐÔ˜iÑ'ÐÔØˆtr   c                 óL   — | j                              |¦  «        }|€dS |j        S )z5
        Get item by ID without removing it.
        N)r   Úgetr   ©r    r"   r,   s      r   r/   zSizeConstrainedBuffer.getb   s)   € ð
 Œ{Š˜wÑ'Ô'ˆØˆ<Ø4ØŒyÐr   c                 óŠ   — || j         vrdS | j         |         }| xj        t          |j        ¦  «        z  c_        | j         |= dS )zP
        Delete item by ID. Returns True if item was found and deleted.
        FT)r   r   r&   r   r0   s      r   ÚdeletezSizeConstrainedBuffer.deletel   sM   € ð
 ˜$œ+Ð%Ð%Ø5àŒ{˜7Ô#ˆØÐÔc $¤)™nœnÑ,ÐÔØŒK˜Ð Øˆtr   Úsecondsc                 óÆ   — t          j         ¦   «         |z
  }g }| j                             ¦   «         D ].}|j        |k    r!|                     |j        |j        f¦  «         Œ/|S )zJ
        Get all items that were inserted at least `seconds` ago.
        )r(   r   Úvaluesr   Úappendr   r   )r    r3   Úcutoff_timeÚresultr,   s        r   Úget_older_thanz$SizeConstrainedBuffer.get_older_thany   sd   € õ
 ”i‘k”k GÑ+ˆØˆà”K×&Ò&Ñ(Ô(ð 	4ð 	4ˆDØŒ~ Ò,Ð,Ø—’˜tœw¨¬	Ð2Ñ3Ô3Ð3øàˆr   )NN)r   r	   r
   Ú__doc__ÚintÚtypingÚCallabler   r!   r   Úboolr-   r/   r2   r   ÚlistÚtupler9   r   r   r   r   r      s)  € € € € € ðð ð$ 9=Ø9=ð	Fð FàðFð ”/ 3 %¨ +Ô.°Ñ5ðFð ”? C 5¨$ ;Ô/°$Ñ6ð	Fð Fð Fð Fð4(˜3ð ( eð (°ð (ð (ð (ð (ðT˜3ð  5¨4¡<ð ð ð ð ð˜cð  dð ð ð ð ð eð °°U¸3À¸:Ô5FÔ0Gð ð ð ð ð ð r   r   )r   Údataclassesr(   r<   Ú	dataclassr   r   r   r   r   ú<module>rC      s£   ðØ Ð Ð Ð Ø Ð Ð Ð Ø €€€Ø €€€ð Ôðð ð ð ð ñ ô ñ Ôððwð wð wð wð wñ wô wð wð wð wr   