Ë
    V¼Sf¦9  ã            	       ó„  — d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZm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dlmZ ddlmZ ddlmZmZm Z m!Z! ddl"m#Z#  G d„ dejH                  e	«      Z% G d„ de«      Z&de e%gef   de&fd„Z'de#de(de)dejT                  fd„Z+eefde,de,de,de,fd„Z-y)zEHelpers for applying Google Cloud Firestore changes in a transaction.é    N)Úgapic_v1)Úretry_async)Ú_BaseTransactionalÚBaseTransactionÚMAX_ATTEMPTSÚ_CANT_BEGINÚ_CANT_ROLLBACKÚ_CANT_COMMITÚ_WRITE_READ_ONLYÚ_INITIAL_SLEEPÚ
_MAX_SLEEPÚ_MULTIPLIERÚ_EXCEED_ATTEMPTS_TEMPLATE)Ú
exceptions)Úasync_batch)Ú_helpers)Útypes)ÚAsyncDocumentReference)ÚDocumentSnapshot)Ú
AsyncQuery)ÚAnyÚAsyncGeneratorÚCallableÚ	Coroutine)ÚClientc                   ó(  ‡ — e Zd ZdZedfdˆ fd„Zdeddfˆ fd„Zddeddfd	„Z	dd
„Z
defd„Zej                  j                  dfdedej                   dedeeef   fd„Zej                  j                  dfdej                   dedeeef   fd„Zˆ xZS )ÚAsyncTransactionaD  Accumulate read-and-write operations to be sent in a transaction.

    Args:
        client (:class:`~google.cloud.firestore_v1.client.Client`):
            The client that created this transaction.
        max_attempts (Optional[int]): The maximum number of attempts for
            the transaction (i.e. allowing retries). Defaults to
            :attr:`~google.cloud.firestore_v1.transaction.MAX_ATTEMPTS`.
        read_only (Optional[bool]): Flag indicating if the transaction
            should be read-only or should allow writes. Defaults to
            :data:`False`.
    FÚreturnNc                 óZ   •— t         t        | �  |«       t        j                  | ||«       y ©N)Úsuperr   Ú__init__r   )ÚselfÚclientÚmax_attemptsÚ	read_onlyÚ	__class__s       €újC:\Users\user\Documents\project_loop\venv\Lib\site-packages\google/cloud/firestore_v1/async_transaction.pyr"   zAsyncTransaction.__init__B   s%   ø€ ÜÔ Ñ.¨vÔ6Ü× Ñ   |°YÕ?ó    Ú	write_pbsc                 ób   •— | j                   rt        t        «      ‚t        t        | �  |«       y)a  Add `Write`` protobufs to this transaction.

        Args:
            write_pbs (List[google.cloud.proto.firestore.v1.                write.Write]): A list of write protobufs to be added.

        Raises:
            ValueError: If this transaction is read-only.
        N)Ú
_read_onlyÚ
ValueErrorr   r!   r   Ú_add_write_pbs)r#   r*   r'   s     €r(   r.   zAsyncTransaction._add_write_pbsF   s(   ø€ ð �?Š?ÜÔ-Ó.Ð.äÔ Ñ4°YÕ?r)   Úretry_idc              ƒ   óp  K  — | j                   r*t        j                  | j                  «      }t	        |«      ‚| j
                  j                  j                  | j
                  j                  | j                  |«      dœ| j
                  j                  ¬«      ƒ d{  –—† }|j                  | _        y7 Œ­w)zìBegin the transaction.

        Args:
            retry_id (Optional[bytes]): Transaction ID of a transaction to be
                retried.

        Raises:
            ValueError: If the current transaction has already begun.
        )ÚdatabaseÚoptions©ÚrequestÚmetadataN)Úin_progressr   ÚformatÚ_idr-   Ú_clientÚ_firestore_apiÚbegin_transactionÚ_database_stringÚ_options_protobufÚ_rpc_metadataÚtransaction)r#   r/   ÚmsgÚtransaction_responses       r(   Ú_beginzAsyncTransaction._beginU   s˜   è ø€ ð ×ÒÜ×$Ñ$ T§X¡XÓ.ˆCÜ˜S“/Ð!à%)§\¡\×%@Ñ%@×%RÑ%Rà ŸL™L×9Ñ9Ø×1Ñ1°(Ó;ñð —\‘\×/Ñ/ð &Só &
÷  
Ðð (×3Ñ3ˆ�ð 
ús   ‚BB6ÂB4ÂB6c              ƒ   óZ  K  — | j                   st        t        «      ‚	 | j                  j                  j                  | j                  j                  | j                  dœ| j                  j                  ¬«      ƒ d{  –—†  | j                  «        y7 Œ# | j                  «        w xY w­w)zÀRoll back the transaction.

        Raises:
            ValueError: If no transaction is in progress.
            google.api_core.exceptions.GoogleAPICallError: If the rollback fails.
        )r1   r?   r3   N)
