Example usage for com.mongodb DBObject containsField

List of usage examples for com.mongodb DBObject containsField

Introduction

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

Prototype

boolean containsField(String s);

Source Link

Document

Checks if this object contains a field with the given name.

Usage

From source file:tango.mongo.MongoUtils.java

License:Open Source License

public static int[] getIntArray(DBObject dbo, String key) {
    //IJ.log("mongoutils:"+dbo.get(key));
    if (!dbo.containsField(key))
        return null;
    Object o = dbo.get(key);/*from  ww  w.j a v  a  2s . c  om*/
    if (o instanceof BasicDBList) {
        BasicDBList list = ((BasicDBList) dbo.get(key));
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = (Integer) (list.get("" + i));
        }
        return res;
    } else if (o instanceof int[])
        return (int[]) o;
    else
        return new int[] { (Integer) o };
}

From source file:tango.mongo.MongoUtils.java

License:Open Source License

public static double[] getDoubleArray(DBObject dbo, String key) {
    if (!dbo.containsField(key))
        return null;
    BasicDBList list = ((BasicDBList) dbo.get(key));
    double[] res = new double[list.size()];
    for (int i = 0; i < list.size(); i++) {
        res[i] = (Double) (list.get("" + i));
    }//  www .  ja va 2s  . c o m
    return res;
}

From source file:uk.ac.ebi.eva.commons.models.converters.data.DBObjectToVariantAnnotationConverter.java

License:Apache License

private List<String> convertSoField(DBObject ct) {
    List<String> soAccessionNames = new LinkedList<>();
    if (ct.containsField(AnnotationFieldNames.SO_ACCESSION_FIELD)) {
        if (ct.get(AnnotationFieldNames.SO_ACCESSION_FIELD) instanceof List) {
            List<Integer> list = (List) ct.get(AnnotationFieldNames.SO_ACCESSION_FIELD);
            for (Integer so : list) {
                soAccessionNames.add(ConsequenceTypeMappings.accessionToTerm.get(so));
            }//from  www.jav  a  2s . c o  m
        } else {
            soAccessionNames.add(ConsequenceTypeMappings.accessionToTerm
                    .get(ct.get(AnnotationFieldNames.SO_ACCESSION_FIELD)));
        }
    }
    return soAccessionNames;
}

From source file:uk.ac.ebi.eva.commons.models.converters.data.DBObjectToVariantAnnotationConverter.java

License:Apache License

private List<Score> convertProteinSubstitutionScoresField(DBObject ct) {
    List<Score> proteinSubstitutionScores = new LinkedList<>();
    if (ct.containsField(AnnotationFieldNames.PROTEIN_SUBSTITUTION_SCORE_FIELD)) {
        List<DBObject> list = (List) ct.get(AnnotationFieldNames.PROTEIN_SUBSTITUTION_SCORE_FIELD);
        for (DBObject dbObject : list) {
            proteinSubstitutionScores//from  w w w . ja  v  a2 s . c o  m
                    .add(new Score(getDefault(dbObject, AnnotationFieldNames.SCORE_SCORE_FIELD, 0.0),
                            getDefault(dbObject, AnnotationFieldNames.SCORE_SOURCE_FIELD, ""),
                            getDefault(dbObject, AnnotationFieldNames.SCORE_DESCRIPTION_FIELD, "")));
        }
    }

    if (ct.containsField(AnnotationFieldNames.POLYPHEN_FIELD)) {
        DBObject dbObject = (DBObject) ct.get(AnnotationFieldNames.POLYPHEN_FIELD);
        proteinSubstitutionScores
                .add(new Score(getDefault(dbObject, AnnotationFieldNames.SCORE_SCORE_FIELD, 0.0), "Polyphen",
                        getDefault(dbObject, AnnotationFieldNames.SCORE_DESCRIPTION_FIELD, "")));
    }

    if (ct.containsField(AnnotationFieldNames.SIFT_FIELD)) {
        DBObject dbObject = (DBObject) ct.get(AnnotationFieldNames.SIFT_FIELD);
        proteinSubstitutionScores
                .add(new Score(getDefault(dbObject, AnnotationFieldNames.SCORE_SCORE_FIELD, 0.0), "Sift",
                        getDefault(dbObject, AnnotationFieldNames.SCORE_DESCRIPTION_FIELD, "")));
    }
    return proteinSubstitutionScores;
}

