3
O6br                 @   s  d Z ddlZddlZddlZddlZddlmZmZ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 ddlmZmZ ddlmZ ydd	lmZ d
ZW n ek
r   dZY nX ddlmZ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dl)m*Z* ddl+m,Z, dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8dZ9e0de1de2diZ:d d!d"d#Z;d$Z<ed%d&Z=d'd( Z>d)d* Z?d+d, Z@d-d. ZAedddddgZBeddddddddddddgZCddOdPZDdQdR ZEG dSdT dTeFZGG dUdV dVeFZHG dWdX dXeGZIG dYdZ dZeHZJG d[d\ d\eKZLejMd]jNZOd^ZPd_d` ZQdadb ZRejMdcjNZSddde ZTejMdfjNZUdgdh ZVdidj ZWdkdl ZXerejYZXddmdnZZdodp Z[dqdr Z\dsdt Z]erej^Z]ddudvZ_ejMdwjNZ`ejMdxjNZadydz Zbd{d| Zcd}d~ ZderejeZddddZedd ZfdddZgdddZher.ejiZhdddZjejMdjNZkdd Zldd Zmdd ZnerhejoZndddZpdd Zqdd ZrdddZsdddZtdd ZuG dd deFZvdZwG dd devZxdd Zydd ZzerejzZze0de1de2diZ{dd Z|dd Z}er
ej}Z}dd Z~dd Zer&ejZdd Zdd Zdd ZerJejZdd Zer^ejZdd Zdd Zdd ZG dd deFZG dd deFZejejejejiZddÄ ZdS )a>  **DEPRECATED** Tools for creating `messages
<http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol>`_ to be sent to
MongoDB.

.. note:: This module is for internal use and is generally not needed by
   application developers.

.. versionchanged:: 3.12
  This module is deprecated and will be removed in PyMongo 4.0.
    N)CodecOptionsdecodeencode_decode_selective_dict_to_bson_make_c_string)DEFAULT_CODEC_OPTIONS)Int64)_inflate_bsonDEFAULT_RAW_BSON_OPTIONSRawBSONDocument)bStringIO)SON)	_cmessageTF)ConfigurationErrorCursorNotFoundDocumentTooLargeExecutionTimeoutInvalidOperationNotPrimaryErrorOperationFailureProtocolError)HelloCompat)DEFAULT_READ_CONCERN)ReadPreference)WriteConcernil        i?                   s     s       s           s       s   documents     s   updates     s   deletes     	documentsupdatesZdeletes)insertupdatedeletez%s.%sreplace)Zunicode_decode_error_handlerc               C   s   t jttS )z(Generate a pseudo random 32 bit integer.)randomrandint	MIN_INT32	MAX_INT32 r,   r,   N/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/message.py_randinta   s    r.   c             C   sJ   |j }|j}|rF|tjj ks(t|dkrFd| kr>td| fg} || d< | S )z-Add $readPreference to spec when appropriate.r   z$queryz$readPreference)modedocumentr   ZSECONDARY_PREFERREDlenr   )specread_preferencer/   r0   r,   r,   r-   _maybe_add_read_preferencef   s    r4   c             C   s   t | | jjdS )z<Convert an Exception into a failure document for publishing.)errmsgZerrtype)str	__class____name__)	exceptionr,   r,   r-   _convert_exceptionx   s    r:   c       	      C   s  |j dd}d|d}|j d|j dd}|r|j drN|d	dd
id|d< n6d|j dd|d}d|krv|d |d< |g|d< |S | dkrt|d |d< nv| dkrd|krd|d dg|d< nN|j ddko|dkr|d d }|d j d|d j d}d|dg|d< |S )z6Convert a legacy write result to write command format.nr   r   )okr;   r5   err Zwtimeout@   T)r5   codeerrInfoZwriteConcernErrorr@      )indexr@   r5   rA   ZwriteErrorsr$   r"   r%   Zupserted)rC   _idZupdatedExistingFr#   urD   q)getr1   )		operationcommandresultZaffectedresr5   errorr%   rD   r,   r,   r-   _convert_write_result~   s2    




rM   tailableoplogReplayrB   noCursorTimeout   	awaitData    allowPartialResults   $queryfilter$orderbysort$hinthint$commentcomment$maxScanmaxScan
$maxTimeMS	maxTimeMS$maxmax$minmin
$returnKey	returnKey$showRecordIdshowRecordId$showDiskLoc	$snapshotsnapshotc                s   t d| fg}d|krT|jdd |j D  d|kr@|jd d|kr\|jd n||d< |rh||d< |rt||d	< |rt||d
< |dk rd|d< |r||d< |jr|	o|	j r|j|d< |r||d< |
dk	r|
|d<  r|j fddtj D  |S )z!Generate a find command document.findz$queryc             S   s,   g | ]$\}}|t kr t | |fn||fqS r,   )
_MODIFIERS).0keyvalr,   r,   r-   
<listcomp>   s   z%_gen_find_command.<locals>.<listcomp>z$explainz$readPreferencerW   
projectionskiplimitr   TZsingleBatch	batchSizeZreadConcern	collationNZallowDiskUsec                s    g | ]\}} |@ r|d fqS )Tr,   )ro   optrq   )optionsr,   r-   rr      s   )	r   r%   itemspopabslevelin_transactionr0   _OPTIONS)collr2   rs   rt   ru   
batch_sizery   read_concernrw   sessionallow_disk_usecmdr,   )ry   r-   _gen_find_command   s:    


