Skip to content

Commit 790f4a9

Browse files
author
Brendan W. McAdams
committed
To correct a regression, make WriteConcern immutable (Otherwise you can mutate static presets like SAFE at runtime)
* Reintroduced constructors which accept continueOnErrorForInsert args * To enable you to set continueOnErrorForInsert with the presets like SAFE, immutable "new WriteConcern like this with COEI changed" method added.
1 parent bda0e0a commit 790f4a9

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

src/main/com/mongodb/WriteConcern.java

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,32 @@ public WriteConcern( int w , int wtimeout , boolean fsync ){
146146
* @param j whether writes should wait for a journaling group commit
147147
*/
148148
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){
149170
_w = w;
150171
_wtimeout = wtimeout;
151172
_fsync = fsync;
152173
_j = j;
174+
_continueOnErrorForInsert = continueOnInsertError;
153175
}
154176

155177
/**
@@ -169,16 +191,35 @@ public WriteConcern( int w , int wtimeout , boolean fsync , boolean j ){
169191
* @param j whether writes should wait for a journaling group commit
170192
*/
171193
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 ){
172216
_w = w;
173217
_wtimeout = wtimeout;
174218
_fsync = fsync;
175219
_j = j;
220+
_continueOnErrorForInsert = continueOnInsertError;
176221
}
177222

178-
/**
179-
* Gets the object representing the "getlasterror" command
180-
* @return
181-
*/
182223
public BasicDBObject getCommand(){
183224
BasicDBObject _command = new BasicDBObject( "getlasterror" , 1 );
184225

@@ -215,14 +256,6 @@ public Object getWObject(){
215256
return _w;
216257
}
217258

218-
/**
219-
* Sets the w value (the write strategy)
220-
* @param w
221-
*/
222-
public void setW(int w) {
223-
_w = w;
224-
}
225-
226259
/**
227260
* Gets the w parameter (the write strategy)
228261
* @return
@@ -239,14 +272,6 @@ public String getWString(){
239272
return _w.toString();
240273
}
241274

242-
/**
243-
* Sets the write timeout (in milliseconds)
244-
* @param wtimeout
245-
*/
246-
public void setWtimeout(int wtimeout) {
247-
this._wtimeout = wtimeout;
248-
}
249-
250275
/**
251276
* Gets the write timeout (in milliseconds)
252277
* @return
@@ -255,14 +280,6 @@ public int getWtimeout(){
255280
return _wtimeout;
256281
}
257282

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-
266283
/**
267284
* Gets the fsync flag (fsync to disk on the server)
268285
* @return
@@ -339,14 +356,6 @@ public boolean equals( Object o ){
339356
return _fsync == that._fsync && _w == that._w && _wtimeout == that._wtimeout && _j == that._j && _continueOnErrorForInsert == that._continueOnErrorForInsert;
340357
}
341358

342-
/**
343-
* Sets the j parameter (journal syncing)
344-
* @param j
345-
*/
346-
public void setJ(boolean j) {
347-
this._j = j;
348-
}
349-
350359
/**
351360
* Gets the j parameter (journal syncing)
352361
* @return
@@ -356,13 +365,20 @@ public boolean getJ() {
356365
}
357366

358367
/**
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.
360369
* If there is a document which does not validate in the client, an exception will still
361370
* be thrown in the client.
371+
* This will return a *NEW INSTANCE* of WriteConcern with your preferred continueOnInsert value
372+
*
362373
* @param continueOnErrorForInsert
363374
*/
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");
366382
}
367383

368384
/**

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ public void testMultiInsertWithContinue() {
271271
DBObject inserted2 = BasicDBObjectBuilder.start("_id", id).add("x",3).add("y",4).get();
272272
DBObject inserted3 = BasicDBObjectBuilder.start().add("x",5).add("y",6).get();
273273
WriteConcern wc = new WriteConcern();
274-
wc.setContinueOnErrorForInsert(true);
275-
WriteResult r = c.insert(wc, inserted1, inserted2, inserted3);
274+
WriteConcern newWC = wc.continueOnErrorForInsert(true);
275+
WriteResult r = c.insert(newWC, inserted1, inserted2, inserted3);
276276
assertEquals( c.count(), 2 );
277277
}
278278

0 commit comments

Comments
 (0)