Search in sources :

Example 26 with WriteResult

use of com.mongodb.WriteResult in project symmetric-ds by JumpMind.

the class MongoDatabaseWriter method delete.

@Override
protected LoadStatus delete(CsvData data, boolean useConflictDetection) {
    statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    try {
        DB db = clientManager.getDB(objectMapper.mapToDatabase(this.targetTable));
        DBCollection collection = db.getCollection(objectMapper.mapToCollection(this.targetTable));
        String[] columnNames = sourceTable.getColumnNames();
        Map<String, String> newData = data.toColumnNameValuePairs(columnNames, CsvData.ROW_DATA);
        Map<String, String> oldData = data.toColumnNameValuePairs(columnNames, CsvData.OLD_DATA);
        Map<String, String> pkData = data.toKeyColumnValuePairs(this.sourceTable);
        DBObject query = objectMapper.mapToDBObject(sourceTable, newData, oldData, pkData, true);
        WriteResult results = collection.remove(query, WriteConcern.ACKNOWLEDGED);
        if (results.getN() != 1) {
            log.warn("Attempted to remove a single object" + query.toString() + ".  Instead removed: " + results.getN());
        }
        return LoadStatus.SUCCESS;
    } finally {
        statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) DBObject(com.mongodb.DBObject) DB(com.mongodb.DB)

Example 27 with WriteResult

use of com.mongodb.WriteResult in project symmetric-ds by JumpMind.

the class MongoDatabaseWriter method upsert.

protected LoadStatus upsert(CsvData data) {
    statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    try {
        DB db = clientManager.getDB(objectMapper.mapToDatabase(this.targetTable));
        DBCollection collection = db.getCollection(objectMapper.mapToCollection(this.targetTable));
        String[] columnNames = sourceTable.getColumnNames();
        Map<String, String> newData = data.toColumnNameValuePairs(columnNames, CsvData.ROW_DATA);
        Map<String, String> oldData = data.toColumnNameValuePairs(columnNames, CsvData.OLD_DATA);
        Map<String, String> pkData = data.toKeyColumnValuePairs(this.sourceTable);
        DBObject query = objectMapper.mapToDBObject(sourceTable, newData, oldData, pkData, true);
        DBObject object = objectMapper.mapToDBObject(sourceTable, newData, oldData, pkData, false);
        WriteResult results = collection.update(query, object, true, false, WriteConcern.ACKNOWLEDGED);
        if (results.getN() == 1) {
            return LoadStatus.SUCCESS;
        } else {
            throw new SymmetricException("Failed to write data: " + object.toString());
        }
    } finally {
        statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) SymmetricException(org.jumpmind.symmetric.SymmetricException) DBObject(com.mongodb.DBObject) DB(com.mongodb.DB)

Aggregations

WriteResult (com.mongodb.WriteResult)27 BasicDBObject (com.mongodb.BasicDBObject)17 DBObject (com.mongodb.DBObject)14 DBCollection (com.mongodb.DBCollection)9 Test (org.junit.Test)5 BulkWriteResult (com.mongodb.BulkWriteResult)3 DB (com.mongodb.DB)3 Date (java.util.Date)3 List (java.util.List)3 ImmutableList (com.google.common.collect.ImmutableList)2 MongoException (com.mongodb.MongoException)2 QueryBuilder (com.mongodb.QueryBuilder)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)2 ObjectId (org.bson.types.ObjectId)2 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)2 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)2 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)2 Stream (org.graylog2.plugin.streams.Stream)2