U
    >i>                     @  s   d Z ddlm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 ddlmZ G d	d
 d
ejdZG dd deZdS )z=Helpers for batch requests to the Google Cloud Firestore API.    )annotationsN)DictUnion)retry)_helpers)BaseDocumentReference)writec                   @  s   e Zd ZdZddddZdd Zdd	d
dZdddddZej	dd Z
ddddddZd#dddddddZd$dddddddZd%dddd d!d"ZdS )&	BaseBatcha  Accumulate write operations to be sent in a batch.

    This has the same set of methods for write operations that
    :class:`~google.cloud.firestore_v1.document.DocumentReference` does,
    e.g. :meth:`~google.cloud.firestore_v1.document.DocumentReference.create`.

    Args:
        client (:class:`~google.cloud.firestore_v1.client.Client`):
            The client that created this batch.
    None)returnc                 C  s"   || _ g | _i | _d | _d | _d S N)_client
_write_pbs_document_referencesZwrite_resultsZcommit_time)selfclient r   H/tmp/pip-unpacked-wheel-599y42ri/google/cloud/firestore_v1/base_batch.py__init__(   s
    zBaseBatch.__init__c                 C  s
   t | jS r   )lenr   r   r   r   r   __len__/   s    zBaseBatch.__len__r   )	referencec                 C  s   |j | jkS r   )_document_pathr   )r   r   r   r   r   __contains__2   s    zBaseBatch.__contains__zlist[write_pb.Write])	write_pbsr   c                 C  s   | j | dS )a  Add `Write`` protobufs to this transaction.

        This method intended to be over-ridden by subclasses.

        Args:
            write_pbs (List[google.cloud.firestore_v1.                write_pb2.Write]): A list of write protobufs to be added.
        N)r   extend)r   r   r   r   r   _add_write_pbs5   s    	zBaseBatch._add_write_pbsc                 C  s
   t  dS )zySends all accumulated write operations to the server. The details of this
        write depend on the implementing class.N)NotImplementedErrorr   r   r   r   commit@   s    zBaseBatch.commitdict)r   document_datar   c                 C  s(   t |j|}|| j|j< | | dS )a  Add a "change" to this batch to create a document.

        If the document given by ``reference`` already exists, then this
        batch will fail when :meth:`commit`-ed.

        Args:
            reference (:class:`~google.cloud.firestore_v1.document.DocumentReference`):
                A document reference to be created in this batch.
            document_data (dict): Property names and values to use for
                creating a document.
        N)r   Zpbs_for_creater   r   r   )r   r   r!   r   r   r   r   createF   s    zBaseBatch.createFzUnion[bool, list])r   r!   merger   c                 C  sB   |dk	rt |j||}nt |j|}|| j|j< | | dS )a  Add a "change" to replace a document.

        See
        :meth:`google.cloud.firestore_v1.document.DocumentReference.set` for
        more information on how ``option`` determines how the change is
        applied.

        Args:
            reference (:class:`~google.cloud.firestore_v1.document.DocumentReference`):
                A document reference that will have values set in this batch.
            document_data (dict):
                Property names and values to use for replacing a document.
            merge (Optional[bool] or Optional[List<apispec>]):
                If True, apply merging instead of overwriting the state
                of the document.
        FN)r   Zpbs_for_set_with_merger   Zpbs_for_set_no_merger   r   )r   r   r!   r#   r   r   r   r   setV   s       zBaseBatch.setNz_helpers.WriteOption | None)r   field_updatesoptionr   c                 C  s>   |j jdkrtdt|j||}|| j|j< | | dS )a  Add a "change" to update a document.

        See
        :meth:`google.cloud.firestore_v1.document.DocumentReference.update`
        for more information on ``field_updates`` and ``option``.

        Args:
            reference (:class:`~google.cloud.firestore_v1.document.DocumentReference`):
                A document reference that will be updated in this batch.
            field_updates (dict):
                Field names or paths to update and values to update with.
            option (Optional[:class:`~google.cloud.firestore_v1.client.WriteOption`]):
                A write option to make assertions / preconditions on the server
                state of the document before applying changes.
        ZExistsOptionz5you must not pass an explicit write option to update.N)	__class____name__
ValueErrorr   Zpbs_for_updater   r   r   )r   r   r%   r&   r   r   r   r   updatex   s      zBaseBatch.update)r   r&   r   c                 C  s*   t |j|}|| j|j< | |g dS )ay  Add a "change" to delete a document.

        See
        :meth:`google.cloud.firestore_v1.document.DocumentReference.delete`
        for more information on how ``option`` determines how the change is
        applied.

        Args:
            reference (:class:`~google.cloud.firestore_v1.document.DocumentReference`):
                A document reference that will be deleted in this batch.
            option (Optional[:class:`~google.cloud.firestore_v1.client.WriteOption`]):
                A write option to make assertions / preconditions on the server
                state of the document before applying changes.
        N)r   Zpb_for_deleter   r   r   )r   r   r&   write_pbr   r   r   delete   s    zBaseBatch.delete)F)N)N)r(   
__module____qualname____doc__r   r   r   r   abcabstractmethodr   r"   r$   r*   r,   r   r   r   r   r	      s   
 &   r	   )	metaclassc                   @  s    e Zd ZdZdddddZdS )BaseWriteBatchzBase class for a/sync implementations of the `commit` RPC. `commit` is useful
    for lower volumes or when the order of write operations is important.z2retries.Retry | retries.AsyncRetry | object | Nonezfloat | None)r   timeoutc                 C  s&   | j j| jdd}t||}||fS )z+Shared setup for async/sync :meth:`commit`.N)ZdatabaseZwritesZtransaction)r   Z_database_stringr   r   Zmake_retry_timeout_kwargs)r   r   r4   requestkwargsr   r   r   _prep_commit   s    zBaseWriteBatch._prep_commitN)r(   r-   r.   r/   r7   r   r   r   r   r3      s   r3   )r/   
__future__r   r0   typingr   r   Zgoogle.api_corer   retriesZgoogle.cloud.firestore_v1r   Z'google.cloud.firestore_v1.base_documentr   Zgoogle.cloud.firestore_v1.typesr   r+   ABCMetar	   r3   r   r   r   r   <module>   s    