r   c             C   s4   t d| fd|fg}|r ||d< |dk	r0||d< |S )z$Generate a getMore command document.getMoreZ
collectionrv   Nra   )r   )	cursor_idr   r   max_await_time_msr   r,   r,   r-   _gen_get_more_command   s    r   c               @   sF   e Zd ZdZd ZdZdZdd Zdd Zdd Z	dd Z
d!ddZdS )"_QueryzA query operation.flagsdbr   ntoskipr2   fieldscodec_optionsr3   ru   r   namer   rw   r   clientr   _as_commandexhaustNc             C   sp   || _ || _|| _|| _|| _|| _|| _|| _|| _|	| _	|
| _
|| _|| _|| _|| _d| _d | _|| _d S )Nrm   )r   r   r   r   r2   r   r   r3   r   ru   r   rw   r   r   r   r   r   r   )selfr   r   r   r   r2   r   r   r3   ru   r   r   rw   r   r   r   r   r,   r,   r-   __init__   s$    z_Query.__init__c             C   s   t | j| jf S )N)_UJOINr   r   )r   r,   r,   r-   	namespace  s    z_Query.namespacec             C   s   d}|j dkr| j rd}n.|j dkr,d}n| jjsJtd| jj|j f |j dk rn| jd k	rntd|j f |j dk r| jd k	rtd|j f |j| j	| j
 |S )	NF   TrB   zDread concern level of %s is not valid with a max wire version of %d.   zDSpecifying a collation is unsupported with a max wire version of %d.zESpecifying allowDiskUse is unsupported with a max wire version of %d.)max_wire_versionr   r   Zok_for_legacyr   r}   rw   r   validate_sessionr   r   )r   	sock_infoZuse_find_cmdr,   r,   r-   use_command  s(    
z_Query.use_commandc             C   s   | j dk	r| j S d| jk}t| j| j| j| j| j| j| j| j	| j
| j| j}|rdd| _td|fg}| j}|j| |r|j|d| j| | r|j r|j|| |j||| j | j}|jr|jj r|jj| j|d| j}|| jf| _ | j S )z.Return a find command document for this query.Nz$explainexplainF)r   r2   r   r   r   r   ru   r   r   r   rw   r   r   r   r   add_server_api	_apply_tor3   r~   Z_update_read_concernsend_cluster_timer   
_encrypter_bypass_auto_encryptionencryptr   r   )r   r   r   r   r   r   r,   r,   r-   
as_command1  s0    



z_Query.as_commandFc          
   C   s   |r| j dB }n| j }| j }| j}|r| j|d }|jrntd|| j| j|d| j|j	d\}}}	}
|||	fS t
| jdf }d	}n2| jdkrdp| j}| jr|rt| j|}n| j}|jrt|| j}t||| j|||rdn| j| j|j	dS )
z:Get a query message, possibly setting the secondaryOk bit.r   r   F)ctxz$cmdr   r   N)r   r   r2   r   op_msg_enabled_op_msgr   r3   r   compression_contextr   r   ru   re   Z	is_mongosr4   queryr   r   )r   Zset_secondary_okr   use_cmdr   nsr2   
request_idmsgsize_	ntoreturnr,   r,   r-   get_messageQ  s4    
z_Query.get_message)r   r   r   r   r2   r   r   r3   ru   r   r   r   rw   r   r   r   r   r   )F)r8   
__module____qualname____doc__	__slots__sock_mgrr   r   r   r   r   r   r,   r,   r,   r-   r      s        r   c               @   sB   e Zd ZdZdZdZdd Zdd Zdd Zdd Z	dddZ
dS )_GetMorezA getmore operation.r   r   r   r   r   r   r3   r   r   r   r   r   r   c             C   sL   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	d | _
|| _d S )N)r   r   r   r   r   r3   r   r   r   r   r   r   )r   r   r   r   r   r   r3   r   r   r   r   r   r,   r,   r-   r     s    z_GetMore.__init__c             C   s   t | j| jf S )N)r   r   r   )r   r,   r,   r-   r     s    z_GetMore.namespacec             C   s>   d}|j dkr| j rd}n|j dkr*d}|j| j| j |S )NFr   TrB   )r   r   r   r   r   )r   r   r   r,   r,   r-   r     s    
z_GetMore.use_commandc             C   s   | j dk	r| j S t| j| j| j| j}| jr@| jj|d| j| |j	| |j
|| j| j | j}|jr|jj r|jj| j|d| j}|| jf| _ | j S )z1Return a getMore command document for this query.NF)r   r   r   r   r   r   r   r   r3   r   r   r   r   r   r   r   r   )r   r   r   r   r,   r,   r-   r     s     



z_GetMore.as_commandFc          
   C   s   | j  }|j}|r| j|d }|jrh| jr4tj}nd}t||| jddd| j	|jd\}}	}
}||	|
fS t
| jdf }td|dd|d| j	|dS t|| j| j|S )zGet a getmore message.r   NF)r   z$cmdr   r   )r   r   r   r   r   _OpMsgEXHAUST_ALLOWEDr   r   r   r   r   get_morer   r   )r   Zdummy0r   r   r   r   r2   r   r   r   r   r   r,   r,   r-   r     s     

