
    ?h!#                    |   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZm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* ddl+m,Z,m-Z-m.Z. ddl/m0Z0 ddlm1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z>m?Z?m@Z@mAZAmBZB ddlmCZC ee2e3f   ZDe G d d             ZE e        G d d             ZF G d de	      ZG G d d      ZH edeeIeJef          ZKd!eKd"eLd#eeK   d$dfd%ZMd!eNd"eLd$dfd&ZOd'eId(eId)ePd$dfd*ZQd+eRd$eRfd,ZSd-eLd$eLfd.ZTy)/z
Batch class definitions.
    N)deque)ThreadPoolExecutoras_completedFuture)	dataclassfield)Real)AnyCallableDequeDictListOptionalSequenceSetTupleTypeTypeVarUnioncast)ReadTimeoutResponse)ConnectionError)	HTTPError)
Connection)ConsistencyLevel)_find_value_typeVALUE_ARRAY_TYPESWHERE_OPERATORS)UUID   )BatchRequestObjectsBatchRequestReferenceBatchRequestBatchResponse   )Cluster)&BATCH_REF_DEPRECATION_NEW_V14_CLS_NS_W&BATCH_REF_DEPRECATION_OLD_V14_CLS_NS_WBATCH_EXECUTOR_SHUTDOWN_W)UnexpectedStatusCodeException)_capitalize_first_lettercheck_batch_result_check_positive_num_decode_json_response_dict_decode_json_response_list)	_Warningsc                   F    e Zd ZU eed<    ed      Zee   ed<   defdZ	y)Shard
class_nameN)defaulttenantreturnc                 D    t        | j                  | j                  f      S N)hashr4   r6   selfs    W/home/chris/cleankitchens-env/lib/python3.12/site-packages/weaviate/batch/crud_batch.py__hash__zShard.__hash__?   s    T__dkk233    )
__name__
__module____qualname__str__annotations__r   r6   r   intr>    r?   r=   r3   r3   :   s&    O!$/FHSM/4# 4r?   r3   c                   \    e Zd ZU dZdZeed<   dZee	e
      ed<   dZee	e
      ed<   ddZy)	WeaviateErrorRetryConfa  Configures how often objects should be retried when Weavaite returns an error and which errors should be included
    or excluded.
    By default, all errors are retried.

    Parameters
    ----------
    number_retries: int
        How often a batch that includes objects with errors should be retried. Must be >=1.
    errors_to_exclude: Optional[List[str]]
        Which errors should NOT be retried. All other errors will be retried. An object will be skipped, when the given
        string is part of the weaviate error message.

        Example: errors_to_exclude =["string1", "string2"] will match the error with message "Long error message that
        contains string1".
    errors_to_include: Optional[List[str]]
        Which errors should be retried. All other errors will NOT be retried. An object will be included, when the given
        string is part of the weaviate error message.

        Example: errors_to_include =["string1", "string2"] will match the error with message "Long error message that
        contains string1".
       number_retriesNerrors_to_excludeerrors_to_includec                 v   | j                   $| j                  t        | j                  dz         t	        | j
                  dt               dt        t        t              dd fd} || j                           || j                         | j                  $t        | j                        dk(  rt        d      y y )Nz% can either include or exclude errorsrJ   
error_listr7   c                 D    | y t        d | D              rt        d      y )Nc              3   >   K   | ]  }t        |t                 y wr9   )
isinstancerC   ).0entrys     r=   	<genexpr>zLWeaviateErrorRetryConf.__post_init__.<locals>.check_lists.<locals>.<genexpr>h   s     F%z%--Fs   zList entries must be strings.)any
ValueError)rN   s    r=   check_listsz9WeaviateErrorRetryConf.__post_init__.<locals>.check_listse   s+    !F:FF !@AA Gr?   r   z=errors_to_include has 0 entries and no error will be retried.)rK   rL   rV   rA   r.   rJ   rE   r   r   rC   len)r<   rW   s     r=   __post_init__z$WeaviateErrorRetryConf.__post_init___   s    !!-$2H2H2TT__/VVWWD//1A3G	BHT#Y$7 	BD 	B 	D**+D**+!!-#d6L6L2MQR2R\]] 3S-r?   r7   N)r@   rA   rB   __doc__rJ   rE   rD   rK   r   r   rC   rL   rY   rF   r?   r=   rH   rH   C   sA    , NC-1xS	*1-1xS	*1^r?   rH   c                       e Zd ZdZdefdZy)BatchExecutorz
    Weaviate Batch Executor to run batch requests in separate thread.
    This class implements an additional method `is_shutdown` that us used my the context manager.
    r7   c                     | j                   S )z
        Check if executor is shutdown.

        Returns
        -------
        bool
            Whether the BatchExecutor is shutdown.
        )	_shutdownr;   s    r=   is_shutdownzBatchExecutor.is_shutdownx   s     ~~r?   N)r@   rA   rB   r[   boolr`   rF   r?   r=   r]   r]   r   s    

T 
r?   r]   c                      e Zd ZdZdefdZdedd fdZddd	d	ded
ddf	de	e
   de	e   de
de
de	e   de	eee   gdf      dede
de	e   dd fdZd]dZ	 	 	 d^dedede	e   de	e   de	e   defdZ	 	 d_dededed ed!e	e   de	e   ddfd"Zd#ed$edefd%Zd&eddfd'Zd#ed$edefd(Zd$edefd)Z d$e!de!fd*Z"de#fd+Z$de#fd,Z%d#ed$ede&e	e   e
f   fd-Z'd.eddfd/Z(d]d0Z)d]d1Z*	 	 	 d`ded2ed3ed4ede	e   defd5Z+de
fd6Z,de
fd7Z-dad8e
defd9Z.dad8e
defd:Z/d]d;Z0d]d<Z1defd=Z2defd>Z3e4de&e
e
f   fd?       Z5e4de	e
   fd@       Z6e6jn                  dAe	e
   ddfdB       Z6e4defdC       Z8e8jn                  dAeddfdD       Z8e4de9edf   fdE       Z:e:jn                  dFe	e9eef      ddfdG       Z:e4de	e
   fdH       Z;e4de	e
   fdI       Z<dbdJZ=d]dKZ>dbdLZ?dMedNedOeddfdPZ@	 dcdQe	eeA      dRe
