本文整理汇总了Java中com.mongodb.WriteConcern类的典型用法代码示例。如果您正苦于以下问题:Java WriteConcern类的具体用法?Java WriteConcern怎么用?Java WriteConcern使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WriteConcern类属于com.mongodb包,在下文中一共展示了WriteConcern类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newMongoDbClientOptionsFactoryBean
import com.mongodb.WriteConcern; //导入依赖的package包/类
/**
* New mongo db client options factory bean.
*
* @param mongo the mongo properties.
* @return the mongo client options factory bean
*/
public static MongoClientOptionsFactoryBean newMongoDbClientOptionsFactoryBean(final AbstractMongoInstanceProperties mongo) {
try {
final MongoClientOptionsFactoryBean bean = new MongoClientOptionsFactoryBean();
bean.setWriteConcern(WriteConcern.valueOf(mongo.getWriteConcern()));
bean.setHeartbeatConnectTimeout(Long.valueOf(mongo.getTimeout()).intValue());
bean.setHeartbeatSocketTimeout(Long.valueOf(mongo.getTimeout()).intValue());
bean.setMaxConnectionLifeTime(mongo.getConns().getLifetime());
bean.setSocketKeepAlive(mongo.isSocketKeepAlive());
bean.setMaxConnectionIdleTime(Long.valueOf(mongo.getIdleTimeout()).intValue());
bean.setConnectionsPerHost(mongo.getConns().getPerHost());
bean.setSocketTimeout(Long.valueOf(mongo.getTimeout()).intValue());
bean.setConnectTimeout(Long.valueOf(mongo.getTimeout()).intValue());
bean.afterPropertiesSet();
return bean;
} catch (final Exception e) {
throw new BeanCreationException(e.getMessage(), e);
}
}
示例2: insert
import com.mongodb.WriteConcern; //导入依赖的package包/类
public Object insert(DBCollection collection, WriteConcern writeConcern) {
DBObject document = new BasicDBObject();
// 匹配_id
for (int i = 0, n = columns.size(); i < n; i++) {
// document.put(columns.get(i), values.get(i).getValue());
String tempColumn = columns.get(i);
if (3 == tempColumn.length() && tempColumn.equals("_id")) {
document.put(tempColumn, new ObjectId(values.get(i).getValue().toString()));
} else {
document.put(tempColumn, values.get(i).getValue());
}
}
log(document);
// TODO: WriteConcern.ACKNOWLEDGED需要可以配置
// WriteResult result = collection.insert(document, WriteConcern.ACKNOWLEDGED);
// collection.insert(document, MongoComponent.getInstance().getDefaultWriteConcern());
collection.insert(document, writeConcern);
Object oid = document.get("_id");
if (null != oid) {
return oid.toString();
}
return null;
}
示例3: writeConcern
import com.mongodb.WriteConcern; //导入依赖的package包/类
/**
* Prepares {@link WriteConcern} from given {@link WriteConcernEnum}
*
* @param writeConcernEnum write concern provided in config.
* @return write concern for mongo client option.
*/
public static WriteConcern writeConcern(WriteConcernEnum writeConcernEnum) {
switch (writeConcernEnum) {
case MAJORITY:
return WriteConcern.MAJORITY;
case JOURNALED:
return WriteConcern.JOURNALED;
case ACKNOWLEDGED:
return WriteConcern.ACKNOWLEDGED;
case UNACKNOWLEDGED:
return WriteConcern.UNACKNOWLEDGED;
default:
return null;
}
}
示例4: main
import com.mongodb.WriteConcern; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("mydb");
DBCollection coll = db.getCollection("questionsCollection");
mongoClient.setWriteConcern(WriteConcern.JOURNALED);
GIFTParser p = new GIFTParser();
BasicDBObject doc = null;
for (Question q : p.parserGIFT("Data/questionsGIFT")) {
doc = new BasicDBObject("category", q.getCategory())
.append("question", q.getText())
.append("correctanswer", q.getCorrectAnswer())
.append("wrongAnswers",q.getWrongAnswers());
coll.insert(doc);
}
DBCursor cursor = coll.find();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
}
示例5: removeOldPackageTrack
import com.mongodb.WriteConcern; //导入依赖的package包/类
public static void removeOldPackageTrack(int p_nodeid)
{
try {
logger.info("Purging Package Logs for NodeID: " + p_nodeid);
DB db;
String dbName = com.clavain.muninmxcd.p.getProperty("mongo.dbessentials");
db = m.getDB(dbName);
db.setWriteConcern(WriteConcern.SAFE);
DBCollection col = db.getCollection("trackpkg");
BasicDBObject query = new BasicDBObject();
query.append("node", p_nodeid);
col.remove(query);
db.setWriteConcern(WriteConcern.NONE);
} catch (Exception ex)
{
logger.error("Error in removeOldPackageTrack: " + ex.getLocalizedMessage());
}
}
示例6: removeConfigurationByQuery
import com.mongodb.WriteConcern; //导入依赖的package包/类
public static boolean removeConfigurationByQuery(BasicDBObject query, MongoConnection mongoConnection) {
DBCollection dbCol = mongoConnection.getDBCollection();
if (dbCol == null) {
throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
}
try {
if(query ==null) {
return false;
}
WriteResult result = dbCol.remove(query, WriteConcern.SAFE);
if(result.getLastError().ok()) {
return true;
}
} catch (Exception err) {
throw new MongoConfigRuntimeException(err);
}
return true;
}
示例7: mongo
import com.mongodb.WriteConcern; //导入依赖的package包/类
@Override
public Mongo mongo() throws Exception {
List<ServerAddress> serverAddresses = hosts.stream()
.map(this::createServerAddress)
.collect(toList());
LOG.info("Connecting to DB hosts: {}...", serverAddresses);
if (serverAddresses.size() == 1) {
// create a mongo client that connects to a single database,
// this is NOT the same as calling the constructor with a list of ServerAddresses with only one element!
return new MongoClient(serverAddresses.get(0), mongoCredentials());
} else {
// create a mongo client that connects to a replicaset
MongoClientOptions options = MongoClientOptions.builder()
.writeConcern(WriteConcern.ACKNOWLEDGED)
.build();
return new MongoClient(serverAddresses, mongoCredentials(), options);
}
}
示例8: init
import com.mongodb.WriteConcern; //导入依赖的package包/类
public void init(
Stage.Context context,
List<Stage.ConfigIssue> issues,
ReadPreference readPreference,
WriteConcern writeConcern
) {
mongoClient = createClient(context, issues, readPreference, writeConcern);
if (!issues.isEmpty()) {
return;
}
mongoDatabase = createMongoDatabase(context, issues, readPreference, writeConcern);
if (!issues.isEmpty()) {
return;
}
mongoCollection = createMongoCollection(context, issues, readPreference, writeConcern);
}
示例9: createMongoDatabase
import com.mongodb.WriteConcern; //导入依赖的package包/类
private MongoDatabase createMongoDatabase(
Stage.Context context,
List<Stage.ConfigIssue> issues,
ReadPreference readPreference,
WriteConcern writeConcern
) {
MongoDatabase mongoDatabase = null;
try {
if (readPreference != null) {
mongoDatabase = mongoClient.getDatabase(database).withReadPreference(readPreference);
} else if (writeConcern != null) {
mongoDatabase = mongoClient.getDatabase(database).withWriteConcern(writeConcern);
}
} catch (MongoClientException e) {
issues.add(context.createConfigIssue(
Groups.MONGODB.name(),
MONGO_CONFIG_PREFIX + "database",
Errors.MONGODB_02,
database,
e.toString()
));
}
return mongoDatabase;
}
示例10: createMongoCollection
import com.mongodb.WriteConcern; //导入依赖的package包/类
private MongoCollection createMongoCollection(
Stage.Context context,
List<Stage.ConfigIssue> issues,
ReadPreference readPreference,
WriteConcern writeConcern
) {
MongoCollection mongoCollection = null;
try {
if (readPreference != null) {
mongoCollection = mongoDatabase.getCollection(collection).withReadPreference(readPreference);
} else if (writeConcern != null) {
mongoCollection = mongoDatabase.getCollection(collection).withWriteConcern(writeConcern);
}
} catch (MongoClientException e) {
issues.add(context.createConfigIssue(
Groups.MONGODB.name(),
MONGO_CONFIG_PREFIX + "collection",
Errors.MONGODB_03,
collection,
e.toString()
));
}
return mongoCollection;
}
示例11: init
import com.mongodb.WriteConcern; //导入依赖的package包/类
@SuppressWarnings("deprecation")
private void init()
{
if(mongoClient != null) return;
try {
MongoCredential credential = MongoCredential.createCredential(
MongoDBDrive.getInstance().getUsername(),
MongoDBDrive.getInstance().getDatabase(),
MongoDBDrive.getInstance().getPassword().toCharArray());
MongoDBDrive.getInstance().mongoClient = new MongoClient(
new ServerAddress(MongoDBDrive.getInstance().getUrl()),
Arrays.asList(credential));
MongoDBDrive.getInstance().mongoClient.setWriteConcern(WriteConcern.NORMAL);
} catch (Exception e) {
return;
}
return;
}
示例12: connect
import com.mongodb.WriteConcern; //导入依赖的package包/类
public static void connect(String server, int port, String dbname, String user, String pass) throws UnknownHostException {
disconnect();
ServerAddress sa = new ServerAddress(server, port);
if (user != null && user.length() > 0) {
List<MongoCredential> creds = new ArrayList<>();
creds.add(MongoCredential.createScramSha1Credential(user, dbname, pass.toCharArray()));
Connection = new MongoClient(sa, creds);
} else {
Connection = new MongoClient(sa);
}
Database = Connection.getDatabase(dbname);
Server = server;
Port = port;
DatabaseName = dbname;
User = user;
Password = pass;
Connection.setWriteConcern(WriteConcern.SAFE);
Database.listCollectionNames().first();
}
示例13: testUpload
import com.mongodb.WriteConcern; //导入依赖的package包/类
@Test
public void testUpload() throws IOException {
MongoFileStoreConfig config = MongoFileStoreConfig.builder()//
.bucket("mongofs").chunkSize(ChunkSize.medium_256K)//
.enableCompression(true).enableEncryption(new BasicCrypto())//
.writeConcern(WriteConcern.SAFE) //
.build();
MongoFileStore store = new MongoFileStore(database, config);
ByteArrayInputStream in = new ByteArrayInputStream(LoremIpsum.LOREM_IPSUM.getBytes());
MongoFile mongoFile = store.upload("loremIpsum.txt", "test/plain", null, false, in);
assertNotNull(mongoFile);
assertEquals(32087, mongoFile.getLength());
}
示例14: setReadWriteMode
import com.mongodb.WriteConcern; //导入依赖的package包/类
@Override
public void setReadWriteMode(String readWriteMode) {
if (readWriteMode == null || readWriteMode.equals(lastReadWriteMode)) {
return;
}
lastReadWriteMode = readWriteMode;
try {
Map<String, String> map = Splitter.on(", ").withKeyValueSeparator(":").split(readWriteMode);
String read = map.get("read");
if (read != null) {
ReadPreference readPref = ReadPreference.valueOf(read);
if (!readPref.equals(this.readPreference)) {
this.readPreference = readPref;
}
}
String write = map.get("write");
if (write != null) {
WriteConcern writeConcern = WriteConcern.valueOf(write);
if (!writeConcern.equals(this.writeConcern)) {
this.writeConcern = writeConcern;
}
}
} catch (Exception e) {
// unsupported or parse error - ignore
}
}
示例15: newEntry
import com.mongodb.WriteConcern; //导入依赖的package包/类
@Nonnull
@Override
public Entry newEntry(@Nonnull final Revision from,
@Nonnull final Revision to) {
return new MemoryEntry(from, to) {
private Diff commit = new Diff(from, to);
@Override
public void append(@Nonnull String path, @Nonnull String changes) {
// super.append() will apply to diff cache in base class
super.append(path, changes);
commit.append(path, changes);
}
@Override
public void done() {
try {
changes.insert(commit.doc, WriteConcern.UNACKNOWLEDGED);
} catch (MongoException e) {
LOG.warn("Write back of diff cache entry failed", e);
}
}
};
}