z_GetMore.get_messageN)r   r   r   r   r   r   r3   r   r   r   r   r   )F)r8   r   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   r   z  s     r   c                   s   e Zd Z fddZ  ZS )_RawBatchQueryc                s4   t t| j| |jdkrdS |jr0| j r0dS dS )NrB   TF)superr   r   r   r   r   )r   r   )r7   r,   r-   r     s    
z_RawBatchQuery.use_command)r8   r   r   r   __classcell__r,   r,   )r7   r-   r     s   r   c                   s   e Zd Z fddZ  ZS )_RawBatchGetMorec                s4   t t| j| |jdkrdS |jr0| j r0dS dS )NrB   TF)r   r   r   r   r   r   )r   r   )r7   r,   r-   r     s    
z_RawBatchGetMore.use_command)r8   r   r   r   r   r,   r,   )r7   r-   r     s   r   c               @   s<   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dS )_CursorAddresszEThe server address (host, port) of a cursor, with namespace property.c             C   s   t j| |}||_|S )N)tuple__new___CursorAddress__namespace)clsaddressr   r   r,   r,   r-   r     s    z_CursorAddress.__new__c             C   s   | j S )zThe namespace this cursor.)r   )r   r,   r,   r-   r     s    z_CursorAddress.namespacec             C   s   | | j f j S )N)r   __hash__)r   r,   r,   r-   r     s    z_CursorAddress.__hash__c             C   s*   t |tr&t| t|ko$| j|jkS tS )N)
isinstancer   r   r   NotImplemented)r   otherr,   r,   r-   __eq__  s    
z_CursorAddress.__eq__c             C   s
   | |k S )Nr,   )r   r   r,   r,   r-   __ne__   s    z_CursorAddress.__ne__N)
r8   r   r   r   r   propertyr   r   r   r   r,   r,   r,   r-   r     s   r   z<iiiiiiB   c             C   s>   |j |}t }ttt| |dd| t||j}||| fS )zDTakes message data, compresses it, and adds an OP_COMPRESSED header.r   i  )compressr.   _pack_compression_header_COMPRESSION_HEADER_SIZEr1   Zcompressor_id)rH   datar   
compressedr   headerr,   r,   r-   	_compress  s    

r   c             C   s<   t dg}|j| | jdd}td|d d dd|dtS )	z$Data to send to do a lastError.
    getlasterrorr   .r   z.$cmdN)r   r   r   )r   r%   splitr   r   )r   argsr   Zsplitnsr,   r,   r-   __last_error  s
    

r   z<iiiic             C   s(   t  }tdt| |d| }||| fS )ztTakes message data and adds a message header based on the operation.

    Returns the resultant message string.
    rQ   r   )r.   _pack_headerr1   )rH   r   ridmessager,   r,   r-   __pack_message$  s    r   z<ic                s   t t|dkr<|d  }djdt| |gt|fS  fdd|D }|s^tddjt|t| dj|gttt|fS )zGet an OP_INSERT messager   r   r   s       c                s   g | ]}| qS r,   r,   )ro   doc)
check_keysr   optsr,   r-   rr   ;  s    z_insert.<locals>.<listcomp>zcannot do an empty bulk insert)r   r1   joinr   r   	_pack_intrc   map)collection_namedocsr   r   r   encodedr,   )r   r   r   r-   _insert1  s    r   c       
      C   s.   t | ||||\}}td||\}}	||	|fS )z9Internal compressed unacknowledged insert message helper.i  )r   r   )
r   r   r   continue_on_errorr   r   	op_insertmax_bson_sizer   r   r,   r,   r-   _insert_compressedD  s    r   c             C   sN   t | ||||\}}td|\}	}
|rDt| |\}	}}|	|
| |fS |	|
|fS )zInternal insert message helper.i  )r   r   r   )r   r   r   safelast_error_argsr   r   r   r   r   r   gler   r,   r,   r-   _insert_uncompressedM  s    r   c             C   s*   |rt | |||||S t| ||||||S )z**DEPRECATED** Get an **insert** message.

    .. versionchanged:: 3.12
      This function is deprecated and will be removed in PyMongo 4.0.
    )r   r   )r   r   r   r   r   r   r   r   r,   r,   r-   r$   [  s
    
r$   c       
      C   sX   d}|r|d7 }|r|d7 }t }||||}	djtt| t|||d||	gt|	fS )zGet an OP_UPDATE message.r   r   r   r   F)r   r   _ZERO_32r   r   r1   )
r   upsertmultir2   r   r   r   r   r   Zencoded_updater,   r,   r-   _updatei  s    
r   c             C   s2   t | ||||||\}}	td||\}
}|
||	fS )z9Internal compressed unacknowledged update message helper.i  )r   r   )r   r   r   r2   r   r   r   r   	op_updater   r   r   r,   r,   r-   _update_compressedz  s    r   c	             C   sR   t | ||||||\}	}
td|	\}}|rHt| |\}}}||| |
fS |||
fS )zInternal update message helper.i  )r   r   r   )r   r   r   r2   r   r   r   r   r   r   r   r   r   r   r   r,   r,   r-   _update_uncompressed  s    r   c
       
   
   C   s2   |	rt | |||||||	S t| ||||||||	S )z**DEPRECATED** Get an **update** message.

    .. versionchanged:: 3.12
      This function is deprecated and will be removed in PyMongo 4.0.
    )r   r   )
r   r   r   r2   r   r   r   r   r   r   r,   r,   r-   r%     s
    
