3
O6bkj                 @   sL  d Z ddl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 ddlmZmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZ ddlmZmZmZmZmZm Z m!Z! ddl"m#Z# ddl$m%Z% dZ&dZ'dZ(dZ)dZ*d%Z+dZ,G dd de-Z.dd Z/dd Z0G dd de-Z1G dd  d e-Z2G d!d" d"e-Z3G d#d$ d$e-Z4dS )&z<The bulk write operations interface.

.. versionadded:: 2.7
    N)islice)ObjectId)RawBSONDocument)SON)_validate_session_write_concern)validate_is_mappingvalidate_is_document_typevalidate_ok_for_replacevalidate_ok_for_update)_RETRYABLE_ERROR_CODES)validate_collation_or_none)BulkWriteErrorConfigurationErrorInvalidOperationOperationFailure)_INSERT_UPDATE_DELETE_do_batched_insert_randint_BulkWriteContext_EncryptedBulkWriteContext)ReadPreference)WriteConcern         @   insertupdatedeleteopc               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_Runz,Represents a batch of write operations.
    c             C   s   || _ g | _g | _d| _dS )z%Initialize a new Run object.
        r   N)op_type	index_mapops
idx_offset)selfr#    r(   K/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/bulk.py__init__B   s    z_Run.__init__c             C   s
   | j | S )zGet the original index of an operation in this run.

        :Parameters:
          - `idx`: The Run index that maps to the original index.
        )r$   )r'   idxr(   r(   r)   indexJ   s    z
_Run.indexc             C   s   | j j| | jj| dS )zAdd an operation to this Run instance.

        :Parameters:
          - `original_index`: The original index of this operation
            within a larger bulk operation.
          - `operation`: The operation document.
        N)r$   appendr%   )r'   Zoriginal_index	operationr(   r(   r)   addR   s    z_Run.addN)__name__
__module____qualname____doc__r*   r,   r/   r(   r(   r(   r)   r"   ?   s   r"   c             C   s^  |j dd}| jtkr(|d  |7  < n| jtkrD|d  |7  < n| jtkr|j d}|rt|}x"|D ]}| j|d | |d< qjW |d j| |d  |7  < |d  || 7  < n|d  |7  < |d	  |d	 7  < |j d
}|r<xJ|D ]B}|j }	|d | }
| j|
|	d< | j	|
 |	t
< |d
 j|	 qW |j d}|rZ|d j| dS )z<Merge a write command result into the full bulk result.
    nr   	nInsertednRemovedupsertedr,   	nUpsertednMatched	nModifiedwriteErrorswriteConcernErrorwriteConcernErrorsN)getr#   r   r   r   lenr,   extendcopyr%   _UOPr-   )runfull_resultoffsetresultZaffectedr7   Z
