Example usage for com.mongodb DBObject keySet

List of usage examples for com.mongodb DBObject keySet

Introduction

In this page you can find the example usage for com.mongodb DBObject keySet.

Prototype

Set<String> keySet();

Source Link

Document

Returns this object's fields' names

Usage

From source file:org.fudgemsg.mapping.MongoDBFudgeBuilder.java

License:Apache License

/**
 * {@inheritDoc}//www  . java  2 s. c om
 */
@Override
public MutableFudgeFieldContainer buildMessage(FudgeSerializationContext context, DBObject dbObject) {
    if (dbObject == null) {
        return null;
    }
    MutableFudgeFieldContainer msg = context.newMessage();
    for (String key : dbObject.keySet()) {
        Object value = dbObject.get(key);
        if (value instanceof List<?>) {
            for (Object element : (List<?>) value) {
                msg.add(key, decodeObjectValue(context, element));
            }
        } else {
            msg.add(key, decodeObjectValue(context, value));
        }
    }
    return msg;
}

From source file:org.geogit.storage.mongo.MongoGraph.java

License:Open Source License

public <T extends Element> Set<String> getIndexedKeys(Class<T> elementClass) {
    List<DBObject> indices = collection.getIndexInfo();
    Set<String> results = new HashSet<String>();
    final String prefix;
    if (elementClass.equals(Vertex.class)) {
        prefix = "v";
    } else if (elementClass.equals(Edge.class)) {
        prefix = "e";
    } else {/*from   w w w. j ava 2  s. c o m*/
        throw new UnsupportedOperationException(
                "Can only retrieve indices for edges or vertices, not " + elementClass);
    }
    for (DBObject index : indices) {
        Object untypedName = index.get("name");
        if (untypedName instanceof String) {
            String name = (String) untypedName;
            if (name.startsWith(prefix)) {
                Object untypedKeys = index.get("key");
                if (untypedKeys instanceof DBObject) {
                    DBObject keys = (DBObject) untypedKeys;
                    for (String k : keys.keySet()) {
                        results.add(k.substring("properties.".length()));
                    }
                }
            }
        }
    }
    return Collections.unmodifiableSet(results);
}

From source file:org.gitective.mongo.MongoObjectIndexTable.java

License:Open Source License

public void get(Context options, Set<ObjectIndexKey> objects,
        AsyncCallback<Map<ObjectIndexKey, Collection<ObjectInfo>>> callback) {
    Map<ObjectIndexKey, Collection<ObjectInfo>> out = new HashMap<ObjectIndexKey, Collection<ObjectInfo>>();
    for (ObjectIndexKey objId : objects) {
        DBObject fetch = collection.findOne(new IdObject(objId.asString()));
        if (fetch == null)
            continue;
        Object values = fetch.get(VALUES);
        if (values instanceof DBObject) {
            DBObject dbo = (DBObject) values;
            Collection<ObjectInfo> chunks = out.get(objId);
            if (chunks == null) {
                chunks = new ArrayList<ObjectInfo>(4);
                out.put(objId, chunks);/*  w w w  .  ja v a 2s. co m*/
            }
            for (String key : dbo.keySet()) {
                byte[] value = MongoUtils.getBytes(dbo, key);
                key = unescapeKey(key);
                try {
                    chunks.add(
                            new ObjectInfo(ChunkKey.fromString(key), 0, GitStore.ObjectInfo.parseFrom(value)));
                } catch (InvalidProtocolBufferException e) {
                    callback.onFailure(new DhtException(e));
                    return;
                }
            }
        }
    }
    callback.onSuccess(out);
}

From source file:org.gitective.mongo.MongoRepositoryTable.java

License:Open Source License

public Collection<CachedPackInfo> getCachedPacks(RepositoryKey repo) throws DhtException, TimeoutException {
    DBObject object = collection.findOne(new IdObject(repo.asInt()));
    if (object == null)
        return Collections.emptyList();
    Object packs = object.get(PACKS);
    if (!(packs instanceof DBObject))
        return Collections.emptyList();
    DBObject dbo = (DBObject) packs;
    List<CachedPackInfo> info = new ArrayList<CachedPackInfo>();
    for (String key : dbo.keySet()) {
        byte[] value = MongoUtils.getBytes(dbo, key);
        try {/*from  w  ww .j  a va2s . c  o m*/
            info.add(CachedPackInfo.parseFrom(value));
        } catch (InvalidProtocolBufferException e) {
            throw new DhtException(e);
        }
    }
    return info;
}

From source file:org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.java

License:LGPL

private int getNumberOfEmbeddedAssociations(DBObject entity) {
    int numberOfReferences = 0;

    for (String fieldName : entity.keySet()) {
        Object field = entity.get(fieldName);
        if (isAssociation(field)) {
            numberOfReferences++;//from   ww w. j  a  v  a  2s .c om
        }
    }

    return numberOfReferences;
}

From source file:org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.java

License:LGPL

