
    h                        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 ddlmZmZmZ ddlmZ 	 dd	lmZ d
Z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-dZ.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%&      Z=d' Z>d( Z?d) Z@d* ZA eg d+      ZB eg d,      ZC	 	 	 dvd-ZDd. ZE G d/ d0eF      ZG G d1 d2eF      ZH G d3 d4eG      ZI G d5 d6eH      ZJ G d7 d8eK      ZL ej                  d9      j                  ZOd:ZPd; ZQd< ZR ej                  d=      j                  ZSd> ZT ej                  d?      j                  ZUd@ ZVdA ZWdB ZXerej                  ZX	 dwdCZZdD Z[dE Z\dF Z]erej                  Z]	 dwdGZ_ ej                  dH      j                  Z` ej                  dI      j                  ZadJ ZbdK ZcdL Zderej                  ZddwdMZedN Zf	 	 dxdOZg	 dydPZherej                  Zh	 	 dxdQZj ej                  dR      j                  ZkdS ZldT ZmdU Znerej                  ZndwdVZpdW ZqdX ZrdzdYZsd{dZZtd[ Zu G d\ d]eF      Zvd^Zw G d_ d`ev      Zxda Zydb Zzerej                  Zze0dce1dde2deiZ{df Z|dg Z}erej                  Z}dh Z~di Zerej                  Zdj Zdk Zdl Zerej                  Zdm Zerej                  Zdn Zdo Zdp Z G dq dreF      Z G ds dteF      Zej                  ej                  ej                  ej                  iZdu Zy# e$ r dZY w xY w)|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_selective_dict_to_bson_make_c_stringdecodeencode)DEFAULT_CODEC_OPTIONS)Int64)StringIOb)DEFAULT_RAW_BSON_OPTIONSRawBSONDocument_inflate_bsonSON)	_cmessageTF)ConfigurationErrorCursorNotFoundDocumentTooLargeExecutionTimeoutInvalidOperationNotPrimaryErrorOperationFailureProtocolError)HelloCompat)DEFAULT_READ_CONCERN)ReadPreference)WriteConcernii   i?                   s            s           s       s   documents     s   updates     s   deletes     	documentsupdatesdeletes)insertupdatedeletez%s.%sreplace)unicode_decode_error_handlerc                  >    t        j                  t        t              S )z(Generate a pseudo random 32 bit integer.)randomrandint	MIN_INT32	MAX_INT32 r!   M/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/pymongo/message.py_randintr4   _   s    >>)Y//r!   c                     |j                   }|j                  }|rB|t        j                  j                   k7  st	        |      dkD  rd| vrt        d| fg      } || d<   | S )z-Add $readPreference to spec when appropriate.r   $query$readPreference)modedocumentr   SECONDARY_PREFERREDlenr   )specread_preferencer8   r9   s       r3   _maybe_add_read_preferencer>   d   sf    D''H ;;@@@CMTUDU44()*D"*Kr!   c                 F    t        |       | j                  j                  dS )z<Convert an Exception into a failure document for publishing.)errmsgerrtype)str	__class____name__)	exceptions    r3   _convert_exceptionrF   t   s    )n1D1D1M1MNNr!   c                    |j                  dd      }d|d}|j                  d|j                  dd            }|rG|j                  d      r|d	dd
id|d<   n*d|j                  dd      |d}d|v r|d   |d<   |g|d<   |S | dk(  rt        |d         |d<   |S | dk(  rbd|v rd|d   dg|d<   |S |j                  d      du r=|dk(  r8|d   d   }|d   j                  d|d   j                  d            }d|dg|d<   |S )z6Convert a legacy write result to write command format.nr   r   )okrH   r@   err wtimeout@   T)r@   codeerrInfowriteConcernErrorrN      )indexrN   r@   rO   writeErrorsr(   r%   r)   upserted)rR   _idupdatedExistingFr&   urU   q)getr;   )		operationcommandresultaffectedresr@   errorr)   rU   s	            r3   _convert_write_resultr`   y   sU    zz#q!H
"CZZ&**UB"78F::j!28"R\^bQc'dC#$  FA)>&QEF"#))#4i "'CJHw{+,C J 
h	)*6*3EFGC
O J ZZ)*e3A Y'*F+//%)?@C)*378C
OJr!   ))tailabler    )oplogReplayrQ   )noCursorTimeout   )	awaitData    )allowPartialResults   ))r6   filter)z$orderbysort)z$hinthint)z$commentcomment)z$maxScanmaxScan)z
$maxTimeMS	maxTimeMS)z$maxmax)z$minmin)z
$returnKey	returnKey)z$showRecordIdshowRecordId)z$showDiskLocrr   )z	$snapshotsnapshotc                 |   t        d| fg      }d|v rr|j                  |j                         D cg c]  \  }}|t        v rt        |   |fn||f c}}       d|v r|j	                  d       d|v r|j	                  d       n||d<   |r||d<   |r||d<   |rt        |      |d<   |d	k  rd
|d<   |r||d<   |j                  r|	r|	j                  s|j                  |d<   |r||d<   |
|
|d<   |r>|j                  t        j                         D cg c]  \  }}||z  s|d
f c}}       |S c c}}w c c}}w )z!Generate a find command document.findr6   $explainr7   ri   
projectionskiplimitr   TsingleBatch	batchSizereadConcern	collationallowDiskUse)
r   r)   items
_MODIFIERSpopabslevelin_transactionr9   _OPTIONS)collr<   rw   rx   ry   
batch_sizeoptionsread_concernr}   sessionallow_disk_usecmdkeyvalopts                  r3   _gen_find_commandr      s^    ~
C4

 !%

C +.*;C#&#sK	
 GGJ#GG%&H&LF5zG19!%C%K7w/E/E)22M$K!,N

0@RHCGcMS$KRSJ?: Ss   !D2
D8
"D8
c                 D    t        d| fd|fg      }|r||d<   |||d<   |S )z$Generate a getMore command document.getMore
collectionr{   rn   r   )	cursor_idr   r   max_await_time_msr   s        r3   _gen_get_more_commandr      s=    
	9%d';<
=C%K$,KJr!   c                   <    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d	dZy)
_QueryzA query operation.)flagsdbr   ntoskipr<   fieldscodec_optionsr=   ry   r   namer   r}   r   clientr   _as_commandexhaustNc                     || _         || _        || _        || _        || _        || _        || _        || _        || _        |	| _	        |
| _
        || _        || _        || _        || _        d| _        d | _        || _        y )Nru   )r   r   r   r   r<   r   r   r=   r   ry   r   r}   r   r   r   r   r   r   )selfr   r   r   r   r<   r   r   r=   ry   r   r   r}   r   r   r   r   s                    r3   __init__z_Query.__init__  s    & 
		*.(
$",	r!   c                 @    t         | j                  | j                  fz  S N_UJOINr   r   r   s    r3   	namespacez_Query.namespace8      $)),,,r!   c                    d}|j                   dk\  r| j                  sd}nV|j                   dk\  rd}nD| j                  j                  s.t	        d| j                  j
                  |j                   fz        |j                   dk  r%| j                  t	        d|j                   fz        |j                   dk  r%| j                  t	        d|j                   fz        |j                  | j                  | j                         |S )	NF   TrQ   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   ok_for_legacyr   r   r}   r   validate_sessionr   r   )r   	sock_infouse_find_cmds      r3   use_commandz_Query.use_command;  s   %%*4<<L''1,L""00$1$$**I,F,FGH  %%)dnn.H$!$-$>$>#@A 
 %%)d.A.A.M$!$-$>$>#@A 
 	""4;;=r!   c                 t   | j                   | j                   S d| j                  v }t        | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        }|rd| _        t        d|fg      }| j                  }|j                  |       |r>|j!                  |d| j"                  |       |s|j$                  s|j'                  ||       |j)                  ||| j*                         | j*                  }|j,                  rH|j,                  j.                  s2|j,                  j1                  | j2                  |d| j4                        }|| j2                  f| _         | j                   S )z.Return a find command document for this query.rv   explainF)r   r<   r   r   r   r   ry   r   r   r   r}   r   r   r   r   add_server_api	_apply_tor=   r   _update_read_concernsend_cluster_timer   
_encrypter_bypass_auto_encryptionencryptr   r   )r   r   r   r   r   r   s         r3   
as_commandz_Query.as_commandY  sj    '###		)IIIIKKLLJJOOJJNNLL
 !DI	3'()C,,  %c5$*>*>	J7#9#9,,S)<##C$++>V%6%6%N%N##++DGGS%ASASTC<r!   c           
         |r| j                   dz  }n| j                   }| j                         }| j                  }|r}| j                  |      d   }|j                  rEt        d|| j                  | j                  |d| j                  |j                        \  }}}	}
|||	fS t        | j                  dfz  }d}nR| j                  dk(  xr dxs | j                  }| j                  r%|rt        | j                  |      }n| j                  }|j                  rt        || j                        }t!        ||| j"                  |||rd	n| j$                  | j                  |j                        S )
z:Get a query message, possibly setting the secondaryOk bit.r   r   Fctx$cmdr   r    N)r   r   r<   r   op_msg_enabled_op_msgr   r=   r   compression_contextr   r   ry   rp   	is_mongosr>   queryr   r   )r   set_secondary_okr   use_cmdr   nsr<   
request_idmsgsize_	ntoreturns               r3   get_messagez_Query.get_message  sP   JJNEJJE^^yy??9-a0D''+2GG(($&&!55	,(
Cq "3,,477F++BI
 1,2EdooIzz #DJJ	 :I $

I-dD4H4HIDLLD--	
 		
r!   F)rD   
__module____qualname____doc__	__slots__sock_mgrr   r   r   r   r   r   r2   r!   r3   r   r      s3    I, HI$L-<% N2
r!   r   c                   8    e Zd ZdZdZdZd Zd Zd Zd Z	d
dZ
y	)_GetMorezA getmore operation.)r   r   r   r   r   r   r=   r   r   r   r   r   r   c                     || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        d | _
        || _        y r   )r   r   r   r   r   r=   r   r   r   r   r   r   )r   r   r   r   r   r   r=   r   r   r   r   r   s               r3   r   z_GetMore.__init__  s\     	""*.!2 r!   c                 @    t         | j                  | j                  fz  S r   r   r   s    r3   r   z_GetMore.namespace  r   r!   c                     d}|j                   dk\  r| j                  sd}n|j                   dk\  rd}|j                  | j                  | j                         |S )NFr   TrQ   )r   r   r   r   r   )r   r   r   s      r3   r   z_GetMore.use_command  sP    %%*4<<G''1,G""4;;=r!   c                 t   | j                   | j                   S t        | j                  | j                  | j                  | j
                        }| j                  r(| j                  j                  |d| j                  |       |j                  |       |j                  || j                  | j                         | j                  }|j                  rH|j                  j                  s2|j                  j                  | j                  |d| j                         }|| j                  f| _         | j                   S )z1Return a getMore command document for this query.F)r   r   r   r   r   r   r   r   r=   r   r   r   r   r   r   r   r   )r   r   r   r   s       r3   r   z_GetMore.as_command  s     '####NNDIIt~~t7M7M
 <<LL""3t/C/CYO  %##Ct{{CV%6%6%N%N##++DGGS%ASASTC<r!   c           
         | j                         }|j                  }|r| j                  |      d   }|j                  rZ| j                  rt
        j                  }nd}t        ||| j                  ddd| j                  |j                        \  }}	}
}||	|
fS t        | j                  dfz  }t        d|dd|d| j                  |      S t        || j                  | j                  |      S )zGet a getmore message.r   NFr   r   r   )r   r   r   r   r   _OpMsgEXHAUST_ALLOWEDr   r   r   r   r   get_morer   r   )r   dummy0r   r   r   r   r<   r   r   r   r   r   s               r3   r   z_GetMore.get_message  s     ^^++??9-a0D''=="22EE+2GG&&!55	,(
Cq "3,,477F++BB2tT43E3E3OODNNDNNC@@r!   Nr   )rD   r   r   r   r   r   r   r   r   r   r   r2   r!   r3   r   r     s-    I D6-	 *Ar!   r   c                        e Zd Z fdZ xZS )_RawBatchQueryc                 ~    t         t        |   |       |j                  dk\  ry|j                  r| j
                  syyNrQ   TF)superr   r   r   r   r   r   r   rC   s     r3   r   z_RawBatchQuery.use_command&  s8    nd/	:%%*%%dllr!   rD   r   r   r   __classcell__rC   s   @r3   r   r   %       r!   r   c                        e Zd Z fdZ xZS )_RawBatchGetMorec                 ~    t         t        |   |       |j                  dk\  ry|j                  r| j
                  syyr   )r   r   r   r   r   r   r   s     r3   r   z_RawBatchGetMore.use_command2  s9    1)<%%*%%dllr!   r   r   s   @r3   r   r   1  r   r!   r   c                   8    e Zd ZdZd Zed        Zd Zd Zd Z	y)_CursorAddresszEThe server address (host, port) of a cursor, with namespace property.c                 @    t         j                  | |      }||_        |S r   )tuple__new___CursorAddress__namespace)clsaddressr   r   s       r3   r   z_CursorAddress.__new__@  s    }}S'*$r!   c                     | j                   S )zThe namespace this cursor.)r   r   s    r3   r   z_CursorAddress.namespaceE  s     r!   c                 >    | | j                   fz   j                         S r   )r   __hash__r   s    r3   r   z_CursorAddress.__hash__J  s!     ((**4466r!   c                     t        |t              r2t        |       t        |      k(  xr | j                  |j                  k(  S t        S r   )
isinstancer   r   r   NotImplementedr   others     r3   __eq__z_CursorAddress.__eq__O  s7    e^,;%,.T4>>U__3TTr!   c                     | |k(   S r   r2   r   s     r3   __ne__z_CursorAddress.__ne__T  s    5=  r!   N)
rD   r   r   r   r   propertyr   r   r   r  r2   r!   r3   r   r   =  s-    O
    7

!r!   r   z<iiiiiiB   c           
          |j                  |      }t               }t        t        t	        |      z   |dd| t	        |      |j
                        }|||z   fS )zDTakes message data, compresses it, and adds an OP_COMPRESSED header.r   i  )compressr4   _pack_compression_header_COMPRESSION_HEADER_SIZEr;   compressor_id)rZ   datar   
compressedr   headers         r3   	_compressr  \  s[    d#JJ% 3z?2	D	F v
***r!   c           	          t        dg      }|j                  |       | j                  dd      }t        d|d   dz   dd|dt              S )zData to send to do a lastError.)getlasterrorr   .r   r   .$cmdr   N)r   r)   splitr   r	   )r   argsr   splitnss       r3   __last_errorr  m  sM    
"#
$CJJtooc1%GGAJ(!Rd<QRRr!   z<iiiic                 X    t               }t        dt        |      z   |d|       }|||z   fS )ztTakes message data and adds a message header based on the operation.

    Returns the resultant message string.
    rd   r   )r4   _pack_headerr;   )rZ   r
  ridmessages       r3   __pack_messager  x  s2    
 *C2D	>39=G$r!   <ic                    t         }t        |      dk(  r5 ||d   ||      }dj                  dt        |       |g      t        |      fS |D cg c]  } ||||       }}|st	        d      dj                  t        |      t        |       dj                  |      g      t        t        t        |            fS c c}w )zGet an OP_INSERT messager   r   r!   r$   cannot do an empty bulk insert)r   r;   joinr   r   	_pack_intro   map)collection_namedocs
check_keysr   optsr   encodeddocs           r3   _insertr'    s    F
4yA~a*d3xx#/
 w< 	 9==vc:t,=G=?@@88Yu%~o'FQXHYZ[]`C^   >s   B<c                 P    t        | ||||      \  }}t        d||      \  }}	||	|fS )z9Internal compressed unacknowledged insert message helper.  )r'  r  )
