Java Code Examples for com.mongodb.BasicDBObject#keySet()

The following examples show how to use com.mongodb.BasicDBObject#keySet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBObjectsAssert.java    From bdt with Apache License 2.0 6 votes vote down vote up
private boolean isContained(List<DBObject> item, BasicDBObject doc) {
    boolean res = false;
    for (int i = 0; i < item.size() && !res; i++) {
        DBObject aux = item.get(i);
        aux.removeField("_id");
        aux.removeField("timestamp");

        if (aux.keySet().equals(doc.keySet())) {
            res = true;
        }
        // Obtenemos los columnNames
        List<String> cols = new ArrayList<String>(doc.keySet());
        for (int x = 0; x < cols.size() && res; x++) {
            if (!aux.get(cols.get(x)).equals(doc.get(cols.get(x)))) {
                res = false;
            } else {
                res = true;
            }
        }
    }
    return res;
}
 
Example 2
Source File: MongoDBDataStore.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private List<Map> findAction(Map queryObj, MongoCollection<Document> collection) {

    Map queryparmes = queryObj; // input
    BasicDBObject query = new BasicDBObject();// output

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

    // filterBson.append("_id", 0); //代码含义:查询返回结果中,不包含mongodb的_id字段

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

    int pageIndex = 0; // 初始 index
    int pageSize = 0; // 初始pageSize
    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);
}
 
Example 3
Source File: RollupStorageTest.java    From hvdf with Apache License 2.0 4 votes vote down vote up
@Test
public void testRollupGroupCountField() throws Exception {

	long sampleTime = TimeUnit.MINUTES.toMillis(1);
	int testSize = 10000;
	
	String configPath = "plugin_config/rollup_group_count.json";
	Channel channel = getConfiguredChannel(configPath);
	
	for(int i=0; i < testSize; i++){
 	BasicDBObject sample = new BasicDBObject(Sample.TS_KEY, i*sampleTime);
 	sample.append(Sample.DATA_KEY, new BasicDBObject("v", i%25));
 	sample.append(Sample.SOURCE_KEY, "sensor1");
 	channel.pushSample(sample, false, new BasicDBList());
	}
	    	
	// get all the rollup documents
	List<Sample> samples = channel.query(null, TimeUnit.MINUTES.toMillis(testSize), 
			TimeUnit.MINUTES.toMillis(testSize), null, null, testSize);
	
	// Each document is 60 samples, may be a partial document at end
	assertEquals((testSize/60) + (testSize%60 > 0 ? 1 : 0), samples.size());
	
	int totalRollupCount = 0;
	for(Sample rollupDoc : samples){
		
		// For each doc, ensure the group count total matches the sample total
		BasicDBObject v = (BasicDBObject)rollupDoc.getData().get("v");
		int rollupCount = v.getInt("count");
		BasicDBObject groups = (BasicDBObject)v.get("group_count");
		int localTotal = 0;
		for(String groupKey : groups.keySet()){
			localTotal += groups.getInt(groupKey);
		}
		
    	assertEquals(rollupCount, localTotal);    	    		
		totalRollupCount += localTotal;
	}
	
	assertEquals(testSize, totalRollupCount);    	
}
 
Example 4
Source File: MongodbInputDiscoverFieldsImpl.java    From pentaho-mongodb-plugin with Apache License 2.0 4 votes vote down vote up
private static void processRecord( BasicDBObject rec, String path, String name, Map<String, MongoField> lookup ) {
  for ( String key : rec.keySet() ) {
    Object fieldValue = rec.get( key );

    if ( fieldValue instanceof BasicDBObject ) {
      processRecord( (BasicDBObject) fieldValue, path + "." + key, name + "." + //$NON-NLS-1$ //$NON-NLS-2$
          key, lookup );
    } else if ( fieldValue instanceof BasicDBList ) {
      processList( (BasicDBList) fieldValue, path + "." + key, name + "." + //$NON-NLS-1$ //$NON-NLS-2$
          key, lookup );
    } else {
      // some sort of primitive
      String finalPath = path + "." + key; //$NON-NLS-1$
      String finalName = name + "." + key; //$NON-NLS-1$
      if ( !lookup.containsKey( finalPath ) ) {
        MongoField newField = new MongoField();
        int kettleType = mongoToKettleType( fieldValue );
        // Following suit of mongoToKettleType by interpreting null as String type
        newField.m_mongoType = String.class;
        if ( fieldValue != null ) {
          newField.m_mongoType = fieldValue.getClass();
        }
        newField.m_fieldName = finalName;
        newField.m_fieldPath = finalPath;
        newField.m_kettleType = ValueMeta.getTypeDesc( kettleType );
        newField.m_percentageOfSample = 1;

        lookup.put( finalPath, newField );
      } else {
        // update max indexes in array parts of name
        MongoField m = lookup.get( finalPath );
        Class<?> fieldClass = String.class;
        if ( fieldValue != null ) {
          fieldClass = fieldValue.getClass();
        }
        if ( !m.m_mongoType.isAssignableFrom( fieldClass ) ) {
          m.m_disparateTypes = true;
        }
        m.m_percentageOfSample++;
        updateMinMaxArrayIndexes( m, finalName );
      }
    }
  }
}
 
Example 5
Source File: IsCorrectQuery.java    From secure-data-service with Apache License 2.0 4 votes vote down vote up
private boolean matches(Query arg) {
        String queryKey;
        String argKey;

        for (String key : query.getQueryObject().keySet()) {
            queryKey = query.getQueryObject().get(key).getClass().getSimpleName();
            argKey = arg.getQueryObject().get(key).getClass().getSimpleName();

            if (!queryKey.equals(argKey)) {
                return false;
            } else if (queryKey.equals("BasicDBObject")) {
                BasicDBObject queryObj = (BasicDBObject) query.getQueryObject().get(key);
                BasicDBObject argObj = (BasicDBObject) arg.getQueryObject().get(key);

//                System.out.println(key);
//                System.out.print("\t"+queryObj);
//                System.out.println(" "+argObj);

                for (String key2 : queryObj.keySet()) {
                    if (queryObj.get(key2).getClass() != argObj.get(key2).getClass())
                    {
                        return false;
                    } else if (key2.equals("$in")) {
                        List<?> queryVal = (List<?>) queryObj.get(key2);
                        List<?> argVal = (List<?>) argObj.get(key2);
                        if (queryVal.size() != argVal.size()) {
                            return false;
                        }
                        for (int i = 0; i < queryVal.size(); ++i) {
                            if (!(queryVal.get(i).equals(argVal.get(i)))) {
                                return false;
                            }
                        }
                        return true;
                    }
                }
            } else if (!query.getQueryObject().get(key).equals(arg.getQueryObject().get(key))) {
                return false;
            }
        }

        return true;
    }