ddfdSZBdTeAdee   fdUZCe4defdV       ZDeDjn                  dAeddfdW       ZDe4de
fdX       ZEeEjn                  dAe
ddfdY       ZEe4de
fdZ       ZFeFjn                  dAe
ddfd[       ZFd&ed#ede&eGef   fd\ZHy)dBatcha  
    Batch class used to add multiple objects or object references at once into weaviate.
    To add data to the Batch use these methods of this class: `add_data_object` and
    `add_reference`. This object also stores 2 recommended batch size variables, one for objects
    and one for references. The recommended batch size is updated with every batch creation, and
    is the number of data objects/references that can be sent/processed by the Weaviate server in
    `creation_time` interval (see `configure` or `__call__` method on how to set this value, by
    default it is set to 10). The initial value is None/batch_size and is updated with every batch
    create methods. The values can be accessed with the getters: `recommended_num_objects` and
    `recommended_num_references`.
    NOTE: If the UUID of one of the objects already exists then the existing object will be
    replaced by the new object.

    This class can be used in 3 ways:

    Case I:
        Everything should be done by the user, i.e. the user should add the
        objects/object-references and create them whenever the user wants. To create one of the
        data type use these methods of this class: `create_objects`, `create_references` and
        `flush`. This case has the Batch instance's batch_size set to None (see docs for the
        `configure` or `__call__` method). Can be used in a context manager, see below.

    Case II:
        Batch auto-creates when full. This can be achieved by setting the Batch instance's
        batch_size set to a positive integer (see docs for the `configure` or `__call__` method).
        The batch_size in this case corresponds to the sum of added objects and references.
        This case does not require the user to create the batch/s, but it can be done. Also to
        create non-full batches (last batch/es) that do not meet the requirement to be auto-created
        use the `flush` method. Can be used in a context manager, see below.

    Case III:
        Similar to Case II but uses dynamic batching, i.e. auto-creates either objects or
        references when one of them reached the `recommended_num_objects` or
        `recommended_num_references` respectively. See docs for the `configure` or `__call__`
        method for how to enable it.

    Context-manager support: Can be use with the `with` statement. When it exists the context-
        manager it calls the `flush` method for you. Can be combined with `configure`/`__call__`
        method, in order to set it to the desired Case.

    Examples
    --------
    Here are examples for each CASE described above. Here `client` is an instance of the
    `weaviate.Client`.

    >>> object_1 = '154cbccd-89f4-4b29-9c1b-001a3339d89d'
    >>> object_2 = '154cbccd-89f4-4b29-9c1b-001a3339d89c'
    >>> object_3 = '254cbccd-89f4-4b29-9c1b-001a3339d89a'
    >>> object_4 = '254cbccd-89f4-4b29-9c1b-001a3339d89b'

    For Case I:

    >>> client.batch.shape
    (0, 0)
    >>> client.batch.add_data_object({}, 'MyClass')
    >>> client.batch.add_data_object({}, 'MyClass')
    >>> client.batch.add_reference(object_1, 'MyClass', 'myProp', object_2)
    >>> client.batch.shape
    (2, 1)
    >>> client.batch.create_objects()
    >>> client.batch.shape
    (0, 1)
    >>> client.batch.create_references()
    >>> client.batch.shape
    (0, 0)
    >>> client.batch.add_data_object({}, 'MyClass')
    >>> client.batch.add_reference(object_3, 'MyClass', 'myProp', object_4)
    >>> client.batch.shape
    (1, 1)
    >>> client.batch.flush()
    >>> client.batch.shape
    (0, 0)

    Or with a context manager:

    >>> with client.batch as batch:
    ...     batch.add_data_object({}, 'MyClass')
    ...     batch.add_reference(object_3, 'MyClass', 'myProp', object_4)
    >>> # flush was called
    >>> client.batch.shape
    (0, 0)

    For Case II:

    >>> client.batch(batch_size=3)
    >>> client.batch.shape
    (0, 0)
    >>> client.batch.add_data_object({}, 'MyClass')
    >>> client.batch.add_reference(object_1, 'MyClass', 'myProp', object_2)
    >>> client.batch.shape
    (1, 1)
    >>> client.batch.add_data_object({}, 'MyClass') # sum of data_objects and references reached
    >>> client.batch.shape
    (0, 0)

    Or with a context manager and `__call__` method:

    >>> with client.batch(batch_size=3) as batch:
    ...     batch.add_data_object({}, 'MyClass')
    ...     batch.add_reference(object_3, 'MyClass', 'myProp', object_4)
    ...     batch.add_data_object({}, 'MyClass')
    ...     batch.add_reference(object_1, 'MyClass', 'myProp', object_4)
    >>> # flush was called
    >>> client.batch.shape
    (0, 0)

    Or with a context manager and setter:

    >>> client.batch.batch_size = 3
    >>> with client.batch as batch:
    ...     batch.add_data_object({}, 'MyClass')
    ...     batch.add_reference(object_3, 'MyClass', 'myProp', object_4)
    ...     batch.add_data_object({}, 'MyClass')
    ...     batch.add_reference(object_1, 'MyClass', 'myProp', object_4)
    >>> # flush was called
    >>> client.batch.shape
    (0, 0)

    For Case III:
    Same as Case II but you need to configure or enable 'dynamic' batching.

    >>> client.batch.configure(batch_size=3, dynamic=True) # 'batch_size' must be an valid int

    Or:

    >>> client.batch.batch_size = 3
    >>> client.batch.dynamic = True

    See the documentation of the `configure`( or `__call__`) and the setters for more information
    on how/why and what you need to configure/set in order to use a particular Case.
    
connectionc                 V   d| _         d| _        || _        t               | _        t               | _        t        d      | _        t        d      | _	        g | _
        g | _        t        j                         | _        t        | _        d| _        d| _        t'        t(        t+        | j                  j,                  d   dz  d            | _        d	| _        d	| _        d
