Ë
    3ýjý?  ã                   ó¶  — d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
mZmZ dZdZd	Zd
ZdZe› de› �Ze› de› �Ze› de› �Ze› de› �Zdedee   defd„Zdededee
   defd„Zdedefd„Z ee«      Zej;                  «       Zdefd„Z ej;                  «       Z!ej;                  «       Z"dede#defd„Z$d„ Z% ed¬«       G d„ d«      «       Z& G d„ d«      Z'y) ad  HIMPORT client-side fieldset registry for redis-py.

`HIMPORT` lets a client register an ordered list of hash field names once per
connection (a *fieldset*) and then create many hashes by sending only values.
Because a fieldset is server-side session state bound to a single physical
connection, redis-py keeps a client-level registry of the fieldsets the
application has declared and prepares them lazily, per connection, on first use
by ``himport_set``.

This module holds only that registry. It is pure in-memory state with no I/O and
no ``asyncio`` primitives, so a single class is shared by both the sync and async
clients.

Example::

    >>> from redis.himport import HImportRegistry
    >>> registry = HImportRegistry()
    >>> registry.prepare("account_data", ["name", "email", "age"])
    >>> registry.get("account_data").fields
    ('name', 'email', 'age')
é    N)ÚIterableÚIterator)Ú	dataclass)Ú	DataError)Ú
EncodableTÚFieldTÚKeyTÚHIMPORTÚPREPAREÚSETÚDISCARDÚ
DISCARDALLú Úfieldset_nameÚfieldsÚreturnc                 ó"   — t         t        | g|¢­S )zMPositional wire args for ``HIMPORT PREPARE fieldset_name field [field ...]``.)Ú_HIMPORTÚ_PREPARE)r   r   s     úG/root/telegram-saas/.venv/lib/python3.12/site-packages/redis/himport.pyÚhimport_prepare_commandr   5   s   € ä”h Ð7°Ñ7Ð7ó    ÚkeyÚvaluesc                 ó$   — t         t        | |g|¢­S )zMPositional wire args for ``HIMPORT SET key fieldset_name value [value ...]``.)r   Ú_SET)r   r   r   s      r   Úhimport_set_commandr   :   s   € ô ”d˜C Ð8°Ñ8Ð8r   c                 ó   — t         t        | fS )z;Positional wire args for ``HIMPORT DISCARD fieldset_name``.)r   Ú_DISCARD)r   s    r   Úhimport_discard_commandr    A   s   € ä”h Ð.Ð.r   c                 ó  — t        | t        «      r+t        | «      t        k(  xr | j	                  «       t
        k(  S t        | t        t        t        f«      r6t        | «      }t        |«      t        k(  xr |j	                  «       t        k(  S y)ab  Return ``True`` if ``command_name`` names the ``HIMPORT SET`` command.

    Redis command names are case-insensitive on the wire, and a caller using the
    raw ``execute_command`` API may pass the name in any case and as either
    ``str`` or ``bytes`` (e.g. ``execute_command("himport set", ...)`` or
    ``b"HIMPORT SET"``). The connection-state-aware ``HIMPORT SET`` path keys off
    the command name, so it must recognise all of those spellings: an exact
    comparison against :data:`HIMPORT_SET` would miss them and send a bare SET
    that fails with ``no such fieldset`` for a fieldset registered in the client
    but not yet PREPAREd on the borrowed connection. The length check keeps the
    per-command cost on the hot path to a single comparison for the common case
    of a differently-sized command name (``upper()`` runs only on a size match).
    F)
