Example usage for com.mongodb WriteConcern ACKNOWLEDGED

List of usage examples for com.mongodb WriteConcern ACKNOWLEDGED

Introduction

In this page you can find the example usage for com.mongodb WriteConcern ACKNOWLEDGED.

Prototype

WriteConcern ACKNOWLEDGED

To view the source code for com.mongodb WriteConcern ACKNOWLEDGED.

Click Source Link

Document

Write operations that use this write concern will wait for acknowledgement, using the default write concern configured on the server.

Usage

From source file:org.obiba.magma.datasource.mongodb.MongoDBValueTable.java

License:Open Source License

DBObject asDBObject() {
    if (dbObject == null) {
        dbObject = getValueTableCollection().findOne(BasicDBObjectBuilder.start() //
                .add("datasource", getMongoDBDatasource().asDBObject().get("_id")) //
                .add("name", getName()) //
                .get());/*from  w  ww.jav a  2  s  .  c o  m*/
        // create DBObject if not found
        if (dbObject == null) {
            dbObject = BasicDBObjectBuilder.start() //
                    .add("datasource", getMongoDBDatasource().asDBObject().get("_id")) //
                    .add("name", getName()) //
                    .add("entityType", getEntityType()) //
                    .add(MongoDBDatasource.TIMESTAMPS_FIELD, MongoDBDatasource.createTimestampsObject()).get();
            getValueTableCollection().insert(dbObject, WriteConcern.ACKNOWLEDGED);
        }
    }
    return dbObject;
}

From source file:org.opencb.opencga.storage.mongodb.metadata.MongoDBStudyConfigurationManager.java

License:Apache License

private MongoDBStudyConfigurationManager(MongoDataStoreManager mongoManager, boolean closeConnection,
        MongoCredentials credentials, String collectionName) throws UnknownHostException {
    super(null);/*from w w  w  .j a v a  2  s. c o m*/
    // Mongo configuration
    this.mongoManager = mongoManager;
    this.closeConnection = closeConnection;
    MongoDataStore db = mongoManager.get(credentials.getMongoDbName(), credentials.getMongoDBConfiguration());
    collection = db.getCollection(collectionName).withReadPreference(ReadPreference.primary())
            .withWriteConcern(WriteConcern.ACKNOWLEDGED);
    mongoLock = new MongoLock(collection, "_lock");
}

From source file:org.openmama.dbwriter.MamaMongodb.java

/**
 * @return Write concern for database writes
 *///w  w  w.j a va 2  s. c  o  m
private WriteConcern getWriteConcern(final String config) {

    switch (config.toLowerCase()) {
    case "none":
    case "errors_ignored":
    case "errorsignored":
        return WriteConcern.ERRORS_IGNORED;
    case "safe":
    case "acknowledged":
        return WriteConcern.ACKNOWLEDGED;
    case "normal":
    case "unacknowledged":
        return WriteConcern.UNACKNOWLEDGED;
    case "fsync_safe":
    case "fsyncsafe":
    case "fsynced":
        return WriteConcern.FSYNCED;
    case "journal_safe":
    case "journalsafe":
    case "journaled":
        return WriteConcern.JOURNALED;
    case "replicas_safe":
    case "replicassafe":
    case "replica_acknowledged":
    case "replicaacknowledged":
        return WriteConcern.REPLICA_ACKNOWLEDGED;
    case "majority":
        return WriteConcern.MAJORITY;
    default:
        logger.warning(String.format("%s writeTo concern not valid - using UNACKNOWLEDGED", config));
        return WriteConcern.UNACKNOWLEDGED;
    }

}

From source file:org.sglover.checksum.dao.mongo.MongoChecksumDAO.java

License:Open Source License