n_upserteddocZwrite_errorsreplacementr+   Zwc_errorr(   r(   r)   _merge_command^   s6    







rI   c             C   s(   | d r| d j dd d t| dS )z:Raise a BulkWriteError from the full bulk api result.
    r;   c             S   s   | d S )Nr,   r(   )errorr(   r(   r)   <lambda>   s    z)_raise_bulk_write_error.<locals>.<lambda>)keyN)sortr   )rD   r(   r(   r)   _raise_bulk_write_error   s    rN   c               @   s   e Zd ZdZdd Zedd Zdd Zd"d
dZd#ddZ	d$ddZ
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd	S )%_Bulkz,The private guts of the bulk write API.
    c             C   s`   |j |jjdtdd| _|| _g | _d| _|| _d| _	d| _
d| _d| _d| _d| _d| _dS )z%Initialize a _Bulk instance.
        replace)Zunicode_decode_error_handlerZdocument_class)codec_optionsFTN)Zwith_optionsrQ   _replacedict
collectionorderedr%   executedbypass_doc_valuses_collationuses_array_filters	uses_hintis_retryableretryingstarted_retryable_writecurrent_run)r'   rT   rU   bypass_document_validationr(   r(   r)   r*      s    z_Bulk.__init__c             C   s$   | j jjj}|r|j rtS tS d S )N)rT   databaseclientZ
_encrypterZ_bypass_auto_encryptionr   r   )r'   Z	encrypterr(   r(   r)   bulk_ctx_class   s    z_Bulk.bulk_ctx_classc             C   s:   t d| t|tpd|ks&t |d< | jjt|f dS )z3Add an insert document to the list of ops.
        document_idN)r   
isinstancer   r   r%   r-   r   )r'   rc   r(   r(   r)   
add_insert   s    

z_Bulk.add_insertFNc       	      C   s   t | td|fd|fd|fd|fg}t|}|dk	rFd| _||d< |dk	r\d| _||d< |dk	rrd| _||d	< |r|d
| _| jjt	|f dS )zACreate an update document and add it to the list of ops.
        qumultiupsertNT	collationZarrayFiltershintF)
r
   r   r   rX   rY   rZ   r[   r%   r-   r   )	r'   selectorr   ri   rj   rk   Zarray_filtersrl   cmdr(   r(   r)   
add_update   s     z_Bulk.add_updatec             C   sl   t | td|fd|fd
d|fg}t|}|dk	rBd| _||d< |dk	rXd| _||d	< | jjt|f dS )zACreate a replace document and add it to the list of ops.
        rg   rh   ri   Frj   NTrk   rl   )ri   F)r	   r   r   rX   rZ   r%   r-   r   )r'   rm   rH   rj   rk   rl   rn   r(   r(   r)   add_replace   s    z_Bulk.add_replacec             C   sj   t d|fd|fg}t|}|dk	r2d| _||d< |dk	rHd| _||d< |tkrVd| _| jjt|f dS )z@Create a delete document and add it to the list of ops.
        rg   limitNTrk   rl   F)	r   r   rX   rZ   _DELETE_ALLr[   r%   r-   r   )r'   rm   rq   rk   rl   rn   r(   r(   r)   
add_delete   s    z_Bulk.add_deletec             c   s`   d}xPt | jD ]B\}\}}|dkr.t|}n|j|krF|V  t|}|j|| qW |V  dS )ziGenerate batches of operations, batched by type of
        operation, in the order **provided**.
        N)	enumerater%   r"   r#   r/   )r'   rC   r+   r#   r.   r(   r(   r)   gen_ordered   s    

z_Bulk.gen_orderedc             c   s`   t tt tt tg}x*t| jD ]\}\}}|| j|| q"W x|D ]}|jrH|V  qHW dS )zbGenerate batches of operations, batched by type of
        operation, in arbitrary order.
        N)r"   r   r   r   rt   r%   r/   )r'   
operationsr+   r#   r.   rC   r(   r(   r)   gen_unordered   s    
z_Bulk.gen_unorderedc          	   C   s(  |j dk r&| jrtd| jr&td|j dk r>| jr>td| jjj}| jjj}	|	j	}
| j
sht|| _
| j
}|j|	| x|r"t|j }| j|||||
||j| jj}xL|jt|jk rt|| jjfd| jfg}|js|j|d< | jo|j dkrd	|d
< |r<|r*| j r*|j  d	| _|j||tj| |j|||	 |j| t |j|jd }|j!|||	\}}|j"di }|j"ddt#krt$j%|}t&|||j| t'| t&|||j| d| _(d| _| jrd|krP | jt|7  _qW | jr|d rP t|d  | _
}q~W d S )N   z5Must be connected to MongoDB 3.4+ to use a collation.z.Must be connected to MongoDB 3.4+ to use hint.   z6Must be connected to MongoDB 3.6+ to use arrayFilters.rU   writeConcern   TbypassDocumentValidationr<   coder   Fr;   ))max_wire_versionrX   r   rZ   rY   rT   r`   namera   _event_listenersr^   nextZvalidate_session	_COMMANDSr#   rb   rQ   r&   r?   r%   r   rU   Zis_server_defaultrc   rW   r]   Z_start_retryable_writeZ	_apply_tor   ZPRIMARYZsend_cluster_timeadd_server_apir   executer>   r   rA   deepcopyrI   rN   r\   )r'   	generatorwrite_concernsession	sock_infoop_id	retryablerD   db_namera   	listenersrC   cmd_namebwcrn   r%   rF   to_sendZwcefullr(   r(   r)   _execute_command  sj    








