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

The following examples show how to use com.mongodb.DBObject#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: EntitySessionController.java    From restfiddle with Apache License 2.0 6 votes vote down vote up
private void dbRefToRelation(DBObject dbObject) {
if (dbObject == null) {
    return;
}
if (dbObject.containsField("_id")) 
    dbObject.put("_id", ((ObjectId) dbObject.get("_id")).toHexString());
for (String key : dbObject.keySet()) {
    Object obj = dbObject.get(key);
    if (obj instanceof DBRef) {
	DBRef ref = (DBRef) obj;
	dbObject.put(key, dbRefToRel(ref));
    } else if (obj instanceof DBObject) {
	dbRefToRelation((DBObject) obj);
    }
}

   }
 
Example 2
Source File: ItemsMetadata.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
private void loadItemsMetadata() throws IOException {
  itemsMetadata = new ConcurrentHashMap<>();
  DBObject contents = (DBObject) JSON.parse(loadFileAsString());
  if (contents != null) {
    for (String key : contents.keySet()) {
      try {
        DBObject metadata = (DBObject) contents.get(key);
        Map<String, String> map = new HashMap<>();
        for (String metadataKey : metadata.keySet()) {
          map.put(metadataKey, metadata.get(metadataKey).toString());
        }
        itemsMetadata.put(key, map);
      } catch (ClassCastException e) {
        log.warn("Attribute '{}' should be an object", key);
      }
    }
  }
}
 
Example 3
Source File: MongoEntity.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static Map<String, List<Entity>> extractContainerData(DBObject dbObj) {
    ContainerDocumentHolder containerDocumentHolder = new ContainerDocumentHolder();
    Map<String, List<Entity>> containerData = new HashMap<String, List<Entity>>();

    for (String key : dbObj.keySet()) {
        if (containerDocumentHolder.isContainerDocument(key)) {
            List<DBObject> values = (List<DBObject>) dbObj.get(key);
            List<Entity> subEntityList = new ArrayList<Entity>();
            for (DBObject subEntity : values) {
                subEntityList.add(fromDBObject(subEntity));
            }
            containerData.put(key, subEntityList);
        }
    }

    return containerData;
}
 
Example 4
Source File: EntityDataController.java    From restfiddle with Apache License 2.0 6 votes vote down vote up
private void dbRefToRelation(DBObject dbObject) {
if (dbObject == null) {
    return;
}
if (dbObject.containsField("_id")) 
    dbObject.put("_id", ((ObjectId) dbObject.get("_id")).toHexString());
for (String key : dbObject.keySet()) {
    Object obj = dbObject.get(key);
    if (obj instanceof DBRef) {
	DBRef ref = (DBRef) obj;
	dbObject.put(key, dbRefToRel(ref));
    } else if (obj instanceof DBObject) {
	dbRefToRelation((DBObject) obj);
    }
}

   }
 
Example 5
Source File: MongoDBToJSON.java    From act with GNU General Public License v3.0 6 votes vote down vote up
public static JSONObject conv(DBObject o) {
  JSONObject result = new JSONObject();
  try {
    for (String k : o.keySet()) {
      Object v = o.get(k);
      if (v instanceof BasicDBList) {
        result.put(k, conv((BasicDBList)v));
      } else if (v instanceof DBObject) {
        result.put(k, conv((DBObject)v));
      } else {
        result.put(k, v);
      }
    }
    return result;
  } catch (JSONException je) {
    return null;
  }
}
 
Example 6
Source File: GroupCountRollup.java    From hvdf with Apache License 2.0 6 votes vote down vote up
public GroupCountRollup(PluginConfiguration config){
	
	this.fieldExtention = "." + "group_count";
	
	// any field mentioned in the config is a target
	DBObject rawConfig = config.getRaw();
	ranges = new ArrayList<RangeCounter>(rawConfig.keySet().size());
	
	for(String fieldName : rawConfig.keySet()){
		Object rangesObj = rawConfig.get(fieldName);
		if(rangesObj instanceof BasicDBList){
			ranges.add(new RangeCounter(fieldName, (BasicDBList)rangesObj));				
		} else {
			// the field must specify a range of values
			throw new ServiceException("Expected a list of range values", 
					ConfigurationError.INVALID_CONFIG_TYPE).
						set("configuring", GroupCountRollup.class).
						set("field", fieldName);	
		}
	}		
}
 
