o
    ˷e0                     @   sh   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 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.    N)DictUnion)retry)_helpers)BaseDocumentReferencec                	   @   s   e Zd ZdZdddZdd Zdefd	d
ZdeddfddZ	e
jdd ZdededdfddZ	ddededeeef ddfddZ	ddededejddfddZ	ddedejddf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.
    returnNc                 C   s"   || _ g | _i | _d | _d | _d S N)_client
_write_pbs_document_referenceswrite_resultscommit_time)selfclient r   [/var/www/ideatree/venv/lib/python3.10/site-packages/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__	referencec                 C   s   |j | jv S r	   )_document_pathr   )r   r   r   r   r   __contains__0   s   zBaseBatch.__contains__	write_pbsc                 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.proto.firestore.v1.                write_pb2.Write]): A list of write protobufs to be added.
        N)r   extend)r   r   r   r   r   _add_write_pbs3   s   	zBaseBatch._add_write_pbsc                 C   s   t  )zySends all accumulated write operations to the server. The details of this
        write depend on the implementing class.)NotImplementedErrorr   r   r   r   commit>   s   zBaseBatch.commitdocument_datac                 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   pbs_for_creater   r   r   )r   r   r   r   r   r   r   createD   s   zBaseBatch.createFmergec                 C   sB   |du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   pbs_for_set_with_merger   pbs_for_set_no_merger   r   )r   r   r   r"   r   r   r   r   setT   s   zBaseBatch.setfield_updatesoptionc                 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.
        ExistsOptionz5you must not pass an explicit write option to update.N)	__class____name__
ValueErrorr   pbs_for_updater   r   r   )r   r   r&   r'   r   r   r   r   updatev   s   zBaseBatch.updatec                 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   pb_for_deleter   r   r   )r   r   r'   write_pbr   r   r   delete   s   zBaseBatch.delete)r   N)Fr	   )r*   
__module____qualname____doc__r   r   r   r   listr   abcabstractmethodr   dictr!   r   boolr%   r   WriteOptionr-   r0   r   r   r   r   r      sJ    



&
r   )	metaclassc                   @   s$   e Zd ZdZdejdef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.r   timeoutc                 C   s&   | j j| jdd}t||}||fS )z+Shared setup for async/sync :meth:`commit`.N)databasewritestransaction)r
   _database_stringr   r   make_retry_timeout_kwargs)r   r   r<   requestkwargsr   r   r   _prep_commit   s   zBaseWriteBatch._prep_commitN)r*   r1   r2   r3   retriesRetryfloatrD   r   r   r   r   r;      s    r;   )r3   r5   typingr   r   google.api_corer   rE   google.cloud.firestore_v1r   'google.cloud.firestore_v1.base_documentr   ABCMetar   r;   r   r   r   r   <module>   s    