List of usage examples for com.mongodb WriteConcern MAJORITY
WriteConcern MAJORITY
To view the source code for com.mongodb WriteConcern MAJORITY.
Click Source Link
From source file:com.torodb.torod.mongodb.unsafe.ToroQueryCommandProcessor.java
License:Open Source License
private WriteConcern getWriteConcern(BsonDocument document) { WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED; if (document.containsKey("writeConcern")) { BsonDocument writeConcernObject = document.get("writeConcern").asDocument(); BsonValue w = writeConcernObject.get("w"); int wtimeout = 0; boolean fsync = false; boolean j = false; boolean continueOnError; BsonValue jObject = writeConcernObject.get("j"); if (jObject != null && jObject.isBoolean() && jObject.asBoolean().getValue()) { fsync = true;// w w w. j av a 2 s.c o m j = true; continueOnError = true; } BsonValue wtimeoutObject = writeConcernObject.get("wtimneout"); if (wtimeoutObject != null && wtimeoutObject.isNumber()) { wtimeout = wtimeoutObject.asNumber().intValue(); } if (w != null) { if (w.isNumber()) { if (w.asNumber().intValue() <= 1 && wtimeout > 0) { throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1"); } writeConcern = new WriteConcern(w.asNumber().intValue(), wtimeout, fsync, j); } else if (w.isString() && w.asString().getValue().equals("majority")) { if (wtimeout > 0) { throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1"); } writeConcern = new WriteConcern.Majority(wtimeout, fsync, j); } else { throw new IllegalArgumentException("w:" + w + " is not supported"); } } } return writeConcern; }
From source file:documentation.CausalConsistencyExamples.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string *///w ww . ja v a2s. c o m public static void main(final String[] args) { setupDatabase(); MongoClient client = MongoClients.create(); // Start Causal Consistency Example 1 // Example 1: Use a causally consistent session to ensure that the update occurs before the insert. ClientSession session1 = client .startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); Date currentDate = new Date(); MongoCollection<Document> items = client.getDatabase("test").withReadConcern(ReadConcern.MAJORITY) .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)) .getCollection("test"); items.updateOne(session1, eq("sku", "111"), set("end", currentDate)); Document document = new Document("sku", "nuts-111").append("name", "Pecans").append("start", currentDate); items.insertOne(session1, document); // End Causal Consistency Example 1 // Start Causal Consistency Example 2 // Example 2: Advance the cluster time and the operation time to that of the other session to ensure that // this client is causally consistent with the other session and read after the two writes. ClientSession session2 = client .startSession(ClientSessionOptions.builder().causallyConsistent(true).build()); session2.advanceClusterTime(session1.getClusterTime()); session2.advanceOperationTime(session1.getOperationTime()); items = client.getDatabase("test").withReadPreference(ReadPreference.secondary()) .withReadConcern(ReadConcern.MAJORITY) .withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)) .getCollection("items"); for (Document item : items.find(session2, eq("end", BsonNull.VALUE))) { System.out.println(item); } // End Causal Consistency Example 2 }
From source file:mongodb.JavaDocAdd.java
public static void main(String[] args) { try {/*from ww w .j a va2 s . c o m*/ MongoClient mongoClient = new MongoClient("localhost", 27017); mongoClient.setWriteConcern(WriteConcern.MAJORITY); DB db = mongoClient.getDB("words"); DBCollection collection = db.getCollection("word_stats"); JavaDocAdd.showNewDocs(collection, "Before Additions"); JavaDocAdd.addSelfie(collection); JavaDocAdd.showNewDocs(collection, "After adding single"); JavaDocAdd.addGoogleAndTweet(collection); JavaDocAdd.showNewDocs(collection, "After adding mutliple"); } catch (Exception e) { System.out.println(e); } }
From source file:mongodb.JavaDocDelete.java
public static void main(String[] args) { try {/*from w w w.j a v a 2 s .c o m*/ MongoClient mongoClient = new MongoClient("localhost", 27017); mongoClient.setWriteConcern(WriteConcern.MAJORITY); DB db = mongoClient.getDB("words"); DBCollection collection = db.getCollection("word_stats"); JavaDocDelete.showNewDocs(collection, "Before delete"); JavaDocDelete.removeNewDocs(collection); JavaDocDelete.showNewDocs(collection, "After delete"); } catch (Exception e) { System.out.println(e); } }
From source file:net.acesinc.nifi.processors.mongodb.PartialUpdateMongo.java
protected WriteConcern getWriteConcern(final ProcessContext context) { final String writeConcernProperty = context.getProperty(WRITE_CONCERN).getValue(); WriteConcern writeConcern = null;/* ww w . j a v a 2 s . co m*/ switch (writeConcernProperty) { case WRITE_CONCERN_ACKNOWLEDGED: writeConcern = WriteConcern.ACKNOWLEDGED; break; case WRITE_CONCERN_UNACKNOWLEDGED: writeConcern = WriteConcern.UNACKNOWLEDGED; break; case WRITE_CONCERN_FSYNCED: writeConcern = WriteConcern.FSYNCED; break; case WRITE_CONCERN_JOURNALED: writeConcern = WriteConcern.JOURNALED; break; case WRITE_CONCERN_REPLICA_ACKNOWLEDGED: writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED; break; case WRITE_CONCERN_MAJORITY: writeConcern = WriteConcern.MAJORITY; break; default: writeConcern = WriteConcern.ACKNOWLEDGED; } return writeConcern; }
From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java
License:Apache License
/** * Returns the default write concern depending on MongoDB deployment. * <ul>/* w w w .j a v a 2 s .c om*/ * <li>{@link WriteConcern#MAJORITY}: for a MongoDB replica set</li> * <li>{@link WriteConcern#ACKNOWLEDGED}: for single MongoDB instance</li> * </ul> * * @param db the connection to MongoDB. * @return the default write concern to use for Oak. */ public static WriteConcern getDefaultWriteConcern(@Nonnull DB db) { WriteConcern w; if (checkNotNull(db).getMongo().getReplicaSetStatus() != null) { w = WriteConcern.MAJORITY; } else { w = WriteConcern.ACKNOWLEDGED; } return w; }
From source file:org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.java
License:Apache License
/** * Returns {@code true} if the default write concern on the {@code db} is * sufficient for Oak. On a replica set Oak expects at least w=2. For * a single MongoDB node deployment w=1 is sufficient. * * @param db the database./* ww w.j a va 2 s. c o m*/ * @return whether the write concern is sufficient. */ public static boolean hasSufficientWriteConcern(@Nonnull DB db) { Object wObj = checkNotNull(db).getWriteConcern().getWObject(); int w; if (wObj instanceof Number) { w = ((Number) wObj).intValue(); } else if (wObj == null) { // default acknowledged w = 1; } else if (WriteConcern.MAJORITY.getWString().equals(wObj)) { // majority in a replica set means at least w=2 w = 2; } else { throw new IllegalArgumentException("Unknown write concern: " + db.getWriteConcern()); } if (db.getMongo().getReplicaSetStatus() != null) { return w >= 2; } else { return w >= 1; } }
From source file:org.apache.nifi.mongodb.AbstractMongoDBControllerService.java
License:Apache License
protected WriteConcern getWriteConcern(final ConfigurationContext context) { final String writeConcernProperty = context.getProperty(WRITE_CONCERN).getValue(); WriteConcern writeConcern = null;//from ww w . ja v a 2 s . c o m switch (writeConcernProperty) { case WRITE_CONCERN_ACKNOWLEDGED: writeConcern = WriteConcern.ACKNOWLEDGED; break; case WRITE_CONCERN_UNACKNOWLEDGED: writeConcern = WriteConcern.UNACKNOWLEDGED; break; case WRITE_CONCERN_FSYNCED: writeConcern = WriteConcern.FSYNCED; break; case WRITE_CONCERN_JOURNALED: writeConcern = WriteConcern.JOURNALED; break; case WRITE_CONCERN_REPLICA_ACKNOWLEDGED: writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED; break; case WRITE_CONCERN_MAJORITY: writeConcern = WriteConcern.MAJORITY; break; default: writeConcern = WriteConcern.ACKNOWLEDGED; } return writeConcern; }
From source file:org.apache.tapestry5.mongodb.modules.MongodbModule.java
License:Apache License
/** * Contribute coercions for {@link WriteConcern} and {@link ReadPreference} to have them from * {@link org.apache.tapestry5.ioc.annotations.Symbol} * * @param configuration lets help the {@link org.apache.tapestry5.ioc.services.TypeCoercer} service *///from w w w .j ava 2 s .co m public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) { configuration.add(new CoercionTuple(String.class, WriteConcern.class, new Coercion<String, WriteConcern>() { public WriteConcern coerce(String input) { if (input.equalsIgnoreCase("FSYNC_SAFE")) { return WriteConcern.FSYNC_SAFE; } else if (input.equalsIgnoreCase("JOURNAL_SAFE")) { return WriteConcern.JOURNAL_SAFE; } else if (input.equalsIgnoreCase("MAJORITY")) { return WriteConcern.MAJORITY; } else if (input.equalsIgnoreCase("NONE")) { return WriteConcern.NONE; } else if (input.equalsIgnoreCase("REPLICAS_SAFE")) { return WriteConcern.REPLICAS_SAFE; } else if (input.equalsIgnoreCase("SAFE")) { return WriteConcern.SAFE; } else if (input.equalsIgnoreCase("NORMAL")) { return WriteConcern.NORMAL; } else // WriteConcern.ACKNOWLEDGED IS OUR DEFAULT { return WriteConcern.ACKNOWLEDGED; } } })); configuration .add(new CoercionTuple(String.class, ReadPreference.class, new Coercion<String, ReadPreference>() { public ReadPreference coerce(String input) { if (input.equalsIgnoreCase("SECONDARY")) { return ReadPreference.secondary(); } else // PRIMARY IS OUR DEFAULT { return ReadPreference.primary(); } } })); }
From source file:org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec.java
License:Open Source License
/** * Connect with the specified properties and return the Connection. *//*www . j a v a 2s.c om*/ public Connection connectToDataSource(EISAccessor accessor, Properties properties) throws DatabaseException, ValidationException { if ((this.connectionFactory == null) && (this.name == null)) { this.connectionFactory = new MongoConnectionFactory(); } if (!properties.isEmpty()) { if (this.connectionSpec == null) { this.connectionSpec = new MongoJCAConnectionSpec(); } MongoJCAConnectionSpec spec = (MongoJCAConnectionSpec) this.connectionSpec; String host = (String) properties.get(HOST); String port = (String) properties.get(PORT); String db = (String) properties.get(DB); if (host != null) { if (host.indexOf(',') == -1) { spec.getHosts().add(host); if (port != null) { spec.getPorts().add(new Integer(port)); } } else { int startIndex = 0; while (startIndex < (host.length() - 1)) { int endIndex = host.indexOf(',', startIndex); if (endIndex == -1) { endIndex = host.length(); } String nextHost = host.substring(startIndex, endIndex); spec.getHosts().add(nextHost); startIndex = endIndex + 1; } while (startIndex < (port.length() - 1)) { int endIndex = port.indexOf(',', startIndex); if (endIndex == -1) { endIndex = port.length(); } String nextPort = port.substring(startIndex, endIndex); spec.getPorts().add(new Integer(nextPort)); startIndex = endIndex + 1; } } } if (db != null) { spec.setDB(db); } String user = (String) properties.get("user"); Object password = properties.get("password"); if (password instanceof String) { password = ((String) password).toCharArray(); } if ((user != null) && (user.length() != 0)) { spec.setUser(user); spec.setPassword((char[]) password); } // Allows setting of read preference as a property. Object preference = properties.get(READ_PREFERENCE); if (preference instanceof ReadPreference) { spec.setReadPreference((ReadPreference) preference); } else if (preference instanceof String) { String constant = (String) preference; if (constant.equals("PRIMARY")) { spec.setReadPreference(ReadPreference.PRIMARY); } else if (constant.equals("SECONDARY")) { spec.setReadPreference(ReadPreference.SECONDARY); } else { throw new EISException("Invalid read preference property value: " + constant); } } // Allows setting of write concern as a property. Object concern = properties.get(WRITE_CONCERN); if (concern instanceof WriteConcern) { spec.setWriteConcern((WriteConcern) concern); } else if (concern instanceof String) { String constant = (String) concern; if (constant.equals("FSYNC_SAFE")) { spec.setWriteConcern(WriteConcern.FSYNC_SAFE); } else if (constant.equals("ACKNOWLEDGED")) { spec.setWriteConcern(WriteConcern.ACKNOWLEDGED); } else if (constant.equals("JOURNAL_SAFE")) { spec.setWriteConcern(WriteConcern.JOURNAL_SAFE); } else if (constant.equals("MAJORITY")) { spec.setWriteConcern(WriteConcern.MAJORITY); } else if (constant.equals("NONE")) { spec.setWriteConcern(WriteConcern.NONE); } else if (constant.equals("NORMAL")) { spec.setWriteConcern(WriteConcern.NORMAL); } else if (constant.equals("REPLICAS_SAFE")) { spec.setWriteConcern(WriteConcern.REPLICAS_SAFE); } else if (constant.equals("SAFE")) { spec.setWriteConcern(WriteConcern.SAFE); } else { throw new EISException("Invalid read preference property value: " + constant); } } // Allows setting of options as a property. Object options = properties.get(OPTIONS); if (options instanceof Number) { spec.setOptions(((Number) options).intValue()); } else if (options instanceof String) { spec.setOptions(Integer.valueOf(((String) options))); } } return super.connectToDataSource(accessor, properties); }