Search in sources :

Example 1 with BulkWriteResult

use of com.mongodb.BulkWriteResult in project GNS by MobilityFirst.

the class MongoRecords method bulkUpdate.

/**
   *
   * @param collectionName
   * @param values
   * @throws FailedDBOperationException
   * @throws RecordExistsException
   */
public void bulkUpdate(String collectionName, Map<String, JSONObject> values) throws FailedDBOperationException, RecordExistsException {
    //String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    DBCollection collection = db.getCollection(collectionName);
    String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    db.requestEnsureConnection();
    BulkWriteOperation unordered = collection.initializeUnorderedBulkOperation();
    for (Map.Entry<String, JSONObject> entry : values.entrySet()) {
        BasicDBObject query = new BasicDBObject(primaryKey, entry.getKey());
        JSONObject value = entry.getValue();
        if (value != null) {
            DBObject document;
            try {
                document = (DBObject) JSON.parse(value.toString());
            } catch (Exception e) {
                throw new FailedDBOperationException(collectionName, "bulkUpdate", "Unable to parse json" + e.getMessage());
            }
            unordered.find(query).upsert().replaceOne(document);
        } else {
            unordered.find(query).removeOne();
        }
    }
    // Maybe check the result?
    BulkWriteResult result = unordered.execute();
}
Also used : BulkWriteOperation(com.mongodb.BulkWriteOperation) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) BulkWriteResult(com.mongodb.BulkWriteResult)

Example 2 with BulkWriteResult

use of com.mongodb.BulkWriteResult in project jackrabbit-oak by apache.

the class MongoCacheConsistencyTest method getFixture.

@Override
public DocumentStoreFixture getFixture() throws Exception {
    Fongo fongo = new OakFongo("fongo") {

        private String suppressedEx = null;

        @Override
        protected void afterInsert(WriteResult result) {
            maybeThrow();
        }

        @Override
        protected void afterFindAndModify(DBObject result) {
            maybeThrow();
        }

        @Override
        protected void afterUpdate(WriteResult result) {
            maybeThrow();
        }

        @Override
        protected void afterRemove(WriteResult result) {
            maybeThrow();
        }

        @Override
        protected void beforeExecuteBulkWriteOperation(boolean ordered, Boolean bypassDocumentValidation, List<?> writeRequests, WriteConcern aWriteConcern) {
            // suppress potentially set exception message because
            // fongo bulk writes call other update methods
            suppressedEx = exceptionMsg;
            exceptionMsg = null;
        }

        @Override
        protected void afterExecuteBulkWriteOperation(BulkWriteResult result) {
            exceptionMsg = suppressedEx;
            suppressedEx = null;
            maybeThrow();
        }

        private void maybeThrow() {
            if (exceptionMsg != null) {
                throw new MongoException(exceptionMsg);
            }
        }
    };
    DocumentMK.Builder builder = provider.newBuilder().setAsyncDelay(0);
    final DocumentStore store = new MongoDocumentStore(fongo.getMongo(), "oak", builder);
    return new DocumentStoreFixture() {

        @Override
        public String getName() {
            return "MongoDB";
        }

        @Override
        public DocumentStore createDocumentStore(DocumentMK.Builder builder) {
            return store;
        }
    };
}
Also used : Fongo(com.github.fakemongo.Fongo) OakFongo(com.mongodb.OakFongo) MongoException(com.mongodb.MongoException) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) OakFongo(com.mongodb.OakFongo) DBObject(com.mongodb.DBObject) BulkWriteResult(com.mongodb.BulkWriteResult) DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) WriteResult(com.mongodb.WriteResult) BulkWriteResult(com.mongodb.BulkWriteResult) DocumentStoreFixture(org.apache.jackrabbit.oak.plugins.document.DocumentStoreFixture) WriteConcern(com.mongodb.WriteConcern) List(java.util.List)

Example 3 with BulkWriteResult

use of com.mongodb.BulkWriteResult in project newton by muoncore.

the class MongoSagaRepository method clearInterests.

private void clearInterests(Saga saga) {
    Query ops = new Query();
    ops.addCriteria(Criteria.where("sagaId").is(saga.getId()));
    BulkWriteResult execute = mongoTemplate.bulkOps(BulkOperations.BulkMode.ORDERED, SagaInterest.class).remove(ops).execute();
    log.debug("Saga is complete, removed {} interests", execute.getRemovedCount());
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) BulkWriteResult(com.mongodb.BulkWriteResult)

Aggregations

BulkWriteResult (com.mongodb.BulkWriteResult)3 DBObject (com.mongodb.DBObject)2 MongoException (com.mongodb.MongoException)2 Fongo (com.github.fakemongo.Fongo)1 BasicDBObject (com.mongodb.BasicDBObject)1 BulkWriteOperation (com.mongodb.BulkWriteOperation)1 DBCollection (com.mongodb.DBCollection)1 DuplicateKeyException (com.mongodb.DuplicateKeyException)1 OakFongo (com.mongodb.OakFongo)1 WriteConcern (com.mongodb.WriteConcern)1 WriteResult (com.mongodb.WriteResult)1 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)1 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)1 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)1 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DocumentMK (org.apache.jackrabbit.oak.plugins.document.DocumentMK)1