o
    }l~ie                     @  s   d Z ddlmZ ddlZddl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 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)AnyDictUnion)retry)_helpers)BaseDocumentReference)writec                   @  sx   e Zd ZdZd$ddZdd Zd%d
dZd&ddZej	dd Z
d'ddZ	d(d)ddZ	d*d+d d!Z	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.
    returnNonec                 C  s"   || _ g | _i | _d | _d | _d S N)_client
_write_pbs_document_referenceswrite_resultscommit_time)selfclient r   k/var/www/newdalilibackend/backend/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__	referencer   c                 C  s   |j | jv S r   )_document_pathr   )r   r   r   r   r   __contains__2   s   zBaseBatch.__contains__	write_pbslist[write_pb.Write]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  )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_datadict[str, Any]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   pbs_for_creater   r   r!   )r   r   r$   r   r   r   r   createF   s   zBaseBatch.createFdictmergeUnion[bool, list]c                 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   setX   s   zBaseBatch.setNfield_updatesoption_helpers.WriteOption | Nonec                 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   updatez   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   r   )r   r   )r   r   r   r   )r   r   r$   r%   r   r   )F)r   r   r$   r(   r)   r*   r   r   r   )r   r   r.   r%   r/   r0   r   r   )r   r   r/   r0   r   r   )r3   
__module____qualname____doc__r   r   r   r!   abcabstractmethodr#   r'   r-   r6   r9   r   r   r   r   r
      s    




& r
   )	metaclassc                   @  s   e Zd ZdZ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.r   2retries.Retry | retries.AsyncRetry | object | Nonetimeoutfloat | Nonec                 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   rB   requestkwargsr   r   r   _prep_commit   s   zBaseWriteBatch._prep_commitN)r   rA   rB   rC   )r3   r:   r;   r<   rK   r   r   r   r   r@      s    r@   )r<   
__future__r   r=   typingr   r   r   google.api_corer   retriesgoogle.cloud.firestore_v1r   'google.cloud.firestore_v1.base_documentr   google.cloud.firestore_v1.typesr	   r8   ABCMetar
   r@   r   r   r   r   <module>   s    