@@ -146,10 +146,32 @@ public WriteConcern( int w , int wtimeout , boolean fsync ){
146
146
* @param j whether writes should wait for a journaling group commit
147
147
*/
148
148
public WriteConcern ( int w , int wtimeout , boolean fsync , boolean j ){
149
+ this ( w , wtimeout , fsync , j , false );
150
+ }
151
+
152
+ /**
153
+ * Creates a WriteConcern object.
154
+ * <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
155
+ * <p> w represents the number of servers:
156
+ * <ul>
157
+ * <li>{@code w=-1} None, no checking is done</li>
158
+ * <li>{@code w=0} None, network socket errors raised</li>
159
+ * <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
160
+ * <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
161
+ * </ul>
162
+ * </p>
163
+ * @param w number of writes
164
+ * @param wtimeout timeout for write operation
165
+ * @param fsync whether or not to fsync
166
+ * @param j whether writes should wait for a journaling group commit
167
+ * @param continueOnInsertError if batch inserts should continue after the first error
168
+ */
169
+ public WriteConcern ( int w , int wtimeout , boolean fsync , boolean j , boolean continueOnInsertError ){
149
170
_w = w ;
150
171
_wtimeout = wtimeout ;
151
172
_fsync = fsync ;
152
173
_j = j ;
174
+ _continueOnErrorForInsert = continueOnInsertError ;
153
175
}
154
176
155
177
/**
@@ -169,16 +191,35 @@ public WriteConcern( int w , int wtimeout , boolean fsync , boolean j ){
169
191
* @param j whether writes should wait for a journaling group commit
170
192
*/
171
193
public WriteConcern ( String w , int wtimeout , boolean fsync , boolean j ){
194
+ this ( w , wtimeout , fsync , j , false );
195
+ }
196
+
197
+ /**
198
+ * Creates a WriteConcern object.
199
+ * <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
200
+ * <p> w represents the number of servers:
201
+ * <ul>
202
+ * <li>{@code w=-1} None, no checking is done</li>
203
+ * <li>{@code w=0} None, network socket errors raised</li>
204
+ * <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
205
+ * <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
206
+ * </ul>
207
+ * </p>
208
+ * @param w number of writes
209
+ * @param wtimeout timeout for write operation
210
+ * @param fsync whether or not to fsync
211
+ * @param j whether writes should wait for a journaling group commit
212
+ * @param continueOnInsertError if batch inserts should continue after the first error
213
+ * @return
214
+ */
215
+ public WriteConcern ( String w , int wtimeout , boolean fsync , boolean j , boolean continueOnInsertError ){
172
216
_w = w ;
173
217
_wtimeout = wtimeout ;
174
218
_fsync = fsync ;
175
219
_j = j ;
220
+ _continueOnErrorForInsert = continueOnInsertError ;
176
221
}
177
222
178
- /**
179
- * Gets the object representing the "getlasterror" command
180
- * @return
181
- */
182
223
public BasicDBObject getCommand (){
183
224
BasicDBObject _command = new BasicDBObject ( "getlasterror" , 1 );
184
225
@@ -215,14 +256,6 @@ public Object getWObject(){
215
256
return _w ;
216
257
}
217
258
218
- /**
219
- * Sets the w value (the write strategy)
220
- * @param w
221
- */
222
- public void setW (int w ) {
223
- _w = w ;
224
- }
225
-
226
259
/**
227
260
* Gets the w parameter (the write strategy)
228
261
* @return
@@ -239,14 +272,6 @@ public String getWString(){
239
272
return _w .toString ();
240
273
}
241
274
242
- /**
243
- * Sets the write timeout (in milliseconds)
244
- * @param wtimeout
245
- */
246
- public void setWtimeout (int wtimeout ) {
247
- this ._wtimeout = wtimeout ;
248
- }
249
-
250
275
/**
251
276
* Gets the write timeout (in milliseconds)
252
277
* @return
@@ -255,14 +280,6 @@ public int getWtimeout(){
255
280
return _wtimeout ;
256
281
}
257
282
258
- /**
259
- * Sets the fsync flag (fsync to disk on the server)
260
- * @param fsync
261
- */
262
- public void setFsync (boolean fsync ) {
263
- _fsync = fsync ;
264
- }
265
-
266
283
/**
267
284
* Gets the fsync flag (fsync to disk on the server)
268
285
* @return
@@ -339,14 +356,6 @@ public boolean equals( Object o ){
339
356
return _fsync == that ._fsync && _w == that ._w && _wtimeout == that ._wtimeout && _j == that ._j && _continueOnErrorForInsert == that ._continueOnErrorForInsert ;
340
357
}
341
358
342
- /**
343
- * Sets the j parameter (journal syncing)
344
- * @param j
345
- */
346
- public void setJ (boolean j ) {
347
- this ._j = j ;
348
- }
349
-
350
359
/**
351
360
* Gets the j parameter (journal syncing)
352
361
* @return
@@ -356,13 +365,20 @@ public boolean getJ() {
356
365
}
357
366
358
367
/**
359
- * Sets the "continue inserts on error" mode. This only applies to server side errors.
368
+ * Toggles the "continue inserts on error" mode. This only applies to server side errors.
360
369
* If there is a document which does not validate in the client, an exception will still
361
370
* be thrown in the client.
371
+ * This will return a *NEW INSTANCE* of WriteConcern with your preferred continueOnInsert value
372
+ *
362
373
* @param continueOnErrorForInsert
363
374
*/
364
- public void setContinueOnErrorForInsert (boolean continueOnErrorForInsert ) {
365
- this ._continueOnErrorForInsert = continueOnErrorForInsert ;
375
+ public WriteConcern continueOnErrorForInsert (boolean continueOnErrorForInsert ) {
376
+ if ( _w instanceof Integer )
377
+ return new WriteConcern ((Integer ) _w , _wtimeout , _fsync , _j , continueOnErrorForInsert );
378
+ else if ( _w instanceof String )
379
+ return new WriteConcern ((String ) _w , _wtimeout , _fsync , _j , continueOnErrorForInsert );
380
+ else
381
+ throw new IllegalStateException ("The w parameter must be an int or a String" );
366
382
}
367
383
368
384
/**
0 commit comments