r!  r"  r#  continue_on_errorr$  r   	op_insertmax_bson_sizer  r   s
             r3   _insert_compressedr-    s:    &jJ[]abI}y#.HC]""r!   c                     t        | ||||      \  }}t        d|      \  }	}
|rt        | |      \  }	}}|	|
|z   |fS |	|
|fS )zInternal insert message helper.r)  )r'  r  r  )r!  r"  r#  safelast_error_argsr*  r$  r+  r,  r  r   gler   s                r3   _insert_uncompressedr2    s`      'jJ[]abI}dI.HC"?ODS!C#I},,]""r!   c           	      H    |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-  r2  )r!  r"  r#  r/  r0  r*  r$  r   s           r3   r(   r(     s=     !/4EVX\^abbz4BSUY r!   c           
          d}|r|dz  }|r|dz  }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  r;   )
r!  upsertmultir<   r&  r#  r$  r   r   encoded_updates
             r3   _updater:    sy    E

FCT2N88?+e4%	
 > r!   c           	      T    t        | ||||||      \  }}	t        d||      \  }
}|
||	fS )z9Internal compressed unacknowledged update message helper.  )r:  r  )r!  r7  r8  r<   r&  r#  r$  r   	op_updater,  r  r   s               r3   _update_compressedr>    s>    &tSR\^bcI}y#.HC]""r!   c	           	          t        | ||||||      \  }	}
t        d|	      \  }}|rt        | |      \  }}}|||z   |
fS |||
fS )zInternal update message helper.r<  )r:  r  r  )r!  r7  r8  r<   r&  r/  r0  r#  r$  r=  r,  r  r   r1  r   s                  r3   _update_uncompressedr@    sd      'tSR\^bcI}dI.HC"?ODS!C#I},,]""r!   c
                 P    |	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!  r7  r8  r<   r&  r/  r0  r#  r$  r   s
             r3   r)   r)     sC     !/65$ZY]_bccc4*VZ r!   z<IBz<Bc                    t        |d|      }t        | d      }t        |      }d}	|r~t        d      }