r6   r-   r	   r9   r:   Úrollbackr<   r8   r>   Ú	_clean_up)r#   s    r(   Ú	_rollbackzAsyncTransaction._rollbackl   sŽ   è ø€ ð ×ÒÜœ^Ó,Ð,ð	à—,‘,×-Ñ-×6Ñ6à $§¡× =Ñ =Ø#'§8¡8ñð Ÿ™×3Ñ3ð 7ó ÷ ð ð �N‰NÕðùð �N‰NÕüs/   ‚B+ŸAB Á>BÁ?B ÂB+ÂB ÂB(Â(B+c              ƒ   óô   K  — | j                   st        t        «      ‚t        | j                  | j
                  | j                  «      ƒ d{  –—† }| j                  «        t        |j                  «      S 7 Œ)­w)aÄ  Transactionally commit the changes accumulated.

        Returns:
            List[:class:`google.cloud.proto.firestore.v1.write.WriteResult`, ...]:
            The write results corresponding to the changes committed, returned
            in the same order as the changes were applied to this transaction.
            A write result contains an ``update_time`` field.

        Raises:
            ValueError: If no transaction is in progress.
        N)
r6   r-   r
   Ú_commit_with_retryr9   Ú
_write_pbsr8   rE   ÚlistÚwrite_results)r#   Úcommit_responses     r(   Ú_commitzAsyncTransaction._commitƒ   s`   è ø€ ð ×ÒÜœ\Ó*Ð*ä 2Ø�L‰L˜$Ÿ/™/¨4¯8©8ó!
÷ 
ˆð 	�‰ÔÜ�O×1Ñ1Ó2Ð2ð
ús   ‚A
A8ÁA6Á*A8Ú
referencesÚretryÚtimeoutc              ƒ   óˆ   K  — t        j                  ||«      } | j                  j                  |fd| i|¤Žƒ d{  –—† S 7 Œ­w)at  Retrieves multiple documents from Firestore.

        Args:
            references (List[.AsyncDocumentReference, ...]): Iterable of document
                references to be retrieved.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        Yields:
            .DocumentSnapshot: The next document snapshot that fulfills the
            query, or :data:`None` if the document does not exist.
        r?   N)r   Úmake_retry_timeout_kwargsr9   Úget_all)r#   rN   rO   rP   Úkwargss        r(   rS   zAsyncTransaction.get_all™   sA   è ø€ ô( ×3Ñ3°E¸7ÓCˆØ)�T—\‘\×)Ñ)¨*ÑQÀ$ÐQÈ&ÑQ×QÐQÐQús   ‚9A»A ¼Ac              ƒ   ó  K  — t        j                  ||«      }t        |t        «      r( | j                  j
                  |gfd| i|¤Žƒ d{  –—† S t        |t        «      r |j                  dd| i|¤Žƒ d{  –—† S t        d«      ‚7 Œ;7 Œ­w)aW  
        Retrieve a document or a query result from the database.

        Args:
            ref_or_query The document references or query object to return.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        Yields:
            .DocumentSnapshot: The next document snapshot that fulfills the
            query, or :data:`None` if the document does not exist.
        r?   NzSValue for argument "ref_or_query" must be a AsyncDocumentReference or a AsyncQuery.© )	r   rR   Ú
isinstancer   r9   rS   r   Ústreamr-   )r#   Úref_or_queryrO   rP   rT   s        r(   ÚgetzAsyncTransaction.get°   sž   è ø€ ô( ×3Ñ3°E¸7ÓCˆÜ�lÔ$:Ô;Ø-˜Ÿ™×-Ñ-Ø�ñØ,0ðØ4:ñ÷ ð ô ˜¤jÔ1Ø,˜×,Ñ,ÑH¸ÐHÀÑH×HÐHäØeóð ðøð Iús$   ‚A
BÁBÁ+BÁ8B
Á9BÂ
B©r   Nr    )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   rJ   r.   ÚbytesrB   rF   rM   r   ÚmethodÚDEFAULTÚretriesÚ
AsyncRetryÚfloatr   r   r   rS   rZ   Ú__classcell__©r'   s   @r(   r   r   4   sè   ø„ ñð -9ÀEö @ð@¨ð @°õ @ñ4 Uð 4°dó 4ó.ð.3˜tó 3ð2 %-§O¡O×$;Ñ$;Øñ	RàðRð ×!Ñ!ðRð ð	Rð
 
