U
    >iE                     @  s  U d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZ erd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ded< dZded< edZ ded< edZ!ded< dZ"ded< dZ#ded< dZ$ded< G dd de%Z&G dd de%Z'dS ) zEHelpers for applying Google Cloud Firestore changes in a transaction.    )annotations)TYPE_CHECKINGAnyAsyncGenerator	Coroutine	GeneratorOptionalUnion)retry)types)AsyncStreamGenerator)DocumentSnapshot)ExplainOptions)StreamGenerator)write   z@The transaction has already begun. Current transaction ID: {!r}.str_CANT_BEGINz:The transaction has no transaction ID, so it cannot be {}._MISSING_ID_TEMPLATEzrolled back_CANT_ROLLBACKZ	committed_CANT_COMMITz8Cannot perform write operation in read-only transaction._WRITE_READ_ONLYz.Failed to commit transaction in {:d} attempts._EXCEED_ATTEMPTS_TEMPLATEz,Only read-write transactions can be retried._CANT_RETRY_READ_ONLYc                   @  s   e Zd ZdZedfddddZddd	d
ZdddddZedd Z	edd Z
ddddZd+ddZdd ZddddZd,ddd d!d"d#d$Zd-dd%dd d&d'd(d)d*ZdS ).BaseTransactiona  Accumulate read-and-write operations to be sent in a transaction.

    Args:
        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Nonereturnc                 C  s   || _ || _d | _d S N)Z_max_attempts
_read_only_id)selfZmax_attempts	read_only r#   N/tmp/pip-unpacked-wheel-599y42ri/google/cloud/firestore_v1/base_transaction.py__init__@   s    zBaseTransaction.__init__zlist[write_pb.Write])	write_pbsc                 C  s   t d S r   NotImplementedError)r!   r&   r#   r#   r$   _add_write_pbsE   s    zBaseTransaction._add_write_pbszUnion[bytes, None]z)Optional[types.common.TransactionOptions])retry_idr   c                 C  sL   |dk	r,| j rtttjtjj|ddS | j rDtjtj dS dS dS )a  Convert the current object to protobuf.

        The ``retry_id`` value is used when retrying a transaction that
        failed (e.g. due to contention). It is intended to be the "first"
        transaction that failed (i.e. if multiple retries are needed).

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

        Returns:
            Optional[google.cloud.firestore_v1.types.TransactionOptions]:
            The protobuf ``TransactionOptions`` if ``read_only==True`` or if
            there is a transaction ID to be retried, else :data:`None`.

        Raises:
            ValueError: If ``retry_id`` is not :data:`None` but the
                transaction is read-only.
        N)Zretry_transaction)Z
read_write)r"   )r   
ValueErrorr   r   ZTransactionOptionsZ	ReadWriteZReadOnlyr!   r*   r#   r#   r$   _options_protobufH   s    z!BaseTransaction._options_protobufc                 C  s
   | j dk	S )zDetermine if this transaction has already begun.

        Returns:
            bool: Indicates if the transaction has started.
        Nr    r!   r#   r#   r$   in_progressn   s    zBaseTransaction.in_progressc                 C  s   | j S )zGet the current transaction ID.

        Returns:
            Optional[bytes]: The transaction ID (or :data:`None` if the
            current transaction is not in progress).
        r.   r/   r#   r#   r$   idw   s    zBaseTransaction.idc                 C  s   g | _ d| _dS )zClean up the instance after :meth:`_rollback`` or :meth:`_commit``.

        This intended to occur on success or failure of the associated RPCs.
        N)Z
_write_pbsr    r/   r#   r#   r$   	_clean_up   s    zBaseTransaction._clean_upNc                 C  s   t d S r   r'   r,   r#   r#   r$   _begin   s    zBaseTransaction._beginc                 C  s   t d S r   r'   r/   r#   r#   r$   	_rollback   s    zBaseTransaction._rollbackz&Union[list, Coroutine[Any, Any, list]]c                 C  s   t d S r   r'   r/   r#   r#   r$   _commit   s    zBaseTransaction._commitlistz2retries.Retry | retries.AsyncRetry | object | Nonezfloat | NonezcGenerator[DocumentSnapshot, Any, None] | Coroutine[Any, Any, AsyncGenerator[DocumentSnapshot, Any]])
referencesr
   timeoutr   c                 C  s   t d S r   r'   )r!   r7   r
   r8   r#   r#   r$   get_all   s    	zBaseTransaction.get_all)explain_optionszOptional[ExplainOptions]zStreamGenerator[DocumentSnapshot] | Generator[DocumentSnapshot, Any, None] | Coroutine[Any, Any, AsyncGenerator[DocumentSnapshot, Any]] | Coroutine[Any, Any, AsyncStreamGenerator[DocumentSnapshot]])r
   r8   r:   r   c                C  s   t d S r   r'   )r!   Zref_or_queryr
   r8   r:   r#   r#   r$   get   s    zBaseTransaction.get)N)NN)NN)__name__
__module____qualname____doc__MAX_ATTEMPTSr%   r)   r-   propertyr0   r1   r2   r3   r4   r5   r9   r;   r#   r#   r#   r$   r   4   s(   &

	
    r   c                   @  s<   e Zd ZdZddddZddddZdd	 Zd
d ZdS )_BaseTransactionalaY  Provide a callable object to use as a transactional decorater.

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

    Args:
        to_wrap (Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]):
            A callable that should be run (and retried) in a transaction.
    r   r   c                 C  s   || _ d | _d | _d S r   )to_wrap
current_idr*   )r!   rC   r#   r#   r$   r%      s    z_BaseTransactional.__init__c                 C  s   d| _ d| _dS )zUnset the transaction IDs.N)rD   r*   r/   r#   r#   r$   _reset   s    z_BaseTransactional._resetc                 O  s   t d S r   r'   r!   Ztransactionargskwargsr#   r#   r$   _pre_commit   s    z_BaseTransactional._pre_commitc                 O  s   t d S r   r'   rF   r#   r#   r$   __call__   s    z_BaseTransactional.__call__N)r<   r=   r>   r?   r%   rE   rI   rJ   r#   r#   r#   r$   rB      s
   
rB   N)(r?   
__future__r   typingr   r   r   r   r   r   r	   Zgoogle.api_corer
   retriesZgoogle.cloud.firestore_v1r   Z0google.cloud.firestore_v1.async_stream_generatorr   Z"google.cloud.firestore_v1.documentr   Z'google.cloud.firestore_v1.query_profiler   Z*google.cloud.firestore_v1.stream_generatorr   Zgoogle.cloud.firestore_v1.typesr   Zwrite_pbr@   r   __annotations__r   formatr   r   r   r   r   objectr   rB   r#   r#   r#   r$   <module>   s(   $
y