t	        |      }|D cg c]  }t        |||       }}t        |      t        d |D              z   dz   }t        |      }||z  }t        d |D              }	|||
||g|z   }n||g}dj                  |      ||	fS c c}w )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              3   2   K   | ]  }t        |        y wr   r;   .0r&  s     r3   	<genexpr>z$_op_msg_no_header.<locals>.<genexpr>  s     !Cs#c(!C   r   c              3   2   K   | ]  }t        |        y wr   rD  rE  s     r3   rG  z$_op_msg_no_header.<locals>.<genexpr>  s     <3s8<rH  r!   )	r   _pack_op_msg_flags_typer;   
_pack_byter   sumr  ro   r  )r   r[   
identifierr"  r#  r$  r%  
flags_type
total_sizemax_doc_sizetype_onecstringr&  encoded_docsr   encoded_sizer
  s                    r3   _op_msg_no_headerrU    s     GUD1G(2JWJLa= ,HLMc:t<MM7|c!Cl!CCCaG d
<|<<GX|WETG$88D>:|33 Ns   B?c                 V    t        | |||||      \  }}}	t        d||      \  }
}|
|||	fS )zInternal OP_MSG message helper.  )rU  r  )r   r[   rM  r"  r#  r$  r   r   rO  r,  r  s              r3   _op_msg_compressedrX    sC    %6w
D*d&"C] sC(HCZ..r!   c                 T    t        | |||||      \  }}}t        d|      \  }	}
|	|
||fS )z*Internal compressed OP_MSG message helper.rW  )rU  r  )r   r[   rM  r"  r#  r$  r
  rO  r,  r   
op_messages              r3   _op_msg_uncompressedr[  !  sB    &7w
D*d'#D*m ,D$7J
z:}<<r!   c           	         ||d<   |?d|vr;|r*|j                   st        j                  j                  |d<   n|j                  |d<   t	        t        |            }	 t        j                  |      }	|j                  |	      }
	 |rt        | ||	|