Ð(¨#Ð-Ñ	.óRð4 %-§O¡O×$;Ñ$;Øñ	ð ×!Ñ!ðð ð	ð
 
Ð(¨#Ð-Ñ	.÷r)   r   c                   ó:   ‡ — e Zd ZdZdˆ fd„Zdedefd„Zd„ Zˆ xZ	S )Ú_AsyncTransactionalal  Provide a callable object to use as a transactional decorater.

    This is surfaced via
    :func:`~google.cloud.firestore_v1.async_transaction.transactional`.

    Args:
        to_wrap (Coroutine[[:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`, ...], Any]):
            A coroutine that should be run (and retried) in a transaction.
    r   c                 ó,   •— t         t        | �  |«       y r    )r!   ri   r"   )r#   Úto_wrapr'   s     €r(   r"   z_AsyncTransactional.__init__Ü   s   ø€ ÜÔ! 4Ñ1°'Õ:r)   r?   c              �   ó  K  — |j                  «        |j                  | j                  ¬«      ƒ d{  –—†  |j                  | _        | j                  €| j                  | _         | j
                  |g|¢­i |¤Žƒ d{  –—† S 7 ŒP7 Œ­w)a}  Begin transaction and call the wrapped coroutine.

        Args:
            transaction
                (:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`):
                A transaction to execute the coroutine within.
            args (Tuple[Any, ...]): The extra positional arguments to pass
                along to the wrapped coroutine.
            kwargs (Dict[str, Any]): The extra keyword arguments to pass
                along to the wrapped coroutine.

        Returns:
            Any: result of the wrapped coroutine.

        Raises:
            Exception: Any failure caused by ``to_wrap``.
        )r/   N)rE   rB   r/   r8   Ú
current_idrk   )r#   r?   ÚargsrT   s       r(   Ú_pre_commitz_AsyncTransactional._pre_commitß   s{   è ø€ ð* 	×ÑÔØ× Ñ ¨$¯-©-Ð Ó8×8Ð8ð &Ÿ/™/ˆŒØ�=‰=Ð Ø ŸO™OˆDŒMØ!�T—\‘\ +Ð?°Ò?¸Ñ?×?Ð?ð 	9øð @ús"   ‚0B²B³ABÁ>BÁ?BÂBc              �   óè  K  — | j                  «        |j                  st        j                  nd}d}	 t	        |j
                  «      D ]<  } | j                  |g|¢­i |¤Žƒ d{  –—† }	 |j                  «       ƒ d{  –—†  |c S  t        j                  |j
                  «      }	t        |	«      |‚7 ŒM7 Œ6# |$ r}|}Y d}~Œzd}~ww xY w# t        $ r |j                  «       ƒ d{  –—†7   ‚ w xY w­w)aš  Execute the wrapped callable within a transaction.

        Args:
            transaction
                (:class:`~google.cloud.firestore_v1.transaction.Transaction`):
                A transaction to execute the callable within.
            args (Tuple[Any, ...]): The extra positional arguments to pass
                along to the wrapped callable.
            kwargs (Dict[str, Any]): The extra keyword arguments to pass
                along to the wrapped callable.

        Returns:
            Any: The result of the wrapped callable.

        Raises:
            ValueError: If the transaction does not succeed in
                ``max_attempts``.
        rV   N)Ú_resetr,   r   ÚAbortedÚrangeÚ_max_attemptsro   rM   r   r7   r-   ÚBaseExceptionrF   )