z_Bulk._execute_commandc                s~   g g dddddg d t   fdd}jjj}|j|}|jj|| W dQ R X  d sr d rzt   S )z&Execute using write commands.
        r   )r;   r=   r5   r8   r9   r:   r6   r7   c                s   j | ||  d S )N)r   )r   r   r   )rD   r   r   r'   r   r(   r)   retryable_bulkc  s    
z-_Bulk.execute_command.<locals>.retryable_bulkNr;   r=   )r   rT   r`   ra   Z_tmp_sessionZ_retry_with_sessionr[   rN   )r'   r   r   r   r   ra   sr(   )rD   r   r   r'   r   r)   execute_commandS  s"    
z_Bulk.execute_commandc       	      C   s   | j j}dt| ji}td| j jfd| jfd|fg}| jrN|jdkrNd|d< t|jd|||j	j
dt| j j|d		}t| j j|jd||| j | j j| dS )
z.Execute insert, returning no results.
        wr   rU   rz   r{   Tr|   N)Z
cmd_legacy)rT   r`   intrU   r   r   rW   r~   r   ra   r   r   rQ   r   Z	full_namer%   )	r'   r   rC   r   acknowledgeddbZconcernrn   r   r(   r(   r)   execute_insert_no_resultsq  s    z_Bulk.execute_insert_no_resultsc          	   C   s   | j jj}| j jj}|j}t }| js0t|| _| j}x|rt|j	 }| j
|||||d|j	| j j}	xl|jt|jk rt|| j jfddddifg}
|j|
 t|j|jd}|	j|
||}| jt|7  _qfW t|d | _}q8W dS )zLExecute write commands with OP_MSG and w=0 writeConcern, unordered.
        NrU   Frz   r   r   )rU   F)rT   r`   r   ra   r   r   r^   r   r   r#   rb   rQ   r&   r?   r%   r   r   r   Zexecute_unack)r'   r   r   r   ra   r   r   rC   r   r   rn   r%   r   r(   r(   r)   execute_op_msg_no_results  s*    




z_Bulk.execute_op_msg_no_resultsc             C   sV   g g dddddg d}t  }t }y| j||d||d| W n tk
rP   Y nX dS )zJExecute write commands with OP_MSG and w=0 WriteConcern, ordered.
        r   )r;   r=   r5   r8   r9   r:   r6   r7   NF)r   r   r   r   )r'   r   r   rD   r   r   r(   r(   r)   execute_command_no_results  s     z _Bulk.execute_command_no_resultsc             C   s  | j rtd| jrtd| jr*td| jrB|jdkrBtd|jdkrj| jr^| j||S | j	||S | j
}tt| jd}t }t|}x|r|}t|d}| jo|dk	}y|jtkr| j|||| n|jtkrHx|jD ]\}	|	d	 }
d
}|
rtt|
jdrd}|j||	d |
|	d ||	d ||| j| jd
 qW n2x0|jD ]&}	|j||	d |	d  ||| j qPW W q tk
r   | jrP Y qX qW dS )z<Execute all operations, returning no results (w=0).
        z3Collation is unsupported for unacknowledged writes.z6arrayFilters is unsupported for unacknowledged writes.z.hint is unsupported for unacknowledged writes.r{   zGCannot set bypass_document_validation with unacknowledged write concernrx   )r   Nrh   T$Frg   rj   ri   )r   r   rU   rW   rq   )rX   r   rY   rZ   rW   r~   r   rU   r   r   rT   r   r   r   r   r#   r   r   r   r%   iter
startswith_update_delete)r'   r   r   Zcollr   r   Znext_runrC   Z	needs_ackr.   rG   Z
check_keysr(   r(   r)   execute_no_results  sl    



z_Bulk.execute_no_resultsc          
   C   s   | j std| jrtdd| _|p,| jj}t||}| jrH| j }n| j }| jj	j
}|js|j|}| j|| W dQ R X n| j|||S dS )zExecute operations.
        zNo operations to executez*Bulk operations can only be executed once.TN)r%   r   rV   rT   r   r   rU   ru   rw   r`   ra   r   Z_socket_for_writesr   r   )r'   r   r   r   ra   r   r(   r(   r)   r     s    