| _        | j$                  | _        | j$                  | _        t;               | _        d| _        d| _         d| _!        y)ag  
        Initialize a Batch class instance. This defaults to manual creation configuration.
        See docs for the `configure` or `__call__` method for different types of configurations.

        Parameters
        ----------
        connection : weaviate.connect.Connection
            Connection object to an active and running weaviate instance.
        NT   )maxlen2   r!   
   r&   rI   dynamic)"_shutdown_background_event_new_dynamic_batching_connectionr#   _objects_batchr$   _reference_batchr   _objects_throughput_frame_references_throughput_frame_future_pool_reference_batch_queue	threadingLock_callback_lockr-   	_callback_weaviate_error_retry_batch_sizer   r	   mintimeout_config_creation_time_timeout_retries_connection_error_retries_batching_type_recommended_num_objects_recommended_num_referencesset_Batch__imported_shards_num_workers_consistency_level	_executor)r<   rd   s     r=   __init__zBatch.__init__
  s	    FJ'%)"%13 5 77<A&:?q/)MOCE#'nn. EWGK"*,"4T-=-=-L-LQ-ORT-TVW)XY !)*&-6(,(8(8%+/+;+;(-0U>B26r?   kwargsr7   c                 &     | j                   di |S )av	  
        WARNING: This method will be deprecated in the next major release. Use `configure` instead.

        Parameters
        ----------
        batch_size : Optional[int], optional
            The batch size to be use. This value sets the Batch functionality, if `batch_size` is
            None then no auto-creation is done (`callback` and `dynamic` are ignored). If it is a
            positive number auto-creation is enabled and the value represents: 1) in case `dynamic`
            is False -> the number of data in the Batch (sum of objects and references) when to
            auto-create; 2) in case `dynamic` is True -> the initial value for both
            `recommended_num_objects` and `recommended_num_references`, by default None
        creation_time : Real, optional
            How long it should take to create a Batch. Used ONLY for computing dynamic batch sizes. By default None
        timeout_retries : int, optional
            Number of retries to create a Batch that failed with ReadTimeout, by default 3
        weaviate_error_retries: Optional[WeaviateErrorRetryConf], by default None
            How often batch-elements with an error originating from weaviate (for example transformer timeouts) should
            be retried and which errors should be ignored and/or included. See documentation for WeaviateErrorRetryConf
            for details.
        connection_error_retries : int, optional
            Number of retries to create a Batch that failed with ConnectionError, by default 3
        callback : Optional[Callable[[dict], None]], optional
            A callback function on the results of each (objects and references) batch types.
            By default `weaviate.util.check_batch_result`.
        dynamic : bool, optional
            Whether to use dynamic batching or not, by default False
        num_workers : int, optional
            The maximal number of concurrent threads to run batch import. Only used for non-MANUAL
            batching. i.e. is used only with AUTO or DYNAMIC batching.
            By default, the multi-threading is disabled. Use with care to not overload your weaviate instance.

        Returns
        -------
        Batch
            Updated self.

        Raises
        ------
        TypeError
            If one of the arguments is of a wrong type.
        ValueError
            If the value of one of the arguments is wrong.
        rF   )	configure)r<   r   s     r=   __call__zBatch.__call__4  s    Z t~~'''r?   rh   NrI   Tr!   
batch_sizecreation_timetimeout_retriesconnection_error_retriesweaviate_error_retriescallbackrj   num_workersconsistency_levelc
                     |	| _         |t        |dt               || _        n9t	        t        t        | j                  j                  d   dz  d            | _        t        |dt               t        |dt               || _
        || _        || _        || _        ||sd| _        d| _        | S t        |dt               t        |d	t               t!        |d
       | j"                  |k7  r7| j%                          | j'                          || _        | j)                          || _        |du rd| _        n9d
| _        |dn|| _        |dn|| _        | j.                  | j1                          | j3                          | S )a}
  
        Warnings
        --------
            - It has default values and if you want to change only one use a setter instead or
        provide all the configurations, both the old and new ones.
            - This method will return `None` in the next major release. If you are using the returned
        `Batch` object then you should start using the `client.batch` object instead.

        Parameters
        ----------
        batch_size : Optional[int], optional
            The batch size to be use. This value sets the Batch functionality, if `batch_size` is
            None then no auto-creation is done (`callback` and `dynamic` are ignored). If it is a
            positive number auto-creation is enabled and the value represents: 1) in case `dynamic`
            is False -> the number of data in the Batch (sum of objects and references) when to
            auto-create; 2) in case `dynamic` is True -> the initial value for both
            `recommended_num_objects` and `recommended_num_references`, by default 50
        creation_time : Real, optional
            How long it should take to create a Batch. Used ONLY for computing dynamic batch sizes. By default None
        timeout_retries : int, optional
            Number of retries to create a Batch that failed with ReadTimeout, by default 3
        connection_error_retries : int, optional
            Number of retries to create a Batch that failed with ConnectionError, by default 3
        weaviate_error_retries: WeaviateErrorRetryConf, Optional
            How often batch-elements with an error originating from weaviate (for example transformer timeouts) should
            be retried and which errors should be ignored and/or included. See documentation for WeaviateErrorRetryConf
            for details.
        callback : Optional[Callable[[dict], None]], optional
            A callback function on the results of each (objects and references) batch types.
            By default `weaviate.util.check_batch_result`
        dynamic : bool, optional
            Whether to use dynamic batching or not, by default True
        num_workers : int, optional
            The maximal number of concurrent threads to run batch import. Only used for non-MANUAL
            batching. i.e. is used only with AUTO or DYNAMIC batching.
            By default, the multi-threading is disabled. Use with care to not overload your weaviate instance.

        Returns
        -------
        Batch
            Updated self.

        Raises
        ------
        TypeError
            If one of the arguments is of a wrong type.
        ValueError
            If the value of one of the arguments is wrong.
        Nr   r!   ri   r&   r   r   r   r   rj   Ffixedrh   )r   r.   r	   r|   r   rz   rm   r{   _check_non_negativerE   rw   r}   r~   rx   ry   r   _check_boolr   flushshutdownstartr   r   rk   _update_recommended_batch_size_auto_create)
r<   r   r   r   r   r   r   rj   r   r   s
             r=   r   zBatch.configurec  sh   z "3$E"/D"&tS1A1A1P1PQR1SVX1XZ[-\"]DO->D46PRUV! /)A&%;"g#D"&DKJc:K<GY'+JJLMMO +DJJL%e")D"+D2<2DB*D)5?5GrZD,..6335r?   c                      t        j                          _        d fd}t        j                  |dd      }|j	                          y)zUCreate a background thread that periodically checks how congested the batch queue is.Nc                     t        j                        } j                  9j                  j                         s	 | j	                         }d|d   vs
d|d   d   vrd_        y |d   d   d   }|j                  z  }|d   d   d   }|dk(  r,j                  t        j                  dz  d      z   _        n_||z  }d	|cxkD  rd
kD  rn n|_        nD|d
k  r%t        j                  dz  |dz  |z        _        n|dk  r|dz  |z  _        nd_        d}t        j                  |       j                  j                  j                         sd_        d _        y # t        t        f$ r d}Y _w xY w)Nstatsr   ratePerSecondF
batchStatsqueueLengthr&      g @gffffff?g      ?ri   g?)r'   rm   rk   is_setget_nodes_statusrl   r   r   rz   RequestsHTTPErrorr   timesleep)clusterstatusraterate_per_workerbatch_lengthratiorefresh_timer<   s          r=   periodic_checkz<Batch._update_recommended_batch_size.<locals>.periodic_check  s   d../G//;77>>@'$557FfQi/?&QR)T[J\3\5:2!!9\2?CD&*T->->&>O#)!9\#:=#IL#q(8<8U8UX[ 99A=rY 95 !-t 3%-#-<KD9"c\<? $ = = C_WXEX[`E`=D9 #RZ<Ka<ORW<WD9<=D9*+L 

<(G //;77>>@F -/D).2D+ *;7 '#&L's    (E, )B7E, ,F ?F TbatchSizeRefresh)targetdaemonnamerZ   )rt   Eventrk   Threadr   )r<   r   demons   `  r=   r   z$Batch._update_recommended_batch_size  s>    *3//*;'(	3T   !#

 	r?   data_objectr4   uuidvectorr6   c                     | j                   j                  t        |      ||||      }| j                  j                  t	        ||             | j
                  r| j                          |S )a  
        Add one object to this batch.
        NOTE: If the UUID of one of the objects already exists then the existing object will be
        replaced by the new object.

        Parameters
        ----------
        data_object : dict
            Object to be added as a dict datatype.
        class_name : str
            The name of the class this object belongs to.
        uuid : Optional[UUID], optional
            The UUID of the object as an uuid.UUID object or str. It can be a Weaviate beacon or Weaviate href.
            If it is None an UUIDv4 will generated, by default None
        vector: Sequence or None, optional
            The embedding of the object that should be validated.
            Can be used when:
             - a class does not have a vectorization module.
             - The given vector was generated using the _identical_ vectorization module that is configured for the
             class. In this case this vector takes precedence.

            Supported types are `list`, 'numpy.ndarray`, `torch.Tensor` and `tf.Tensor`,
            by default None.

        Returns
        -------
        str
            The UUID of the added object. If one was not provided a UUIDv4 will be generated.

        Raises
        ------
        TypeError
            If an argument passed is not of an appropriate type.
        ValueError
            If 'uuid' is not of a proper form.
        r4   r   r   r   r6   )rn   addr,   r   r3   r   r   )r<   r   r4   r   r   r6   s         r=   add_data_objectzBatch.add_data_object  sk    X ""&&/
;# ' 
 	""5V#<=r?   from_object_uuidfrom_object_class_namefrom_property_nameto_object_uuidto_object_class_namec                    | j                   j                  dk\  }|"|r t        j                  t        t
        d       |X|s"t        j                  t        t
        d       d}|r2t        |t              st        dt        |             t        |      }| j                  j                  t        |      |||||       | j                  r| j                          yy)ay  
        Add one reference to this batch.

        Parameters
        ----------
        from_object_uuid : UUID
            The UUID of the object, as an uuid.UUID object or str, that should reference another object.
            It can be a Weaviate beacon or Weaviate href.
        from_object_class_name : str
            The name of the class that should reference another object.
        from_property_name : str
            The name of the property that contains the reference.
        to_object_uuid : UUID
            The UUID of the object, as an uuid.UUID object or str, that is actually referenced.
            It can be a Weaviate beacon or Weaviate href.
        to_object_class_name : Optional[str], optional
            The referenced object class name to which to add the reference (with UUID
            `to_object_uuid`), it is included in Weaviate 1.14.0, where all objects are namespaced
            by class name.
            STRONGLY recommended to set it with Weaviate >= 1.14.0. It will be required in future
            versions of Weaviate Server and Clients. Use None value ONLY for Weaviate < v1.14.0,
            by default None
        tenant: str, optional
            Name of the tenant.

        Raises
        ------
        TypeError
            If arguments are not of type str.
        ValueError
            If 'uuid' is not valid or cannot be extracted.
        z1.14Nr!   messagecategory
stacklevelz@'to_object_class_name' must be of type str or None. Given type: )r   r   r   r   r   r6   )rm   server_versionwarningswarnr(   DeprecationWarningr)   rQ   rC   	TypeErrortyper,   ro   r   r   r   )r<   r   r   r   r   r   r6   is_server_version_14s           r=   add_referencezBatch.add_reference=  s    T  $//>>&H',@MM>+
  +'B/ 
 (,$#!"6<#''+,@'A&BD  (@@T'U$!!#;<R#S-1)!5 	" 	
  r?   	data_typebatch_requestc           	      \   i }| j                   | j                   j                  |d<   	 dx}x}}	 	 | j                  j                  d|z   |j	                         |      }t        |d      }|J | j                  U|| j                  j                  k  r<| j                  ||      \  }	}
t        |	      dkD  r| j                  |
       |dz  }|	}| j                  |       	 |j                   d	k(  r|S t/        d| d|      # t        $ r}t        || j                  |       |dz  }| j                  ||      }t        |      dk(  rJt               }d	|_        t#        j$                  | j                  j&                  d   d
z         |_        Y d}~Y d}~n6d}~wt*        $ r'}t        || j,                  |       |dz  }Y d}~nd}~ww xY w# t*        $ r}t+        d      |d}~wt        $ r9 d| d| j                  j&                  d    dt        |       d}t        |      dw xY w)an  
        Create data in batches, either Objects or References. This does NOT guarantee
        that each batch item (only Objects) is added/created. This can lead to a successful
        batch creation but unsuccessful per batch item creation. See the Examples below.

        Parameters
        ----------
        data_type : str
            The data type of the BatchRequest, used to save time for not checking the type of the
            BatchRequest.
        batch_request : weaviate.batch.BatchRequest
            Contains all the data objects that should be added in one batch.
            Note: Should be a sub-class of BatchRequest since BatchRequest
            is just an abstract class, e.g. ObjectsBatchRequest, ReferenceBatchRequest

        Returns
        -------
        requests.Response
            The requests response.

        Raises
        ------
        requests.ReadTimeout
            If the request time-outed.
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        Nr   r   z/batch/pathweaviate_objectparamszbatch responser!   retrymax_retrieserror   rf   z Batch was not added to weaviate.zThe 'zO' creation was cancelled because it took longer than the configured timeout of z*s. Try reducing the batch size (currently zQ) to a lower value. Aim to on average complete batch request within less than 10szCreate z	 in batch)r   valuerm   postget_request_bodyr0   rx   rJ   _retry_on_errorrX   _run_callbackr   _batch_create_error_handlerr}   _batch_retry_after_timeoutr   status_codedatetime	timedeltar{   elapsedRequestsConnectionErrorr~   r+   )r<   r   r   r   timeout_countconnection_countbatch_error_countresponseresponse_jsonbatch_to_retryresponse_json_successfulr   conn_errr   s                 r=   _create_datazBatch._create_data  s   D "$"".*.*A*A*G*GF&'=	1CDDMD,/@0#//44&2(5(F(F(H%  5  H< %?xIY$ZM(44422>-0J0J0Y0YYCGCWCW)9D@(@ ~.2 ../GH-2-,:M$&&}5 3&O+gi[	,JHUUo # /+$($9$9#
 "Q&M$($C$CI}$]M=)Q.#+:/2,+3+=+= ,,;;A>B,(  / / */.$($B$B#
 %)$*1 d ' 	\)*LMS[[ 	1	{ #99=9I9I9X9XYZ9[8\ ]::=m:L9M NPP  g&D0	1sU   G /D  BG 	GBF
G G%GG GG 	H+G&&AH+r   c                     | j                   y | j                  5  | j                  |       d d d        y # 1 sw Y   y xY wr9   )rw   rv   )r<   r   s     r=   r   zBatch._run_callback  s9    >>!   	%NN8$	% 	% 	%s   5>c                     |dk(  r#t        |t              sJ | j                  |      S t        |t              sJ | j	                  |      S )a  
        Readds items (objects or references) that were not added due to a timeout.

        Parameters
        ----------
        data_type : str
            The Batch Request type, can be either 'objects' or 'references'.
        batch_request : BatchRequest
            The Batch Request that TimeOuted.

        Returns
        -------
        BatchRequest
            New Batch Request with objects that were not added or not updated.
        objects)rQ   r#   _readd_objects_after_timeoutr$   _readd_references_after_timeout)r<   r   r   s      r=   r   z Batch._batch_retry_after_timeout  sP    & 	!m-@AAA44]CCm-BCCC77FFr?   c           
         t               }|j                         d   D ]'  }|d   }|j                  dd      }|d   }|d|ind}| j                  j	                  d|z   dz   |z   |      }|j
                  d	k(  r2|j                  t        |      |d
   ||j                  dd             | j                  j                  d|z   dz   |z   |      }	t        |	d      }
|
J |
d
   |d
   k7  s&|j                  dd      |
j                  dd      k7  s|j                  t        |      |d
   ||j                  dd      |       * |S )a  
        Read all objects that were not created or updated because of a TimeOut error.

        Parameters
        ----------
        batch_request : ObjectsBatchRequest
            The ObjectsBatchRequest from which to check if items where created or updated.

        Returns
        -------
        ObjectsBatchRequest
            New ObjectsBatchRequest with only the objects that were not created or updated.
        r   classr6   Nidz	/objects//)r   r   i  
propertiesr   )r4   r   r   r   zRe-add objectsr   )	r#   r   getrm   headr   r   r,   r/   )r<   r   	new_batchobjr4   r6   r   r   response_headr   obj_weavs              r=   r   z"Batch._readd_objects_after_timeout  s   " ()	 113I> %	CWJWWXt,Ft9D+1+=h'4F ,,11 :-3d: 2 M
 ((C/7
C #L 1778T2	    ''++ :-3d: , H
 2(<LMH'''%\)::cgg$?h-?. 7