r%   z<IBz<Bc                s   t |d}t| d}t|}d}	|rtd}
t|} fdd|D }t|tdd |D  d }t|}||7 }td	d |D }	|||
||g| }n||g}d
j|||	fS )zGet a OP_MSG message.

    Note: this method handles multiple documents in a type one payload but
    it does not perform batch splitting and the total message size is
    only checked *after* generating the entire message.
    Fr   r   c                s   g | ]}t | qS r,   )r   )ro   r   )r   r   r,   r-   rr     s    z%_op_msg_no_header.<locals>.<listcomp>c             s   s   | ]}t |V  qd S )N)r1   )ro   r   r,   r,   r-   	<genexpr>  s    z$_op_msg_no_header.<locals>.<genexpr>r   c             s   s   | ]}t |V  qd S )N)r1   )ro   r   r,   r,   r-   r     s    r   )	r   _pack_op_msg_flags_typer1   
_pack_byter   sumr   rc   r   )r   rI   
identifierr   r   r   r   Z
flags_type
total_sizemax_doc_sizeZtype_oneZcstringZencoded_docsr   Zencoded_sizer   r,   )r   r   r-   _op_msg_no_header  s     
r   c             C   s4   t | |||||\}}}	td||\}
}|
|||	fS )zInternal OP_MSG message helper.i  )r   r   )r   rI   r   r   r   r   r   r   r   r   r   r,   r,   r-   _op_msg_compressed  s    r  c             C   s2   t | |||||\}}}td|\}	}
|	|
||fS )z*Internal compressed OP_MSG message helper.i  )r   r   )r   rI   r   r   r   r   r   r   r   r   Z
op_messager,   r,   r-   _op_msg_uncompressed  s    r  c             C   s   ||d< |dk	r<d|kr<|r2|j  r2tjj|d< n
|j|d< tt|}ytj|}	|j|	}
W n t	k
r|   d}	d}
Y nX z*|rt
| ||	|
|||S t| ||	|
||S |	r|
||	< X dS )zGet a OP_MSG message.z$dbNz$readPreferencer>   )r/   r   ZPRIMARY_PREFERREDr0   nextiter
_FIELD_MAPrG   r{   KeyErrorr  r  )r   rI   Zdbnamer3   secondary_okr   r   r   r   r   r   r,   r,   r-   r     s(    


r   c             C   s^   t |||}|rt |d|}	nd}	tt|t|	}
djt| t|t|t|||	g|
fS )zGet an OP_QUERY message.Fr   )r   rc   r1   r   r   r   )ry   r   num_to_skipnum_to_returnr   field_selectorr   r   r   Zefsr   r,   r,   r-   _query  s    r  c	          	   C   s4   t | |||||||\}	}
td|	|\}}|||
fS )z)Internal compressed query message helper.i  )r  r   )ry   r   r  r	  r   r
  r   r   r   op_queryr   r   r   r,   r,   r-   _query_compressed  s    
r  c          	   C   s2   t | |||||||\}}	td|\}
}|
||	fS )zInternal query message helper.i  )r  r   )ry   r   r  r	  r   r
  r   r   r  r   r   r   r,   r,   r-   _query_uncompressed  s    
r  c	       	   
   C   s2   |rt | ||||||||	S t| |||||||S )z**DEPRECATED** Get a **query** message.

    .. versionchanged:: 3.12
      This function is deprecated and will be removed in PyMongo 4.0.
    )r  r  )	ry   r   r  r	  r   r
  r   r   r   r,   r,   r-   r   $  s    
r   z<qc             C   s   dj tt| t|t|gS )zGet an OP_GET_MORE message.r   )r   r   r   r   _pack_long_long)r   r	  r   r,   r,   r-   	_get_more7  s
    r  c             C   s   t dt| |||S )z+Internal compressed getMore message helper.i  )r   r  )r   r	  r   r   r,   r,   r-   _get_more_compressed@  s    r  c             C   s   t dt| ||S )z Internal getMore message helper.i  )r   r  )r   r	  r   r,   r,   r-   _get_more_uncompressedF  s    r  c             C   s   |rt | |||S t| ||S )z**DEPRECATED** Get a **getMore** message.

    .. versionchanged:: 3.12
      This function is deprecated and will be removed in PyMongo 4.0.
    )r  r  )r   r	  r   r   r,   r,   r-   r   N  s    r   c             C   s.   t |d|}djtt| t||gt|fS )zGet an OP_DELETE message.Fr   )r   r   r   r   r   r1   )r   r2   r   r   r   r,   r,   r-   _deleteZ  s    r  c       	      C   s,   t | |||\}}td||\}}|||fS )z9Internal compressed unacknowledged delete message helper.i  )r  r   )	r   r2   r   r   r   	op_deleter   r   r   r,   r,   r-   _delete_compressedd  s    r  c             C   sL   t | |||\}}td|\}}	|rBt| |\}}
}||	|
 |fS ||	|fS )zInternal delete message helper.i  )r  r   r   )r   r2   r   r   r   r   r  r   r   r   r   r   r,   r,   r-   _delete_uncompressedk  s    r  c             C   s&   |rt | ||||S t| |||||S )aS  **DEPRECATED** Get a **delete** message.

    `opts` is a CodecOptions. `flags` is a bit vector that may contain
    the SingleRemove flag or not:

    http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-delete

    .. versionchanged:: 3.12
      This function is deprecated and will be removed in PyMongo 4.0.
    )r  r  )r   r2   r   r   r   r   r   r,   r,   r-   r&   v  s    r&   c             C   s6   t | }tjdd|  j}|d|f|  }td|S )z**DEPRECATED** Get a **killCursors** message.

    .. versionchanged:: 3.12
      This function is deprecated and will be removed in PyMongo 4.0.
    z<iirF   r   i  )r1   structStructpackr   )Z