|||      |	r|
||	<   S S t        | ||	|
||      |	r|
||	<   S S # t        $ r d}	d}
Y Cw xY w# |	r|
||	<   w w xY w)zGet a OP_MSG message.$dbNr7   rK   )r8   r   PRIMARY_PREFERREDr9   nextiter
_FIELD_MAPrY   r   KeyErrorrX  r[  )r   r[   dbnamer=   secondary_okr#  r$  r   r   rM  r"  s              r3   r   r   .  s   GEN"'8'G 4 4)7)I)I)R)RG%&)8)A)AG%&WD^^D)
{{:&'%eWj$
TXZ]^ "&GJ  $E7JjRVW "&GJ   
 "&GJ s$   &B6 C	 C	 6CC	
Cc                     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   ro   r;   r  r  r   )r   r!  num_to_skipnum_to_returnr   field_selectorr$  r#  r%  efsr,  s              r3   _queryrj  H  s     E:t4GNE48Gc#h/M'"/+&-(		
 	 r!   c	           
      V    t        | |||||||      \  }	}
t        d|	|      \  }}|||
fS )z)Internal compressed query message helper.  )rj  r  )r   r!  rf  rg  r   rh  r$  r#  r   op_queryr,  r  r   s                r3   _query_compressedrn  a  sL     %	Hm x-HC]""r!   c           
      T    t        | |||||||      \  }}	t        d|      \  }
}|
||	fS )zInternal query message helper.rl  )rj  r  )r   r!  rf  rg  r   rh  r$  r#  rm  r,  r  r   s               r3   _query_uncompressedrp  {  sJ     %	Hm dH-HC]""r!   c	                 P    |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.
    )rn  rp  )	r   r!  rf  rg  r   rh  r$  r#  r   s	            r3   r   r     sV       

 
	
 	 	r!   z<qc                 j    dj                  t        t        |       t        |      t	        |      g      S )zGet an OP_GET_MORE message.r!   )r  r6  r   r  _pack_long_longr!  rg  r   s      r3   	_get_moreru    s3    88?+m$I&		
 r!   c                 2    t        dt        | ||      |      S )z+Internal compressed getMore message helper.  )r  ru  r!  rg  r   r   s       r3   _get_more_compressedry    s    T9_mYOQTUUr!   c                 0    t        dt        | ||            S )z Internal getMore message helper.rw  )r  ru  rt  s      r3   _get_more_uncompressedr{    s    $	/=) TUUr!   c                 <    |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.
    )ry  r{  rx  s       r3   r   r     s'     #O]IsSS!/=)LLr!   c                     t        |d|      }dj                  t        t        |       t	        |      |g      t        |      fS )zGet an OP_DELETE message.Fr!   r5  )r!  r<   r$  r   r%  s        r3   _deleter~    sF    D%.G88X~o>	%@PRYZ[]`^  r!   c                 N    t        | |||      \  }}t        d||      \  }}|||fS )z9Internal compressed unacknowledged delete message helper.  )r~  r  )	r!  r<   r$  r   r   	op_deleter,  r  r   s	            r3   _delete_compressedr    s6    &dEJI}y#.HC]""r!   c                     t        | |||      \  }}t        d|      \  }}	|rt        | |      \  }}
}||	|
z   |fS ||	|fS )zInternal delete message helper.r  )r~  r  r  )r!  r<   r/  r0  r$  r   r  r,  r  r   r1  r   s               r3   _delete_uncompressedr    sZ    &dEJI}dI.HC"?ODS!C#I},,]""r!   c                 D    |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!  r<   r/  r0  r$  r   r   s          r3   r*   r*     s0     !/4ucJJt_dTYZZr!   c                     t        |       }t        j                  dd|z  z         j                  } |d|g|  }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<iirX   r   i  )r;   structStructpackr  )
cursor_idsnum_cursorsr  op_kill_cursorss       r3   kill_cursorsr    sI     j/K==#"345::D1k7J7O$00r!   c                       e Zd ZdZdZ	 ddZd Zd Zd Ze	d        Z
e	d	        Ze	d
        Ze	d        Ze	d        Zd Zd Zd Zd Zd Zd Zy)_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
                 `   || _         || _        || _        || _        |j                  | _        || _        t        | j                     | _        | j
                  rt        j                  j                         nd | _        || _        |j                  rdnd| _        || _        || _        |	| _        y )NTF)r  r   r  r  enabled_for_commandsr  r   ra  r  datetimenowr  r   r   r  r  r  r  )
r   database_namecmd_namer   operation_idr  r   r  r  r  s
             r3   r   z_BulkWriteContext.__init__0  s     %"!
" 55			*
59\\(++//1t ) = =5
$r!   c           	          | j                   dz   }t        || j                  ||| j                  | j                  |       \  }}}|st        d      |||fS )Nr  cannot do an empty bulk write)r  _do_bulk_write_commandr  r#  r  r   )r   r   r"  r   r   r   to_sends          r3   _batch_commandz _BulkWriteContext._batch_commandJ  sZ    LL7*	#9t||S$T$
 
C "#BCC3''r!   c                     | j                  ||      \  }}}| j                  ||||      }|j                  || j                         ||fS r   )r  write_command_process_responser   )r   r   r"  r   r   r   r  r\   s           r3   executez_BulkWriteContext.executeS  sO    #'#6#6sD#A 
C##CS'B  6wr!   c                 ^    | j                  ||      \  }}}| j                  |||dd|       |S )Nr   F)r  legacy_write)r   r   r"  r   r   r   r  s          r3   execute_unackz_BulkWriteContext.execute_unackY  s:    #'#6#6sD#A 
C 	#z35'Br!   c                      y)z-Should we check keys for this operation type?Fr2   r   s    r3   r#  z_BulkWriteContext.check_keysc       r!   c                 .    | j                   j                  S )z#A proxy for SockInfo.max_bson_size.)r   r,  r   s    r3   r,  z_BulkWriteContext.max_bson_sizeh  s     ~~+++r!   c                 x    | j                   r| j                  j                  dz
  S | j                  j                  S )z&A proxy for SockInfo.max_message_size.rd   )r  r   max_message_sizer   s    r3   r  z"_BulkWriteContext.max_message_sizem  s0     ==>>22R77~~...r!   c                 .    | j                   j                  S )z*A proxy for SockInfo.max_write_batch_size.)r   max_write_batch_sizer   s    r3   r  z&_BulkWriteContext.max_write_batch_sizeu  s     ~~222r!   c                     | j                   S )z:The maximum size of a BSON command before batch splitting.)r,  r   s    r3   max_split_sizez _BulkWriteContext.max_split_sizez  s     !!!r!   c                     |r$t        d|| j                  j                        \  }}| j                  | j                  j                         |||||      S )Nr)  )r  r   r   r  r  copy)r   r   r   rP  acknowledgedr"  r  s          r3   legacy_bulk_insertz$_BulkWriteContext.legacy_bulk_insert  sO    'c4>>3U3UVOJ  OO  "J\<QU
 	
r!   c                    | j                   r\t        j                  j                         | j                  z
  }| j	                  |||      }t        j                  j                         }	 | j
                  j                  ||||| j                        }	| j                   rUt        j                  j                         z
  z   }|	t        | j                  ||	      }
nddi}
| j                  ||
|       t        j                  j                         | _        |	S # t        $ r}| j                   rt        j                  j                         z
  z   }t        |t              r"t        | j                  ||j                        }n(t        |t              r|j                  }nt!        |      }| j#                  |||        d}~ww xY w# t        j                  j                         | _        w xY w)zBA proxy for SocketInfo.legacy_write that handles event publishing.NrI   r   )r  r  r  r  _startr   r  r  r`   r   _succeed	Exceptionr   r   detailsr   rF   _fail)r   r   r   r   rP  r  r"  durationstartr\   replyexcfailures                r3   r  z_BulkWriteContext.legacy_write  s   <<((,,.@H++c:t4C%%))+E	6^^00C|TZZF ||$--113e;xG%1$))S&IE "1IEj%: '//335DO  
	||$--113e;xGc#343DIIsCKKPG_5!kkG05G