C #L 1778T2!  ?%	L r?   c                     t               }|j                         D ]4  }|j                  |d   |d   |d   |d   |j                  dd             6 |S )a  
        Read all objects that were not created or updated because of a TimeOut error.

        Parameters
        ----------
        batch_request : ReferenceBatchRequest
            The ReferenceBatchRequest from which to check if items where created or updated.

        Returns
        -------
        ReferenceBatchRequest
            New ReferenceBatchRequest with only the references that were not created or updated.
        r   r   r   r   r   N)r   r   r   r   r   )r$   r   r   r   )r<   r   r   refs       r=   r   z%Batch._readd_references_after_timeoutN  sp    " *+	 113 	CMM'*+C'D!$%7!8#&';#<"#34%(WW-CT%J  	 r?   c                 "   t        | j                        dk7  rt        j                          | j	                  d| j                        }t               | _        | j                  j                  t        | j                        |j                  j                         z         t        | j                        t        | j                        z  }t        t        |t        | j                        z        d      | _        t!        |d      }|J |S g S )a  
        Creates multiple Objects at once in Weaviate. This does not guarantee that each batch item
        is added/created to the Weaviate server. This can lead to a successful batch creation but
        unsuccessful per batch item creation. See the example bellow.
        NOTE: If the UUID of one of the objects already exists then the existing object will be
        replaced by the new object.

        Examples
        --------
        Here `client` is an instance of the `weaviate.Client`.

        Add objects to the object batch.

        >>> client.batch.add_data_object({}, 'NonExistingClass')
        >>> client.batch.add_data_object({}, 'ExistingClass')

        Note that 'NonExistingClass' is not present in the client's schema and 'ExistingObject'
        is present and has no proprieties. 'client.batch.add_data_object' does not raise an
        exception because the objects added meet the required criteria (See the documentation of
        the 'weaviate.Batch.add_data_object' method for more information).

        >>> result = client.batch.create_objects(batch)

        Successful batch creation even if one data object is inconsistent with the client's schema.
        We can find out more about what objects were successfully created by analyzing the 'result'
        variable.

        >>> import json
        >>> print(json.dumps(result, indent=4))
        [
            {
                "class": "NonExistingClass",
                "creationTimeUnix": 1614852753747,
                "id": "154cbccd-89f4-4b29-9c1b-001a3339d89a",
                "properties": {},
                "deprecations": null,
                "result": {
                    "errors": {
                        "error": [
                            {
                                "message": "class 'NonExistingClass' not present in schema,
                                                            class NonExistingClass not present"
                            }
                        ]
                    }
                }
            },
            {
                "class": "ExistingClass",
                "creationTimeUnix": 1614852753746,
                "id": "b7b1cfbe-20da-496c-b932-008d35805f26",
                "properties": {},
                "vector": [
                    -0.05244319,
                    ...
                    0.076136276
                ],
                "deprecations": null,
                "result": {}
            }
        ]


        As it can be noticed the first object from the batch was not added/created, but the batch
        was successfully created. The batch creation can be successful even if all the objects were
        NOT created. Check the status of the batch objects to find which object and why creation
        failed. Alternatively use 'client.data_object.create' for Object creation that throw an
        error if data item is inconsistent or creation/addition failed.

        To check the results of batch creation when using the auto-creation Batch, use a 'callback'
        (see the docs `configure` or `__call__` method for more information).

        Returns
        -------
        list
            A list with the status of every object that was created.

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        r   r   r   r   r!   zbatch add objects)rX   rn   r1   manual_batchingr   r#   rp   appendr   total_secondssummaxroundfloatr|   r   r0   )r<   r   obj_per_secondress       r=   create_objectszBatch.create_objectsj  s    l t""#q(%%'((#"11 ) H #6"7D**11D''(8+;+;+I+I+KK !!?!?@3..D N -0nuT-@-@'AABA-D) -X7JKC?"?J	r?   c                    t        | j                        dk7  rt        j                          | j	                  d| j                        }t               | _        | j                  j                  t        | j                        |j                  j                         z         t        | j                        t        | j                        z  }t        |t        | j                        z        | _        t        |d      }|J |S g S )a  
        Creates multiple References at once in Weaviate.
        Adding References in batch is faster but it ignores validations like class name
        and property name, resulting in a SUCCESSFUL reference creation of a nonexistent object
        types and/or a nonexistent properties. If the consistency of the References is wanted
        use 'client.data_object.reference.add' to have additional validation against the
        weaviate schema. See Examples below.

        Examples
        --------
        Here `client` is an instance of the `weaviate.Client`.

        Object that does not exist in weaviate.

        >>> object_1 = '154cbccd-89f4-4b29-9c1b-001a3339d89d'

        Objects that exist in weaviate.

        >>> object_2 = '154cbccd-89f4-4b29-9c1b-001a3339d89c'
        >>> object_3 = '254cbccd-89f4-4b29-9c1b-001a3339d89a'
        >>> object_4 = '254cbccd-89f4-4b29-9c1b-001a3339d89b'

        >>> client.batch.add_reference(object_1, 'NonExistingClass', 'existsWith', object_2)
        >>> client.batch.add_reference(object_3, 'ExistingClass', 'existsWith', object_4)

        Both references were added to the batch request without error because they meet the
        required criteria (See the documentation of the 'weaviate.Batch.add_reference' method
        for more information).

        >>> result = client.batch.create_references()

        As it can be noticed the reference batch creation is successful (no error thrown). Now we
        can inspect the 'result'.

        >>> import json
        >>> print(json.dumps(result, indent=4))
        [
            {
                "from": "weaviate://localhost/NonExistingClass/
                                                154cbccd-89f4-4b29-9c1b-001a3339d89a/existsWith",
                "to": "weaviate://localhost/154cbccd-89f4-4b29-9c1b-001a3339d89b",
                "result": {
                    "status": "SUCCESS"
                }
            },
            {
                "from": "weaviate://localhost/ExistingClass/
                                                254cbccd-89f4-4b29-9c1b-001a3339d89a/existsWith",
                "to": "weaviate://localhost/254cbccd-89f4-4b29-9c1b-001a3339d89b",
                "result": {
                    "status": "SUCCESS"
                }
            }
        ]

        Both references were added successfully but one of them is corrupted (links two objects
        of nonexisting class and one of the objects is not yet created). To make use of the
        validation, crete each references individually (see the client.data_object.reference.add
        method).

        Returns
        -------
        list
            A list with the status of every reference added.

        Raises
        ------
        requests.ConnectionError
            If the network connection to weaviate fails.
        weaviate.UnexpectedStatusCodeException
            If weaviate reports a none OK status.
        r   