cursor_idsZnum_cursorsr  Zop_kill_cursorsr,   r,   r-   kill_cursors  s    r  c               @   s   e Zd ZdZd.Zd/ddZdd Zdd Zdd Ze	dd Z
e	dd Ze	dd Ze	dd Ze	d d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- ZdS )0_BulkWriteContextzCA wrapper around SocketInfo for use with write splitting functions.db_namer   op_idr   fieldpublish
start_time	listenersr   r   op_typecodec
cmd_legacyNc
       
      C   st   || _ || _|| _|| _|j| _|| _t| j | _| jrBt	j	j
 nd | _|| _|jrXdnd| _|| _|| _|	| _d S )NTF)r  r   r  r!  enabled_for_commandsr  r   r  r  datetimenowr   r   r   r   r"  r#  r$  )
r   Zdatabase_namecmd_namer   Zoperation_idr!  r   r"  r#  r$  r,   r,   r-   r     s    z_BulkWriteContext.__init__c             C   s@   | j d }t|| j||| j| j| \}}}|s6td|||fS )Nz.$cmdzcannot do an empty bulk write)r  _do_bulk_write_commandr"  r   r#  r   )r   r   r   r   r   r   to_sendr,   r,   r-   _batch_command  s    
z _BulkWriteContext._batch_commandc             C   s8   | j ||\}}}| j||||}|j|| j ||fS )N)r+  write_commandZ_process_responser   )r   r   r   r   r   r   r*  rJ   r,   r,   r-   execute  s    z_BulkWriteContext.executec             C   s*   | j ||\}}}| j|||dd| |S )Nr   F)r+  legacy_write)r   r   r   r   r   r   r*  r,   r,   r-   execute_unack  s    z_BulkWriteContext.execute_unackc             C   s   dS )z-Should we check keys for this operation type?Fr,   )r   r,   r,   r-   r     s    z_BulkWriteContext.check_keysc             C   s   | j jS )z#A proxy for SockInfo.max_bson_size.)r   r   )r   r,   r,   r-   r     s    z_BulkWriteContext.max_bson_sizec             C   s   | j r| jjd S | jjS )z&A proxy for SockInfo.max_message_size.rQ   )r   r   max_message_size)r   r,   r,   r-   r0    s    z"_BulkWriteContext.max_message_sizec             C   s   | j jS )z*A proxy for SockInfo.max_write_batch_size.)r   max_write_batch_size)r   r,   r,   r-   r1    s    z&_BulkWriteContext.max_write_batch_sizec             C   s   | j S )z:The maximum size of a BSON command before batch splitting.)r   )r   r,   r,   r-   max_split_size  s    z _BulkWriteContext.max_split_sizec             C   s2   |rt d|| jj\}}| j| jj |||||S )Ni  )r   r   r   r.  r$  copy)r   r   r   r   acknowledgedr   r   r,   r,   r-   legacy_bulk_insert  s    z$_BulkWriteContext.legacy_bulk_insertc             C   s&  | j r.tjj | j }| j|||}tjj }zy\| jj||||}	| j rtjj | | }|	dk	rtt| j||	}
nddi}
| j	||
| W n t
k
r } zd| j rtjj | | }t|trt| j||j}nt|tr|j}nt|}| j|||  W Y dd}~X nX W dtjj | _X |	S )zKA proxy for SocketInfo.legacy_write that handles event publishing.
        Nr<   r   )r  r&  r'  r   _startr   r.  rM   r   _succeed	Exceptionr   r   detailsr   r:   _fail)r   r   r   r   r   r4  r   durationstartrJ   replyexcfailurer,   r,   r-   r.    s4    


z_BulkWriteContext.legacy_writec       
      C   s   | j r.tjj | j }| j||| tjj }zy8| jj||}| j rftjj | | }| j||| W nh tk
r } zL| j rtjj | | }t	|t
tfr|j}	nt|}	| j||	|  W Y dd}~X nX W dtjj | _X |S )zLA proxy for SocketInfo.write_command that handles event publishing.
        N)r  r&  r'  r   r6  r   r,  r7  r8  r   r   r   r9  r:   r:  )
r   r   r   r   r   r;  r<  r=  r>  r?  r,   r,   r-   r,    s&    
z_BulkWriteContext.write_commandc             C   s0   ||| j < | jj|| j|| jj| j| jj |S )zPublish a CommandStartedEvent.)r  r!  publish_command_startr  r   r   r  
service_id)r   r   r   r   r,   r,   r-   r6  %  s    

z_BulkWriteContext._startc             C   s(   | j j||| j|| jj| j| jj dS )z Publish a CommandSucceededEvent.N)r!  publish_command_successr   r   r   r  rA  )r   r   r=  r;  r,   r,   r-   r7  .  s    z_BulkWriteContext._succeedc             C   s(   | j j||| j|| jj| j| jj dS )zPublish a CommandFailedEvent.N)r!  publish_command_failurer   r   r   r  rA  )r   r   r?  r;  r,   r,   r-   r:  5  s    z_BulkWriteContext._fail)r  r   r  r   r  r  r   r!  r   r   r"  r#  r$  )N)r8   r   r   r   r   r   r+  r-  r/  r   r   r   r0  r1  r2  r5  r.  r,  r6  r7  r:  r,   r,   r,   r-   r    s&     
	
	#	r  i    c               @   s4   e Zd Zf Zdd Zdd Zdd Zedd Zd	S )