Example 7
Source File: FieldsMongoModel.java    From gameserver with Apache License 2.0 5 votes vote down vote up
@Override
public void reload() {
	//Load data from database
	columnClasses.clear();
	rows.clear();
	
	DBObject query = MongoUtil.createDBObject();
	DBObject fields = MongoUtil.createDBObject();
	for ( String c : columnNames ) {
		fields.put(c, "1");
	}
	List<DBObject> results = MongoUtil.queryAllFromMongo(query, database, 
			namespace, collection, fields);
	rows.addAll(results);
	if ( rows.size()>=0 ) {
		DBObject row = rows.get(0);
		for ( String key : row.keySet() ) {
			Object value = row.get(key);
			if ( value != null ) {
				columnClasses.add(value.getClass());
			} else {
				columnClasses.add(Object.class);
			}
		}
		columnSize = columnNames.size();
	}
	Collections.sort(rows, new MyDBObjectComparator());
	logger.debug("Load total {} rows into model", rows.size());
	this.fireTableStructureChanged();
	this.fireTableDataChanged();
	this.fireContentsChanged(this, 0, rows.size());
}
 
Example 8
Source File: DBObjectListModel.java    From gameserver with Apache License 2.0 5 votes vote down vote up
@Override
public void reload() {
	//Load data from database
	columnNames.clear();
	columnClasses.clear();
	rows.clear();
	
	DBObject query = MongoUtil.createDBObject();
	rows.addAll(this.dbObjList);
	if ( rows.size()>0 ) {
		DBObject row = rows.get(0);
		for ( String key : row.keySet() ) {
			if ( hiddenFields.contains(key) ) {
				continue;
			}
			columnNames.add(key);
			Object value = row.get(key);
			if ( value != null ) {
				columnClasses.add(value.getClass());
			} else {
				columnClasses.add(Object.class);
			}
		}
		columnSize = columnNames.size();
	}
	logger.debug("Load total {} rows into model", rows.size());
	this.fireTableStructureChanged();
	this.fireTableDataChanged();
}
 
Example 9
Source File: GeneralMongoModel.java    From gameserver with Apache License 2.0 5 votes vote down vote up
@Override
public void reload() {
	//Load data from database
	columnNames.clear();
	columnClasses.clear();
	rows.clear();
	
	DBObject query = MongoUtil.createDBObject();
	List<DBObject> results = MongoUtil.queryAllFromMongo(query, database, 
			namespace, collection, null);
	rows.addAll(results);
	if ( rows.size()>0 ) {
		DBObject row = rows.get(0);
		for ( String key : row.keySet() ) {
			if ( hiddenFields.contains(key) ) {
				continue;
			}
			columnNames.add(key);
			Object value = row.get(key);
			if ( value != null ) {
				columnClasses.add(value.getClass());
			} else {
				columnClasses.add(Object.class);
			}
		}
		columnSize = columnNames.size();
	}
	logger.debug("Load total {} rows into model", rows.size());
	this.fireTableStructureChanged();
	this.fireTableDataChanged();
}
 
Example 10
Source File: EntityDataController.java    From restfiddle with Apache License 2.0 5 votes vote down vote up
private void relationToDBRef(DBObject dbObject, String projectId) {
for (String key : dbObject.keySet()) {
    Object obj = dbObject.get(key);
    if (obj instanceof DBObject) {
	DBObject doc = (DBObject) obj;
	if (doc.containsField("_rel")) {
	    DBObject relation = (DBObject) doc.get("_rel");
	    dbObject.put(key, new DBRef(projectId + "_" + (String) relation.get("entity"), new ObjectId((String) relation.get("_id"))));
	} else {
	    relationToDBRef(doc, projectId);
	}
    }
}
   }
 
Example 11
Source File: MongoDbStore.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
 private RobotCapabilities objectToCapabilities(DBObject object) {
   if (object == null) {
     return null;
   }

   Map<String, Object> capabilitiesObj =
(Map<String, Object>) object.get(CAPABILITIES_CAPABILITIES_FIELD);
   Map<EventType, Capability> capabilities = CollectionUtils.newHashMap();

   for (Entry<String, Object> capability : capabilitiesObj.entrySet()) {
     EventType eventType = EventType.valueOf(capability.getKey());
     List<Context> contexts = CollectionUtils.newArrayList();
     DBObject capabilityObj = (DBObject) capability.getValue();
     DBObject contextsObj = (DBObject) capabilityObj.get(CAPABILITY_CONTEXTS_FIELD);
     for (String contextId : contextsObj.keySet()) {
       contexts.add(Context.valueOf((String) contextsObj.get(contextId)));
     }
     String filter = (String) capabilityObj.get(CAPABILITY_FILTER_FIELD);

     capabilities.put(eventType, new Capability(eventType, contexts, filter));
   }

   String capabilitiesHash = (String) object.get(CAPABILITIES_HASH_FIELD);
   ProtocolVersion version =
       ProtocolVersion.valueOf((String) object.get(CAPABILITIES_VERSION_FIELD));

   return new RobotCapabilities(capabilities, capabilitiesHash, version);
 }
 
