Search in sources :

Example 1 with QueryStrategy

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

the class MongoDBDataStore method findAction.

@SuppressWarnings({ "rawtypes", "unchecked" })
private List<Map> findAction(Map queryObj, MongoCollection<Document> collection) {
    // input
    Map queryparmes = queryObj;
    // output
    BasicDBObject query = new BasicDBObject();
    Map findparmes = (Map) queryparmes.get(DataStoreProtocol.WHERE);
    String fileds = String.valueOf(queryparmes.get(DataStoreProtocol.FIELDS));
    String sortJson = String.valueOf(queryparmes.get(DataStoreProtocol.SORT));
    String pageindex = String.valueOf(queryparmes.get(DataStoreProtocol.PAGEINDEX));
    String pagesize = String.valueOf(queryparmes.get(DataStoreProtocol.PAGESIZE));
    QueryStrategy qry = new QueryStrategy();
    Map express = new LinkedHashMap();
    express.put(DataStoreProtocol.FIND, findparmes);
    qry.concretProcessor(DataStoreProtocol.FIND, express, query);
    Document sorts = new Document();
    Document filterBson = new Document();
    if (!StringHelper.isEmpty(fileds)) {
        String[] filters = fileds.split(";");
        for (String filter : filters) {
            filterBson.append(filter, 1);
        }
    }
    if (!StringHelper.isEmpty(sortJson)) {
        Map<String, String> sortMap = JSONHelper.toObject(sortJson, Map.class);
        String sortParmes = sortMap.get(DataStoreProtocol.VALUES);
        String sortOrder = sortMap.get(DataStoreProtocol.SORTORDER);
        String[] parames = sortParmes.split(",");
        for (String parame : parames) {
            sorts.append(parame, Integer.parseInt(sortOrder));
        }
    }
    // 初始 index
    int pageIndex = 0;
    // 初始pageSize
    int pageSize = 0;
    if (!StringHelper.isEmpty(pageindex)) {
        pageIndex = Integer.parseInt(pageindex);
    }
    if (!StringHelper.isEmpty(pagesize)) {
        pageSize = Integer.parseInt(pagesize);
    }
    if (log.isDebugEnable()) {
        StringBuilder sb = new StringBuilder();
        for (Object qobj : query.keySet()) {
            sb.append("\r\nshell in package:" + qobj.toString() + ":" + query.get(qobj));
        }
        sb.append("\r\nfilterBson:" + filterBson);
        sb.append("\r\npageIndex:" + pageIndex);
        sb.append("\r\npageSize:" + pageSize);
        sb.append("\r\nsorts:" + sorts);
        log.debug(this, sb.toString());
    }
    log.info(this, "MongoDBDataStore findAction toJson : " + query.toJson());
    MongoCursor<Document> cursor = null;
    if (pageIndex > 0 && pageSize > 0) {
        cursor = collection.find(query).projection(filterBson).sort(sorts).skip((pageIndex - 1) * pageSize).limit(pageSize).iterator();
    } else {
        cursor = collection.find(query).projection(filterBson).sort(sorts).iterator();
    }
    return queryResultFormat(cursor, false);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) QueryStrategy(com.creditease.uav.datastore.mongo.MongodbImplStrategy.QueryStrategy) Document(org.bson.Document) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with QueryStrategy

use of com.creditease.uav.datastore.mongo.MongodbImplStrategy.QueryStrategy 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)

Example 3 with QueryStrategy

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

the class MongoDBDataStore method countAction.

@SuppressWarnings({ "rawtypes", "unchecked" })
private List<Map> countAction(DataStoreMsg msg, Map queryparmes, MongoCollection<Document> collection) {
    // output
    BasicDBObject query = new BasicDBObject();
    Map findparmes = (Map) queryparmes.get(DataStoreProtocol.WHERE);
    String strLimit = String.valueOf(queryparmes.get(DataStoreProtocol.LIMIT));
    QueryStrategy qry = new QueryStrategy();
    Map express = new LinkedHashMap();
    express.put(DataStoreProtocol.FIND, findparmes);
    qry.concretProcessor(DataStoreProtocol.FIND, express, query);
    // for (Object qobj : query.keySet()) {
    // log.info(this, "shell in package:" + qobj.toString() + ":" + query.get(qobj));
    // }
    log.info(this, "MongoDBDataStore countAction toJson : " + query.toJson());
    long countN = collection.count(query);
    if (!StringHelper.isEmpty(strLimit) && countN > Long.parseLong(strLimit)) {
        countN = Long.parseLong(strLimit);
    }
    Map<String, Object> item = new LinkedHashMap<String, Object>();
    item.put(DataStoreProtocol.COUNT, countN);
    List<Map> res = new ArrayList<Map>();
    res.add(item);
    return res;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) BasicDBObject(com.mongodb.BasicDBObject) QueryStrategy(com.creditease.uav.datastore.mongo.MongodbImplStrategy.QueryStrategy) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

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