Skip to content

Commit fefab8c

Browse files
committed
attempt to clean up and standardize writeConcern
1 parent d5ef166 commit fefab8c

File tree

3 files changed

+76
-142
lines changed

3 files changed

+76
-142
lines changed

src/main/com/mongodb/WriteConcern.java

Lines changed: 69 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ public WriteConcern( String w ){
9393
this( w , 0 , false, false );
9494
}
9595

96-
/**
97-
* Tag based Write Concern with configgable j and wtimeout=0, fsync=false
98-
* @param w Write Concern Tag
99-
* @param j whether writes should wait for a journaling group commit
100-
*/
101-
public WriteConcern( String w, boolean j ){
102-
this( w , 0 , false, j );
103-
}
104-
10596
/**
10697
* Calls {@link WriteConcern#WriteConcern(int, int, boolean)} with fsync=false
10798
* @param w number of writes
@@ -154,36 +145,11 @@ public WriteConcern( int w , int wtimeout , boolean fsync ){
154145
* @param fsync whether or not to fsync
155146
* @param j whether writes should wait for a journaling group commit
156147
*/
157-
public WriteConcern( int w , int wtimeout , boolean fsync, boolean j ){
158-
_wValue = w;
159-
_wtimeout = wtimeout;
160-
_fsync = fsync;
161-
_j = j;
162-
_continueOnErrorInsert = false;
163-
}
164-
165-
/**
166-
* Creates a WriteConcern object.
167-
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
168-
* <p> w represents the number of servers:
169-
* <ul>
170-
* <li>{@code w=-1} None, no checking is done</li>
171-
* <li>{@code w=0} None, network socket errors raised</li>
172-
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
173-
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
174-
* </ul>
175-
* </p>
176-
* @param w number of writes
177-
* @param wtimeout timeout for write operation
178-
* @param fsync whether or not to fsync
179-
* @param j whether writes should wait for a journaling group commit
180-
*/
181-
public WriteConcern( int w , int wtimeout , boolean fsync, boolean j , boolean continueInsertOnError ){
148+
public WriteConcern( int w , int wtimeout , boolean fsync , boolean j ){
182149
_wValue = w;
183150
_wtimeout = wtimeout;
184151
_fsync = fsync;
185152
_j = j;
186-
_continueOnErrorInsert = continueInsertOnError;
187153
}
188154

189155
/**
@@ -207,34 +173,8 @@ public WriteConcern( String w , int wtimeout , boolean fsync, boolean j ){
207173
_wtimeout = wtimeout;
208174
_fsync = fsync;
209175
_j = j;
210-
_continueOnErrorInsert = false;
211-
}
212-
213-
/**
214-
* Creates a WriteConcern object.
215-
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
216-
* <p> w represents the number of servers:
217-
* <ul>
218-
* <li>{@code w=-1} None, no checking is done</li>
219-
* <li>{@code w=0} None, network socket errors raised</li>
220-
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
221-
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
222-
* </ul>
223-
* </p>
224-
* @param w number of writes
225-
* @param wtimeout timeout for write operation
226-
* @param fsync whether or not to fsync
227-
* @param j whether writes should wait for a journaling group commit
228-
*/
229-
public WriteConcern( String w , int wtimeout , boolean fsync, boolean j , Boolean continueInsertOnError ){
230-
_wValue = w;
231-
_wtimeout = wtimeout;
232-
_fsync = fsync;
233-
_j = j;
234-
_continueOnErrorInsert = continueInsertOnError;
235176
}
236177

237-
238178
/**
239179
* Gets the object representing the "getlasterror" command
240180
* @return
@@ -258,24 +198,52 @@ public BasicDBObject getCommand(){
258198
}
259199

260200
/**
261-
* Gets the number of servers to write to
262-
* if W is not a string value, returns -999.
263-
*
264-
* You should migrate to using getWValue (returns Object)
265-
* or getWString (String)
201+
* Sets the w value (the write strategy)
202+
* @param wValue
203+
*/
204+
public void setWObject(Object wValue) {
205+
this._wValue = wValue;
206+
}
207+
208+
/**
209+
* Gets the w value (the write strategy)
210+
* @return
211+
*/
212+
public Object getWObject(){
213+
return _wValue;
214+
}
215+
216+
/**
217+
* Sets the w value (the write strategy)
218+
* @param w
219+
*/
220+
public void setW(int w) {
221+
_wValue = w;
222+
}
223+
224+
/**
225+
* Gets the w parameter (the write strategy)
266226
* @return
267227
*/
268228
public int getW(){
269-
if (_wValue instanceof Integer)
270-
return (Integer) _wValue;
271-
else
272-
return -999;
229+
return (Integer) _wValue;
273230
}
274-
231+
232+
/**
233+
* Gets the w parameter (the write strategy) in String format
234+
* @return
235+
*/
275236
public String getWString(){
276237
return _wValue.toString();
277238
}
278239

240+
/**
241+
* Sets the write timeout (in milliseconds)
242+
* @param wtimeout
243+
*/
244+
public void setWtimeout(int wtimeout) {
245+
this._wtimeout = wtimeout;
246+
}
279247

280248
/**
281249
* Gets the write timeout (in milliseconds)
@@ -286,20 +254,19 @@ public int getWtimeout(){
286254
}
287255

288256
/**
289-
* Returns whether writes wait for files to be synced to disk
290-
* @return
257+
* Sets the fsync flag (fsync to disk on the server)
258+
* @param fsync
291259
*/
292-
public boolean fsync(){
293-
return _fsync;
260+
public void setFsync(boolean fsync) {
261+
_fsync = fsync;
294262
}
295-
263+
296264
/**
297-
* Returns whether writes will await a group commit to the
298-
* journal.
299-
* @return boolean
265+
* Gets the fsync flag (fsync to disk on the server)
266+
* @return
300267
*/
301-
public boolean j(){
302-
return _j;
268+
public boolean getFsync(){
269+
return _fsync;
303270
}
304271

305272
/**
@@ -367,70 +334,35 @@ public boolean equals( Object o ){
367334
}
368335

369336
/**
370-
* Gets the w value (either int or string)
371-
* @return
372-
*/
373-
public Object getWValue(){
374-
return _wValue;
375-
}
376-
377-
/**
378-
* Clones this WriteConcern with a new String mode for "w" (WriteConcerns are immutable)
379-
* @param mode
380-
* @return
337+
* Sets the j parameter (journal syncing)
338+
* @param j
381339
*/
382-
public WriteConcern withW(String mode) {
383-
return _instance(mode, _wtimeout, _fsync, _j, _continueOnErrorInsert);
340+
public void setJ(boolean j) {
341+
this._j = j;
384342
}
385343

386344
/**
387-
* Clones this WriteConcern with a new int mode for "w" (WriteConcerns are immutable)
388-
* @param mode
389-
* @param w
390-
* @return
345+
* Gets the j parameter (journal syncing)
346+
* @return
391347
*/
392-
public WriteConcern withW(int w) {
393-
return _instance( w, _wtimeout, _fsync, _j, _continueOnErrorInsert );
394-
}
395-
396-
/**
397-
*
398-
* Clones this WriteConcern with a new "j" (WriteConcerns are immutable)
399-
* @param j
400-
* @return
401-
*/
402-
public WriteConcern withJ(boolean j) {
403-
return _instance( _wValue, _wtimeout, _fsync, _j, _continueOnErrorInsert );
348+
public boolean getJ() {
349+
return _j;
404350
}
405351

406-
407352
/**
408-
*
409-
* Clones this WriteConcern with a new boolean mode for "continue inserts on error" (WriteConcerns are immutable)
410-
* @param cont
411-
* @return
353+
* Sets the "continue inserts on error" mode
354+
* @param continueOnErrorInsert
412355
*/
413-
public WriteConcern withContinueOnErrorForInsert(boolean cont) {
414-
return _instance( _wValue, _wtimeout, _fsync, _j, cont );
356+
public void setContinueOnErrorInsert(boolean continueOnErrorInsert) {
357+
this._continueOnErrorInsert = continueOnErrorInsert;
415358
}
416359

417360
/**
418-
*
419-
* Clones this WriteConcern with a new boolean mode for fsync (WriteConcerns are immutable)
420-
* @param fsync
421-
* @return
361+
* Gets the "continue inserts on error" mode
362+
* @return
422363
*/
423-
public WriteConcern withFsync(boolean fsync) {
424-
return _instance( _wValue, _wtimeout, fsync, _j, _continueOnErrorInsert );
425-
}
426-
427-
428-
protected WriteConcern _instance( Object wValue, int wtimeout, boolean fsync, boolean j, boolean cont ){
429-
if (wValue instanceof Integer)
430-
return new WriteConcern( (Integer) wValue, wtimeout, fsync, j, cont );
431-
else if (wValue instanceof String)
432-
return new WriteConcern( (String) wValue, wtimeout, fsync, j, cont );
433-
else throw new IllegalArgumentException( "W must be a String or Integer." );
364+
public boolean getContinueOnErrorInsert() {
365+
return _continueOnErrorInsert;
434366
}
435367

436368
/**
@@ -446,11 +378,11 @@ public static Majority majorityWriteConcern( int wtimeout, boolean fsync, boolea
446378
}
447379

448380

449-
Object _wValue;
450-
final int _wtimeout;
451-
final boolean _fsync;
452-
final boolean _j;
453-
final boolean _continueOnErrorInsert;
381+
Object _wValue = 0;
382+
int _wtimeout = 0;
383+
boolean _fsync = false;
384+
boolean _j = false;
385+
boolean _continueOnErrorInsert = false;
454386

455387
public static class Majority extends WriteConcern {
456388

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ public void testMultiInsertWithContinue() {
270270
DBObject inserted1 = BasicDBObjectBuilder.start("_id", id).add("x",1).add("y",2).get();
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();
273-
WriteResult r = c.insert(WriteConcern.NORMAL.withContinueOnErrorForInsert( true ), inserted1,inserted2, inserted3);
273+
WriteConcern wc = new WriteConcern();
274+
wc.setContinueOnErrorInsert(true);
275+
WriteResult r = c.insert(wc, inserted1, inserted2, inserted3);
274276
assertEquals( c.count(), 2 );
275277
}
276278

src/test/com/mongodb/JavaClientTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,9 @@ public void testWriteConcernValueOf() {
808808
assertEquals( wc1, wc2 );
809809
assertEquals( wc1, wc3 );
810810
assertEquals( wc1.getW(), wc2.getW() );
811-
assertEquals( wc1.getWValue(), wc2.getWValue() );
811+
assertEquals( wc1.getWObject(), wc2.getWObject() );
812812
assertEquals( wc1.getW(), wc3.getW() );
813-
assertEquals( wc1.getWValue(), wc3.getWValue() );
813+
assertEquals( wc1.getWObject(), wc3.getWObject() );
814814
}
815815

816816
@Test
@@ -822,9 +822,9 @@ public void testWriteConcernMajority() {
822822
assertEquals( wc1, wc2 );
823823
assertEquals( wc1, wc3 );
824824
assertEquals( wc1.getWString(), wc2.getWString() );
825-
assertEquals( wc1.getWValue(), wc2.getWValue() );
825+
assertEquals( wc1.getWObject(), wc2.getWObject() );
826826
assertEquals( wc1.getWString(), wc3.getWString() );
827-
assertEquals( wc1.getWValue(), wc3.getWValue() );
827+
assertEquals( wc1.getWObject(), wc3.getWObject() );
828828
}
829829

830830
@Test

0 commit comments

Comments
 (0)