z_Bulk.execute)FFNNN)FNN)NN)r0   r1   r2   r3   r*   propertyrb   rf   ro   rp   rs   ru   rw   r   r   r   r   r   r   r   r(   r(   r(   r)   rO      s$   	 
 

LFrO   c               @   s4   e Zd ZdZdZdd Zdd Zd	d
 Zdd ZdS )BulkUpsertOperationz/An interface for adding upsert operations.
    
__selector__bulk__collationc             C   s   || _ || _|| _d S )N)_BulkUpsertOperation__selector_BulkUpsertOperation__bulk_BulkUpsertOperation__collation)r'   rm   bulkrk   r(   r(   r)   r*     s    zBulkUpsertOperation.__init__c             C   s   | j j| j|dd| jd dS )zUpdate one document matching the selector.

        :Parameters:
          - `update` (dict): the update operations to apply
        FT)ri   rj   rk   N)r   ro   r   r   )r'   r   r(   r(   r)   
update_one#  s    
zBulkUpsertOperation.update_onec             C   s   | j j| j|dd| jd dS )zUpdate all documents matching the selector.

        :Parameters:
          - `update` (dict): the update operations to apply
        T)ri   rj   rk   N)r   ro   r   r   )r'   r   r(   r(   r)   r   -  s    
zBulkUpsertOperation.updatec             C   s   | j j| j|d| jd dS )zReplace one entire document matching the selector criteria.

        :Parameters:
          - `replacement` (dict): the replacement document
        T)rj   rk   N)r   rp   r   r   )r'   rH   r(   r(   r)   replace_one7  s    zBulkUpsertOperation.replace_oneN)r   r   r   )	r0   r1   r2   r3   	__slots__r*   r   r   r   r(   r(   r(   r)   r     s   

r   c               @   sL   e Zd ZdZdZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )BulkWriteOperationz9An interface for adding update or remove operations.
    r   r   r   c             C   s   || _ || _|| _d S )N)_BulkWriteOperation__selector_BulkWriteOperation__bulk_BulkWriteOperation__collation)r'   rm   r   rk   r(   r(   r)   r*   G  s    zBulkWriteOperation.__init__c             C   s   | j j| j|d| jd dS )zUpdate one document matching the selector criteria.

        :Parameters:
          - `update` (dict): the update operations to apply
        F)ri   rk   N)r   ro   r   r   )r'   r   r(   r(   r)   r   L  s    zBulkWriteOperation.update_onec             C   s   | j j| j|d| jd dS )zUpdate all documents matching the selector criteria.

        :Parameters:
          - `update` (dict): the update operations to apply
        T)ri   rk   N)r   ro   r   r   )r'   r   r(   r(   r)   r   U  s    zBulkWriteOperation.updatec             C   s   | j j| j|| jd dS )zReplace one entire document matching the selector criteria.

        :Parameters:
          - `replacement` (dict): the replacement document
        )rk   N)r   rp   r   r   )r'   rH   r(   r(   r)   r   ^  s    zBulkWriteOperation.replace_onec             C   s   | j j| jt| jd dS )zARemove a single document matching the selector criteria.
        )rk   N)r   rs   r   _DELETE_ONEr   )r'   r(   r(   r)   
