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();
}
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;
}
};
}
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());
}
Aggregations