Search in sources :

Example 1 with UpdateStrategy

use of com.creditease.uav.datastore.mongo.MongodbImplStrategy.UpdateStrategy in project uavstack by uavorg.

the class MongoDBDataStore method update.

@SuppressWarnings("rawtypes")
@Override
protected boolean update(DataStoreMsg msg) {
    boolean isSuccess = false;
    String collectionName = (String) msg.get(DataStoreProtocol.MONGO_COLLECTION_NAME);
    MongoCollection<Document> collection = this.datasource.getSourceConnect().getCollection(collectionName);
    // collection no exist
    if (null == collection) {
        if (log.isTraceEnable()) {
            log.warn(this, "MongoDB[" + this.datasource.getDataStoreConnection().getDbName() + "] Collection[" + collectionName + "] NO EXIST.");
        }
        return adaptor.handleUpdateResult(isSuccess, msg, this.datasource.getDataStoreConnection());
    }
    // prepare query
    String updateObj = (String) adaptor.prepareUpdateObj(msg, this.datasource.getDataStoreConnection());
    // @SuppressWarnings("rawtypes")
    Map params = JSONHelper.toObject(DataStoreHelper.decorateInForMongoDB(updateObj), Map.class);
    BasicDBObject condition = new BasicDBObject();
    BasicDBObject update = new BasicDBObject();
    boolean isRemove = true;
    for (Object keyObj : params.keySet()) {
        if (keyObj.toString().equals(DataStoreProtocol.WHERE)) {
            QueryStrategy qry = new QueryStrategy();
            qry.concretProcessor(DataStoreProtocol.WHERE, params, condition);
        } else if (keyObj.toString().equals(DataStoreProtocol.UPDATE)) {
            isRemove = false;
            UpdateStrategy up = new UpdateStrategy();
            up.concretProcessor(DataStoreProtocol.UPDATE, params, update);
        } else {
            log.err(this, "can not figure out, please check it out " + keyObj.toString());
        }
    }
    if (isRemove) {
        log.info(this, "condition: " + condition.toString());
        DeleteResult res = collection.deleteMany(condition);
        log.info(this, "DeletedCount:" + res.getDeletedCount());
        isSuccess = true;
    } else {
        log.info(this, "condition: " + condition.toString());
        log.info(this, "update: " + update.toString());
        UpdateResult res = collection.updateMany(condition, update);
        log.info(this, "ModifiedCount:" + res.getModifiedCount());
        isSuccess = true;
    }
    return adaptor.handleUpdateResult(isSuccess, msg, this.datasource.getDataStoreConnection());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) UpdateStrategy(com.creditease.uav.datastore.mongo.MongodbImplStrategy.UpdateStrategy) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) QueryStrategy(com.creditease.uav.datastore.mongo.MongodbImplStrategy.QueryStrategy) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DeleteResult(com.mongodb.client.result.DeleteResult) UpdateResult(com.mongodb.client.result.UpdateResult)

Aggregations

QueryStrategy (com.creditease.uav.datastore.mongo.MongodbImplStrategy.QueryStrategy)1 UpdateStrategy (com.creditease.uav.datastore.mongo.MongodbImplStrategy.UpdateStrategy)1 BasicDBObject (com.mongodb.BasicDBObject)1 DeleteResult (com.mongodb.client.result.DeleteResult)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Document (org.bson.Document)1