
    (phj6                         S r SSKrSSKrSSKJrJrJrJrJr  SSK	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJrJ r J!r!J"r"   " S S5      r# " S S5      r$g)zDClasses for representing bundles for the Google Cloud Firestore API.    N)BundledDocumentMetadataBundledQueryBundleElementBundleMetadata
NamedQuery)_datetime_to_pb_timestampUTC)limit_type_of_query)
AsyncQuery)
BaseClient)DocumentSnapshot)	BaseQuery)DocumentReference)_helpers)	Timestamp)json_format)DictListOptionalUnionc                   ~   \ rS rSr% SrSr\\S'   S\SS4S jr	S	\
SS 4S
 jrS\S\SS 4S jrS\S\S\R                  4S jrS\S\S\R                  SS4S jrS	\S\S\R                  4S jrS\S	\S\R                  S\4S jrS\\R                  \4   SS4S jrS\S\S\4S jrS\4S jrS\S\4S jrS rSrg)FirestoreBundle-   a  A group of serialized documents and queries, suitable for
longterm storage or query resumption.

If any queries are added to this bundle, all associated documents will be
loaded and stored in memory for serialization.

Usage:

.. code-block:: python

    from google.cloud.firestore import Client, _helpers
    from google.cloud.firestore_bundle import FirestoreBundle

    db = Client()
    bundle = FirestoreBundle('my-bundle')
    bundle.add_named_query('all-users', db.collection('users')._query())
    bundle.add_named_query(
        'top-ten-hamburgers',
        db.collection('hamburgers').limit(limit=10),
    )
    serialized: str = bundle.build()

    # Store somewhere like a Google Cloud Storage bucket for retrieval by
    # a client SDK.