_EncryptedBulkWriteContextc             C   sb   | j d }t|| j||| j| j| \}}|s4td|jddd }tt||d  t	}||fS )Nz.$cmdzcannot do an empty bulk writer!   r   	   )
r  _encode_batched_write_commandr"  r   r#  r   rC   r
   
memoryviewr   )r   r   r   r   r   r*  Z	cmd_startr,   r,   r-   r+  G  s    
z)_EncryptedBulkWriteContext._batch_commandc             C   s2   | j ||\}}| jj| j|t| j|d}||fS )N)r   r   r   )r+  r   rI   r  _UNICODE_REPLACE_CODEC_OPTIONSr   )r   r   r   r   batched_cmdr*  rJ   r,   r,   r-   r-  U  s    z"_EncryptedBulkWriteContext.executec             C   s4   | j ||\}}| jj| j|tdd| j|d |S )Nr   )w)Zwrite_concernr   r   )r+  r   rI   r  r   r   )r   r   r   r   rI  r*  r,   r,   r-   r/  ]  s
    z(_EncryptedBulkWriteContext.execute_unackc             C   s   t S )z Reduce the batch splitting size.)_MAX_SPLIT_SIZE_ENC)r   r,   r,   r-   r2  d  s    z)_EncryptedBulkWriteContext.max_split_sizeN)	r8   r   r   r   r+  r-  r/  r   r2  r,   r,   r,   r-   rD  D  s
   rD  c             C   s,   | dkrt d||f nt d| f dS )z-Internal helper for raising DocumentTooLarge.r$   zfBSON document too large (%d bytes) - the connected server supports BSON document sizes up to %d bytes.z%r command document too largeN)r   )rH   Zdoc_sizemax_sizer,   r,   r-   _raise_document_too_largej  s    rM  c                s   fdd}|p| }	d}
t  }|jtjdt| |jt  |j  }}d}g }t}|jol|pj|	 }x|D ]}||||}t	|}||j
k}||7 }||jk r| r|j| |j| d}qv|rNy>|rd|j  }}n||j |	\}}|j||d|	|| W n< tk
rL } z|r0|}
n|s:dS  W Y dd}~X nX |rbtd||j
 || }|j| |j  |j| |g}qvW |std	|rd|j  }}n||j |\}}|j||d||| |
dk	r|
dS )
z*Insert `docs` using multiple batches.
    c                s2   t d| \}}|r*t \}}}||7 }||fS )z6Build the insert message with header and GLE.
        i  )r   r   )Zinsert_message	send_safer   Zfinal_messageerror_messager   )r   r   r,   r-   _insert_message|  s    z+_do_batched_insert.<locals>._insert_messageNz<iFTr   r$   zcannot do an empty bulk insert)r   writer  r  intr   tellr   r   r1   r   r0  appendgetvaluer5  r   rM  seektruncater   )r   r   r   r   r   r   r   r   rP  rN  Z
last_errorr   Zmessage_lengthZ	begin_locZhas_docsr*  r   r   r   r   Zencoded_lengthZ	too_larger   r   r>  r   r,   )r   r   r-   _do_batched_insertw  sd    








rX  s
   documents s   updates s   deletes c             C   s|  |j }|j}	|j}
|rdnd}|j| |jd |jt|d| |jd |j }|jd y|jt|   W n tk
r   tdY nX | t	t
fkrd}g }d}x|D ]}t|||}t|}|j | }|dko||
k}| o||k}|s|rttj |  }t|t|| ||
kr&P |j| |j| |d7 }||	krP qW |j }|j| |jt||  ||fS )	zCreate a batched OP_MSG write.s       s      r!   F   zUnknown commandr   r   )r   r1  r0  rQ  r   rS  _OP_MSG_MAPr  r   _UPDATE_DELETEr1   listr  keysrM  rT  rV  r   )rH   rI   r   r   ackr   r   bufr   r1  r0  r   Zsize_locationr*  idxr   valueZ
doc_lengthZnew_message_sizedoc_too_largeZunacked_doc_too_largewrite_oplengthr,   r,   r-   _batched_op_msg_impl  sN    









rf  c       
   	   C   s,   t  }t| |||||||\}}	|j |fS )zOEncode the next batched insert, update, or delete operation
    as OP_MSG.
    )r   rf  rU  )
rH   rI   r   r   r_  r   r   r`  r*  r   r,   r,   r-   _encode_batched_op_msg  s    rg  c             C   s6   t | ||||||\}}td||jj\}	}
|	|
|fS )z]Create the next batched insert, update, or delete operation
    with OP_MSG, compressed.
    i  )rg  r   r   r   )rH   rI   r   r   r_  r   r   r   r*  r   r   r,   r,   r-   _batched_op_msg_compressed%  s    rh  c          	   C   sx   t  }|jt |jd t| |||||||\}}	|jd t }
|jt|
 |jd |jt|	 |
|j |fS )z"OP_MSG implementation entry point.s         r   r   )r   rQ  _ZERO_64rf  rV  r.   r   rU  )rH   rI   r   r   r_  r   r   r`  r*  re  r   r,   r,   r-   _batched_op_msg4  s    



rj  c             C   sf   | j ddd |d< d|kr2t|d jdd}nd}|jjrRt|||||||S t|||||||S )zRCreate the next batched insert, update, or delete operation
    using OP_MSG.
    r   r   r   z$dbZwriteConcernrJ  T)r   boolrG   r   r   rh  rj  )r   rH   rI   r   r   r   r   r_  r,   r,   r-   _do_batched_op_msgM  s    rl  c             C   s6   t | ||||||\}}td||jj\}	}