:w9
	 '//335DOs&   *B
D 	G"BG  GG %G-c                 H   | j                   r\t        j                  j                         | j                  z
  }| j	                  |||       t        j                  j                         }	 | j
                  j                  ||| j                        }| j                   r7t        j                  j                         z
  z   }| j                  |||       t        j                  j                         | _        |S # t        $ rw}| j                   ret        j                  j                         z
  z   }t        |t        t        f      r|j                  }	nt        |      }	| j                  ||	|        d}~ww xY w# t        j                  j                         | _        w xY w)zCA proxy for SocketInfo.write_command that handles event publishing.N)r  r  r  r  r  r   r  r  r  r  r   r   r   r  rF   r  )
r   r   r   r   r"  r  r  r  r  r  s
             r3   r  z_BulkWriteContext.write_command  sC   <<((,,.@HKKZ.%%))+E	6NN00S$**ME||$--113e;xGj%: '//335DO  	||$--113e;xGcO5E#FG!kkG05G

:w9	 '//335DOs&   *A*C9 9	E9A2E44E99E< <%F!c                     ||| 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"  s       r3   r  z_BulkWriteContext._start  sV    DJJ,,LLNN""JJNN%%	
 
r!   c           	          | j                   j                  ||| j                  || j                  j                  | j
                  | j                  j                         y)z Publish a CommandSucceededEvent.N)r  publish_command_successr   r   r   r  r  )r   r   r  r  s       r3   r  z_BulkWriteContext._succeed  sE    ..IINN""JJNN%%	
r!   c           	          | j                   j                  ||| j                  || j                  j                  | j
                  | j                  j                         y)zPublish a CommandFailedEvent.N)r  publish_command_failurer   r   r   r  r  )r   r   r  r  s       r3   r  z_BulkWriteContext._fail  sE    ..IINN""JJNN%%	
r!   r   )rD   r   r   r   r   r   r  r  r  r  r#  r,  r  r  r  r  r  r  r  r  r  r2   r!   r3   r  r    s    MI4 %4(   , , / / 3 3 " "
B0