r#   r?   rn   rT   Úretryable_exceptionsÚlast_excÚattemptÚresultÚexcr@   s
             r(   Ú__call__z_AsyncTransactional.__call__ý   só   è ø€ ð& 	�‰Œà(3×(>Ò(>ŒZ×ÒÀBð 	ð ˆð	Ü  ×!:Ñ!:Ó;ò #�Ø/˜t×/Ñ/°ÐM¸dÒMÀfÑM×M�ð#Ø%×-Ñ-Ó/×/Ð/Ø!’Mð	#ô ,×2Ñ2°;×3LÑ3LÓMˆCÜ˜S“/ xÐ/ð Nøà/ùà+ò #Ø"•Hûð#ûô ò 	ð ×'Ñ'Ó)×)Ñ)Øð		üsv   ‚1C2´1C Á%B3Á&C Á+B7Á>B5Á?B7ÂC ÂC2Â-C Â5B7Â7CÂ<CÂ>C ÃCÃC ÃC/Ã'C*Ã(C/Ã/C2r[   )
r\   r]   r^   r_   r"   r   r   ro   r{   rf   rg   s   @r(   ri   ri   Ñ   s*   ø„ ñõ;ð@Ø+ð@à	ó@ö<0r)   ri   rk   r   c                 ó   — t        | «      S )a‡  Decorate a callable so that it runs in a transaction.

    Args:
        to_wrap
            (Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]):
            A callable that should be run (and retried) in a transaction.

    Returns:
        Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]:
        the wrapped callable.
    )ri   )rk   s    r(   Úasync_transactionalr}   0  s   € ô ˜wÓ'Ð'r)   r$   r*   Útransaction_idc              ƒ   óô   K  — t         }	 	 | j                  j                  | j                  ||dœ| j                  ¬«      ƒ d{  –—† S 7 Œ# t
        j                  $ r Y nw xY wt        |«      ƒ d{  –—†7  }Œn­w)a.  Call ``Commit`` on the GAPIC client with retry / sleep.

    Retries the ``Commit`` RPC on Unavailable. Usually this RPC-level
    retry is handled by the underlying GAPICd client, but in this case it
    doesn't because ``Commit`` is not always idempotent. But here we know it
    is "idempotent"-like because it has a transaction ID. We also need to do
    our own retry to special-case the ``INVALID_ARGUMENT`` error.

    Args:
        client (:class:`~google.cloud.firestore_v1.client.Client`):
            A client with GAPIC client and configuration details.
        write_pbs (List[:class:`google.cloud.proto.firestore.v1.write.Write`, ...]):
            A ``Write`` protobuf instance to be committed.
        transaction_id (bytes):
            ID of an existing transaction that this commit will run in.

    Returns:
        :class:`google.cloud.firestore_v1.types.CommitResponse`:
        The protobuf response from ``Commit``.

    Raises:
        ~google.api_core.exceptions.GoogleAPICallError: If a non-retryable
            exception is encountered.
    )r1   Úwritesr?   r3   N)r   r:   Úcommitr<   r>   r   ÚServiceUnavailableÚ_sleep)r$   r*   r~   Úcurrent_sleeps       r(   rH   rH   B  s�   è ø€ ô6 #€MØ
ð	Ø×.Ñ.×5Ñ5à &× 7Ñ 7Ø'Ø#1ñð
  ×-Ñ-ð 6ó ÷ ð ð ùô ×,Ñ,ò 	áð	úô % ]Ó3×3Ð3ˆð ùsF   ‚A8‹8A
 ÁAÁA
 ÁA8ÁA
 Á
A ÁA8ÁA Á A8Á1A4Á2A8r„   Ú	max_sleepÚ
multiplierc              ƒ   ó’   K  — t        j                  d| «      }t        j                  |«      ƒ d{  –—†  t	        || z  |«      S 7 Œ­w)aÌ  Sleep and produce a new sleep time.

    .. _Exponential Backoff And Jitter: https://www.awsarchitectureblog.com/                                        2015/03/backoff.html

    Select a duration between zero and ``current_sleep``. It might seem
    counterintuitive to have so much jitter, but
    `Exponential Backoff And Jitter`_ argues that "full jitter" is
    the best strategy.

    Args:
        current_sleep (float): The current "max" for sleep interval.
        max_sleep (Optional[float]): Eventual "max" sleep time
        multiplier (Optional[float]): Multiplier for exponential backoff.

    Returns:
        float: Newly doubled ``current_sleep`` or ``max_sleep`` (whichever
        is smaller)
    g        N)ÚrandomÚuniformÚasyncioÚsleepÚmin)r„   r…   r†   Úactual_sleeps       r(   rƒ   rƒ   o  sA   è ø€ ô, —>‘> # }Ó5€LÜ
�-‰-˜Ó
%×%Ð%Üˆz˜MÑ)¨9Ó5Ð5ð &ús   ‚/A±A²A).r_   rŠ   rˆ   Úgoogle.api_corer   r   rc   Ú*google.cloud.firestore_v1.base_transactionr   r   r   r   r	   r
   r   r   r   r   r   r   Úgoogle.cloud.firestore_v1r   r   r   Ú(google.cloud.firestore_v1.async_documentr   r   Ú%google.cloud.firestore_v1.async_queryr   Útypingr   r   r   r   Ú google.cloud.firestore_v1.clientr   ÚAsyncWriteBatchr   ri   r}   rJ   r`   ÚCommitResponserH   re   rƒ   rV   r)   r(   ú<module>r—      sñ   ðñ Ló Û å $Ý 2÷÷ ÷ ñ õ 'Ý 1Ý .Ý +å KÝ EÝ <ß ;Ó ;õ 4ôZ�{×2Ñ2°Oô Zôz\Ð,ô \ð~(ØÐ'Ð(¨#Ð-Ñ.ð(àó(ð$*4Øð*4Ø#ð*4Ø5:ð*4à
×Ñó*4ð\ .8È[ñ6Øð6Ø%*ð6ØEJð6à
ô6r)   