remove_oneg  s    zBulkWriteOperation.remove_onec             C   s   | j j| jt| jd dS )z=Remove all documents matching the selector criteria.
        )rk   N)r   rs   r   rr   r   )r'   r(   r(   r)   removem  s    zBulkWriteOperation.removec             C   s   t | j| j| jS )zSpecify that all chained update operations should be
        upserts.

        :Returns:
          - A :class:`BulkUpsertOperation` instance, used to add
            update operations to this bulk operation.
        )r   r   r   r   )r'   r(   r(   r)   rj   s  s    
zBulkWriteOperation.upsertN)r   r   r   )r0   r1   r2   r3   r   r*   r   r   r   r   r   rj   r(   r(   r(   r)   r   A  s   			r   c               @   s:   e Zd ZdZdZdddZddd	Zd
d ZdddZdS )BulkOperationBuilderzL**DEPRECATED**: An interface for executing a batch of write operations.
    r   TFc             C   s   t |||| _dS )a(  **DEPRECATED**: Initialize a new BulkOperationBuilder instance.

        :Parameters:
          - `collection`: A :class:`~pymongo.collection.Collection` instance.
          - `ordered` (optional): If ``True`` all operations will be executed
            serially, in the order provided, and the entire execution will
            abort on the first error. If ``False`` operations will be executed
            in arbitrary order (possibly in parallel on the server), reporting
            any errors that occurred after attempting all operations. Defaults
            to ``True``.
          - `bypass_document_validation`: (optional) If ``True``, allows the
            write to opt-out of document level validation. Default is
            ``False``.

        .. note:: `bypass_document_validation` requires server version
          **>= 3.2**

        .. versionchanged:: 3.5
           Deprecated. Use :meth:`~pymongo.collection.Collection.bulk_write`
           instead.

        .. versionchanged:: 3.2
          Added bypass_document_validation support
        N)rO   _BulkOperationBuilder__bulk)r'   rT   rU   r_   r(   r(   r)   r*     s    zBulkOperationBuilder.__init__Nc             C   s   t d| t|| j|S )a;  Specify selection criteria for bulk operations.

        :Parameters:
          - `selector` (dict): the selection criteria for update
            and remove operations.
          - `collation` (optional): An instance of
            :class:`~pymongo.collation.Collation`. This option is only
            supported on MongoDB 3.4 and above.

        :Returns:
          - A :class:`BulkWriteOperation` instance, used to add
            update and remove operations to this bulk operation.

        .. versionchanged:: 3.4
           Added the `collation` option.

        rm   )r   r   r   )r'   rm   rk   r(   r(   r)   find  s    
zBulkOperationBuilder.findc             C   s   | j j| dS )zInsert a single document.

        :Parameters:
          - `document` (dict): the document to insert

        .. seealso:: :ref:`writes-and-ids`
        N)r   rf   )r'   rc   r(   r(   r)   r     s    zBulkOperationBuilder.insertc             C   s"   |dk	rt f |}| jj|ddS )zExecute all provided operations.

        :Parameters:
          - write_concern (optional): the write concern for this bulk
            execution.
        N)r   )r   r   r   )r'   r   r(   r(   r)   r     s    
zBulkOperationBuilder.execute)TF)N)N)	r0   r1   r2   r3   r   r*   r   r   r   r(   r(   r(   r)   r     s    


r   )r   r   r    )5r3   rA   	itertoolsr   Zbson.objectidr   Zbson.raw_bsonr   Zbson.sonr   Zpymongo.client_sessionr   Zpymongo.commonr   r   r	   r
   Zpymongo.helpersr   Zpymongo.collationr   Zpymongo.errorsr   r   r   r   Zpymongo.messager   r   r   r   r   r   r   Zpymongo.read_preferencesr   Zpymongo.write_concernr   rr   r   Z
_BAD_VALUEZ_UNKNOWN_ERRORZ_WRITE_CONCERN_ERRORr   rB   objectr"   rI   rN   rO   r   r   r   r(   r(   r(   r)   <module>   s<   $(	   )>