From source file:uuidupdater.DatabaseTool.java

private HashSet<String[]> readCollection(String playerfield) {
    DBCursor dbc = mongo.getDB(db).getCollection(collection).find();
    HashSet<String> playernames = new HashSet<>();
    HashSet<String[]> lookupArrays = new HashSet<>();
    boolean isList = mongo.getDB(db).getCollection(collection)
            .findOne(new BasicDBObject(playerfield, new BasicDBObject("$exists", true)))
            .get(playerfield) instanceof BasicDBList;

    while (dbc.hasNext()) {
        DBObject dbo = dbc.next();
        ObjectId oid = (ObjectId) dbo.get("_id");
        if (!dbo.containsField(playerfield))
            continue;
        if (!isList) {
            String playername = (String) dbo.get(playerfield);
            if (!playernames.contains(playername)) {
                playernames.add(playername);
                occurencies.put(playername, new HashSet<ObjectId>());
            }/*from   w  w w .  j a  va  2 s .c  o m*/
            if (!players.containsKey(oid)) {
                players.put(oid, new HashSet<String>());
            }
            players.get(oid).add(playername);
            occurencies.get(playername).add(oid);
        } else {
            for (Object object : (BasicDBList) dbo.get(playerfield)) {
                String playername = (String) object;
                if (!playernames.contains(playername)) {
                    playernames.add(playername);
                    occurencies.put(playername, new HashSet<ObjectId>());
                }
                if (!players.containsKey(oid)) {
                    players.put(oid, new HashSet<String>());
                }
                players.get(oid).add(playername);
                occurencies.get(playername).add(oid);
            }
        }
    }
    Iterator iterator = playernames.iterator();
    while (iterator.hasNext()) {
        String[] next100 = new String[100];
        int i = 0;
        while (i < 100 && iterator.hasNext()) {
            next100[i] = (String) iterator.next();
            i++;
        }
        if (!iterator.hasNext()) {
            next100 = shortenArray(next100);
        }
        lookupArrays.add(next100);
    }
    return lookupArrays;
}

From source file:v7cr.v7db.Versioning.java

License:Open Source License

/**
 * inserts a first version of an object.
 * //  ww w  . j av  a2s.  com
 * It must not already have a _version field. _version will be set to 1.
 * 
 */
public static WriteResult insert(DBCollection collection, DBObject object) {
    if (object.containsField(VERSION)) {
        throw new IllegalArgumentException("The object already has a _version " + object);
    }
    object.put(VERSION, 1);
    return collection.insert(object);
}

From source file:v7db.files.mongodb.SimpleGarbageCollector.java

License:Open Source License

static void purge(DBCollection contents, DBCollection references) throws MongoException, DecoderException {

    List<Object> refsToPurge = new ArrayList<Object>();
    Set<String> potentialGarbage = new HashSet<String>();
    Set<String> notGarbage = new HashSet<String>();

    for (DBObject x : references.find()) {
        if (x.containsField("purge")) {
            refsToPurge.add(x.get("_id"));
            for (Object r : BSONUtils.values(x, "refs")) {
                String h = Hex.encodeHexString((byte[]) r);
                potentialGarbage.add(h);
            }//from  w w w .  j a  v a2s .c o  m
            for (Object r : BSONUtils.values(x, "refHistory")) {
                String h = Hex.encodeHexString((byte[]) r);
                potentialGarbage.add(h);
            }
        } else {
            for (Object r : BSONUtils.values(x, "refs")) {
                String h = Hex.encodeHexString((byte[]) r);
                notGarbage.add(h);
            }
            for (Object r : BSONUtils.values(x, "refHistory")) {
                String h = Hex.encodeHexString((byte[]) r);
                notGarbage.add(h);
            }
        }
    }

    potentialGarbage.removeAll(notGarbage);
    // TODO: bases must not be removed
    for (String g : potentialGarbage) {
        contents.remove(new BasicDBObject("_id", Hex.decodeHex(g.toCharArray())), WriteConcern.SAFE);
    }
    for (Object x : refsToPurge) {
        references.remove(new BasicDBObject("_id", x), WriteConcern.SAFE);
    }
}

