#NEWLINE# @ staticmethod #NEWLINE# def _generate_insert_many_batches( collname , documents , ordered , write_concern , max_bson , max_count ) : #NEWLINE# #INDENT# msg = SON ( [ ( 'insert' , collname ) , ( 'ordered' , ordered ) , ( 'writeConcern' , write_concern . document ) ] ) #NEWLINE# buf = io . BytesIO ( ) #NEWLINE# buf . write ( BSON . encode ( msg ) ) #NEWLINE# buf . seek ( ( - 1 ) , io . SEEK_END ) #NEWLINE# buf . write ( b'\x04documents\x00' ) #NEWLINE# docs_start = buf . tell ( ) #NEWLINE# buf . write ( b'\x00\x00\x00\x00' ) #NEWLINE# def prepare_command( ) : #NEWLINE# #INDENT# docs_end = ( buf . tell ( ) + 1 ) #NEWLINE# buf . write ( b'\x00\x00' ) #NEWLINE# total_length = buf . tell ( ) #NEWLINE# buf . seek ( docs_start ) #NEWLINE# buf . write ( struct . pack ( '<i' , ( docs_end - docs_start ) ) ) #NEWLINE# buf . seek ( 0 ) #NEWLINE# buf . write ( struct . pack ( '<i' , total_length ) ) #NEWLINE# return BSON ( buf . getvalue ( ) ) #NEWLINE# #UNINDENT# idx = 0 #NEWLINE# for doc in documents : #NEWLINE# #INDENT# key = str ( idx ) . encode ( 'ascii' ) #NEWLINE# value = BSON . encode ( doc ) #NEWLINE# enough_size = ( ( ( ( ( buf . tell ( ) + len ( key ) ) + 2 ) + len ( value ) ) - docs_start ) > max_bson ) #NEWLINE# enough_count = ( idx >= msg ) #NEWLINE# if ( enough_size or enough_count ) : #NEWLINE# #INDENT# ( yield prepare_command ( ) ) #NEWLINE# buf . seek ( ( docs_start + 4 ) ) #NEWLINE# buf . truncate ( ) #NEWLINE# idx = 0 #NEWLINE# key = b'0' #NEWLINE# #UNINDENT# buf . write ( ( ( b'\x03' + key ) + b'\x00' ) ) #NEWLINE# buf . write ( value ) #NEWLINE# idx += 1 #NEWLINE# #UNINDENT# ( yield prepare_command ( ) )