r!   r  i    c                   2    e Zd ZdZd Zd Zd Zed        Zy)_EncryptedBulkWriteContextr2   c           	         | j                   dz   }t        || j                  ||| j                  | j                  |       \  }}|st        d      |j                  dd      dz   }t        t        |      |d  t              }||fS )Nr  r  r#   r   	   )
r  _encode_batched_write_commandr  r#  r  r   rR   r   
memoryviewr   )r   r   r"  r   r   r  	cmd_starts          r3   r  z)_EncryptedBulkWriteContext._batch_command  s    LL7*	4t||S$T
W "#BCC IIgq)A-	JsOIJ79QRG|r!   c                     | j                  ||      \  }}| j                  j                  | j                  || j                  | j
                  |      }||fS )N)r   r   r   )r  r   r[   r  r  r   )r   r   r"  r   batched_cmdr  r\   s          r3   r  z"_EncryptedBulkWriteContext.execute  sV    #223=W''LL+TZZ^d ( 
 wr!   c                     | j                  ||      \  }}| j                  j                  | j                  |t	        d      | j
                  |       |S )Nr   )w)write_concernr   r   )r  r   r[   r  r   r   )r   r   r"  r   r  r  s         r3   r  z(_EncryptedBulkWriteContext.execute_unack  sS    #223=WLL&+LL 	 	
 r!   c                     t         S )z Reduce the batch splitting size.)_MAX_SPLIT_SIZE_ENCr   s    r3   r  z)_EncryptedBulkWriteContext.max_split_size  s
     #"r!   N)	rD   r   r   r   r  r  r  r  r  r2   r!   r3   r  r    s*    I	 # #r!   r  c                 H    | dk(  rt        d||fz        t        | d      )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 command document too large)r   )rZ   doc_sizemax_sizes      r3   _raise_document_too_larger    s;    H "8,-
 	
 )MNNr!   c           	      "     fd}|xs | }	d}
t               }|j                  t        j                  dt	        |                   |j                  t                      |j                         x}}d}g }t        }|j                  xr |xs |	 }|D ]  } ||||      }t        |      }||j                  kD  }||z  }||j                  k  r'|s%|j                  |       |j                  |       d}b|rF	 |rd|j                         }}n ||j                         |	      \  }}|j                  ||d|	||       |rt!        d||j                         ||z   }|j#                  |       |j%                          |j                  |       |g} |st'        d      |rd|j                         }}n ||j                         |      \  }}|j                  ||d|||       |
|
y# t        $ r}|r|}
n	|sY d}~ y Y d}~d}~ww xY w)	z%Insert `docs` using multiple batches.c                 X    t        d|       \  }}|rt              \  }}}||z  }||fS )z-Build the insert message with header and GLE.r)  )r  r  )insert_message	send_safer   final_messageerror_messager   r!  r0  s         r3   _insert_messagez+_do_batched_insert.<locals>._insert_message(  s?    $24$H!
M+7+Y(Jq]*M=((r!   Nr  FTr   r(   r  )r   writer  r  intr   tellr   r  r;   r,  r  appendgetvaluer  r   r  seektruncater   )r!  r"  r#  r/  r0  r*  r$  r   r  r  
last_errorr
  message_length	begin_lochas_docsr  r   r  r&  r%  encoded_length	too_larger  r   r  r   s   `   `                     r3   _do_batched_insertr  #  s   
) ---IJ:DJJv{{4%6!789JJ~o./!%,NYHGF||7T%6Y 7H )j$/W"S%6%66	.(C000JJwNN3H#T]]_C.t}}	JHC&&sCIwQ %h@Q@QR"^3		)

7%S)V ?@@C
)$--/4@
C:sAtWhG  ? $ 
$!$J  	
s   <AG--	H6H	H		Hs
   documents s   updates s   deletes c                    |j                   }|j                  }	|j                  }
|rdnd}|j                  |       |j                  d       |j                  t	        |d|             |j                  d       |j                         }|j                  d       	 |j                  t        |           | t        t        fv rd}g }d}|D ]  }t	        |||      }t        |      }|j                         |z   }|dk(  xr ||
kD  }| xr ||kD  }|s|r6t        t        j                               |    }t        |t        |      |       ||
kD  r n0|j                  |       |j!                  |       |dz  }||	k(  s n |j                         }|j#                  |       |j                  t%        ||z
               ||fS # t        $ r t        d      w xY w)	zCreate a batched OP_MSG write.r$   s      r#   F   Unknown commandr   r   )r,  r  r  r  r   r  _OP_MSG_MAPrb  r   _UPDATE_DELETEr;   listra  keysr  r  r  r  )rZ   r[   r"  r#  ackr$  r   bufr,  r  r  r   size_locationr  idxr&  value
doc_lengthnew_message_sizedoc_too_largeunacked_doc_too_largewrite_oplengths                          r3   _batched_op_msg_implr    s   %%M33++#&,?EIIe IIgIImGUD12 IIgHHJMII!"2		+i() Wg&&
G
C c:t4Z
88:
2qJ&69I&I %( HZ--G1JOO-.y9H%hE
MJ..		%sq&&16 XXZFHH]IIi./0F?O  20112s   G Gc           
      d    t               }t        | |||||||      \  }}	|j                         |fS )zOEncode the next batched insert, update, or delete operation
    as OP_MSG.
    )r   r  r  )
