/src/main/com/mongodb/WriteConcern.java

https://github.com/pohutukawa/mongo-java-driver · Java · 146 lines · 60 code · 23 blank · 63 comment · 2 complexity · 1d47e6fe426097c37812ec1f45e629a1 MD5 · raw file

  1. // WriteConcern.java
  2. /**
  3. * Copyright (C) 2008 10gen Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.mongodb;
  18. /**
  19. * <p>WriteConcern control the write behavior for with various options, as well as exception raising on error conditions.</p>
  20. *
  21. * <p>
  22. * <b>w</b>
  23. * <ul>
  24. * <li>-1 = don't even report network errors </li>
  25. * <li> 0 = default, don't call getLastError by default </li>
  26. * <li> 1 = basic, call getLastError, but don't wait for slaves</li>
  27. * <li> 2+= wait for slaves </li>
  28. * </ul>
  29. * <b>wtimeout</b> how long to wait for slaves before failing
  30. * <ul>
  31. * <li>0 = indefinite </li>
  32. * <li>> 0 = ms to wait </li>
  33. * </ul>
  34. * </p>
  35. * <p><b>fsync</b> force fsync to disk </p>
  36. *
  37. * @dochub databases
  38. */
  39. public class WriteConcern {
  40. /** No exceptions are raised, even for network issues */
  41. public final static WriteConcern NONE = new WriteConcern(-1);
  42. /** Exceptions are raised for network issues, but not server errors */
  43. public final static WriteConcern NORMAL = new WriteConcern(0);
  44. /** Exceptions are raised for network issues, and server errors; waits on a server for the write operation */
  45. public final static WriteConcern SAFE = new WriteConcern(1);
  46. @Deprecated /** use SAFE */
  47. public final static WriteConcern STRICT = SAFE;
  48. /** Exceptions are raised for network issues, and server errors and the write operation waits for the server to flush the data to disk*/
  49. public final static WriteConcern FSYNC_SAFE = new WriteConcern(true);
  50. /** Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation*/
  51. public final static WriteConcern REPLICAS_SAFE = new WriteConcern(2);
  52. public WriteConcern(){
  53. this(0);
  54. }
  55. /** <p>Specifies the number of servers to wait for on the write operation, and exception raising behavior </p>
  56. * <p> w represents # of servers:
  57. * <ul>
  58. * <li>{@code w=-1} None, no checking is done</li>
  59. * <li>{@code w=0} None, network socket errors raised</li>
  60. * <li>{@code w=1} Checks server for errors as well as network socket errors raised</li>
  61. * <li>{@code w>1} Checks servers (w) for errors as well as network socket errors raised</li>
  62. * </ul>
  63. * </p>
  64. *
  65. **/
  66. public WriteConcern( int w ){
  67. this( w , 0 , false );
  68. }
  69. /** <p>Specifies the number of servers to wait for on the write operation, and the amount of time (ms) to wait.</p>
  70. * <p> Note: w should be > 1 </p>
  71. *
  72. **/
  73. public WriteConcern( int w , int wtimeout ){
  74. this( w , wtimeout , false );
  75. }
  76. public WriteConcern( boolean fsync ){
  77. this( 1 , 0 , fsync);
  78. }
  79. /** <p>Specifies the number of servers to wait for on the write operation, and the amount of time (ms) to wait.</p>
  80. * <p> Note: w should be > 1 </p>
  81. **/
  82. public WriteConcern( int w , int wtimeout , boolean fsync ){
  83. _w = w;
  84. _wtimeout = wtimeout;
  85. _fsync = fsync;
  86. _command = new BasicDBObject( "getlasterror" , 1 );
  87. if ( _w > 0 ){
  88. _command.put( "w" , _w );
  89. _command.put( "wtimeout" , wtimeout );
  90. }
  91. if ( _fsync )
  92. _command.put( "fsync" , true );
  93. }
  94. public BasicDBObject getCommand(){
  95. return _command;
  96. }
  97. /** @return the number of servers to write to */
  98. public int getW(){
  99. return _w;
  100. }
  101. /** @return the write timeout (in milliseconds) */
  102. public int getWtimeout(){
  103. return _wtimeout;
  104. }
  105. /** @return If files are sync'd to disk. */
  106. public boolean fsync(){
  107. return _fsync;
  108. }
  109. public boolean raiseNetworkErrors(){
  110. return _w >= 0;
  111. }
  112. public boolean callGetLastError(){
  113. return _w > 0;
  114. }
  115. @Override
  116. public String toString(){
  117. return "WriteConcern " + _command;
  118. }
  119. final int _w;
  120. final int _wtimeout;
  121. final boolean _fsync;
  122. final BasicDBObject _command;
  123. }