Ú
isinstanceÚstrÚlenÚ_HIMPORT_SET_LENÚupperÚHIMPORT_SETÚbytesÚ	bytearrayÚ
memoryviewÚ_HIMPORT_SET_BYTES)Úcommand_nameÚraws     r   Úis_himport_set_commandr.   J   sx   € ô �,¤Ô$ä�ÓÔ!1Ñ1ò 4Ø×"Ñ"Ó$¬Ñ3ð	
ô �,¤¬	´:Ð >Ô?Ü�LÓ!ˆÜ�3‹xÔ+Ñ+ÒQ°·	±	³Ô?QÑ0QÐQØr   Úupper_tokenÚupper_token_bytesc                 ó  — t        |«      }t        | t        «      r#t        | «      |k(  xr | j                  «       |k(  S t        | t        t
        t        f«      r.t	        | «      }t        |«      |k(  xr |j                  «       |k(  S y)a  Case-insensitive match of a single ``str``/``bytes`` command token.

    ``upper_token`` / ``upper_token_bytes`` must already be upper-cased. The
    length guard keeps the hot path to a single comparison for a differently
    sized token (``upper()`` runs only on a size match).
    F)r$   r"   r#   r&   r(   r)   r*   )Úvaluer/   r0   Únr-   s        r   Ú	_token_isr4   g   sr   € ô 	ˆKÓ€AÜ�%œÔÜ�5‹z˜Q‰Ò? 5§;¡;£=°KÑ#?Ð?Ü�%œ%¤¬JÐ7Ô8Ü�E‹lˆÜ�3‹x˜1‰}ÒA §¡£Ð0AÑ!AÐAØr   c                 ó8  — | sy| d   }t        |«      r&t        | «      dk  ry| d   | d   t        | dd «      fS t        | «      dk\  rSt        |t        t
        «      r>t        | d   t        t        «      r&t        | «      dk  ry| d   | d   t        | dd «      fS y)a�  Detect an ``HIMPORT SET`` command in ``args`` and return its operands.

    ``HIMPORT SET`` reaches the raw ``execute_command`` API in two wire-equivalent
    forms that the serializer both accept:

    * the joined form ``("HIMPORT SET", key, fieldset, *values)`` (``args[0]`` is
      the two-word command name the request packer splits on the space), and
    * the split form ``("HIMPORT", "SET", key, fieldset, *values)``.

    Both are case- and encoding-insensitive. The connection-state-aware executor
    and the pipeline pre-flight need the operands at the right offsets for either
    form, so this returns ``(key, fieldset_name, values_list)`` when ``args`` is an
    ``HIMPORT SET`` with enough operands, or ``None`` otherwise (a non-``HIMPORT
    SET`` command, or one with too few operands -- which falls through to the plain
    send so the server returns its own arity error).
    Nr   é   é   é   é   )r.   r$   Úlistr4   r   Ú_HIMPORT_BYTESr   Ú
_SET_BYTES)ÚargsÚfirsts     r   Úparse_himport_set_argsr?   w   s¡   € ñ" ØØ�‰G€Eä˜eÔ$Üˆt‹9�qŠ=ØØ�A‰w˜˜Q™¤ d¨1¨2 h£Ð/Ð/ô 	ˆD‹	�QŠÜ�eœX¤~Ô6Ü�d˜1‘gœt¤ZÔ0äˆt‹9�qŠ=ØØ�A‰w˜˜Q™¤ d¨1¨2 h£Ð/Ð/Ør   T)Úfrozenc                   ó:   — e Zd ZU dZeed<   eedf   ed<   eed<   y)ÚHImportFieldseta$  An immutable HIMPORT fieldset entry.

    Attributes:
        name: Fieldset name used by ``HIMPORT SET`` / ``HIMPORT DISCARD``.
        fields: Ordered field names, exactly as supplied by the caller. They are
            never reordered or deduplicated (HLD R.2): the server canonicalizes
            field order internally and rejects duplicate field names, so the
            client only preserves the caller's positional order.
        version: Monotonic stamp bumped each time the fieldset is (re)declared.
            Connections compare the version they last prepared against this value
            to detect a stale prepared state; the stamp is drawn from the registry's
            mutation clock (:attr:`HImportRegistry.revision`), which never repeats,
            so discarding and re-declaring the same name yields a fresh version
            rather than a colliding one. This is the prepare-side signal; the
            discard-side counterpart is the clock advancing on removal, since a
            removed fieldset leaves no entry to stamp.
    Úname.r   ÚversionN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   Ú__annotations__Útupler   Úint© r   r   rB   rB   œ   s"   … ñð$ ƒIØ�&˜#�+ÑÓØ„Lr   rB   c                   ó>  — e Zd ZdZdd„Zdefd„Zedee	   de