rZ   r[   r"  r#  r  r$  r   r  r  r   s
             r3   _encode_batched_op_msgr    s;     *C%i$
CQUWZ\_`JGQ<<>7""r!   c           	      |    t        | ||||||      \  }}t        d||j                  j                        \  }	}
|	|
|fS )z]Create the next batched insert, update, or delete operation
    with OP_MSG, compressed.
    rW  )r  r  r   r   )rZ   r[   r"  r#  r  r$  r   r
  r  r   r   s              r3   _batched_op_msg_compressedr
    sJ     +9gtZQTVZ\_`MD'dCMM,M,MNOJsG##r!   c           
      r   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   r  _ZERO_64r  r  r4   r  r  )rZ   r[   r"  r#  r  r$  r   r  r  r  r   s              r3   _batched_op_msgr    s    
*C IIhII12*7D*c4cOGV
 HHQKJIIi
#$HHQKIIi s||~w..r!   c           	          | j                  dd      d   |d<   d|v rt        |d   j                  dd            }nd}|j                  j                  rt        |||||||      S t        |||||||      S )zRCreate the next batched insert, update, or delete operation
    using OP_MSG.
    r  r   r   r]  writeConcernr  T)r  boolrY   r   r   r
  r  )r   rZ   r[   r"  r#  r$  r   r  s           r3   _do_batched_op_msgr    s     __S!,Q/GEN 7>*..sA67
}}(())WdJPSUY[^__9gtZdCPPr!   c           	      |    t        | ||||||      \  }}t        d||j                  j                        \  }	}
|	|
|fS )zFCreate the next batched insert, update, or delete command, compressed.rl  )r  r  r   r   )r   rZ   r[   r"  r#  r$  r   r
  r  r   r   s              r3   !_batched_write_command_compressedr    sJ    19gtZsMD'  dCMM,M,MNOJsG##r!   c           
      d    t               }t        | |||||||      \  }}	|j                         |fS )z:Encode the next batched insert, update, or delete command.)r   _batched_write_command_implr  )
r   rZ   r[   r"  r#  r$  r   r  r  r   s
             r3   r  r    s;    
*C,9gtZsCJGQ <<>7""r!   c           
      r   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   r  r  r  r  r4   r  r  )r   rZ   r[   r"  r#  r$  r   r  r  r  r   s              r3   _batched_write_commandr    s    
*C IIhII12 29gtZsCOGV
 HHQKJIIi
#$HHQKIIi s||~w..r!   c           	      r    |j                   j                  rt        | ||||||      S t        | ||||||      S )z#Batched write commands entry point.)r   r   r  r  r   rZ   r[   r"  r#  r$  r   s          r3   _do_batched_write_commandr  7  sF    
}}((0y'4T3
 	
 ")YzSWY\]]r!   c           	      x    |j                   j                  dkD  rt        | ||||||      S t        | ||||||      S )z Bulk write commands entry point.r   )r   r   r  r  r  s          r3   r  r  @  sF    
}}%%)!)YzSWY\]]$Y	7D*VZ\_``r!   c                    |j                   }|j                  }	|t        z   }
|j                  }|j	                  t
               |j	                  t        |              |j	                  t               |j	                  t               |j                         }|j	                  t        |             |j                  dd       |j                          	 |j	                  t        |          |t         t"        fv rd}|j                         dz
  }g }d}|D ]	  }t        t%        |            }t        |||      }t'        |      |
kD  }|r6t)        t*        j-                               |   }t/        |t'        |      |       |dk\  xr+ |j                         t'        |      z   t'        |      z   |k\  }||	k\  }|s|r ne|j	                  t0               |j	                  |       |j	                  t               |j	                  |       |j3                  |       |dz  } |j	                  t4               |j                         }|j                  |       |j	                  t7        ||z
  dz
               |j                  |       |j	                  t7        ||z
               ||fS # t        $ r t        d      w xY w)z(Create a batched OP_QUERY write command.r   r    r  Fr   r   r   )r,  r  _COMMAND_OVERHEADr  r  r6  r   _ZERO_8_SKIPLIMr  r   r  r  _OP_MAPrb  r   r  r  rB   r;   r  ra  r  r  _BSONOBJr  _ZERO_16r  )r   rZ   r[   r"  r#  r$  r   r  r,  r  max_cmd_sizer  command_start
list_startr  r  r&  r   r  r  r  enough_dataenough_documentsr  s                           r3   r  r  G  sT   %%M33 !#44L''N IIhIIa	lIIgIIh HHJMIIfWo HHROLLN2		')$% Wg&&
 aJG
C CksJ- E
\1JOO-.y9H%hE
MJQhYCHHJS$9CJ$F>#Y"66*		(		#		'		%sq'. IIh XXZFHHZIIi+a/01HH]IIi./0F?U  20112s   J* *J?c                       e Zd ZdZdZ ej                  d      j                  ZdZ	d Z
ddZdeddfd	Zd
 Zd Zed        Zed        Zy)_OpReplyz$A MongoDB OP_REPLY response message.)r   r   number_returnedr%   z<iqiir   c                 N    || _         t        |      | _        || _        || _        y r   )r   r
   r   r*  r%   )r   r   r   r*  r%   s        r3   r   z_OpReply.__init__  s$    
y)."r!   Nc                 z   | j                   dz  r&|t        d      d|fz  }d|dd}t        |d|      | j                   dz  rt        j                  | j
                        j                         }|j                  dd       |d	   j                  t        j                        rt        |d	   |      |j                  d
      dk(  r+t        |j                  d	      |j                  d
      |      t        d|j                  d	      z  |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   z"No cursor id for getMore operationzCursor not found, cursor id: %dr   +   )rI   r@   rN   r    rI   z$errrN   2   zdatabase error: %s)r   r   r   bsonBSONr%   r   
setdefault
startswithr   LEGACY_ERRORr   rY   r   r   )r   r   user_fieldsr   errobjerror_objects         r3   raw_responsez_OpReply.raw_response  s6    ::> #$HII 4ylBCb9F b&11ZZ!^99T^^4;;=L##D!,F#..{/G/GH%l6&:LII!!&)R/& $$V,l.>.>v.F  #$|'7'7'??  ( 
 >>NN##	r!   Fc                     | 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`
        )r7  r/  
