
    ^f                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dZdZ G d	 d
e          Z G d d          ZdS )z#Logical Clocks and Synchronization.    )annotations)islice)
itemgetter)Lock)Any)LamportClock	timetuplez0_lamport(clock={0}, timestamp={1}, id={2} {3!r})c                      e Zd ZdZdZ	 dddZddZddZddZddZ	ddZ
ddZ e ed                    Z e ed                    Z e ed                    Z e ed                    ZdS ) r	   a`  Tuple of event clock information.

    Can be used as part of a heap to keep events ordered.

    Arguments:
    ---------
        clock (Optional[int]):  Event clock value.
        timestamp (float): Event UNIX timestamp value.
        id (str): Event host id (e.g. ``hostname:pid``).
        obj (Any): Optional obj to associate with this event.
     Nclock
int | None	timestampfloatidstrobjr   returnc                @    t                               | ||||f          S N)tuple__new__)clsr   r   r   r   s        >/var/www/html/env/lib/python3.11/site-packages/kombu/clocks.pyr   ztimetuple.__new__   s!     }}S5)R"=>>>    c                    t          j        |  S r   )R_CLOCKformatselfs    r   __repr__ztimetuple.__repr__#   s    ~t$$r   r   c                     t          |           S r   )r   r   s    r   __getnewargs__ztimetuple.__getnewargs__&   s    T{{r   otherboolc                    	 | d         |d         }}|r |r||k    r| d         |d         k     S ||k     S | d         |d         k     S # t           $ r
 t          cY S w xY w)Nr         )
IndexErrorNotImplemented)r   r#   ABs       r   __lt__ztimetuple.__lt__)   s    		"7E!HqA Q 667U1X--1u7U1X%% 	" 	" 	"!!!!	"s   +A A A AAc                    || k     S r   r   r   r#   s     r   __gt__ztimetuple.__gt__6   s    t|r   c                    || k      S r   r   r.   s     r   __le__ztimetuple.__le__9   s    4<r   c                    | |k      S r   r   r.   s     r   __ge__ztimetuple.__ge__<   s    %<r   r   r'   r&      r   )
r   r   r   r   r   r   r   r   r   r	   r   r   )r   r   )r#   r   r   r$   )__name__
__module____qualname____doc__	__slots__r   r    r"   r,   r/   r1   r3   propertyr   r   r   r   r   r   r   r   r	   r	      s       
 
 I GK? ? ? ? ?
% % % %   " " " "                  HZZ]]##EA''I	**Q--	 	 B
(::a==
!
!CCCr   r	   c                  L    e Zd ZdZdZdefdd	ZddZddZddZ	ddZ
ddZdS )r   a  Lamport's logical clock.

    From Wikipedia:

    A Lamport logical clock is a monotonically incrementing software counter
    maintained in each process.  It follows some simple rules:

        * A process increments its counter before each event in that process;
        * When a process sends a message, it includes its counter value with
          the message;
        * On receiving a message, the receiver process sets its counter to be
          greater than the maximum of its own value and the received value
          before it considers the message received.

    Conceptually, this logical clock can be thought of as a clock that only
    has meaning in relation to messages moving between processes.  When a
    process receives a message, it resynchronizes its logical clock with
    the sender.

    See Also
    --------
        * `Lamport timestamps`_

        * `Lamports distributed mutex`_

    .. _`Lamport Timestamps`: https://en.wikipedia.org/wiki/Lamport_timestamps
    .. _`Lamports distributed mutex`: https://bit.ly/p99ybE

    *Usage*

    When sending a message use :meth:`forward` to increment the clock,
    when receiving a message use :meth:`adjust` to sync with
    the time stamp of the incoming message.

    r   initial_valueintr   
type[Lock]r   Nonec                2    || _          |            | _        d S r   )valuemutex)r   r=   r   s      r   __init__zLamportClock.__init__m   s     #
TVV


r   r#   c                    | j         5  t          | j        |          dz   x}| _        |cd d d            S # 1 swxY w Y   d S Nr'   )rC   maxrB   )r   r#   rB   s      r   adjustzLamportClock.adjusts   s    Z 	 	!$TZ!7!7!!;;EDJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   !6::c                r    | j         5  | xj        dz  c_        | j        cd d d            S # 1 swxY w Y   d S rF   )rC   rB   r   s    r   forwardzLamportClock.forwardx   s    Z 	 	JJ!OJJ:	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ,00hlist[tuple[int, str]]tuple[int, str]c                <   |d         d         |d         d         k    rwg }t          |t          |dd                    D ]=}|d         d         |d         d         k    r n|                    |d                    >t          |d           d         S |d         S )a  Sort heap of events.

        List of tuples containing at least two elements, representing
        an event, where the first element is the event's scalar clock value,
        and the second element is the id of the process (usually
        ``"hostname:pid"``): ``sh([(clock, processid, ...?), (...)])``

        The list must already be sorted, which is why we refer to it as a
        heap.

        The tuple will not be unpacked, so more than two elements can be
        present.

        Will return the latest event.
        r   r'   Nc                    | d         S rF   r   )events    r   <lambda>z(LamportClock.sort_heap.<locals>.<lambda>   s
    %( r   )key)zipr   appendsorted)r   rK   samePNs       r   	sort_heapzLamportClock.sort_heap}   s      Q47ad1gD!VAq$//00 # #a58r!uQx''EBqE""""$$:$:;;;A>>tr   r   c                *    t          | j                  S r   )r   rB   r   s    r   __str__zLamportClock.__str__   s    4:r   c                    d| j          dS )Nz<LamportClock: >)rB   r   s    r   r    zLamportClock.__repr__   s    .....r   N)r=   r>   r   r?   r   r@   )r#   r>   r   r>   )r   r>   )rK   rL   r   rM   r5   )r6   r7   r8   r9   rB   r   rD   rH   rJ   rX   rZ   r    r   r   r   r   r   E   s        " "J E $%       
   
   6   / / / / / /r   r   N)r9   
__future__r   	itertoolsr   operatorr   	threadingr   typingr   __all__r   r   r	   r   r   r   r   <module>rc      s    ) ) " " " " " "                        
'
<3" 3" 3" 3" 3" 3" 3" 3"lW/ W/ W/ W/ W/ W/ W/ W/ W/ W/r   