Args:
    name (str): The Id of the bundle.
   BUNDLE_SCHEMA_VERSIONnamereturnNc                 X    Xl         0 U l        0 U l        [        SSS9U l        S U l        g )Nr   )secondsnanos)r   	documentsnamed_queriesr   latest_read_time_deserialized_metadata)selfr   s     W/var/www/html/venv/lib/python3.13/site-packages/google/cloud/firestore_bundle/bundle.py__init__FirestoreBundle.__init__M   s-    	8:46+4Qa+HIM#    snapshotc           
         / nUR                   R                  nU R                  R                  U5      nU(       a  UR                  R
                  nUSL =(       d8    [        R                  " UR                  UR                  R                  5      S:  nU(       a5  [        U[        UUR                  UR                  US9S9U R                  U'   U R                  UR                  5        U R                  5         U $ )a  Adds a document to the bundle.

Args:
    snapshot (DocumentSnapshot): The fully-loaded Firestore document to
        be preserved.

Example:

.. code-block:: python

    from google.cloud import firestore

    db = firestore.Client()
    collection_ref = db.collection(u'users')

    bundle = firestore.FirestoreBundle('my bundle')
    bundle.add_document(collection_ref.documents('some_id').get())

Returns:
    FirestoreBundle: self
Nr   )r   	read_timeexistsqueriesr*   metadata)	reference_document_pathr!   getr0   r.   r   compare_timestampsr,   r*   _BundledDocumentr   r-   _update_last_read_time_reset_metadata)r%   r*   original_queriesfull_document_pathoriginal_documentshould_use_snaphots         r&   add_documentFirestoreBundle.add_documentT   s    . 13"*"4"4"C"C NN../AB099AA %  **""!**44 		 	 1A!0+&00#??,	2DNN-. 	##H$6$67r)   queryc                    [        U[        5      (       d"  [        S[        U5      R                   S35      eXR
                  ;   a  [        SU S35      eU R                  X!S9nU R                  XU5        U R                  5         U $ )a/  Adds a query to the bundle, referenced by the provided name.

Args:
    name (str): The name by which the provided query should be referenced.
    query (Query): Query of documents to be fully loaded and stored in
        the bundle for future access.

Example:

.. code-block:: python

    from google.cloud import firestore

    db = firestore.Client()
    collection_ref = db.collection(u'users')

    bundle = firestore.FirestoreBundle('my bundle')
    bundle.add_named_query('all the users', collection_ref._query())

Returns:
    FirestoreBundle: self

Raises:
    ValueError: If anything other than a BaseQuery (e.g., a Collection)
        is supplied. If you have a Collection, call its `_query()`
        method to get what this method expects.
    ValueError: If the supplied name has already been added.
z&Attempted to add named query of type: z. Expected BaseQuery.zQuery name conflict: z has already been added.)
query_name)	
isinstancer   
ValueErrortype__name__r"   _save_documents_from_query_save_named_queryr7   )r%   r   r>   
_read_times       r&   add_named_queryFirestoreBundle.add_named_query   s    : %++8;''((=? 
 %%%4TF:RSTT 44U4L
 	tJ7r)   r@   c                    [         R                   R                  R                  [        S9n[	        U[
        5      (       a4  SS KnUR                  5       nUR                  U R                  X5      5      $ UR                  5        Ht  nU R                  U5        U R                  R                  UR                  R                  5      nUR                   R"                  R%                  U5        UR&                  nMv     U$ )Ntzinfor   )datetimeminreplacer	   rA   r   asyncioget_event_looprun_until_complete_process_async_querystreamr<   r!   r3   r1   r2   r0   r.   appendr,   )r%   r>   r@   rG   rP   loopdocbundled_documents           r&   rE   *FirestoreBundle._save_documents_from_query   s     &&**22#2>
eZ(())+D**4+D+DU+WXX <<>Cc"#~~11#--2N2NO%%--44Z@J	 "
 r)   r,   c                 b    U R                  UUUS9U R                  U'   U R                  U5        g )N)r   r*   r,   )_build_named_queryr"   r6   )r%   r   r>   r,   s       r&   rF   !FirestoreBundle._save_named_query   s=     $(#:#: $; $
4 
 	##I.r)   c                   #    [         R                   R                  R                  [        S9nUR	                  5         S h  vN nU R                  U5        U R                  R                  UR                  R                  5      nUR                  R                  R                  U5        UR                  nM|   Nw
 U$ 7f)NrK   )rM   rN   rO   r	   rT   r<   r!   r3   r1   r2   r0   r.   rU   r,   )r%   r*   r@   rG   rW   rX   s         r&   rS   $FirestoreBundle._process_async_query   s      &&**22#2>
!* 	'#c"#~~11#--2N2NO%%--44Z@J		'*
 s)   <B?B<B:B<A4B?:B<<B?c           
          [        U[        UUR                  5       R                  [	        U5      S9[
        R                  " U5      S9$ )N)parentstructured_query
limit_type)r   bundled_queryr,   )r   r   _to_protobuf_pbr
   r   build_timestamp)r%   r   r*   r,   s       r&   r[   "FirestoreBundle._build_named_query   sI     &!)!6!6!8!<!<.x8
 ..y9
 	
r)   c                     [        U[        5      (       a  UO
[        U5      n[        R                  " X R
                  5      S:X  a  X l        g g )Nr   )rA   r   r   r   r4   r#   )r%   r,   _tss      r&   r6   &FirestoreBundle._update_last_read_time   sJ    
 )Y// *95 	 &&s,A,ABaG$'! Hr)   bundle_elementclientrC   c          
      @   SSK Jn  [        U SS5      c  0 U l        US:X  a  UR                  U l        gUS:X  a.  UR                  U R                  UR                  R                  '   gUS:X  a.  UR                  U R                  UR                  R                  '   gUS:X  Gao  [        R                  " UR                  R                  5      n[        [        R                  " U" UR                  S	9R                  U5      S
[!        UR"                  UR$                  US9U R                  UR                  R                     R&                  UR                  R(                  UR                  R*                  S9nU R-                  U5        U R.                  R1                  UR2                  R4                  5      nU R                  UR                  R                     R6                   H(  nUR                  R6                  R9                  U5        M*     g[;        SU 35      e)zmApplies BundleElements to this FirestoreBundle instance as a part of
deserializing a FirestoreBundle string.
r   )Document_doc_metadata_mapNr0   
namedQuerydocumentMetadatadocument)mappingT)rl   )datar-   r1   r,   create_timeupdate_timez"Unexpected type of BundleElement: )(google.cloud.firestore_v1.types.documentrn   getattrro   r0   r$   named_queryr"   r   document_metadatar   DocumentReferenceValuerr   r   decode_dictfieldsr   collection_namedocument_idr,   ru   rv   r<   r!   r3   r1   r2   r.   rU   rB   )	r%   rk   rl   rC   rn   doc_ref_valuer*   rX   r@   s	            r&   _add_bundle_element#FirestoreBundle._add_bundle_element  s    	F4,d3;%'D":*8*A*AD'\!BPB\B\D~99>>?'' 00 ""0055 Z$;;'',,M ())^%<%<=DDf +!11!--!
 00"++00)*33??*33??H  h'#~~11(2D2D2S2ST"44'',,g
 !))1188D
 A$HIIr)   c                    SnU R                   R                  5        H  nXR                  [        US95      -  nM     SnU R                  R                  5        Hh  nXR                  [        UR
                  S95      -  nUS-  nXR                  [        UR                  R                  5       R                  S95      -  nMj     [        U R                  =(       dP    [        U R                  [        R                  " 5       [        R                  U[!        UR#                  S5      5      S9S	9nU R                  U5       U 3$ )
a  Iterates over the bundle's stored documents and queries and produces
a single length-prefixed json string suitable for long-term storage.

Example:

.. code-block:: python

    from google.cloud import firestore

    db = firestore.Client()
    collection_ref = db.collection(u'users')

    bundle = firestore.FirestoreBundle('my bundle')
    bundle.add_named_query('app-users', collection_ref._query())

    serialized_bundle: str = bundle.build()

    # Now upload `serialized_bundle` to Google Cloud Storage, store it
    # in Memorystore, or any other storage solution.

Returns:
    str: The length-prefixed string representation of this bundle'
        contents.
 )ry   r   )rz   r   )rr   zutf-8)idru   versiontotal_documentstotal_bytes)r0   )r"   values_compile_bundle_elementr   r!   r0   r*   rd   re   r$   r   r   r   rf   r   r   lenencode)r%   bufferry   document_countrX   r0   s         r&   buildFirestoreBundle.build3  s4   2   --446K22+6 F 7   $ 5 5 7220@0I0IJ F aN22-66CCEII F !8 #000 99$446'== .g 67	#
 ..x89&BBr)   c                     [         R                  " [        R                  " UR                  5      5      n[        U5       U 3$ N)jsondumpsr   MessageToDictre   r   )r%   rk   serialized_bes      r&   r   'FirestoreBundle._compile_bundle_elementm  s6    