|	|
|fS )zKCreate the next batched insert, update, or delete command, compressed.
    i  )rF  r   r   r   )r   rH   rI   r   r   r   r   r   r*  r   r   r,   r,   r-   !_batched_write_command_compresseda  s    rm  c       
   	   C   s,   t  }t| |||||||\}}	|j |fS )z?Encode the next batched insert, update, or delete command.
    )r   _batched_write_command_implrU  )
r   rH   rI   r   r   r   r   r`  r*  r   r,   r,   r-   rF  o  s    rF  c          	   C   sx   t  }|jt |jd t| |||||||\}}	|jd t }
|jt|
 |jd |jt|	 |
|j |fS )z?Create the next batched insert, update, or delete command.
    s         r   r   )r   rQ  ri  rn  rV  r.   r   rU  )r   rH   rI   r   r   r   r   r`  r*  re  r   r,   r,   r-   _batched_write_command|  s    



ro  c             C   s0   |j jrt| ||||||S t| ||||||S )z#Batched write commands entry point.)r   r   rm  ro  )r   rH   rI   r   r   r   r   r,   r,   r-   _do_batched_write_command  s
    rp  c             C   s4   |j jdkr t| ||||||S t| ||||||S )z Bulk write commands entry point.r   )r   r   rl  rp  )r   rH   rI   r   r   r   r   r,   r,   r-   r)    s
    r)  c             C   s  |j }|j}	|t }
|j}|jt |jt|  |jt |jt |j	 }|jt
| |jdd |j  y|jt|  W n tk
r   tdY nX |ttfkrd}|j	 d }g }d}x|D ]}tt|}t
|||}t||
k}|rttj | }t|t|| |dko<|j	 t| t| |k}||	k}|sR|rTP |jt |j| |jt |j| |j| |d7 }qW |jt |j	 }|j| |jt|| d  |j| |jt||  ||fS )z(Create a batched OP_QUERY write command.r   r   zUnknown commandFr   r   r   )r   r1  _COMMAND_OVERHEADr2  rQ  r   r   _ZERO_8_SKIPLIMrS  r   rV  rW  _OP_MAPr  r   r[  r\  r6   r1   r]  r  r^  rM  _BSONOBJrT  _ZERO_16r   )r   rH   rI   r   r   r   r   r`  r   r1  Zmax_cmd_sizer2  Zcommand_startZ
list_startr*  ra  r   rp   rb  rc  rd  Zenough_dataZenough_documentsre  r,   r,   r-   rn    s^    












rn  c               @   sp   e Zd ZdZdZejdjZdZ	dd	 Z
dddZd
ed
dfddZdd Zdd Zedd Zedd Zd
S )_OpReplyz$A MongoDB OP_REPLY response message.r   r   number_returnedr"   z<iqiir   c             C   s    || _ t|| _|| _|| _d S )N)r   r	   r   rx  r"   )r   r   r   rx  r"   r,   r,   r-   r     s    
z_OpReply.__init__Nc             C   s   | j d@ r>|dkrtdd|f }d|dd}t|d|n| j d@ rtj| jj }|jd	d |d
 jt	j
rt|d
 |n&|jddkrt|jd
|jd|td|jd
 |jd|| jr| jgS g S )a  Check the response header from the database, without decoding BSON.

        Check the response for errors and unpack.

        Can raise CursorNotFound, NotPrimaryError, ExecutionTimeout, or
        OperationFailure.

        :Parameters:
          - `cursor_id` (optional): cursor_id we sent to get this response -
            used for raising an informative exception when we get cursor id not
            valid at server response.
        r   Nz"No cursor id for getMore operationzCursor not found, cursor id: %dr   +   )r<   r5   r@   r   r<   z$errr@   2   zdatabase error: %s)r   r   r   bsonZBSONr"   r   
setdefault
startswithr   ZLEGACY_ERRORr   rG   r   r   )r   r   user_fieldsr   ZerrobjZerror_objectr,   r,   r-   raw_response  s,    




z_OpReply.raw_responseFc             C   s,   | j | |rtj| j|S tj| j||S )ae  Unpack a response from the database and decode the BSON document(s).

        Check the response for errors and unpack, returning a dictionary
        containing the response data.

        Can raise CursorNotFound, NotPrimaryError, ExecutionTimeout, or
        OperationFailure.

        :Parameters:
          - `cursor_id` (optional): cursor_id we sent to get this response -
            used for raising an informative exception when we get cursor id not
            valid at server response
          - `codec_options` (optional): an instance of
            :class:`~bson.codec_options.CodecOptions`
        )r  r{  Z
decode_allr"   _decode_all_selective)r   r   r   r~  legacy_responser,   r,   r-   unpack_response-  s
    
z_OpReply.unpack_responsec             C   s   | j  }| jdkst|d S )zUnpack a command response.r   r   )r  rx  AssertionError)r   r   r,   r,   r-   command_responseE  s    z_OpReply.command_responsec             C   s   t dS )z)Return the bytes of the command response.N)NotImplementedError)r   r,   r,   r-   raw_command_responseK  s    z_OpReply.raw_command_responsec             C   s   dS )z+Is the moreToCome bit set on this response?Fr,   )r   r,   r,   r-   more_to_comeP  s    z_OpReply.more_to_comec             C   s0   | j |\}}}}t|dd }| ||||S )z%Construct an _OpReply from raw bytes.   N)UNPACK_FROMbytes)r   r   r   r   r   rx  r"   r,   r,   r-   unpackU  s    z_OpReply.unpack)r   r   rx  r"   )NN)r8   r   r   r   r   r  r  unpack_fromr  OP_CODEr   r  rH  r  r  r  r   r  classmethodr  r,   r,   r,   r-   rw    s   
)rw  c               @   s   e Zd ZdZdZejdjZdZ	dZ
dZdZd
d Zdi fddZdeddfddZdd Zdd Zedd Zedd ZdS )r   z"A MongoDB OP_MSG response message.r   r   rx  payload_documentz<IBii  r   rQ   c             C   s   || _ || _d S )N)r   r  )r   r   r  r,   r,   r-   r   n  s    z_OpMsg.__init__Nc             C   s   t t| j|t}|gS )zp
        cursor_id is ignored
        user_fields is used to determine which fields must not be decoded
        )r   r   r  r   )r   r   r~  Zinflated_responser,   r,   r-   r  r  s    
z_OpMsg.raw_responseFc             C   s   | s
t tj| j||S )zUnpack a OP_MSG command response.

        :Parameters:
          - `cursor_id` (optional): Ignored, for compatibility with _OpReply.
          - `codec_options` (optional): an instance of
            :class:`~bson.codec_options.CodecOptions`
        )r  r{  r  r  )r   r   r   r~  r  r,   r,   r-   r  |  s    
z_OpMsg.unpack_responsec             C   s   | j  d S )zUnpack a command response.r   )r  )r   r,   r,   r-   r    s    z_OpMsg.command_responsec             C   s   | j S )z)Return the bytes of the command response.)r  )r   r,   r,   r-   r    s    z_OpMsg.raw_command_responsec             C   s   | j | j@ S )z+Is the moreToCome bit set on this response?)r   MORE_TO_COME)r   r,   r,   r-   r    s    z_OpMsg.more_to_comec             C   s   | j |\}}}|dkrH|| j@ r0td|f || jA rHtd|f |dkr^td|f t||d krvtdt|dd }| ||S )z#Construct an _OpMsg from raw bytes.r   z-Unsupported OP_MSG flag checksumPresent: 0x%xzUnsupported OP_MSG flags: 0x%xz%Unsupported OP_MSG payload type: 0x%xr   z$Unsupported OP_MSG reply: >1 sectionN)r  CHECKSUM_PRESENTr   r  r1   r  )r   r   r   Zfirst_payload_typeZfirst_payload_sizer  r,   r,   r-   r    s"    



z_OpMsg.unpack)r   r   rx  r  r   i   )r8   r   r   r   r   r  r  r  r  r  r  r  r   r   r  rH  r  r  r  r   r  r  r  r,   r,   r,   r-   r   a  s    
r   c
             C   s  t d||d|d|||dtddddd}tt|}
|	j}|rFtjj }|j|| \}}}|rtjj | }|	j|||| j	| j
d tjj }| j|| | j|}y|jd|}W nv tk
r( } zX|rtjj | | }t|ttfr|j}nt|}|	j|||
|| j	| j
d  W Y dd}~X nX d|krR||jd||f ddd	}n|r`|d ni }d|d
< |rtjj | | }|	j|||
|| j	| j
d |S )zESimple query helper for retrieving a first (and possibly only) batch.r   NF)rA  cursorz%s.%s)Z
firstBatchidr   g      ?)r  r<   r<   )r   r   r  r  r%  r&  r'  r   r@  r   rA  send_messageZreceive_messager  r8  r   r   r   r9  r:   rC  r   rB  )r   r   r   r   r   r  r   r3   r   r!  r   r  r<  r   r   r   Zencoding_durationr=  r   r>  r;  r?  rJ   r,   r,   r-   _first_batch  sT    