Example 12
Source File: QueryProperties.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static TagSet toTags( DBObject tagsDocument )
{
	List<Tag> tagList = new ArrayList<Tag>( );
	for ( String key : tagsDocument.keySet( ) )
	{
		tagList.add( new Tag( key, tagsDocument.get( key ).toString( ) ) );
	}
	return new TagSet( tagList );
}
 
Example 13
Source File: RollupStorageInterceptor.java    From hvdf with Apache License 2.0 5 votes vote down vote up
@Override
public void pushSample(DBObject sample, boolean isList, BasicDBList resultIds) {
	
	if(isList){
		
		// Use the batch API to send a number of samples
		updateBatch((BasicDBList)sample);			
	}
	else if(sample != null){
		
		// This is a document, place it straight in appropriate collection
		BasicDBObject doc = ((BasicDBObject) sample);
		long timestamp = this.rollupPeriod * (doc.getLong(Sample.TS_KEY) / this.rollupPeriod);			
		DBCollection collection = collectionAllocator.getCollection(timestamp);
		
		// Ask the id allocator for the query
		BasicDBObject query = this.idFactory.getQuery(sample.get(Sample.SOURCE_KEY), timestamp);
		
		// Build the update clause using the ops list
		BasicDBObject update = new BasicDBObject();
		for(RollupOperation rollupOp : this.rollupOps){
			
			DBObject updateClause = rollupOp.getUpdateClause(sample);
			
			// Check for top level operators that already exist so they dont overwrite
			for(String key : updateClause.keySet()){
				BasicDBObject existingClause = (BasicDBObject) update.get(key);
				if(existingClause != null){
					// Merge the arguments to the top level op
					existingClause.putAll((DBObject)updateClause.get(key));
				} else {
					update.put(key, updateClause.get(key));
				}
			}
		}
		
		collection.update(query, update, true, false);
	}
}
 
Example 14
Source File: CsvCombine.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Parse an element (table row) representing a complex type and return the associated NR map.
 */
private Map<String, Object> parseDbElement(DBObject dbElement, int joinKey, String entityName,
        List<DBCollection> supportingCollections) {
    Map<String, Object> result = new HashMap<String, Object>();

    Set<String> keySet = dbElement.keySet();

    // add all entries from this table rowprefix
    for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
        String curKey = it.next();
        if (curKey.equals("_id") || curKey.equals("JoinKey") || curKey.equals("ParentJoinKey")) {
            continue;
        }

        String curVal = "" + dbElement.get(curKey);
        addMapEntry(curKey, curVal, result);

        /**
         * Now pick up the supporting list of list files.
         * The outer 'if' statement ensures this is only called if
         * further levels of hierarchy exist
         */
        for (Iterator<DBCollection> iter = supportingCollections.iterator(); iter.hasNext();) {
            String collectionName = iter.next().toString();
            if (collectionName.lastIndexOf('_') == entityName.length()) {
                String listName = collectionName.substring(entityName.length() + 1);
                addMapEntry(listName,
                        getListFromCollection(collectionName, joinKey, supportingCollections), result);
            }
        }
    }
    return result;
}
 
Example 15
Source File: MongoUserManager.java    From gameserver with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the user's relation object.
 * @param userId
 * @param relObj
 * @param type
 * @return
 */
private Relation constructUserRelationObject(UserId userId, DBObject relObj, RelationType type) {
	Relation relation = new Relation();
	relation.set_id(userId);
	relation.setType(type);
	Set<String> keySet = relObj.keySet();
	for (Iterator<String> iter = keySet.iterator(); iter.hasNext();) {
		String username = iter.next();
		DBObject peopleObj = (DBObject)relObj.get(username);
		UserId id = UserId.fromBytes((byte[])peopleObj.get(ID));
		UserId myId = UserId.fromBytes((byte[])peopleObj.get("myId"));
		String rolename = (String)peopleObj.get("rolename");
		Integer win = (Integer)peopleObj.get("win");
		Integer lose = (Integer)peopleObj.get("lose");
		int level = 1;
		Integer levelObj = (Integer)peopleObj.get("level");
		if ( levelObj != null ) {
			level = levelObj.intValue();
		}
		People p = new People();
		p.setId(id);
		p.setMyId(myId);
		p.setUsername(username);
		p.setRolename(rolename);
		if ( win != null ) {
			p.setWin(win.intValue());
		}
		if ( lose != null ) {
			p.setLose(lose.intValue());
		}
		p.setLevel(level);
		relation.addPeople(p);
	}
	relation.clearChangeMark();
	return relation;
}
 