From source file:v7db.files.mongodb.Vermongo.java

License:Open Source License

/**
 * inserts a new object into the collection. The _version property must not
 * be present in the object, and will be set to 1 (integer).
 * //from   w  ww  . j a  v a2s  . com
 * @param object
 */
static void insert(DBCollection collection, DBObject object) {
    if (object.containsField(_VERSION))
        throw new IllegalArgumentException();

    object.put(_VERSION, 1);
    collection.insert(object, WriteConcern.SAFE);
}

From source file:v7db.files.mongodb.Vermongo.java

License:Open Source License

/**
 * updates an existing object. The object must have been the _version
 * property set to the version number of the base revision (i.e. the version
 * number to be replaced). If the current version in the DB does not have a
 * matching version number, the operation aborts with an
 * UpdateConflictException.//from   w w w.j  av a 2s  . c  om
 * 
 * After the update is successful, _version in the object is updated to the
 * new version number.
 * 
 * The version that was replaced is moved into the collection's shadow
 * collection.
 * 
 * @param collection
 * @param object
 * @throws UpdateConflictException
 */
static void update(DBCollection collection, DBObject object) throws UpdateConflictException {
    if (!object.containsField(_VERSION))
        throw new IllegalArgumentException("the base version number needs to be included as _version");

    int baseVersion = (Integer) object.get(_VERSION);

    // load the base version
    {
        DBObject base = collection.findOne(new BasicDBObject("_id", getId(object)));
        if (base == null) {
            throw new IllegalArgumentException("document to update not found in collection");
        }
        Object bV = base.get(_VERSION);
        if (bV instanceof Integer) {
            if (baseVersion != (Integer) bV) {
                throw new UpdateConflictException(object, base);
            }
        } else {
            throw new UpdateConflictException(object, base);
        }
        // copy to shadow
        DBCollection shadow = getShadowCollection(collection);
        base.put("_id", new BasicDBObject("_id", getId(base)).append(_VERSION, baseVersion));
        WriteResult r = shadow.insert(base, WriteConcern.SAFE);

        // TODO: if already there, no error
        r.getLastError().throwOnError();
    }

    try {
        object.put(_VERSION, baseVersion + 1);
        DBObject found = collection
                .findAndModify(new BasicDBObject("_id", getId(object)).append(_VERSION, baseVersion), object);

        if (found == null) {
            // document has changed in the mean-time. get the latest version
            // again
            DBObject base = collection.findOne(new BasicDBObject("_id", getId(object)));
            if (base == null) {
                throw new IllegalArgumentException("document to update not found in collection");
            }
            throw new UpdateConflictException(object, base);
        }

    } catch (RuntimeException e) {
        object.put(_VERSION, baseVersion);
        throw e;
    }

}

From source file:xbdd.webapp.resource.feature.AdminUtils.java

License:Apache License

private DBObject renameDoc(String product, String newname, DBObject doc) {
    doc.put("_id", ((String) doc.get("_id")).replaceAll(product + "/", newname + "/"));
    if (doc.containsField("coordinates")) {
        DBObject coordinates = (DBObject) doc.get("coordinates");
        coordinates.put("product", newname);
        doc.put("coordinates", coordinates);
    }/*from   w  w w.  java2s.c o m*/
    return doc;
}