2323import org .bson .types .ObjectId ;
2424
2525import java .util .ArrayList ;
26- import java .util .Collection ;
2726import java .util .Collections ;
2827import java .util .HashMap ;
2928import java .util .Iterator ;
@@ -162,10 +161,7 @@ void killCursors( ServerAddress addr , List<Long> all ){
162161 if ( all == null || all .size () == 0 )
163162 return ;
164163
165- OutMessage om = new OutMessage ( _mongo , 2007 );
166- om .writeInt ( 0 ); // reserved
167-
168- om .writeInt ( Math .min ( NUM_CURSORS_PER_BATCH , all .size () ) );
164+ OutMessage om = OutMessage .killCursors (_mongo , Math .min ( NUM_CURSORS_PER_BATCH , all .size ()));
169165
170166 int soFar = 0 ;
171167 int totalSoFar = 0 ;
@@ -177,9 +173,7 @@ void killCursors( ServerAddress addr , List<Long> all ){
177173
178174 if ( soFar >= NUM_CURSORS_PER_BATCH ){
179175 _connector .say ( this , om ,com .mongodb .WriteConcern .NONE );
180- om = new OutMessage ( _mongo , 2007 );
181- om .writeInt ( 0 ); // reserved
182- om .writeInt ( Math .min ( NUM_CURSORS_PER_BATCH , all .size () - totalSoFar ) );
176+ om = OutMessage .killCursors (_mongo , Math .min ( NUM_CURSORS_PER_BATCH , all .size () - totalSoFar ));
183177 soFar = 0 ;
184178 }
185179 }
@@ -232,15 +226,11 @@ protected WriteResult insert(DBObject[] arr, boolean shouldApply , com.mongodb.W
232226
233227 int cur = 0 ;
234228 int maxsize = _mongo .getMaxBsonObjectSize ();
235- while ( cur < arr .length ){
236- OutMessage om = new OutMessage ( _mongo , 2002 , encoder );
229+ while ( cur < arr .length ) {
237230
238- int flags = 0 ;
239- if ( concern .getContinueOnErrorForInsert () ) flags |= 1 ;
240- om .writeInt ( flags );
241- om .writeCString ( _fullNameSpace );
231+ OutMessage om = OutMessage .insert ( this , encoder , concern );
242232
243- for ( ; cur <arr .length ; cur ++ ){
233+ for ( ; cur <arr .length ; cur ++ ){
244234 DBObject o = arr [cur ];
245235 om .putObject ( o );
246236
@@ -264,21 +254,7 @@ public WriteResult remove( DBObject o , com.mongodb.WriteConcern concern, DBEnco
264254
265255 if ( willTrace () ) trace ( "remove: " + _fullNameSpace + " " + JSON .serialize ( o ) );
266256
267- OutMessage om = new OutMessage ( _mongo , 2006 , encoder );
268-
269- om .writeInt ( 0 ); // reserved
270- om .writeCString ( _fullNameSpace );
271-
272- Collection <String > keys = o .keySet ();
273-
274- if ( keys .size () == 1 &&
275- keys .iterator ().next ().equals ( "_id" ) &&
276- o .get ( keys .iterator ().next () ) instanceof ObjectId )
277- om .writeInt ( 1 );
278- else
279- om .writeInt ( 0 );
280-
281- om .putObject ( o );
257+ OutMessage om = OutMessage .remove (this , encoder , o );
282258
283259 return _connector .say ( _db , om , concern );
284260 }
@@ -298,7 +274,7 @@ Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int
298274
299275 if ( willTrace () ) trace ( "find: " + _fullNameSpace + " " + JSON .serialize ( ref ) );
300276
301- OutMessage query = OutMessage .query ( _mongo , options , _fullNameSpace , numToSkip , chooseBatchSize (batchSize , limit , 0 ) , ref , fields , readPref ,
277+ OutMessage query = OutMessage .query ( this , options , numToSkip , chooseBatchSize (batchSize , limit , 0 ) , ref , fields , readPref ,
302278 encoder );
303279
304280 Response res = _connector .call ( _db , this , query , null , 2 , readPref , decoder );
@@ -334,17 +310,7 @@ public WriteResult update( DBObject query , DBObject o , boolean upsert , boolea
334310 trace ( "update: " + _fullNameSpace + " " + JSON .serialize ( query ) + " " + JSON .serialize ( o ) );
335311 }
336312
337- OutMessage om = new OutMessage ( _mongo , 2001 , encoder );
338- om .writeInt ( 0 ); // reserved
339- om .writeCString ( _fullNameSpace );
340-
341- int flags = 0 ;
342- if ( upsert ) flags |= 1 ;
343- if ( multi ) flags |= 2 ;
344- om .writeInt ( flags );
345-
346- om .putObject ( query );
347- om .putObject ( o );
313+ OutMessage om = OutMessage .update (this , encoder , upsert , multi , query , o );
348314
349315 return _connector .say ( _db , om , concern );
350316 }
@@ -446,12 +412,8 @@ private void _advance(){
446412 if ( _curResult .cursor () <= 0 )
447413 throw new RuntimeException ( "can't advance a cursor <= 0" );
448414
449- OutMessage m = new OutMessage ( _mongo , 2005 );
450-
451- m .writeInt ( 0 );
452- m .writeCString ( _collection ._fullNameSpace );
453- m .writeInt ( chooseBatchSize (_batchSize , _limit , _numFetched ) );
454- m .writeLong ( _curResult .cursor () );
415+ OutMessage m = OutMessage .getMore (_collection , _curResult .cursor (),
416+ chooseBatchSize (_batchSize , _limit , _numFetched ));
455417
456418 Response res = _connector .call ( DBApiLayer .this , _collection , m , _host , _decoder );
457419 _numGetMores ++;
0 commit comments