referencesr  zCreate references)rX   ro   r1   r  r   r$   rq   r  r   r  r	  r  r  r|   r   r0   )r<   r   ref_per_secr  s       r=   create_referenceszBatch.create_references  s    T t$$%*%%'((&"33 ) H %:$;D!--44D))*X-=-=-K-K-MM d??@311D K 05[5I\I\C]5]/^D,,X7JKC?"?J	r?   c                 `    t        |      dk7  r | j                  ||      }|t        |      fS y)a  
        Flush BatchRequest in current thread/process.

        Parameters
        ----------
        data_type : str
            The data type of the BatchRequest, used to save time for not checking the type of the
            BatchRequest.
        batch_request : weaviate.batch.BatchRequest
            Contains all the data objects that should be added in one batch.
            Note: Should be a sub-class of BatchRequest since BatchRequest
            is just an abstract class, e.g. ObjectsBatchRequest, ReferenceBatchRequest

        Returns
        -------
        Tuple[requests.Response, int]
            The request response and number of items sent with the BatchRequest as tuple.
        r   r  )Nr   )rX   r   )r<   r   r   r   s       r=   _flush_in_threadzBatch._flush_in_thread:  sA    0 }"((#+ ) H S///r?   
force_waitc           
         | j                   | j                          nJ| j                   j                         r0t        j                  t
        t        d       | j                          | j                   J | j                   j                  | j                  d| j                        }| j                  j                  |       t        | j                        dkD  r%| j                  j                  | j                         t               | _	        t!               | _        |s2| j"                  dkD  r#t        | j                        | j"                  k  ryd}t%        | j                        D ]P  }|j'                         \  }}|7| j(                  j                  ||j*                  j-                         z         Od}R |r+| j.                  t1        | j.                  d	z  d      | _        nt        | j(                        dk7  r| j.                  | j2                  sut5        | j(                        t        | j(                        z  d