fd„«       Zded	e
defd
„Zdedee	   defd„Zdedefd„Zdefd„Zedefd„«       Zdee   dee   fd„Zdededz  fd„Zdee   fd„Zdee
eef      fd„Zdedefd„Zdefd„Zdee   fd„Zdefd„Zy)ÚHImportRegistryaÆ  Client-level registry of HIMPORT fieldsets.

    Pure in-memory state, shared by the sync and async clients. It is mutated only
    through the client's ``himport_prepare`` / ``himport_discard`` /
    ``himport_discard_all`` methods and exposed read-only through the client's
    ``himport_registry`` property. Mutations are serialized under a lock so the
    revision bump and dict change stay consistent when a sync ``Redis`` instance is
    shared across threads. Reads that iterate or snapshot the registry take the same
    lock, so a concurrent mutation cannot make them observe a torn view or raise
    ``dictionary changed size during iteration``; single-key/scalar reads (``get``,
    ``__contains__``, ``__len__``, :attr:`revision`) are atomic and stay lock-free.
    The async client runs single-threaded and never contends.

    The registry always starts empty; fieldsets are declared at runtime through the
    client's ``himport_prepare`` method.
    r   Nc                 óR   — i | _         t        j                  «       | _        d| _        y )Nr   )Ú
_fieldsetsÚ	threadingÚLockÚ_lockÚ	_revision©Úselfs    r   Ú__init__zHImportRegistry.__init__Ç   s"   € Ø68ˆŒô —^‘^Ó%ˆŒ
ð  ˆ�r   c                 óD   — | xj                   dz  c_         | j                   S )Nr7   ©rT   rU   s    r   Ú_advancezHImportRegistry._advanceÝ   s   € Ø�Š˜!Ñ�Ø�~‰~Ðr   r   c                 óŒ   — t        | t        t        t        t        f«      rt        d«      ‚t        | «      }|st        d«      ‚|S )aú  Validate and materialize the caller's field iterable into a tuple.

        Done *before* the mutation lock is taken: consuming an arbitrary iterable
        can be slow, or -- for a generator that inspects this same registry -- can
        re-enter a locked read (e.g. ``yield`` then ``registry.names()``). Running
        it under the non-reentrant ``_lock`` would stall every registry user or
        deadlock permanently. Field order is preserved; nothing is reordered or
        deduplicated.
        zWHIMPORT fields must be a collection of field names, not a single string or binary valuez-HIMPORT fieldset must have at least one field)r"   r#   r(   r)   r*   r   rJ   )r   Úfield_tuples     r   Ú_materialize_fieldsz#HImportRegistry._materialize_fieldsá   sI   € ô  �fœs¤E¬9´jÐAÔBÜð6óð ô ˜F“mˆÙÜÐKÓLÐLØÐr   rC   r\   c                 ó\   — t        ||| j                  «       ¬«      }|| j                  |<   |S )N)rC   r   rD   )rB   rZ   rP   )rV   rC   r\   Úfieldsets       r   Ú_setzHImportRegistry._setû   s2   € ô
 #ØØØ—M‘M“Oô