private void init() {
    if (db == null) {
        throw new RuntimeException("Mongo DB must not be null");
    }//from  w w  w  .  j  av a  2s.  c  om

    this.checksums = getCollection(db, checksumsCollectionName, WriteConcern.ACKNOWLEDGED);

    // {
    // DBObject keys = BasicDBObjectBuilder
    // .start("e", 1)
    // .add("n", 1)
    // .add("v", 1)
    // .get();
    // this.checksums.ensureIndex(keys, "byNodeId", false);
    // }
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

private void init() {
    map.put("name", "nm");
    map.put("location", "l");
    map.put("misc", "mi");
    map.put("money", "m");
    map.put("date", "d");
    map.put("org", "o");

    map.put("nm", "name");
    map.put("l", "location");
    map.put("mi", "misc");
    map.put("m", "money");
    map.put("d", "date");
    map.put("o", "org");

    allTypes.add("name");
    allTypes.add("location");
    allTypes.add("org");
    allTypes.add("misc");
    allTypes.add("date");
    allTypes.add("money");

    if (db == null) {
        throw new RuntimeException("Mongo DB must not be null");
    }/*w  ww.j  a va 2 s.  c o  m*/

    this.entitiesData = getCollection(db, entitiesCollectionName, WriteConcern.ACKNOWLEDGED);

    {
        DBObject keys = BasicDBObjectBuilder.start("n", 1).add("v", 1).get();
        this.entitiesData.ensureIndex(keys, "main", false);
    }
}

From source file:org.sglover.entities.dao.mongo.MongoSimilarityDAO.java

License:Open Source License

private void init() {
    if (db == null) {
        throw new RuntimeException("Mongo DB must not be null");
    }//w  w  w  .  j  av  a 2  s.com

    this.similarityData = getCollection(db, similarityCollectionName, WriteConcern.ACKNOWLEDGED);

    {
        DBObject keys = BasicDBObjectBuilder.start("tx", 1).get();
        this.similarityData.ensureIndex(keys, "btTxn", false);
    }
}

From source file:org.sglover.entities.dao.mongo.MongoUserTrackingDAO.java

License:Open Source License

public void init() {
    if (db == null) {
        throw new RuntimeException("Mongo DB must not be null");
    }/*  w w w .  j a v  a 2  s.  c o  m*/

    this.data = getCollection(db, collectionName, WriteConcern.ACKNOWLEDGED);

    {
        DBObject keys = BasicDBObjectBuilder.start("ic", 1).add("u", 1).add("t", 1).get();
        this.data.ensureIndex(keys, "main", false);
    }

    {
        DBObject keys = BasicDBObjectBuilder.start("tx", 1).get();
        this.data.ensureIndex(keys, "byTxn", false);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

private WriteConcern potentiallyForceAcknowledgedWrite(WriteConcern wc) {

    if (ObjectUtils.nullSafeEquals(WriteResultChecking.EXCEPTION, writeResultChecking)
            && MongoClientVersion.isMongo3Driver()) {
        if (wc == null || wc.getWObject() == null
                || (wc.getWObject() instanceof Number && ((Number) wc.getWObject()).intValue() < 1)) {
            return WriteConcern.ACKNOWLEDGED;
        }//from w w  w .j  a v a  2  s . co m
    }
    return wc;
}

From source file:org.teiid.translator.mongodb.MongoDBUpdateExecution.java

License:Open Source License

private void executeInternal() throws TranslatorException {

    DBCollection collection = getCollection(this.visitor.mongoDoc.getTargetTable());
    MongoDocument mongoDoc = this.visitor.mongoDoc;
    AggregationOptions options = this.executionFactory.getOptions(this.executionContext.getBatchSize());

    List<WriteResult> executionResults = new ArrayList<WriteResult>();

    if (this.command instanceof Insert) {
        // get pull key based documents to embed
        LinkedHashMap<String, DBObject> embeddedDocuments = fetchEmbeddedDocuments();

        // check if this document need to be embedded in any other document
        if (mongoDoc.isMerged()) {
            DBObject match = getInsertMatch(mongoDoc, this.visitor.columnValues);
            BasicDBObject insert = this.visitor.getInsert(embeddedDocuments);

            if (mongoDoc.getMergeKey().getAssociation() == Association.MANY) {
                removeParentKey(mongoDoc, insert);
                BasicDBObject insertDoc = new BasicDBObject(mongoDoc.getQualifiedName(true), insert);
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "update - {\"$match\": {" + match + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "update - {\"$push\": {" + insertDoc + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
                executionResults.add(collection.update(match, new BasicDBObject("$push", insertDoc), false, //$NON-NLS-1$
                        true, WriteConcern.ACKNOWLEDGED));
            } else {
                insert.remove("_id"); //$NON-NLS-1$
                BasicDBObject insertDoc = new BasicDBObject(mongoDoc.getQualifiedName(true), insert);
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "update - {\"$match\": {" + match + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "update - {\"$set\": {" + insertDoc + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
                executionResults.add(collection.update(match, new BasicDBObject("$set", insertDoc), false, true, //$NON-NLS-1$
                        WriteConcern.ACKNOWLEDGED));
            }//from  w  w w  . java2s. c  o  m
        } else {
            for (String docName : embeddedDocuments.keySet()) {
                DBObject embeddedDoc = embeddedDocuments.get(docName);
                embeddedDoc.removeField("_id"); //$NON-NLS-1$
            }
            // gets its own collection
            BasicDBObject in = this.visitor.getInsert(embeddedDocuments);
            LogManager.logDetail(LogConstants.CTX_CONNECTOR, "{\"insert\": {" + in + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
            executionResults.add(collection.insert(in, WriteConcern.ACKNOWLEDGED));
        }
    } else if (this.command instanceof Update) {
        // get pull key based documents to embed
        LinkedHashMap<String, DBObject> embeddedDocuments = fetchEmbeddedDocuments();
        DBObject match = new BasicDBObject();
        if (this.visitor.match != null) {
            match = this.visitor.match;
        }
        if (mongoDoc.isMerged()) {
            // multi items in array update not available, http://jira.mongodb.org/browse/SERVER-1243
            // this work-around for above issue
            List<String> parentKeyNames = parentKeyNames(mongoDoc);

            DBObject documentMatch = new BasicDBObject("$match", match); //$NON-NLS-1$                  
            DBObject projection = new BasicDBObject("$project", buildProjectForUpdate(mongoDoc)); //$NON-NLS-1$
            Cursor output = collection.aggregate(Arrays.asList(documentMatch, projection), options);
            while (output.hasNext()) {
                BasicDBObject row = (BasicDBObject) output.next();
                buildUpdate(mongoDoc, collection, row, parentKeyNames, 0, null, executionResults,
                        new UpdateOperationImpl());
            }
        } else {
            for (String docName : embeddedDocuments.keySet()) {
                DBObject embeddedDoc = embeddedDocuments.get(docName);
                embeddedDoc.removeField("_id"); //$NON-NLS-1$
            }
            BasicDBObject u = this.visitor.getUpdate(embeddedDocuments);
            LogManager.logDetail(LogConstants.CTX_CONNECTOR, "update - {\"$match\": {" + match + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
            LogManager.logDetail(LogConstants.CTX_CONNECTOR, "update - {\"$set\": {" + u + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
            executionResults.add(collection.update(match, new BasicDBObject("$set", u), false, true, //$NON-NLS-1$
                    WriteConcern.ACKNOWLEDGED));
        }

        // if the update is for the "embeddable" table, then since it is copied to other tables
        // those references need to be updated. I know this is not atomic operation, but not sure
        // how else to handle it.
        if (mongoDoc.isEmbeddable()) {
            updateReferenceTables(collection, mongoDoc, match, options);
        }
    } else {
        // Delete
        DBObject match = new BasicDBObject();
        if (this.visitor.match != null) {
            match = this.visitor.match;
        }

        if (mongoDoc.isEmbeddable()) {
            DBObject m = new BasicDBObject("$match", match); //$NON-NLS-1$
            Cursor output = collection.aggregate(Arrays.asList(m), options);
            while (output.hasNext()) {
                DBObject row = output.next();
                if (row != null) {
                    for (MergeDetails ref : mongoDoc.getEmbeddedIntoReferences()) {
                        DBCollection parent = getCollection(ref.getParentTable());
                        DBObject parentMatch = buildParentMatch(row, ref);
                        DBObject refMatch = new BasicDBObject("$match", parentMatch); //$NON-NLS-1$
                        Cursor referenceOutput = parent.aggregate(Arrays.asList(refMatch), options);
                        if (referenceOutput.hasNext()) {
                            throw new TranslatorException(MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18010,
                                    this.visitor.mongoDoc.getTargetTable().getName(), ref.getParentTable()));
                        }
                    }
                }
            }
        }

        if (mongoDoc.isMerged()) {
            List<String> parentKeyNames = parentKeyNames(mongoDoc);

            DBObject documentMatch = new BasicDBObject("$match", match); //$NON-NLS-1$                      
            DBObject projection = new BasicDBObject("$project", buildProjectForUpdate(mongoDoc)); //$NON-NLS-1$
            Cursor output = collection.aggregate(Arrays.asList(documentMatch, projection), options);
            while (output.hasNext()) {
                BasicDBObject row = (BasicDBObject) output.next();
                buildUpdate(mongoDoc, collection, row, parentKeyNames, 0, null, executionResults,
                        new DeleteOperationImpl(match));
            }
        } else {
            LogManager.logDetail(LogConstants.CTX_CONNECTOR, "remove - {\"$match\": {" + match + "}}"); //$NON-NLS-1$ //$NON-NLS-2$
            executionResults.add(collection.remove(match, WriteConcern.ACKNOWLEDGED));
        }
    }

    if (!executionResults.isEmpty()) {
        if (this.command instanceof Insert) {
            if (this.executionContext.getCommandContext().isReturnAutoGeneratedKeys()) {
                addAutoGeneretedKeys(executionResults.get(0));
            }
        }

        int updated = 0;
        for (WriteResult result : executionResults) {
            updated += result.getN();
        }

        this.results = new int[1];
        this.results[0] = updated;
    }
}

From source file:org.teiid.translator.mongodb.MongoDBUpdateExecution.java

License:Open Source License

private void updateReferenceTables(DBCollection collection, MongoDocument mongoDoc, DBObject match,
        AggregationOptions options) throws TranslatorException {
    DBObject m = new BasicDBObject("$match", match); //$NON-NLS-1$
    Cursor output = collection.aggregate(Arrays.asList(m), options);
    while (output.hasNext()) {
        DBObject row = output.next();//from w w  w .j  a v a  2  s  .c om
        if (row != null) {
            for (MergeDetails ref : mongoDoc.getEmbeddedIntoReferences()) {
                DBCollection parent = getCollection(ref.getParentTable());
                //DBObject parentmatch = new BasicDBObject(ref.getReferenceName()+".$id", row.get("_id")); //$NON-NLS-1$ //$NON-NLS-2$
                DBObject parentmatch = buildParentMatch(row, ref);
                row.removeField("_id"); //$NON-NLS-1$
                parent.update(parentmatch, new BasicDBObject("$set", new BasicDBObject(ref.getName(), row)), //$NON-NLS-1$
                        false, true, WriteConcern.ACKNOWLEDGED);

                // see if there are nested references
                Table parentTable = this.metadata.getTable(mongoDoc.getTable().getParent().getName(),
                        ref.getParentTable());
                MongoDocument parentMongoDocument = new MongoDocument(parentTable, this.metadata);
                if (parentMongoDocument.isEmbeddable()) {
                    updateReferenceTables(parent, parentMongoDocument, parentmatch, options);
                }
            }
        }
    }
}