r  i   )rN   r   )rO   rB   )rP   rQ   )rR   rS   )rT   rU   )rV   rW   )rX   rY   )rZ   r[   )r\   r]   )r^   r_   )r`   ra   )rb   rc   )rd   re   )rf   rg   )rh   ri   )rj   ri   )rk   rl   )NNN)N)N)N)FN)F)FN)N)r   )r   N)r   r&  r(   r  r{  r   r   r   r   r   r   Zbson.codec_optionsr   Z
bson.int64r	   Zbson.raw_bsonr
   r   r   Zbson.py3compatr   r   Zbson.sonr   Zpymongor   Z_use_cImportErrorZpymongo.errorsr   r   r   r   r   r   r   r   Zpymongo.hello_compatr   Zpymongo.read_concernr   Zpymongo.read_preferencesr   Zpymongo.write_concernr   r+   r*   rq  Z_INSERTr[  r\  Z_EMPTYru  rr  rv  r   ri  rs  rt  r  r   rH  r.   r4   r:   rM   r   rn   r   r   objectr   r   r   r   r   r   r  r  r   r   r   r   r   r   r   r   r   r   rP  r$   r   r   r   Z_update_messager%   r   r   r   r  r  r   r  r  r  Z_query_messager   r  r  r  r  Z_get_more_messager   r  r  r  r&   r  r  rK  rD  rM  rX  rZ  rf  rg  rh  rj  rl  rm  rF  ro  rp  r)  rn  rw  r   r  r  Z_UNPACK_REPLYr  r,   r,   r,   r-   <module>   s4   
(' 
% V

	

	

	



	



 .&RA
	

KkS