z  }t1        t7        t9        |t;        | j<                        z        | j.                  dz         d      | _        g }| j                  D ];  }	| j                   j                  | j                  d|	      }|j                  |       = d}t%        |      D ]P  }|j'                         \  }
}|
7| j>                  j                  ||
j*                  j-                         z         Od}R |r+| j@                  t1        | j@                  d	z  d      | _         nt        | j>                        dk7  rt| j@                  ht5        | j>                        t        | j>                        z  }t7        t9        |t;        | j<                        z        | j@                  d	z        | _         g | _
        g | _        y)a  
        Send BatchRequest in a separate thread/process. This methods submits a task to create only
        the ObjectsBatchRequests to the BatchExecutor and adds the ReferencesBatchRequests to a
        queue, then it carries on in the main thread until `num_workers` tasks have been submitted.
        When we have reached number of tasks to be equal to `num_workers` it waits for all the
        tasks to finish and handles the responses. After all ObjectsBatchRequests have been handled
        it created separate tasks for each ReferencesBatchRequests, then it handles their responses
        as well. This mechanism of creating References after Objects is constructed in this manner
        to eliminate potential error when creating references from a object that does not yet
        exists (object that is part of another task).

        Parameters
        ----------
        force_wait : bool
            Whether to wait on all created tasks even if we do not have `num_workers` tasks created
        Nr!   r   r   r  r   FTr&   g      ?   r  )!r   r   r`   r   r   r*   RuntimeWarningsubmitr  rn   rr   r  rX   ro   rs   r#   r$   r   r   resultrp   r   r  r   r
  rl   r	  rz   r  r  r|   rq   r   )r<   r  futuretimeout_occurreddone_futureresponse_objects
nr_objectsr  reference_future_poolreference_batchresponse_referencesnr_referencesr  s                r=   _send_batch_requestszBatch._send_batch_requestsZ  s   " >>!JJL^^'')MM1'
 JJL~~)))&&!!-- ' 
 	  (t$$%)''..t/D/DE13 5 7d//!3D<M<M8NQUQbQb8b '(9(9: 
	(K+6+=+=+?(j  +..55!1!9!9!G!G!II
 $( 
	(  = = I,/0M0MQR0RTU,VD)../14--9.. D223c$:X:X6YY\``  -0.51D1D+EEF11C7 -D) !##:: 	1O^^**%%&- + F
 "((0	1 !'(=> 		(K1<1C1C1E. #.1188!$7$?$?$M$M$OO $( 		(  @ @ L/243S3SWX3XZ[/\D,112a700<d??@311D K 03kE$*=*=$>>?00140D,
 &(#r?   c                    | j                   dk(  rC| j                  J t        | j                        | j                  k\  r| j	                  d       y| j                   dk(  r| j                         | j                  k\  s| j                         | j                  k\  rF| j                  dk(  r%t        j                  d       | j                  dk(  r%| j	                  d       yt        d| j                    d	      )
ad  
        Auto create both objects and references in the batch. This protected method works with a
        fixed batch size and with dynamic batching. For a 'fixed' batching type it auto-creates
        when the sum of both objects and references equals batch_size. For dynamic batching it
        creates both batch requests when only one is full.
        r   NFr  rj   r   r!   zUnsupported batching type "")r   ry   r	  shaper%  num_objectsr   num_referencesr   r   r   rV   r;   s    r=   r   zBatch._auto_create  s     ')##///4::$"2"22))U);  I-  "d&C&CC&&(D,L,LL33q8JJqM 33q8 ))U);6t7J7J6K1MNNr?   c                 (    | j                  d       y)z
        Flush both objects and references to the Weaviate server and call the callback function
        if one is provided. (See the docs for `configure` or `__call__` for how to set one.)
        Tr'  N)r%  r;   s    r=   r   zBatch.flush  s    
 	!!T!2r?   whereoutputdry_runc                 p   t        |t              st        dt        |       d      t        |t              st        dt        |       d      t        |t              st        dt        |       d      t        |t
              st        dt        |       d      i }| j                  | j                  j                  |d<   |||d<   t        |      t        |      d	||d
}	 | j                  j                  d||      }t        |d      }
|
J |
S # t        $ r}	t        d      |	d}	~	ww xY w)a	  
        Delete objects that match the 'match' in batch.

        Parameters
        ----------
        class_name : str
            The class name for which to delete objects.
        where : dict
            The content of the `where` filter used to match objects that should be deleted.
        output : str, optional
            The control of the verbosity of the output, possible values:
            - "minimal" : The result only includes counts. Information about objects is omitted if
            the deletes were successful. Only if an error occurred will the object be described.
            - "verbose" : The result lists all affected objects with their ID and deletion status,
            including both successful and unsuccessful deletes.
            By default "minimal"
        dry_run : bool, optional
            If True, objects will not be deleted yet, but merely listed, by default False

        Examples
        --------

        If we want to delete all the data objects that contain the word 'weather' we can do it like
        this:

        >>> result = client.batch.delete_objects(
        ...     class_name='Dataset',
        ...     output='verbose',
        ...     dry_run=False,
        ...     where={
        ...         'operator': 'Equal',
        ...         'path': ['description'],
        ...         'valueText': 'weather'
        ...     }
        ... )
        >>> print(json.dumps(result, indent=4))
        {
            "dryRun": false,
            "match": {
                "class": "Dataset",
                "where": {
                    "operands": null,
                    "operator": "Equal",
                    "path": [
                        "description"
                    ],
                    "valueText": "weather"
                }
            },
            "output": "verbose",
            "results": {
                "failed": 0,
                "limit": 10000,
                "matches": 2,
                "objects": [
                    {
                        "id": "1eb28f69-c66e-5411-bad4-4e14412b65cd",
                        "status": "SUCCESS"
                    },
                    {
                        "id": "da217bdd-4c7c-5568-9576-ebefe17688ba",
                        "status": "SUCCESS"
                    }
                ],
                "successful": 2
            }
        }

        Returns
        -------
        dict
            The result/status of the batch delete.
        z.'class_name' must be of type str. Given type: .z*'where' must be of type dict. Given type: z*'output' must be of type str. Given type: z,'dry_run' must be of type bool. Given type: Nr   r6   )r   r-  )matchr.  dryRunz/batch/objectsr   z Batch delete was not successful.zDelete in batch)rQ   rC   r   r   dictra   r   r   r,   _clean_delete_objects_whererm   deleter   r/   )r<   r4   r-  r.  r/  r6   r   payloadr   r   r  s              r=   delete_objectszBatch.delete_objects  sb   d *c*LTR\M]L^^_`aa%&HeUVWXX&#&HfVWXYY'4(J4PW=/YZ[\\!#"".*.*A*A*G*GF&'%F8 2*=4U; 
	\''..% ' / H )3DE
	 ' 	\)*LMS[[	\s   +D 	D5$D00D5c                 ,    t        | j                        S )z
        Get current number of objects in the batch.

        Returns
        -------
        int
            The number of objects in the batch.
        )rX   rn   r;   s    r=   r*  zBatch.num_objectsd  s     4&&''r?   c                 ,    t        | j                        S )z
        Get current number of references in the batch.

        Returns
        -------
        int
            The number of references in the batch.
        )rX   ro   r;   s    r=   r+  zBatch.num_referencesp  s     4(())r?   indexc                 8    | j                   j                  |      S )a  
        Remove and return the object at index (default last).

        Parameters
        ----------
        index : int, optional
            The index of the object to pop, by default -1 (last item).

        Returns
        -------
        dict
            The popped object.

        Raises
        -------
        IndexError
            If batch is empty or index is out of range.
        )rn   popr<   r;  s     r=   
pop_objectzBatch.pop_object|  s    ( ""&&u--r?   c                 8    | j                   j                  |      S )a  
        Remove and return the reference at index (default last).

        Parameters
        ----------
        index : int, optional
            The index of the reference to pop, by default -1 (last item).

        Returns
        -------
        dict
            The popped reference.

        Raises
        -------
        IndexError
            If batch is empty or index is out of range.
        )ro   r=  r>  s     r=   pop_referencezBatch.pop_reference  s    ( $$((//r?   c                 8    | j                   j                          y)z8
        Remove all the objects from the batch.
        N)rn   emptyr;   s    r=   empty_objectszBatch.empty_objects  s    
 	!!#r?   c                 8    | j                   j                          y)z;
        Remove all the references from the batch.
        N)ro   rC  r;   s    r=   empty_referenceszBatch.empty_references  s    
 	##%r?   c                 6    | j                   j                         S )z
        Check if batch contains any objects.

        Returns
        -------
        bool
            Whether the Batch object list is empty.
        )rn   is_emptyr;   s    r=   is_empty_objectszBatch.is_empty_objects  s     ""++--r?   c                 6    | j                   j                         S )z
        Check if batch contains any references.

        Returns
        -------
        bool
            Whether the Batch reference list is empty.
        )ro   rH  r;   s    r=   is_empty_referenceszBatch.is_empty_references  s     $$--//r?   c                 V    t        | j                        t        | j                        fS )a"  
        Get current number of objects and references in the batch.

        Returns
        -------
        Tuple[int, int]
            The number of objects and references, respectively, in the batch as a tuple,
            i.e. returns (number of objects, number of references).
        )rX   rn   ro   r;   s    r=   r)  zBatch.shape  s%     D''(#d.C.C*DEEr?   c                     | j                   S )aF  
        Setter and Getter for `batch_size`.

        Parameters
        ----------
        value : Optional[int]
            Setter ONLY: The new value for the batch_size. If NOT None it will try to auto-create
            the existing data if it meets the requirements. If previous value was None then it will
            be set to new value and will change the batching type to auto-create with dynamic set
            to False. See the documentation for `configure` or `__call__` for more info.
            If recommended_num_objects is None then it is initialized with the new value of the
            batch_size (same for references).

        Returns
        -------
        Optional[int]
            Getter ONLY: The current value of the batch_size. It is NOT the current number of
            data in the Batch. See the documentation for `configure` or `__call__` for more info.

        Raises
        ------
        TypeError
            Setter ONLY: If the new value is not of type int.
        ValueError
            Setter ONLY: If the new value has a non positive value.
        )ry   r;   s    r=   r   zBatch.batch_size  s    : r?   r   c                     |d | _         d | _        y t        |dt               || _         | j                  d| _        | j                  || _        | j
                  || _        | j                          y )Nr   r   )ry   r   r.   rE   r   r   r   r<   r   s     r=   r   zBatch.batch_size  sv    =#D"&DE<5 &")D((0,1D)++3/4D,r?   c                      | j                   dk(  S )a)  
        Setter and Getter for `dynamic`.

        Parameters
        ----------
        value : bool
            Setter ONLY: En/dis-able the dynamic batching. If batch_size is None the value is not
            set, otherwise it will set the dynamic to new value and auto-create if it meets the
            requirements.

        Returns
        -------
        bool
            Getter ONLY: Wether the dynamic batching is enabled.

        Raises
        ------
        TypeError
            Setter ONLY: If the new value is not of type bool.
        rj   )r   r;   s    r=   rj   zBatch.dynamic  s    . ""i//r?   c                 |    | j                   y t        |d       |du rd| _         nd| _         | j                          y )Nrj   Tr   )r   r   r   rO  s     r=   rj   zBatch.dynamic%  s>    &E9%D="+D")Dr?   c                 J    | j                   | j                   j                  S d S r9   )r   r   r;   s    r=   r   zBatch.consistency_level2  s%    040G0G0St&&,,]Y]]r?   xc                 8    |t        |      | _        y d | _        y r9   )r   r   )r<   rS  s     r=   r   zBatch.consistency_level6  s    9:"21"5Dr?   c                     | j                   S )z
        The recommended number of objects per batch. If None then it could not be computed.

        Returns
        -------
        Optional[int]
            The recommended number of objects per batch. If None then it could not be computed.
        )r   r;   s    r=   recommended_num_objectszBatch.recommended_num_objects:  s     ,,,r?   c                     | j                   S )a  
        The recommended number of references per batch. If None then it could not be computed.

        Returns
        -------
        Optional[int]
            The recommended number of references per batch. If None then it could not be computed.
        )r   r;   s    r=   recommended_num_referencesz Batch.recommended_num_referencesG  s     ///r?   c                    | j                   | j                   j                         rt        | j                        | _         | j                  dk(  r6| j
                  | j
                  j                         r| j                          | S )z
        Start the BatchExecutor if it was closed.

        Returns
        -------
        Batch
            Updated self.
        )max_workersrj   )r   r`   r]   r   r   rk   r   r   r;   s    r=   r   zBatch.startT  sk     >>!T^^%?%?%A*t7H7HIDN)+++3t7V7V7]7]7_//1r?   c                     | j                   4| j                   j                         s| j                   j                          | j                  | j                  j	                          yy)z-
        Shutdown the BatchExecutor.
        N)r   r`   r   rk   r   r;   s    r=   r   zBatch.shutdownh  sP     &$..*D*D*FNN##%**6++//1 7r?   c                 "    | j                         S r9   )r   r;   s    r=   	__enter__zBatch.__enter__r  s    zz|r?   exc_typeexc_valexc_tbc                 D    | j                          | j                          y r9   )r   r   )r<   r^  r_  r`  s       r=   __exit__zBatch.__exit__u  s    