;#<#<^=O=O#PQm$%m_55r)   c                     SU l         g)z]Hydrating bundles stores cached data we must reset anytime new
queries or documents are addedN)r$   )r%   s    r&   r7   FirestoreBundle._reset_metadataq  s     '+#r)   )r$   ro   r!   r#   r   r"   )rD   
__module____qualname____firstlineno____doc__r   int__annotations__strr'   r   r<   r   rH   rM   rE   rF   r   rS   r   r[   r   r   r6   r   r   r   r   r   r7   __static_attributes__ r)   r&   r   r   -   st   : "#3"NS NT N6%5 6:K 6p,C ,	 ,>O ,\,/			&// / $$	/
 
/  
			

 
 $$	

 

 (x00);<(	(,J- ,JJ ,J^a ,J\8Cs 8Ct6m 6 6+r)   r   c                   .    \ rS rSrSrS\S\SS4S jrSrg)	r5   iw  z_Convenience class to hold both the metadata and the actual content
of a document to be bundled.r*   r0   r   Nc                     Xl         X l        g r   r/   )r%   r*   r0   s      r&   r'   _BundledDocument.__init__{  s    
 ! r)   )r0   r*   )	rD   r   r   r   r   r   r   r'   r   r   r)   r&   r5   r5   w  s(    $!"! *! 
	!r)   r5   )%r   rM   r   *google.cloud.firestore_bundle.types.bundler   r   r   r   r   google.cloud._helpersr   r	   &google.cloud.firestore_bundle._helpersr
   %google.cloud.firestore_v1.async_queryr   %google.cloud.firestore_v1.base_clientr   'google.cloud.firestore_v1.base_documentr   $google.cloud.firestore_v1.base_queryr   "google.cloud.firestore_v1.documentr   google.cloud.firestore_v1r   google.protobuf.timestamp_pb2r   google.protobufr   typingr   r   r   r   r   r5   r   r)   r&   <module>r      s]    K    A F < < D : @ . 3 ' G+ G+T

! 
!r)   