Example 16
Source File: MongoEntity.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static Map<String, List<Map<String, Object>>> extractDenormalizedData(DBObject dbObj) {
    String type = (String) dbObj.get("type");
    Map<String, List<Map<String, Object>>> denormalized = new HashMap<String, List<Map<String, Object>>>();

    for (String key : dbObj.keySet()) {
        if (EmbeddedDocumentRelations.isDenormalization(type, key)) {
            List<Map<String, Object>> values = (List<Map<String, Object>>) dbObj.get(key);
            denormalized.put(key, values);
        }
    }

    return denormalized;
}
 
Example 17
Source File: MongoCollectionReader.java    From bluima with Apache License 2.0 4 votes vote down vote up
@Override
public void getNext(JCas jCas) throws IOException, CollectionException {

    // text & id
    DBObject doc = cur.next();
    Object text = doc.get(TEXT);
    if (text != null)
        jCas.setDocumentText(doc.get(TEXT).toString());
    else
        jCas.setDocumentText("");
    Header h = new Header(jCas);
    h.setDocId(doc.get(ID).toString());
    if (doc.containsField(TITLE) && doc.get(TITLE) != null)
        h.setTitle(doc.get(TITLE).toString());
    else
        h.setTitle("");
    h.addToIndexes();

    // all other annotations, from mappings
    for (String dbListsName : doc.keySet()) {

        for (String annotClass : ALL_MAPPINGS_KEYS) {
            MongoFieldMapping fm = ALL_MAPPINGS.get(annotClass);

            if (fm.shortName.equals(dbListsName)) {

                BasicDBList dbList = (BasicDBList) doc.get(dbListsName);
                for (Object o : dbList) {
                    BasicDBObject dbO = (BasicDBObject) o;

                    try {
                        Annotation a = getAnnotationByClassName(jCas,
                                annotClass);
                        a.setBegin(dbO.getInt(BEGIN));// LATER maybe opt.
                        a.setEnd(dbO.getInt(END));

                        Type t = a.getType();
                        for (Feature f : t.getFeatures()) {
                            // System.err.println("f.short "
                            // + f.getShortName());

                            if (fm.fieldMappings.containsKey(f
                                    .getShortName())) {

                                String fieldKey = fm.fieldMappings.get(f
                                        .getShortName());
                                String range = f.getRange().getShortName();

                                MongoFieldMapping.readFieldFromDb(fieldKey,
                                        range, a, f, dbO, jCas);
                            }
                        }
                        a.addToIndexes();

                    } catch (Exception e) {
                        LOG.error("while processing docId " + doc.get(ID),
                                e);
                    }
                }
            }
        }
    }
}
 
Example 18
Source File: QueryProperties.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the full names of selected fields in an ordered list.
 */
@SuppressWarnings("unchecked")
public List<String> getSelectedFieldNames()
{
    Object propValue = getPropertiesMap().get( SELECTED_FIELDS_PROP );
    if( propValue instanceof List<?> )
        return (List<String>)propValue;

    if( propValue instanceof String )
    {
        DBObject projectionKeys = null;
        try
        {
            projectionKeys = parseExprToDBObject( (String)propValue );
        }
        catch( OdaException ex )
        {
            // log and ignore
            getLogger().log( Level.INFO, 
                    Messages.bind( "Ignoring invalid Selected Fields expression: {0}", propValue ), ex ); //$NON-NLS-1$
            return Collections.emptyList();
        }
        
        // extract the included fields to a list of selected fields
        List<String> selectedFieldNames = new ArrayList<String>(projectionKeys.keySet().size());
        for( String key : projectionKeys.keySet() )
        {
            Object value = projectionKeys.get( key );
            if( value instanceof Integer )
            {
                if( (Integer)value == 0 )   // field is being excluded
                    continue;
            }
            else if( value instanceof Boolean && (Boolean)value == Boolean.FALSE )   // field is being excluded
                continue;

            selectedFieldNames.add( key );
        }
        return selectedFieldNames;
    }

    return Collections.emptyList();    // null or non-recognized data type
}
 