r?   shardshow_many_failuresc                 H    (t        t              st        dt               d      +t        d   t              st        dt               d      dt
        dt        f fd d      s*t        d       t        j                  d	        d      s)yy)
ay  Wait for the all the vectors of the batch imported objects to be indexed.

        Upon network error, it will retry to get the shards' status for `how_many_failures` times
        with exponential backoff (2**n seconds with n=0,1,2,...,how_many_failures).

        Parameters
        ----------
            shards {Optional[List[Shard]]} -- The shards to check the status of. If None it will
                check the status of all the shards of the imported objects in the batch.
            how_many_failures {int} -- How many times to try to get the shards' status before
                raising an exception. Default 5.
        Nz2'shards' must be of type List[Shard]. Given type: r1  r   how_manyr7   c           	          	 t        fdxs j                  D              S # t        $ rK}t        d| dd| z   d|         | k(  r|t	        j
                  d| z          | dz         cY d }~S d }~ww xY w)Nc              3   R   K   | ]  }t        j                  |               y wr9   )all_get_shards_readiness)rR   shardr<   s     r=   rT   zCBatch.wait_for_vector_indexing.<locals>.is_ready.<locals>.<genexpr>  s)       2259:s   $'z+Error while getting class shards statuses: z, trying again with 2**n=r&   zs exponential backoff with n=r!   )ri  r   r   printr   r   )rf  erd  is_readyr<   rc  s     r=   rn  z0Batch.wait_for_vector_indexing.<locals>.is_ready  s    . !'!A4+A+A   + .A!D]^_ai^i]j  kH  IQ  HR  S %0G

1h;'1--.s   !% 	A9A A4.A94A9z'Waiting for async indexing to finish...g      ?)
rQ   listr   r   r3   rE   ra   rl  r   r   )r<   rc  rd  rn  s   ```@r=   wait_for_vector_indexingzBatch.wait_for_vector_indexingy  s     j&>PQUV\Q]P^^_`aajE&BPQUV\Q]P^^_`aa	.s 	.t 	. 	. 1+;<JJt 1+r?   rk  c           
      *   t        |j                  t              s"t        dt	        |j                         d      dt        |j                         d|j                  dnd|j                    }	 | j                  j                  |      }t        |d	      }|J |D cg c]G  }t        t        |j                  d
            dk(  t        t        |j                  d            dk(  z  I c}S # t        $ r}t        d      |d }~ww xY wc c}w )Nz9'class_name' argument must be of type `str`! Given type: r1  z/schema/z/shards z?tenant=)r   zDClass shards' status could not be retrieved due to connection error.zGet shards' statusr   READYvectorQueueSizer   )rQ   r4   rC   r   r   r,   r6   rm   r   r   r0   r   rE   )r<   rk  r   r   r   r  s         r=   rj  zBatch._get_shards_readiness  sE   %**C0#E$4$456a9 
 253C3CDEWSXS_S_SgRowx}  yE  yE  xF  nG  MH  I	''+++6H )3GH 
  #uyy*+w6C#456!;=
 	
 ' 	)V	
s   4C3 $AD3	D<DDc                     | j                   S )a  
        Setter and Getter for `creation_time`.

        Parameters
        ----------
        value : Real
            Setter ONLY: Set new value to creation_time. The recommended_num_objects/references
            values are updated to this new value. If the batch_size is not None it will auto-create
            the batch if the requirements are met.

        Returns
        -------
        Real
            Getter ONLY: The `creation_time` value.

        Raises
        ------
        TypeError
            Setter ONLY: If the new value is not of type Real.
        ValueError
            Setter ONLY: If the new value has a non positive value.
        )r|   r;   s    r=   r   zBatch.creation_time  s    2 """r?   c                    t        |dt               | j                  <t        | j                  t	        |      z  t	        | j
                        z        | _        | j                  <t        | j                  t	        |      z  t	        | j
                        z        | _        || _        | j                  r| j                          y y )Nr   )	r.   r	   r   r  r  r|   r   r   r   rO  s     r=   r   zBatch.creation_time  s    E?D9++7/4005<?%H[H[B\\0D, ((4,1--e<uTEXEX?YY-D) $ r?   c                     | j                   S )a  
        Setter and Getter for `timeout_retries`.

        Properties
        ----------
        value : int
            Setter ONLY: The new value for `timeout_retries`.

        Returns
        -------
        int
            Getter ONLY: The `timeout_retries` value.

        Raises
        ------
        TypeError
            Setter ONLY: If the new value is not of type int.
        ValueError
            Setter ONLY: If the new value has a non positive value.
        )r}   r;   s    r=   r   zBatch.timeout_retries  s    . $$$r?   c                 4    t        |dt               || _        y )Nr   )r   rE   r}   rO  s     r=   r   zBatch.timeout_retries  s    E#4c: %r?   c                     | j                   S )a  
        Setter and Getter for `connection_error_retries`.

        Properties
        ----------
        value : int
            Setter ONLY: The new value for `connection_error_retries`.

        Returns
        -------
        int
            Getter ONLY: The `connection_error_retries` value.

        Raises
        ------
        TypeError
            Setter ONLY: If the new value is not of type int.
        ValueError
            Setter ONLY: If the new value has a non positive value.
        )r~   r;   s    r=   r   zBatch.connection_error_retries   s    . ---r?   c                 4    t        |dt               || _        y )Nr   )r   rE   r~   rO  s     r=   r   zBatch.connection_error_retries  s    E#=sC).&r?   c                     |dk(  rt               }n
t               }| j                  J |j                  || j                  j                  | j                  j
                        }||fS )Nr   )r#   r$   rx    add_failed_objects_from_responserK   rL   )r<   r   r   r   successful_responsess        r=   r   zBatch._retry_on_error  sn     	!K^K`I-/I))555(II&&88&&88 

 ...r?   rZ   )NNN)NN)minimalFN))r7   rc   )Nrf   )Ir@   rA   rB   r[   r   r   r
   r   r-   r   rE   r	   rH   r   r   r4  ra   r   r   r   rC   r    r   r   r   r"   r   r   r%   r   r   r#   r   r$   r   ro  r  r  r   r  r%  r   r   r8  r*  r+  r?  rA  rD  rF  rI  rK  propertyr)  r   setterrj   r   r   rV  rX  r   r   r]  rb  r3   rp  rj  r   r   r   BatchRequestTyper   rF   r?   r=   rc   rc      s   BH(7: (7T-( -( -(b %'(, ()CG;M8<hSMh  ~h 	h
 #&h !))? @h 8T$ZL$$678h h h $$45h 
hT3r  $%) $99 9 tn	9
 "9 9 
