@@ -704,50 +704,48 @@ def _get_more(collection_name, num_to_return, cursor_id, ctx=None):
704
704
class _BulkWriteContext (object ):
705
705
"""A wrapper around SocketInfo for use with write splitting functions."""
706
706
707
- __slots__ = ('db_name' , 'command' , ' sock_info' , 'op_id' ,
707
+ __slots__ = ('db_name' , 'sock_info' , 'op_id' ,
708
708
'name' , 'field' , 'publish' , 'start_time' , 'listeners' ,
709
709
'session' , 'compress' , 'op_type' , 'codec' )
710
710
711
- def __init__ (self , database_name , command , sock_info , operation_id ,
711
+ def __init__ (self , database_name , cmd_name , sock_info , operation_id ,
712
712
listeners , session , op_type , codec ):
713
713
self .db_name = database_name
714
- self .command = command
715
714
self .sock_info = sock_info
716
715
self .op_id = operation_id
717
716
self .listeners = listeners
718
717
self .publish = listeners .enabled_for_commands
719
- self .name = next ( iter ( command ))
718
+ self .name = cmd_name
720
719
self .field = _FIELD_MAP [self .name ]
721
720
self .start_time = datetime .datetime .now () if self .publish else None
722
721
self .session = session
723
722
self .compress = True if sock_info .compression_context else False
724
723
self .op_type = op_type
725
724
self .codec = codec
726
- sock_info .add_server_api (command )
727
725
728
- def _batch_command (self , docs ):
726
+ def _batch_command (self , cmd , docs ):
729
727
namespace = self .db_name + '.$cmd'
730
728
request_id , msg , to_send = _do_batched_op_msg (
731
- namespace , self .op_type , self . command , docs , self .check_keys ,
729
+ namespace , self .op_type , cmd , docs , self .check_keys ,
732
730
self .codec , self )
733
731
if not to_send :
734
732
raise InvalidOperation ("cannot do an empty bulk write" )
735
733
return request_id , msg , to_send
736
734
737
- def execute (self , docs , client ):
738
- request_id , msg , to_send = self ._batch_command (docs )
739
- result = self .write_command (request_id , msg , to_send )
735
+ def execute (self , cmd , docs , client ):
736
+ request_id , msg , to_send = self ._batch_command (cmd , docs )
737
+ result = self .write_command (cmd , request_id , msg , to_send )
740
738
client ._process_response (result , self .session )
741
739
return result , to_send
742
740
743
- def execute_unack (self , docs , client ):
744
- request_id , msg , to_send = self ._batch_command (docs )
741
+ def execute_unack (self , cmd , docs , client ):
742
+ request_id , msg , to_send = self ._batch_command (cmd , docs )
745
743
# Though this isn't strictly a "legacy" write, the helper
746
744
# handles publishing commands and sending our message
747
745
# without receiving a result. Send 0 for max_doc_size
748
746
# to disable size checking. Size checking is handled while
749
747
# the documents are encoded to BSON.
750
- self .unack_write (request_id , msg , 0 , to_send )
748
+ self .unack_write (cmd , request_id , msg , 0 , to_send )
751
749
return to_send
752
750
753
751
@property
@@ -778,12 +776,12 @@ def max_split_size(self):
778
776
"""The maximum size of a BSON command before batch splitting."""
779
777
return self .max_bson_size
780
778
781
- def unack_write (self , request_id , msg , max_doc_size , docs ):
779
+ def unack_write (self , cmd , request_id , msg , max_doc_size , docs ):
782
780
"""A proxy for SocketInfo.unack_write that handles event publishing.
783
781
"""
784
782
if self .publish :
785
783
duration = datetime .datetime .now () - self .start_time
786
- cmd = self ._start (request_id , docs )
784
+ cmd = self ._start (cmd , request_id , docs )
787
785
start = datetime .datetime .now ()
788
786
try :
789
787
result = self .sock_info .unack_write (msg , max_doc_size )
@@ -811,12 +809,12 @@ def unack_write(self, request_id, msg, max_doc_size, docs):
811
809
self .start_time = datetime .datetime .now ()
812
810
return result
813
811
814
- def write_command (self , request_id , msg , docs ):
812
+ def write_command (self , cmd , request_id , msg , docs ):
815
813
"""A proxy for SocketInfo.write_command that handles event publishing.
816
814
"""
817
815
if self .publish :
818
816
duration = datetime .datetime .now () - self .start_time
819
- self ._start (request_id , docs )
817
+ self ._start (cmd , request_id , docs )
820
818
start = datetime .datetime .now ()
821
819
try :
822
820
reply = self .sock_info .write_command (request_id , msg )
@@ -836,9 +834,8 @@ def write_command(self, request_id, msg, docs):
836
834
self .start_time = datetime .datetime .now ()
837
835
return reply
838
836
839
- def _start (self , request_id , docs ):
837
+ def _start (self , cmd , request_id , docs ):
840
838
"""Publish a CommandStartedEvent."""
841
- cmd = self .command .copy ()
842
839
cmd [self .field ] = docs
843
840
self .listeners .publish_command_start (
844
841
cmd , self .db_name ,
@@ -871,10 +868,10 @@ def _fail(self, request_id, failure, duration):
871
868
class _EncryptedBulkWriteContext (_BulkWriteContext ):
872
869
__slots__ = ()
873
870
874
- def _batch_command (self , docs ):
871
+ def _batch_command (self , cmd , docs ):
875
872
namespace = self .db_name + '.$cmd'
876
873
msg , to_send = _encode_batched_write_command (
877
- namespace , self .op_type , self . command , docs , self .check_keys ,
874
+ namespace , self .op_type , cmd , docs , self .check_keys ,
878
875
self .codec , self )
879
876
if not to_send :
880
877
raise InvalidOperation ("cannot do an empty bulk write" )
@@ -885,17 +882,18 @@ def _batch_command(self, docs):
885
882
DEFAULT_RAW_BSON_OPTIONS )
886
883
return cmd , to_send
887
884
888
- def execute (self , docs , client ):
889
- cmd , to_send = self ._batch_command (docs )
885
+ def execute (self , cmd , docs , client ):
886
+ batched_cmd , to_send = self ._batch_command (cmd , docs )
890
887
result = self .sock_info .command (
891
- self .db_name , cmd , codec_options = _UNICODE_REPLACE_CODEC_OPTIONS ,
888
+ self .db_name , batched_cmd ,
889
+ codec_options = _UNICODE_REPLACE_CODEC_OPTIONS ,
892
890
session = self .session , client = client )
893
891
return result , to_send
894
892
895
- def execute_unack (self , docs , client ):
896
- cmd , to_send = self ._batch_command (docs )
893
+ def execute_unack (self , cmd , docs , client ):
894
+ batched_cmd , to_send = self ._batch_command (cmd , docs )
897
895
self .sock_info .command (
898
- self .db_name , cmd , write_concern = WriteConcern (w = 0 ),
896
+ self .db_name , batched_cmd , write_concern = WriteConcern (w = 0 ),
899
897
session = self .session , client = client )
900
898
return to_send
901
899
0 commit comments