o Sb@sUdZddlZddlZddlZddlmZddlmZm Z m Z ddl Z ddl m Z m Z mZmZmZddlmZddlmZmZmZddlmZz dd lmZd ZWn ey\d ZYnwdd lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%dd l&m'Z'ddl(m)Z)ddl*m+Z+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8e/de0de1diZ9dd d!d"Z:e d#d$Z;d%ed+d,Z?d-d.Z@egd/ZAegd0ZB   dd1d2ZCd3d4ZDGd5d6d6eEZFGd7d8d8eEZGGd9d:d:eFZHGd;d<dd>eJZKeLd?jMZNd@ZOdAdBZPeLdCjMZQdDdEZReLdFjMZSeLdGjMZTeLdHjMZUdIdJZVdKdLZWdMdNZXer>ejYZXddOdPZYdQdRZZ ddSdTZ[dUdVZ\erWej]Z\ ddWdXZ^eLdYjMZ_dZd[Z`d\d]Zad^d_ZberuejcZbdd`daZdGdbdcdceEZeddZfGdedfdfeeZgdgehdheidieidje fdkdlZje/dme0dne1doiZkdpdqZldrdsZmerejmZmdtduZndvdwZoerejoZodxdyZpdzd{ZqerejqZqd|d}ZrGd~ddeEZsGdddeEZtesjuesjvetjuetjviZwdS)zTools for creating `messages `_ to be sent to MongoDB. .. note:: This module is for internal use and is generally not needed by application developers. N)BytesIO)AnyDictNoReturn) CodecOptions_decode_selective _dict_to_bson_make_c_stringencode)Int64)DEFAULT_RAW_BSON_OPTIONSRawBSONDocument _inflate_bson)SON) _cmessageTF)ConfigurationErrorCursorNotFoundDocumentTooLargeExecutionTimeoutInvalidOperationNotPrimaryErrorOperationFailure ProtocolError) HelloCompat)ReadPreference) WriteConcerniii?ssssdocumentss updatess deletes documentsupdatesZdeletes)insertupdatedeletereplace)Zunicode_decode_error_handlerzCodecOptions[Dict[str, Any]]_UNICODE_REPLACE_CODEC_OPTIONScCs tttS)z(Generate a pseudo random 32 bit integer.)randomrandint MIN_INT32 MAX_INT32r-r-6/tmp/pip-target-onvjaxws/lib/python/pymongo/message.py_randintTs r/cCsJ|j}|j}|r#|tjjkst|dkr#d|vrtd|fg}||d<|S)z-Add $readPreference to spec when appropriate.r$query$readPreference)modedocumentrZSECONDARY_PREFERREDlenr)specread_preferencer2r3r-r-r._maybe_add_read_preferenceYsr7cCst||jjdS)zsz%_gen_find_command..$explainr1rU projectionskiplimitrTZ singleBatch batchSizeZ readConcern collationNZ allowDiskUsecs g|] \}}|@r|dfqS)Tr-)r_optraoptionsr-r.rbs ) rr%itemspopabslevelin_transactionr3_OPTIONS) collr5rdrerf batch_sizerk read_concernrhsessionallow_disk_usecmdr-rjr._gen_find_commands@    rxcCsNtd|fd|fg}|r||d<|dur||d<|dur%|jdkr%||d<|S)z$Generate a getMore command document.getMoreZ collectionrgNrY rX)rmax_wire_version) cursor_idrrrsmax_await_time_msrX sock_inforwr-r-r._gen_get_more_commandsrc@sPeZdZdZdZdZdZddZddZdd Z d d Z dd dZ dddZ dS)_QueryzA query operation.)flagsdbrrntoskipr5fields codec_optionsr6rfrsnamertrhruclientrv _as_commandexhaustNcCsp||_||_||_||_||_||_||_||_| |_| |_ | |_ | |_ | |_ ||_ ||_d|_d|_||_dS)Nr])rrrrrr5rrr6rtrfrsrhrurrvrrr)selfrrrrrr5rrr6rfrsrtrhrurrvrr-r-r.__init__ s$ z_Query.__init__cC d|_dSNrrr-r-r.reset/ z _Query.resetcCd|j|jfSNz%s.%srrrrr-r-r. namespace2z_Query.namespacecCsRd}|jsd}n|jdkrd}n|jjstd|jj|jf||j|j|S)NFTrFzDread concern level of %s is not valid with a max wire version of %d.) rr{rtZ ok_for_legacyrrovalidate_sessionrru)rr~Z use_find_cmdr-r-r. use_command5s  z_Query.use_commandFc Cs|jdur|jSd|jv}t|j|j|j|j|j|j|j|j |j |j |j }|r2d|_ td|fg}|j }|||rP||d|j||sP|jsP|||||||j|j}|jrl|jjsl|j|j||j}|rt|||||jf|_|jS)z.Return a find command document for this query.NrcexplainF)rr5rxrrrrrfrsrrtrhrurvrradd_server_api _apply_tor6rpZ_update_read_concernsend_cluster_timer _encrypter_bypass_auto_encryptionencryptrr apply_timeout)rr~rrrwrurr-r-r. as_commandFsB        z_Query.as_commandc Cs||_|jr |jdB}n|j}|}|j}|r7|j|ddd}td||j||j|j d\}}} } ||| fS|j dkr>dp@|j } |j rP| rMt |j | } n|j } |j rXt||}t|||j| ||rcdn|j|j|j dS) z:Get a query message, possibly setting the secondaryOk bit.TrrctxrrN)r6r2rrr5r_op_msgrrcompression_contextrsrfr[Z is_mongosr7_queryrr) rr6r~use_cmdrnsr5 request_idmsgsize_ ntoreturnr-r-r. get_messagepsD    z_Query.get_messageF) r; __module__ __qualname____doc__ __slots__sock_mgrr|rrrrrrr-r-r-r.rs& *rc@sLeZdZdZdZdZddZddZdd Zd d Z dd dZ dddZ dS)_GetMorezA getmore operation.) rrrrr|r}rr6rurrrrrXryc CsR||_||_||_||_||_||_||_||_| |_| |_ d|_ | |_ | |_ dSr) rrrrr|rr6rurr}rrrrX) rrrrrr|rr6rurr}rrrXr-r-r.rs z_GetMore.__init__cCrrrrr-r-r.rrz_GetMore.resetcCrrrrr-r-r.rrz_GetMore.namespacecCs2d}|jsd}n|jdkrd}||j|j|S)NFTrF)rr{rrru)rr~rr-r-r.rs z_GetMore.use_commandFcCs|jdur|jSt|j|j|j|j|j|}|jr#|j|d|j || || ||j|j |j }|j rE|j jsE|j |j||j}|rN|j|dd||jf|_|jS)z1Return a getMore command document for this query.NF)rw)rrr|rrrr}rXrurr6rrrrrrrrr)rr~rrwrr-r-r.rs*   z_GetMore.as_commandc Cst|}|j}|r1|j|ddd}|jrtj}nd}t|||jd|j|jd\}} } } || | fSt ||j |j |S)zGet a getmore message.TrrNr) rrrr_OpMsgEXHAUST_ALLOWEDrrr _get_morerr|) rZdummy0r~rrrr5rrrrrr-r-r.rs z_GetMore.get_messageNr) r;rrrrrrrrrrrr-r-r-r.rs rceZdZfddZZS)_RawBatchQueryc,tt|||jdkrdS|jsdSdSNrFTF)superrrr{rrr~r:r-r.r  z_RawBatchQuery.use_commandr;rrr __classcell__r-r-rr.rrcr)_RawBatchGetMorecrr)rrrr{rrrr-r.r"rz_RawBatchGetMore.use_commandrr-r-rr.r!rrc@sFeZdZUdZeed<ddZeddZddZ d d Z d d Z d S)_CursorAddresszEThe server address (host, port) of a cursor, with namespace property._CursorAddress__namespacecCst||}||_|Sr)tuple__new__r)clsaddressrrr-r-r.r2s z_CursorAddress.__new__cC|jS)zThe namespace this cursor.)rrr-r-r.r7z_CursorAddress.namespacecCs||jfSr)r__hash__rr-r-r.r<sz_CursorAddress.__hash__cCs*t|trt|t|ko|j|jkStSr) isinstancerrrNotImplementedrotherr-r-r.__eq__As z_CursorAddress.__eq__cCs ||k Srr-rr-r-r.__ne__Frz_CursorAddress.__ne__N) r;rrrr__annotations__rpropertyrrrrr-r-r-r.r-s   rz||}t}ttt||dd|t||j}|||fS)zDTakes message data, compresses it, and adds an OP_COMPRESSED header.ri)compressr/_pack_compression_header_COMPRESSION_HEADER_SIZEr4Z compressor_id)rLdatar compressedrheaderr-r-r. _compressNs   rz.cs|]}t|VqdSrr4rr-r-r. z$_op_msg_no_header..rcsrrrrr-r-r.rrr) r_pack_op_msg_flags_typer4 _pack_byter sum _pack_intrZjoin)rrM identifierdocsrencodedZ flags_type total_size max_doc_sizeZtype_oneZcstringZ encoded_docsrZ encoded_sizerr-rr._op_msg_no_headerqs  rc Cs2t|||||\}}}td||\} }| |||fS)zInternal OP_MSG message helper.)rr) rrMrrrrrr max_bson_sizerr-r-r._op_msg_compresseds rc Cs0t|||||\}}}td|\}} || ||fS)z*Internal compressed OP_MSG message helper.r)rr) rrMrrrrrrrZ op_messager-r-r._op_msg_uncompresseds rc Cs||d<|durd|vr|jr|j|d<tt|}z t|}||}Wn ty3d}d}Ynwz#|rHt||||||W|rG|||<SSt |||||W|rW|||<SS|r^|||<w)zGet a OP_MSG message.$dbNr1rB) r2r3nextiter _FIELD_MAPrKrmKeyErrorrr) rrMZdbnamer6rrrrrr-r-r.rs.       rc Cs^t|d|}|rt|d|}nd}tt|t|} dt|t|t|t|||g| fS)zGet an OP_QUERY message.Fr)rrZr4rrr ) rkcollection_name num_to_skip num_to_returnqueryfield_selectorrrZefsrr-r-r. _query_impls  rc Cs2t|||||||\}} td||\} } | | | fS)z)Internal compressed query message helper.)rr) rkrrrrrrrop_queryrrrr-r-r._query_compresseds  rc Cs0t|||||||\}}td|\} } | | |fS)zInternal query message helper.r)rr) rkrrrrrrrrrrr-r-r._query_uncompresseds  r c Cs.|r t||||||||St|||||||S)zGet a **query** message.)rr )rkrrrrrrrr-r-r.rsrzrcCs|r t||||St|||S)zGet a **getMore** message.)rrrr-r-r.rs rc@seZdZdZdZddZddZddZd d Ze d d Z e d dZ e ddZ e ddZ ddZddZddZddZddZdS)_BulkWriteContextzCA wrapper around SocketInfo for use with write splitting functions.) db_namer~op_idrfieldpublish start_time listenersrurop_typecodecc Csn||_||_||_||_|j|_||_t|j|_|jr!t j nd|_ ||_ |j r,dnd|_||_||_dS)NTF)rr~rrZenabled_for_commandsrrrrdatetimenowrrurrrr) rZ database_namecmd_namer~Z operation_idrrurrr-r-r.r*s  z_BulkWriteContext.__init__cCs<|jd}t||j|||j|\}}}|std|||fS)N.$cmdcannot do an empty bulk write)r_do_batched_op_msgrrr)rrwrrrrto_sendr-r-r._batch_command:s   z _BulkWriteContext._batch_commandcCs8|||\}}}|||||}|||j||fSr)r" write_commandZ_process_responseru)rrwrrrrr!rNr-r-r.executeCsz_BulkWriteContext.executecCs(|||\}}}||||d||S)Nr)r" unack_write)rrwrrrrr!r-r-r. execute_unackIsz_BulkWriteContext.execute_unackcC|jjS)z#A proxy for SockInfo.max_bson_size.)r~rrr-r-r.rSz_BulkWriteContext.max_bson_sizecCs|jr |jjdS|jjS)z&A proxy for SockInfo.max_message_size.rR)rr~max_message_sizerr-r-r.r)Xs z"_BulkWriteContext.max_message_sizecCr')z*A proxy for SockInfo.max_write_batch_size.)r~max_write_batch_sizerr-r-r.r*`r(z&_BulkWriteContext.max_write_batch_sizecCr)z:The maximum size of a BSON command before batch splitting.)rrr-r-r.max_split_sizeerz _BulkWriteContext.max_split_sizec Cs<|jr|jdus Jtj|j}||||}tj}zxz,|j||}|jrJtj||}|dur?t|j||} nddi} | || |WnBt y} z6|jr|jdus]Jtj||}t | t rtt|j|| j } n t | tr}| j } nt| } ||| |d} ~ wwWtj|_|Stj|_w)zAA proxy for SocketInfo.unack_write that handles event publishing.Nr@r)rrrr_startr~r%rQr_succeed Exceptionrrdetailsrr=_fail) rrwrrrrdurationstartrNreplyexcfailurer-r-r.r%js>    z_BulkWriteContext.unack_writec Cs|jr|jdus Jtj|j}||||tj}zWz|j|||j}|jr1 sectionN)rCHECKSUM_PRESENTrrr4)rrrZfirst_payload_typeZfirst_payload_sizerr-r-r.rs    z _OpMsg.unpack)r;rrrrrrrrrrrrrrrr(rurxr{rr}rrr-r-r-r.rOs*    r)NNNr)xrrr)riorrVtypingrrrrmrrrr r Z bson.int64r Z bson.raw_bsonr r rZbson.sonrZpymongorZ_use_c ImportErrorZpymongo.errorsrrrrrrrrZ pymongo.hellorZpymongo.read_preferencesrZpymongo.write_concernrr,r+rbZ_INSERTZ_UPDATEZ_DELETEZ_EMPTYrgrcrhr rZrdrfrr(rr/r7r=rQrqr^rxrobjectrrrrrrrpackrrrrrrrrrrrrrrr Z_query_messagerr r rrZ_get_more_messagerrr>r9r9intrCrHrTrXrYr[r r:r_rirrrZ _UNPACK_REPLYr-r-r-r.s     (   #  3 6s            7(<  EnO