9B /3 $L L  !$L   	L 
 L  'smL  L  
L \fVfV $fV 
	fVP%m % %GG-9G	G4808	8t2	8m m^_4 _B $ 
x!3&	'	@pt p pdO63   $tt t 	t
 t t 
tl
(S 
(
* 
*. .T .,03 0 0,$&
.$ 
.
0T 
0 FuS#X F F  HSM    <  $    0 0 00 ^^
T 
d 
 
 ^5d#3 ^ ^ Q8E2BC2G,H#I Qd Q Q 
-# 
- 
- 
0HSM 
0 
0(2 s C D 
 NO%tE{+%GJ%	%N
5 
T$Z 
0 #t # #4  4  D     % % %0 &S &T & & .# . .0 $$/c /d / %//%/25/	.	//r?   rc   N)boundr   arg_namer   r7   c                     t        | |      rt        | t              rt        d| d| d      | dk  rt        d| d      y)a  
    Check if the `value` of the `arg_name` is a non-negative number.

    Parameters
    ----------
    value : N (int, float, Real)
        The value to check.
    arg_name : str
        The name of the variable from the original function call. Used for error message.
    data_type : Type[N]
        The data type to check for.

    Raises
    ------
    TypeError
        If the `value` is not of type `data_type`.
    ValueError
        If the `value` has a negative value.
    'z' must be of type r1  r   z:' must be positive, i.e. greater or equal that zero (>=0).N)rQ   ra   r   rV   )r   r  r   s      r=   r   r   1  sS    * eY':eT+B!H:%7	{!DEEqy1XJ&`abb r?   c                 B    t        | t              st        d| d      y)a"  
    Check if bool.

    Parameters
    ----------
    value : bool
        The value to check.
    arg_name : str
        The name of the variable from the original function call. Used for error message.

    Raises
    ------
    TypeError
        If the `value` is not of type bool.
    r  z' must be of type bool.N)rQ   ra   r   )r   r  s     r=   r   r   L  s(    " eT"!H:%<=>> #r?   r   r   r   c                     | |k\  r|t        d|j                  j                   d| dz   dz   d| dz    d| d	t        j                  d	       t        j                  | dz   dz         y
)a  
    Handle errors that occur in Batch creation. This function is going to re-raise the error if
    number of re-tries was reached.
    Parameters
    ----------
    retry : int
        Current number of attempted request calls.
    max_retries : int
        Maximum number of attempted request calls.
    error : Exception
        The exception that occurred (to be re-raised if needed).
    Raises
    ------
    Exception
        The caught exception.
    z[ERROR] Batch z! Exception occurred! Retrying in r!   r&   zs. [r   ]T)filer   N)rl  	__class__r@   sysstderrr   r   r   s      r=   r   r   a  ss    $ 	
1122SAI?
4	{!K=	;ZZ	 	JJ	Qr?   r-  c                    d| v rt        |       }t        |      }d| vrt        d|        | d   t        vrt        d| d    dt               | d   }d|v rd|vrt        d| d	| d
t               | j                  |      | |<   | S d| v r!| d   D cg c]  }t        |       c}| d<   | S t        d|        c c}w )a1  Converts the Python-defined where filter type into the Weaviate-defined
    where filter type used in the Batch REST request endpoint.

    Parameters
    ----------
    where : dict
        The Python-defined where filter.

    Returns
    -------
    dict
        The Weaviate-defined where filter.
    r   operatorz:Where filter is missing required field `operator`. Given: z	Operator z( is not allowed. Allowed operators are: ContainsArrayz
Operator 'z#' is not supported for value type 'z'. Supported value types are: operandsz>Where is missing required fields `path` or `operands`. Given: )r   _convert_value_typerV   r   r   r=  r5  )r-  py_value_typeweaviate_value_typer  operands        r=   r5  r5  ~  s8    (/1-@U"OPUwW  O3E*-. /**9):<  $!g5H&HXJ&IJ]I^^|  ~O  }P  Q  &+YY}%=!" L 
u	QVWaQbcg8Acj
 L OPUwW
 	
 ds   B>_typec                 N    | dk(  ry| dk(  ry| dk(  ry| dk(  ry| d	k(  ry	| d
k(  ry| S )a9  Converts the Python-defined where filter type into the Weaviate-defined
    where filter type used in the Batch REST request endpoint.

    Parameters
    ----------
    _type : str
        The Python-defined where filter type.

    Returns
    -------
    str
        The Weaviate-defined where filter type.
    valueTextListvalueTextArrayvalueStringListvalueStringArrayvalueIntListvalueIntArrayvalueNumberListvalueNumberArrayvalueBooleanListvalueDateListvalueDateArrayrF   )r  s    r=   r  r    sN     	#	#!	.	 	#	#!	$	$!	/	!r?   )Ur[   r   r  rt   r   r   collectionsr   concurrent.futuresr   r   r   dataclassesr   r   numbersr	   typingr
   r   r   r   r   r   r   r   r   r   r   r   r   requestsr   r   requests.exceptionsr   r   r   r   weaviate.connectr   weaviate.data.replicationr   weaviate.gql.filterr   r   r   weaviate.typesr    r"   r#   r$   r%   r   r'   
error_msgsr(   r)   r*   
exceptionsr+   utilr,   r-   r.   r/   r0   r1   r  r3   rH   r]   rc   rE   r  r  rC   r   ra   r   	Exceptionr   r4  r5  r  rF   r?   r=   <module>r     s    
     G G (      + J > ' 6 T T  ] ]  
 7  !,.CCD  4 4 4 +^ +^ +^\& &f/ f/R5 CuS%-./cq cC cDG c c6?t ?s ?t ?* s    Y  SW  :&t & &Rs s r?   