forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteConcern.java
More file actions
453 lines (394 loc) · 15.1 KB
/
WriteConcern.java
File metadata and controls
453 lines (394 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// WriteConcern.java
/**
* Copyright (C) 2008-2011 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
/**
* <p>WriteConcern control the write behavior for with various options, as well as exception raising on error conditions.</p>
*
* <p>
* <b>w</b>
* <ul>
* <li>-1 = don't even report network errors </li>
* <li> 0 = default, don't call getLastError by default </li>
* <li> 1 = basic, call getLastError, but don't wait for slaves</li>
* <li> 2+= wait for slaves </li>
* </ul>
* <b>wtimeout</b> how long to wait for slaves before failing
* <ul>
* <li>0 = indefinite </li>
* <li>> 0 = ms to wait </li>
* </ul>
* </p>
* <p><b>fsync</b> force fsync to disk </p>
*
* @dochub databases
*/
public class WriteConcern implements Serializable {
private static final long serialVersionUID = 1884671104750417011L;
/** No exceptions are raised, even for network issues */
public final static WriteConcern NONE = new WriteConcern(-1);
/** Exceptions are raised for network issues, but not server errors */
public final static WriteConcern NORMAL = new WriteConcern(0);
/** Exceptions are raised for network issues, and server errors; waits on a server for the write operation */
public final static WriteConcern SAFE = new WriteConcern(1);
/** Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation */
public final static WriteConcern MAJORITY = new Majority();
/** Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk*/
public final static WriteConcern FSYNC_SAFE = new WriteConcern(true);
/** Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk*/
public final static WriteConcern JOURNAL_SAFE = new WriteConcern( 1, 0, false, true );
/** Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/
public final static WriteConcern REPLICAS_SAFE = new WriteConcern(2);
// map of the constants from above for use by fromString
private static Map<String, WriteConcern> _namedConcerns = null;
/**
* Default constructor keeping all options as default
*/
public WriteConcern(){
this(0);
}
/**
* Calls {@link WriteConcern#WriteConcern(int, int, boolean)} with wtimeout=0 and fsync=false
* @param w number of writes
*/
public WriteConcern( int w ){
this( w , 0 , false );
}
/**
* Tag based Write Concern with wtimeout=0, fsync=false, and j=false
* @param w Write Concern tag
*/
public WriteConcern( String w ){
this( w , 0 , false, false );
}
/**
* Calls {@link WriteConcern#WriteConcern(int, int, boolean)} with fsync=false
* @param w number of writes
* @param wtimeout timeout for write operation
*/
public WriteConcern( int w , int wtimeout ){
this( w , wtimeout , false );
}
/**
* Calls {@link WriteConcern#WriteConcern(int, int, boolean)} with w=1 and wtimeout=0
* @param fsync whether or not to fsync
*/
public WriteConcern( boolean fsync ){
this( 1 , 0 , fsync);
}
/**
* Creates a WriteConcern object.
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
* <p> w represents the number of servers:
* <ul>
* <li>{@code w=-1} None, no checking is done</li>
* <li>{@code w=0} None, network socket errors raised</li>
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
* </ul>
* </p>
* @param w number of writes
* @param wtimeout timeout for write operation
* @param fsync whether or not to fsync
*/
public WriteConcern( int w , int wtimeout , boolean fsync ){
this(w, wtimeout, fsync, false);
}
/**
* Creates a WriteConcern object.
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
* <p> w represents the number of servers:
* <ul>
* <li>{@code w=-1} None, no checking is done</li>
* <li>{@code w=0} None, network socket errors raised</li>
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
* </ul>
* </p>
* @param w number of writes
* @param wtimeout timeout for write operation
* @param fsync whether or not to fsync
* @param j whether writes should wait for a journaling group commit
*/
public WriteConcern( int w , int wtimeout , boolean fsync , boolean j ){
this( w, wtimeout, fsync, j, false);
}
/**
* Creates a WriteConcern object.
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
* <p> w represents the number of servers:
* <ul>
* <li>{@code w=-1} None, no checking is done</li>
* <li>{@code w=0} None, network socket errors raised</li>
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
* </ul>
* </p>
* @param w number of writes
* @param wtimeout timeout for write operation
* @param fsync whether or not to fsync
* @param j whether writes should wait for a journaling group commit
* @param continueOnInsertError if batch inserts should continue after the first error
*/
public WriteConcern( int w , int wtimeout , boolean fsync , boolean j, boolean continueOnInsertError) {
_w = w;
_wtimeout = wtimeout;
_fsync = fsync;
_j = j;
_continueOnErrorForInsert = continueOnInsertError;
}
/**
* Creates a WriteConcern object.
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
* <p> w represents the number of servers:
* <ul>
* <li>{@code w=-1} None, no checking is done</li>
* <li>{@code w=0} None, network socket errors raised</li>
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
* </ul>
* </p>
* @param w number of writes
* @param wtimeout timeout for write operation
* @param fsync whether or not to fsync
* @param j whether writes should wait for a journaling group commit
*/
public WriteConcern( String w , int wtimeout , boolean fsync, boolean j ){
this( w, wtimeout, fsync, j, false);
}
/**
* Creates a WriteConcern object.
* <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
* <p> w represents the number of servers:
* <ul>
* <li>{@code w=-1} None, no checking is done</li>
* <li>{@code w=0} None, network socket errors raised</li>
* <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
* <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
* </ul>
* </p>
* @param w number of writes
* @param wtimeout timeout for write operation
* @param fsync whether or not to fsync
* @param j whether writes should wait for a journaling group commit
* @param continueOnInsertError if batch inserts should continue after the first error
* @return
*/
public WriteConcern( String w , int wtimeout , boolean fsync, boolean j, boolean continueOnInsertError ){
if (w == null) {
throw new IllegalArgumentException("w can not be null");
}
_w = w;
_wtimeout = wtimeout;
_fsync = fsync;
_j = j;
_continueOnErrorForInsert = continueOnInsertError;
}
public BasicDBObject getCommand(){
BasicDBObject _command = new BasicDBObject( "getlasterror" , 1 );
if ( _w instanceof Integer && ( (Integer) _w > 0) ||
( _w instanceof String && _w != null ) ){
_command.put( "w" , _w );
_command.put( "wtimeout" , _wtimeout );
}
if ( _fsync )
_command.put( "fsync" , true );
if ( _j )
_command.put( "j", true );
return _command;
}
/**
* Sets the w value (the write strategy)
* @param w
*/
public void setWObject(Object w) {
if ( ! (w instanceof Integer) && ! (w instanceof String) )
throw new IllegalArgumentException("The w parameter must be an int or a String");
this._w = w;
}
/**
* Gets the w value (the write strategy)
* @return
*/
public Object getWObject(){
return _w;
}
/**
* Gets the w parameter (the write strategy)
* @return
*/
public int getW(){
return (Integer) _w;
}
/**
* Gets the w parameter (the write strategy) in String format
* @return
*/
public String getWString(){
return _w.toString();
}
/**
* Gets the write timeout (in milliseconds)
* @return
*/
public int getWtimeout(){
return _wtimeout;
}
/**
* Gets the fsync flag (fsync to disk on the server)
* @return
*/
public boolean getFsync(){
return _fsync;
}
/**
* Gets the fsync flag (fsync to disk on the server)
* @return
*/
public boolean fsync(){
return _fsync;
}
/**
* Returns whether network error may be raised (w >= 0)
* @return
*/
public boolean raiseNetworkErrors(){
if (_w instanceof Integer)
return (Integer) _w >= 0;
return _w != null;
}
/**
* Returns whether "getlasterror" should be called (w > 0)
* @return
*/
public boolean callGetLastError(){
if (_w instanceof Integer)
return (Integer) _w > 0;
return _w != null;
}
/**
* Gets the WriteConcern constants by name: NONE, NORMAL, SAFE, FSYNC_SAFE,
* REPLICA_SAFE. (matching is done case insensitively)
* @param name
* @return
*/
public static WriteConcern valueOf(String name) {
if (_namedConcerns == null) {
HashMap<String, WriteConcern> newMap = new HashMap<String, WriteConcern>( 8 , 1 );
for (Field f : WriteConcern.class.getFields())
if (Modifier.isStatic( f.getModifiers() ) && f.getType().equals( WriteConcern.class )) {
try {
newMap.put( f.getName().toLowerCase(), (WriteConcern) f.get( null ) );
} catch (Exception e) {
throw new RuntimeException( e );
}
}
// Thought about doing a synchronize but this seems just as safe and
// I don't care about race conditions.
_namedConcerns = newMap;
}
return _namedConcerns.get( name.toLowerCase() );
}
@Override
public String toString(){
return "WriteConcern " + getCommand() + " / (Continue Inserting on Errors? " + getContinueOnErrorForInsert() + ")";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WriteConcern that = (WriteConcern) o;
if (_continueOnErrorForInsert != that._continueOnErrorForInsert) return false;
if (_fsync != that._fsync) return false;
if (_j != that._j) return false;
if (_wtimeout != that._wtimeout) return false;
if (!_w.equals(that._w)) return false;
return true;
}
@Override
public int hashCode() {
int result = _w.hashCode();
result = 31 * result + _wtimeout;
result = 31 * result + (_fsync ? 1 : 0);
result = 31 * result + (_j ? 1 : 0);
result = 31 * result + (_continueOnErrorForInsert ? 1 : 0);
return result;
}
/**
* Gets the j parameter (journal syncing)
* @return
*/
public boolean getJ() {
return _j;
}
/**
* Toggles the "continue inserts on error" mode. This only applies to server side errors.
* If there is a document which does not validate in the client, an exception will still
* be thrown in the client.
* This will return a *NEW INSTANCE* of WriteConcern with your preferred continueOnInsert value
*
* @param continueOnErrorForInsert
*/
public WriteConcern continueOnErrorForInsert(boolean continueOnErrorForInsert) {
if ( _w instanceof Integer )
return new WriteConcern((Integer) _w, _wtimeout, _fsync, _j, continueOnErrorForInsert);
else if ( _w instanceof String )
return new WriteConcern((String) _w, _wtimeout, _fsync, _j, continueOnErrorForInsert);
else
throw new IllegalStateException("The w parameter must be an int or a String");
}
/**
* Gets the "continue inserts on error" mode
* @return
*/
public boolean getContinueOnErrorForInsert() {
return _continueOnErrorForInsert;
}
/**
* Create a Majority Write Concern that requires a majority of
* servers to acknowledge the write.
*
* @param wtimeout timeout for write operation
* @param fsync whether or not to fsync
* @param j whether writes should wait for a journaling group commit
*/
public static Majority majorityWriteConcern( int wtimeout, boolean fsync, boolean j ) {
return new Majority( wtimeout, fsync, j );
}
Object _w = 0;
int _wtimeout = 0;
boolean _fsync = false;
boolean _j = false;
boolean _continueOnErrorForInsert = false;
public static class Majority extends WriteConcern {
private static final long serialVersionUID = -4128295115883875212L;
public Majority( ) {
super( "majority", 0, false, false );
}
public Majority( int wtimeout, boolean fsync, boolean j ){
super( "majority", wtimeout, fsync, j );
}
@Override
public String toString(){
return "[Majority] WriteConcern " + getCommand();
}
}
}