decode_allr%   _decode_all_selectiver   r   r   r4  legacy_responses        r3   unpack_responsez_OpReply.unpack_response  sC    , 	)$??4>>=AA))$..-UUr!   c                 R    | j                  |      }| j                  dk(  sJ |d   S )Unpack a command response.r   r   r   )r=  r*  )r   r   r"  s      r3   command_responsez_OpReply.command_response  s2    ##-#@##q(((Awr!   c                     t         z)Return the bytes of the command response.)NotImplementedErrorr   s    r3   raw_command_responsez_OpReply.raw_command_response  s
     "!r!   c                      y)+Is the moreToCome bit set on this response?Fr2   r   s    r3   more_to_comez_OpReply.more_to_come  r  r!   c                 `    | j                  |      \  }}}}t        |dd       } | ||||      S )z%Construct an _OpReply from raw bytes.   N)UNPACK_FROMbytes)r   r   r   r   r   r*  r%   s          r3   unpackz_OpReply.unpack  s=     03s/C,y!_ #bc(O	5)_i@@r!   )NN)rD   r   r   r   r   r  r  unpack_fromrK  OP_CODEr   r7  _UNICODE_REPLACE_CODEC_OPTIONSr=  rA  rE  r  rH  classmethodrM  r2   r!   r3   r)  r)    sx    .FI&--(44KG#(X 4V6"
   A Ar!   r)  c                       e Zd ZdZdZ ej                  d      j                  ZdZ	dZ
dZdZd Zd	i fd
Zd	ed	dfdZd Zd Zed        Zed        Zy	)r   z"A MongoDB OP_MSG response message.)r   r   r*  payload_documentz<IBirW  r   r    i   c                      || _         || _        y r   )r   rS  )r   r   rS  s      r3   r   z_OpMsg.__init__  s    
 0r!   Nc                 P    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   rS  r   )r   r   r4  inflated_responses       r3   r7  z_OpMsg.raw_response  s-    
 .D112KAY
 """r!   Fc                 L    |rJ 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:  rS  r;  s        r3   r=  z_OpMsg.unpack_response  s)     #""))$*?*?P[\\r!   c                 ,    | j                  |      d   S )r?  r@  r   )r=  )r   r   s     r3   rA  z_OpMsg.command_response+  s    ##-#@CCr!   c                     | j                   S rC  )rS  r   s    r3   rE  z_OpMsg.raw_command_response/  s    $$$r!   c                 4    | j                   | j                  z  S )rG  )r   MORE_TO_COMEr   s    r3   rH  z_OpMsg.more_to_come3  s     zzD----r!   c                 <   | j                  |      \  }}}|dk7  r<|| j                  z  rt        d|fz        || j                  z  rt        d|fz        |dk7  rt        d|fz        t	        |      |dz   k7  rt        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)rK  CHECKSUM_PRESENTr   r[  r;   rL  )r   r   r   first_payload_typefirst_payload_sizerS  s         r3   rM  z_OpMsg.unpack8  s     9<8L5!#5A:s+++#$VZ_Ya$abbs'''#$Dx$OPP" JN`Mb bccs8)A-- FGG !QR>5*++r!   )rD   r   r   r   r   r  r  rN  rK  rO  r]  r[  r   r   r7  rP  r=  rA  rE  r  rH  rQ  rM  r2   r!   r3   r   r     s    ,MI&--'33KG LO1 &*r # 4]$D% . . , ,r!   r   c
                 J   t        d||d|d|||dt        ddddd      }t        t        |            }
|	j                  }|rt
        j
                  j                         }|j                  ||       \  }}}|rit
        j
                  j                         z
  }|	j                  |||| j                  | j                         t
        j
                  j                         }| j                  ||       | j                  |      }	 |j                  d|      }d|v r||j*                  |d|ddd	}n|r|d   ni }d|d
<   |rOt
        j
                  j                         z
  z   }|	j-                  |||
|| j                  | j                         |S # t        $ r}|r}t
        j
                  j                         z
  z   }t        |t         t"        f      r|j$                  }nt'        |      }|	j)                  |||
|| j                  | j                          d}~ww xY w)zESimple query helper for retrieving a first (and possibly only) batch.r   NF)r  cursorr  )
firstBatchidr   g      ?)ra  rI   rI   )r   r   r_  r`  r  r  r  r   r  r   r  send_messagereceive_messager=  r  r   r   r   r  rF   r  r   r  )r   r   r   r   r   rd  r   r=   r   r  r   r  r  r   r   rP  encoding_durationr  r"  r  r  r  r\   s                          r3   _first_batchrg  T  s?    	
		!E& S	?D,,G!!%%'$)$5$5lI$N!J\$--113e;''Z!2!2y?S?S 	( 	
 !!%%'3-%%j1E$$T=9$ 3'+EOOXZ\`Lab
 !abu%%))+e37HH))fdJ	0A0AiNbNb 	* 	
 MA   ))--/%7;LLH#1ABC++,S1--!!$// .  	s   F 	H"B HH")NNNr   )FNr   )r   )r   N)r   r  r.   r  r/  r   r   r   r   r   r   bson.codec_optionsr	   
bson.int64r
   bson.py3compatr   r   bson.raw_bsonr   r   r   bson.sonr   pymongor   _use_cImportErrorpymongo.errorsr   r   r   r   r   r   r   r   pymongo.hello_compatr   pymongo.read_concernr   pymongo.read_preferencesr   pymongo.write_concernr   r1   r0   r  _INSERTr  r  _EMPTYr!  r  r"  r6  r  r  r   ra  r   rP  r4   r>   rF   r`   r   r   r   r   objectr   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r'  r-  r2  r  r(   r:  r>  r@  _update_messager)   rJ  rK  rU  rX  r[  r   rj  rn  rp  _query_messager   rs  ru  ry  r{  _get_more_messager   r~  r  r  r*   r  r  r  r  r  r  r  r  r  r
  r  r  r  r  r  r  r  r  r)  r   rO  rM  _UNPACK_REPLYrg  r2   r!   r3   <module>r|     sM  	      5  & R R !F	 	 	 - 5 3 .		  


	
..1//
 $yIN
	!-9!U 0
 O
 F  
6 0f|
V |
~mAv mA`	V 		x 	!U !6 )6==499  +"S v}}W%** FMM$$$	*#	# 
$44 \`(#	# 
$44 ]a (&--.33 V]]4 %%
44/= 
$,,'4B #D #2 
#22 %P  &--%**	V
V
 
&88M##[ 	1E
 E
X  %#!2 %#POM` 
"55 ^^<~# 
&==$/. 
//OQ"$# 
$-$K$K!/0 
&==^aEPlAv lA^M,V M,b hoo
NNFMMRG9  Fs   L? ?M
	M