Example 19
Source File: RollupStorageInterceptor.java    From hvdf with Apache License 2.0 4 votes vote down vote up
private void updateBatch(BasicDBList sample) {
	
	// The batch may span collection splits, so maintain
	// a current collection and batch operation
	BulkWriteOperation currentOp = null;
	int currentOpOffset = 0;
	int sampleIdx = 0;
	DBCollection currentColl = null;	
	
	logger.debug("Received batch of size : {}", sample.size());
	
	try{
		for(; sampleIdx < sample.size(); ++sampleIdx){
			
			// prepare the sample to batch
			BasicDBObject doc = (BasicDBObject) (sample.get(sampleIdx));
			long timestamp = this.rollupPeriod * (doc.getLong(Sample.TS_KEY) / this.rollupPeriod);			
			DBCollection collection = collectionAllocator.getCollection(timestamp);
			
			// if the collection has changed, commit the current
			// batch to the collection and start new
			if(collection.equals(currentColl) == false){
				executeBatchUpdate(currentOp, sample);
				currentColl = collection;
				currentOp = collection.initializeUnorderedBulkOperation();
				currentOpOffset = sampleIdx;
			}
			
			// put the doc insert into the batch
			// Ask the id allocator for the query
			BasicDBObject query = this.idFactory.getQuery(doc.get(Sample.SOURCE_KEY), timestamp);
			
			// Build the update clause using the ops list
			BasicDBObject update = new BasicDBObject();
			for(RollupOperation rollupOp : this.rollupOps){
				
				DBObject updateClause = rollupOp.getUpdateClause(doc);
				
				// Check for top level operators that already exist so they dont overwrite
				for(String key : updateClause.keySet()){
					BasicDBObject existingClause = (BasicDBObject) update.get(key);
					if(existingClause != null){
						// Merge the arguments to the top level op
						existingClause.putAll((DBObject)updateClause.get(key));
					} else {
						update.put(key, updateClause.get(key));
					}
				}
			}
			
			currentOp.find(query).upsert().updateOne(update);
		}		
		
		// Finalize the last batch
		executeBatchUpdate(currentOp, sample);		
		
	} catch(Exception ex){
		
		// One of the bulk writes has failed
		BasicDBList failedDocs = new BasicDBList();
		if(ex instanceof BulkWriteException){
			
			// We need to figure out the failures and remove the writes
			// that worked from the batch
			int batchSize = sampleIdx - currentOpOffset;
			BulkWriteException bwex = (BulkWriteException)ex;
			int errorCount = bwex.getWriteErrors().size(); 
			if(errorCount < batchSize){
				
				for(BulkWriteError we : bwex.getWriteErrors()){
					failedDocs.add(sample.get(currentOpOffset + we.getIndex()));
				}
				
				// since we have accounted for the failures in the current
				// batch, move the offset forward to the last sample
				currentOpOffset = sampleIdx;					
			}
		}
		
		// If this happened part way through the batch, send remaining 
		// docs to failed list and update sample to contain only failed docs
		if(currentOpOffset > 0){
			for(; currentOpOffset < sample.size(); ++currentOpOffset)
				failedDocs.add(sample.get(currentOpOffset));
			sample.clear();
			sample.addAll(failedDocs);	
		}
		
		throw ex;
	}
}
 
Example 20
Source File: MongoDB.java    From act with GNU General Public License v3.0 4 votes vote down vote up
private static DBObject compare(DBObject doc, DBObject docref, boolean listsAreSet) {
  boolean different = false;

  BasicDBObject diff = new BasicDBObject();
  Set<String> refKeys = new HashSet<String>();
  refKeys.addAll(docref.keySet());
  for (String k : doc.keySet()) {
    // as numerical calculations are improved, some computed fields are
    // bound to change: e.g., rarity and estimateEnergy
    // so make a special exception for those and ignore its val field...
    // but compare any other key recursively for differences...
    if (k.equals("rarity") || k.equals("estimateEnergy") || k.equals("coefficient"))
      continue;

    Object val = doc.get(k);

    if(!docref.containsKey(k)) {
      // this field is new
      diff.put("+" + k, val);
      different = true;
    } else {
      // field exists in old doc, recursively compare
      Object refval = docref.get(k);
      refKeys.remove(k);

      Object d;
        if ((d = compare(val, refval, listsAreSet)) != null) {
        // keys identical but values differ, add without the + or - to key
        different = true;
        diff.put(k, d);
      } else {
        // values identical and keys same too, do not put in diff.
      }
    }
  }

  // all remaining fields were deleted from old doc
  for (String kref : refKeys) {
    if (kref.equals("rarity") || kref.equals("estimateEnergy") || kref.equals("coefficient")) // see why in loop above
      continue;

    diff.put("-" + kref, docref.get(kref));
    different = true;
  }

  return different ? diff : null;

  // the following is not order invariant and therefore problematic:
  // return org.apache.commons.lang.StringUtils.difference(doc.toString(), docref.toString());
}