@@ -76,7 +76,7 @@ public virtual BulkWriteResult Execute(MongoConnection connection)
7676 }
7777
7878 // protected methods
79- protected abstract BatchSerializer CreateBatchSerializer ( ) ;
79+ protected abstract BatchSerializer CreateBatchSerializer ( int maxBatchCount , int maxBatchLength , int maxDocumentSize , int maxWireDocumentSize ) ;
8080
8181 protected virtual IEnumerable < WriteRequest > DecorateRequests ( IEnumerable < WriteRequest > requests )
8282 {
@@ -120,7 +120,12 @@ private CommandOperation<CommandResult> CreateWriteCommandOperation(IMongoComman
120120
121121 private BulkWriteBatchResult ExecuteBatch ( MongoConnection connection , Batch < WriteRequest > batch , int originalIndex )
122122 {
123- var batchSerializer = CreateBatchSerializer ( ) ;
123+ var maxBatchCount = Math . Min ( _args . MaxBatchCount , connection . ServerInstance . MaxBatchCount ) ;
124+ var maxBatchLength = Math . Min ( _args . MaxBatchLength , connection . ServerInstance . MaxDocumentSize ) ;
125+ var maxDocumentSize = connection . ServerInstance . MaxDocumentSize ;
126+ var maxWireDocumentSize = connection . ServerInstance . MaxWireDocumentSize ;
127+
128+ var batchSerializer = CreateBatchSerializer ( maxBatchCount , maxBatchLength , maxDocumentSize , maxWireDocumentSize ) ;
124129 var writeCommand = CreateWriteCommand ( batchSerializer , batch ) ;
125130 var writeCommandOperation = CreateWriteCommandOperation ( writeCommand ) ;
126131 var writeCommandResult = writeCommandOperation . Execute ( connection ) ;
@@ -139,17 +144,23 @@ private BulkWriteBatchResult ExecuteBatch(MongoConnection connection, Batch<Writ
139144 protected abstract class BatchSerializer : BsonBaseSerializer
140145 {
141146 // private fields
142- private readonly BulkWriteOperationArgs _args ;
143147 private int _batchCount ;
144148 private int _batchLength ;
145149 private BatchProgress < WriteRequest > _batchProgress ;
146150 private int _batchStartPosition ;
147151 private int _lastRequestPosition ;
152+ private readonly int _maxBatchCount ;
153+ private readonly int _maxBatchLength ;
154+ private readonly int _maxDocumentSize ;
155+ private readonly int _maxWireDocumentSize ;
148156
149157 // constructors
150- public BatchSerializer ( BulkWriteOperationArgs args )
158+ public BatchSerializer ( int maxBatchCount , int maxBatchLength , int maxDocumentSize , int maxWireDocumentSize )
151159 {
152- _args = args ;
160+ _maxBatchCount = maxBatchCount ;
161+ _maxBatchLength = maxBatchLength ;
162+ _maxDocumentSize = maxDocumentSize ;
163+ _maxWireDocumentSize = maxWireDocumentSize ;
153164 }
154165
155166 // public properties
@@ -158,6 +169,27 @@ public BatchProgress<WriteRequest> BatchProgress
158169 get { return _batchProgress ; }
159170 }
160171
172+ // protected properties
173+ protected int MaxBatchCount
174+ {
175+ get { return _maxBatchCount ; }
176+ }
177+
178+ protected int MaxBatchLength
179+ {
180+ get { return _maxBatchLength ; }
181+ }
182+
183+ protected int MaxDocumentSize
184+ {
185+ get { return _maxDocumentSize ; }
186+ }
187+
188+ protected int MaxWireDocumentSize
189+ {
190+ get { return _maxWireDocumentSize ; }
191+ }
192+
161193 // public methods
162194 public override void Serialize ( BsonWriter bsonWriter , Type nominalType , object value , IBsonSerializationOptions options )
163195 {
@@ -174,20 +206,14 @@ public override void Serialize(BsonWriter bsonWriter, Type nominalType, object v
174206 continuationBatch . ClearPending ( ) ; // so pending objects can be garbage collected sooner
175207 }
176208
177- var maxBatchLength = _args . MaxBatchLength ;
178- if ( maxBatchLength > _args . MaxDocumentSize )
179- {
180- maxBatchLength = _args . MaxDocumentSize ; // not MaxWireDocumentSize! leave room for overhead
181- }
182-
183209 // always go one document too far so that we can set IsDone as early as possible
184210 var enumerator = batch . Enumerator ;
185211 while ( enumerator . MoveNext ( ) )
186212 {
187213 var request = enumerator . Current ;
188214 AddRequest ( bsonBinaryWriter , request ) ;
189215
190- if ( ( _batchCount > _args . MaxBatchCount || _batchLength > maxBatchLength ) && _batchCount > 1 )
216+ if ( ( _batchCount > _maxBatchCount || _batchLength > _maxBatchLength ) && _batchCount > 1 )
191217 {
192218 var serializedRequest = RemoveOverflow ( bsonBinaryWriter . Buffer ) ;
193219 var nextBatch = new ContinuationBatch < WriteRequest , IByteBuffer > ( enumerator , request , serializedRequest ) ;
0 commit comments