private static boolean isDBObjectAndContentEqual(Object left, Object right) {
    if (left instanceof BasicDBList) {
        if (!(right instanceof BasicDBList)) {
            return false;
        }// www .j a  va2s  .  c  om
        // we don't care about the order here for now
        BasicDBList leftAsList = (BasicDBList) left;
        BasicDBList rightAsList = (BasicDBList) right;
        // check that the fields names are the same
        Set<String> leftFields = new HashSet<String>(leftAsList.keySet());
        Set<String> rightFields = new HashSet<String>(rightAsList.keySet());
        if (!leftFields.equals(rightFields)) {
            return false;
        }
        // check that all left field values are in right
        for (String field : leftFields) {
            // fall back to native equals via the contains. It is out of our current tests
            if (!rightAsList.contains(leftAsList.get(field))) {
                return false;
            }
        }
    } else if (left instanceof DBObject) {
        if (!(right instanceof DBObject)) {
            return false;
        }
        DBObject leftAsDBObject = (DBObject) left;
        DBObject rightAsDBObject = (DBObject) right;
        // check that the fields names are the same
        Set<String> leftFields = new HashSet<String>(leftAsDBObject.keySet());
        Set<String> rightFields = new HashSet<String>(rightAsDBObject.keySet());
        if (!leftFields.equals(rightFields)) {
            return false;
        }
        // check that all left fields are in right and equal
        for (String field : leftFields) {
            boolean matches = isDBObjectAndContentEqual(leftAsDBObject.get(field), rightAsDBObject.get(field));
            if (!matches) {
                return false;
            }
        }
    } else {
        return left == right || (left != null && left.equals(right));
    }
    return true;
}

From source file:org.hibernate.ogm.dialect.mongodb.MongoDBAssociationSnapshot.java

License:Open Source License

public MongoDBAssociationSnapshot(DBObject assoc) {
    this.assoc = assoc;
    this.map = new LinkedHashMap<RowKey, DBObject>();

    for (DBObject row : getRows()) {
        List<String> columnNames = new ArrayList<String>();
        List<Object> columnValues = new ArrayList<Object>();
        DBObject columns = (DBObject) row.get(MongoDBDialect.COLUMNS_FIELDNAME);

        for (String columnKey : columns.keySet()) {
            columnNames.add(columnKey);//w w  w.j a  v  a 2s  .  co m
            columnValues.add(columns.get(columnKey));
        }

        RowKey rowKey = new RowKey((String) row.get(MongoDBDialect.TABLE_FIELDNAME),
                columnNames.toArray(new String[] {}), columnValues.toArray());

        this.map.put(rowKey, row);
    }
}

From source file:org.iternine.jeppetto.dao.mongodb.enhance.DBObjectUtil.java

License:Apache License

@SuppressWarnings({ "unchecked" })
private static Object prepareObjectMembersForMongo(TypeDefinition typeDef, Object object) {
    Class<?> cls = typeDef.getClazz();
    Class<?>[] optionalValueTypes = typeDef.getTypeParameters();

    if (DBObject.class.isAssignableFrom(cls)) {
        BasicDBObject dbo = new BasicDBObject();
        DBObject src = (DBObject) object;

        for (String key : src.keySet()) {
            Object rawObject = src.get(key);
            Object dboValue = (rawObject == null) ? null : toDBObject(rawObject.getClass(), rawObject);

            dbo.put(key, dboValue);/* w w  w.  j a  v  a2  s. c  om*/
        }

        return dbo;
    } else if (Map.class.isAssignableFrom(cls)) {
        return wrapMap(coalesceTypeParam(optionalValueTypes, 1), (Map) object);
    } else if (List.class.isAssignableFrom(cls)) {
        return wrapList(coalesceTypeParam(optionalValueTypes, 0), (List) object);
    } else if (Set.class.isAssignableFrom(cls)) {
        return wrapSet(coalesceTypeParam(optionalValueTypes, 0), (Set) object);
    } else if (Iterable.class.isAssignableFrom(cls)) {
        return wrapList(coalesceTypeParam(optionalValueTypes, 0), (Iterable) object);
    } else {
        throw new IllegalStateException("Unanticipated object " + object + " and type " + cls);
    }
}

From source file:org.iternine.jeppetto.dao.mongodb.MongoDBQueryModelDAO.java

License:Apache License

private Map<String, Set<String>> ensureIndexes(List<String> uniqueIndexes, final boolean unique) {
    if (uniqueIndexes == null || uniqueIndexes.size() == 0) {
        return Collections.emptyMap();
    }//from   w  w  w . j ava 2s  .c  o  m

    Map<String, Set<String>> result = new HashMap<String, Set<String>>();

    for (final String uniqueIndex : uniqueIndexes) {
        final DBObject index = new BasicDBObject();
        String[] indexFields = uniqueIndex.split(",");

        for (String indexField : indexFields) {
            indexField = indexField.trim();

            if (indexField.startsWith("+")) {
                index.put(indexField.substring(1), 1);
            } else if (indexField.startsWith("-")) {
                index.put(indexField.substring(1), -1);
            } else {
                index.put(indexField, 1);
            }
        }

        result.put(uniqueIndex, index.keySet());

        if (queryLogger != null) {
            queryLogger.debug("Ensuring index {} on {}",
                    new Object[] { index.toMap(), getCollectionClass().getSimpleName() });
        }

        dbCollection.ensureIndex(index, createIndexName(uniqueIndex), unique);
    }

    return result;
}

From source file:org.joda.beans.integrate.mongo.BeanMongoDBObject.java

License:Apache License

@Override
public void putAll(DBObject object) {
    for (String name : object.keySet()) {
        put(name, object.get(name));
    }
}