ˆð
 !)ˆ�‰˜ÑØˆr   c                 óŒ   — | j                  |«      }| j                  5  | j                  ||«      cddd«       S # 1 sw Y   yxY w)zñAdd or replace a fieldset, bumping its version, and return the entry.

        Re-declaring an existing name replaces its fields and bumps its version.
        Field order is preserved verbatim; nothing is reordered or deduplicated.
        N)r]   rS   r`   )rV   rC   r   r\   s       r   ÚpreparezHImportRegistry.prepare
  s>   € ð ×.Ñ.¨vÓ6ˆØ�Z‰Zñ 	0Ø—9‘9˜T ;Ó/÷	0÷ 	0ò 	0ús	   ž:ºAc                 ó²   — | j                   5  || j                  vr
	 ddd«       y| j                  |= | j                  «        	 ddd«       y# 1 sw Y   yxY w)zÓRemove a fieldset from the registry.

        Returns ``True`` if a fieldset was removed, ``False`` if ``name`` was not
        registered. Advances :attr:`revision` when a fieldset is actually removed.
        NFT)rS   rP   rZ   ©rV   rC   s     r   ÚdiscardzHImportRegistry.discard  sS   € ð �Z‰Zñ 	Ø˜4Ÿ?™?Ñ*Ø÷	ð 	ð —‘ Ð%Ø�M‰MŒOØ÷	÷ 	ñ 	ús   �A¦AÁAc                 óÌ   — | j                   5  t        | j                  «      }|r*| j                  j                  «        | j	                  «        |cddd«       S # 1 sw Y   yxY w)z†Remove all fieldsets and return the number removed.

        Advances :attr:`revision` when at least one fieldset is removed.
        N)rS   r$   rP   ÚclearrZ   )rV   Úcounts     r   Údiscard_allzHImportRegistry.discard_all$  sM   € ð
 �Z‰Zñ 	Ü˜Ÿ™Ó(ˆEÙØ—‘×%Ñ%Ô'Ø—‘”Ø÷	÷ 	ò 	ús   �AAÁA#c                 ó   — | j                   S )uø  Monotonic mutation clock, advanced on every registry change.

        It is the discard-side counterpart to per-fieldset
        :attr:`HImportFieldset.version`. A connection records the revision it last
        reconciled against; when it differs, a discard (or prepare) has occurred
        since, so the connection recomputes which of its prepared fieldsets are no
        longer registered â€” see :meth:`names_to_discard` â€” and discards those when
        it is released back to the pool.
        rY   rU   s    r   ÚrevisionzHImportRegistry.revision6  s   € ð �~‰~Ðr   Úprepared_namesc                 óŽ   — | j                   5  |D �cg c]  }|| j                  vsŒ|‘Œ c}cddd«       S c c}w # 1 sw Y   yxY w)a5  Return which of ``prepared_names`` are no longer registered.

        Given the fieldset names a connection has prepared on the server, this is
        the set that must be sent ``HIMPORT DISCARD`` (typically when the
        connection is released), because they have been removed from the registry.
        N)rS   rP   )rV   rl   rC   s      r   Únames_to_discardz HImportRegistry.names_to_discardC  sF   € ð �Z‰Zñ 	TØ%3ÖS˜T°tÀ4Ç?Á?Ò7R’DÒS÷	Tñ 	TùÚS÷	Tð 	Tús   �;’6¦6ª;¶;»Ac                 ó8   — | j                   j                  |«      S )z;Return the fieldset registered under ``name``, or ``None``.)rP   Úgetrd   s     r   rp   zHImportRegistry.getP  s   € à�‰×"Ñ" 4Ó(Ð(r   c                 óp   — | j                   5  t        | j                  «      cddd«       S # 1 sw Y   yxY w)z%Return the registered fieldset names.N)rS   r:   rP   rU   s    r   ÚnameszHImportRegistry.namesT  s*   € à�Z‰Zñ 	)Ü˜Ÿ™Ó(÷	)÷ 	)ò 	)ús   �,¬5c                 óŒ   — | j                   5  t        | j                  j                  «       «      cddd«       S # 1 sw Y   yxY w)z0Return a snapshot of ``(name, fieldset)`` pairs.N)rS   r:   rP   ÚitemsrU   s    r   rt   zHImportRegistry.itemsY  s3   € à�Z‰Zñ 	1Ü˜Ÿ™×-Ñ-Ó/Ó0÷	1÷ 	1ò 	1ús	   �#:ºAc                 ó   — || j                   v S ©N)rP   rd   s     r   Ú__contains__zHImportRegistry.__contains__^  s   € Ø�t—‘Ð&Ð&r   c                 ó,   — t        | j                  «      S rv   )r$   rP   rU   s    r   Ú__len__zHImportRegistry.__len__a  s   € Ü�4—?‘?Ó#Ð#r   c                 ó‚   — | j                   5  t        t        | j                  «      «      cd d d «       S # 1 sw Y   y xY wrv   )rS   Úiterr:   rP   rU   s    r   Ú__iter__zHImportRegistry.__iter__d  s1   € ð �Z‰Zñ 	/Üœ˜TŸ_™_Ó-Ó.÷	/÷ 	/ò 	/ús   �5µ>c                 óò   — | j                   5  t        | j                  j                  «       «      }d d d «       dj	                  d„ D «       «      }| j
                  j                  › d|› d�S # 1 sw Y   Œ=xY w)Nz, c              3   óV   K  — | ]!  \  }}|› d t        |j                  «      › �–— Œ# y­w)ú=N)r:   r   )Ú.0rC   r_   s      r   ú	<genexpr>z+HImportRegistry.__repr__.<locals>.<genexpr>n  s.   è ø€ ò 
Ù2@°$¸ˆtˆf�A”d˜8Ÿ?™?Ó+Ð,Ô-ñ
ùs   ‚')ú(ú))rS   r:   rP   rt   ÚjoinÚ	__class__rE   )rV   ÚentriesÚbodys      r   Ú__repr__zHImportRegistry.__repr__k  sp   € Ø�Z‰Zñ 	4Ü˜4Ÿ?™?×0Ñ0Ó2Ó3ˆG÷	4à�y‰yñ 
ØDKô
ó 
ˆð —.‘.×)Ñ)Ð*¨!¨D¨6°Ð3Ð3÷	4ð 	4ús   �$A-Á-A6)r   N) rE   rF   rG   rH   rW   rK   rZ   Ústaticmethodr   r   rJ   r]   r#   rB   r`   rb   Úboolre   ri   Úpropertyrk   r:   rn   rp   rr   rt   Úobjectrw   ry   r   r|   rˆ   rL   r   r   rN   rN   µ   sN  „ ñó" ð,˜#ó ð ð H¨VÑ$4ð ¸ò ó ðð2˜ð ¨5ð °_ó ð0˜Cð 0¨°&Ñ)9ð 0¸oó 0ð˜Cð  Dó ð
˜Só 
ð$ ð
˜#ò 
ó ð
ðT¨x¸©}ð TÀÀcÁó Tð)˜ð ) °$Ñ 6ó )ð)�t˜C‘yó )ð
1�t˜E # Ð"6Ñ7Ñ8ó 1ð
' ð '¨Dó 'ð$˜ó $ð/˜( 3™-ó /ð4˜#ô 4r   rN   )(rH   rQ   Úcollections.abcr   r   Údataclassesr   Úredis.exceptionsr   Úredis.typingr   r   r	   r   r   r   r   Ú_DISCARDALLÚHIMPORT_PREPAREr'   ÚHIMPORT_DISCARDÚHIMPORT_DISCARDALLr#   rJ   r   r   r    r$   r%   Úencoder+   rŠ   r.   r;   r<   r(   r4   r?   rB   rN   rL   r   r   ú<module>r–      sa  ðñó, ß .Ý !å &ß 1Ñ 1ð €Ø€Ø€Ø€Ø€à�J˜a ˜zÐ*€Ø�
˜!˜D˜6Ð"€Ø�J˜a ˜zÐ*€Ø �z  ; -Ð0Ð ð8¨3ð 8¸ÀÑ8Hð 8ÈUó 8ð
9Ø	ð9Ø!ð9Ø+3°JÑ+?ð9à
ó9ð/¨3ð /°5ó /ñ
 �{Ó#Ð Ø ×'Ñ'Ó)Ð ð¨Dó ð2 —‘Ó"€Ø�[‰[‹]€
ð #ð ¸%ð ÀDó ò "ñJ �$Ô÷ð ó ð÷0|4ò |4r   