Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.ebay.cloud.cms.dalapi.persistence.impl.embed.EmbedGetCommand.java

License:Apache License

@Override
public void execute(PersistenceContext context) {
    MetaClass rootMetaClass = getRootEntityMetaClass(entityId, context);
    BitSet arrayBits = helper.checkArrayOnPath(entityId, rootMetaClass);
    // find root object & embed objects                
    DBObject getQuery = buildGetQuery(arrayBits, entityId, rootMetaClass);
    DBObject getField = buildGetFields(arrayBits, entityId, true, rootMetaClass);

    DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, getQuery, getField);
    if (rootObject == null) {
        return;// w ww  .  ja  v a2  s.  co  m
    }
    // extract embed object by id
    DBObject embedObject = EmbedDBObjectFilter.filter(entityId, rootObject, rootMetaClass,
            context.getQueryFields(), helper);
    if (embedObject != null) {
        resultEntity = new BsonEntity(entityType, embedObject);
        int rootVersion = (Integer) rootObject.get(InternalFieldEnum.VERSION.getDbName());
        resultEntity.setVersion(rootVersion);
    }
}

From source file:com.ebay.cloud.cms.dalapi.persistence.impl.root.RootFieldModifyCommand.java

License:Apache License

private void buildArrayBody(DBObject modifyBody) {
    MetaField field = getField();/* www  .  j av a 2s .co  m*/
    BasicDBObject enityObject = (BasicDBObject) getEntity().getNode();
    BasicDBObject fieldObject = (BasicDBObject) enityObject.get(field.getDbName());

    if (fieldObject != null) {
        Object givenValue = (fieldObject).get(V);
        if (givenValue != null) {
            BasicDBObject eachDbo = new BasicDBObject();
            eachDbo.put("$each", givenValue);

            BasicDBObject addToSetDbo = new BasicDBObject();
            addToSetDbo.put(field.getValueDbName(), eachDbo);

            modifyBody.put("$addToSet", addToSetDbo);

            // field length, only update when we do have updates
            BasicDBObject inc = (BasicDBObject) modifyBody.get("$set");
            inc.put(field.getDbName() + DOT + FieldProperty.LENGTH.getDbName(),
                    getEntity().getFieldLength(fieldName));
            // field time stamp
            BasicDBObject set = (BasicDBObject) modifyBody.get("$set");
            set.put(field.getDbName() + DOT + FieldProperty.TIMESTAMP.getDbName(),
                    getEntity().getFieldTimestamp(fieldName));
        }
    }

}

From source file:com.ebay.cloud.cms.dalapi.persistence.impl.root.RootModifyCommand.java

License:Apache License

@Override
public void execute(PersistenceContext context) {
    DBObject queryObject = buildModifyQuery();
    DBObject updateObject = buildModifyBody();
    WriteResult result = null;//from  w  w  w  .  j a v  a2 s  . com
    result = MongoExecutor.update(context, entity.getMetaClass(), queryObject, updateObject);

    //throw exception if modify failed to match any entity
    if (result.getN() == 0) {
        DBObject getResult = RootGetCommand.findDBObject(entity.getId(), entity.getBranchId(), context,
                entity.getMetaClass(), ROOT_FIELDS, true);
        if (getResult == null) {
            throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND,
                    "Can't find entity: oid=" + entity.getId());
        }
        Object currentVersion = getResult.get(InternalFieldEnum.VERSION.getDbName());
        throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT,
                "current version is " + currentVersion + ", but version in repalce body is " + version);
    }
}

From source file:com.ebay.cloud.cms.dalapi.persistence.impl.root.RootReplaceCommand.java

License:Apache License

@Override
public void execute(PersistenceContext context) {
    DBObject getResult = RootGetCommand.findDBObject(entity.getId(), entity.getBranchId(), context,
            entity.getMetaClass(), ROOT_FIELDS, false);
    if (getResult != null) {
        // check version
        int currentVersion = (Integer) getResult.get(InternalFieldEnum.VERSION.getDbName());
        int version = entity.getVersion();
        if ((version != IEntity.NO_VERSION) && (currentVersion != version)) {
            throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT,
                    "current version is " + currentVersion + ", but version in repalce body is " + version);
        }// w  ww . j  a v  a 2s. c o  m

        Date createTime = (Date) getResult.get(InternalFieldEnum.CREATETIME.getDbName());

        DBObject queryObject = buildReplaceRootQuery(currentVersion);
        DBObject updateObject = buildReplaceRootUpdate(currentVersion, createTime);
        try {
            WriteResult result = MongoExecutor.update(context, entity.getMetaClass(), queryObject,
                    updateObject);
            if (result.getN() == 0) {
                throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT,
                        "current version is not " + currentVersion);
            }
            //set version for response
            entity.setVersion(currentVersion + 1);
        } catch (RuntimeException e) {
            entity.setVersion(currentVersion);
            throw e;
        } catch (Throwable t) {
            //if anything bad happens, need to set version back
            entity.setVersion(currentVersion);
        } finally {
            //in update scenario, have to set id value back into entity object because we
            //removed id field before
            entity.setId(entity.getId());
        }
    } else {
        // insert

        //2012/7/13 jianxu1 multi threading issue, if client A and client B send replace at same time
        //at T1, none of A and B find existing entity, so at T2, both A and B call insert, of we do not have 
        //unique index for combination of _branchId and _oid, we end up with duplicated entities.
        //FIX is: http://www.mongodb.org/display/DOCS/Indexes,  composite unique index
        //db.branches.ensureIndex({_branchId: 1, _oid: 1}, {unique: true});
        //TODO: change create repository to add composite unique index for branches, for main branch, we just need unique index on _oid

        DBObject insertObject = entity.getNode();
        insertObject.removeField("_id");
        insertObject.put(InternalFieldEnum.VERSION.getDbName(), IEntity.START_VERSION);
        MongoExecutor.insert(context, entity.getMetaClass(), insertObject);
    }
}

From source file:com.ebay.cloud.cms.metadata.dataloader.MetadataDataLoader.java

License:Apache License

private List<String> getAllRepositoryNames() {
    DBCollection repoCollection = this.mongo.getDB(CMSConsts.SYS_DB).getCollection(CMSConsts.REPOSITORY_COLL);
    BasicDBObject query = new BasicDBObject();

    List<String> repoNameList = new ArrayList<String>();
    DBCursor cursor = repoCollection.find(query);
    while (cursor.hasNext()) {
        DBObject bsonObject = cursor.next();
        String repoName = (String) bsonObject.get(Repository.REPOSITORY_FIELD_NAME);
        repoNameList.add(repoName);//from   ww w.ja  v a2s.  c o m
    }
    return repoNameList;
}

From source file:com.ebay.cloud.cms.metadata.sequence.MongoSequence.java

License:Apache License

private int getNextFromDB() {
    BasicDBObject q = new BasicDBObject();
    BasicDBObject exists = new BasicDBObject();
    exists.put(MongoOperand.exists, true);
    q.put(key, exists);/* w ww. j  av  a 2 s.c o m*/

    BasicDBObject u = new BasicDBObject();
    BasicDBObject o = new BasicDBObject();
    o.put(key, 1);
    u.put(MongoOperand.inc, o);

    try {
        DBObject obj = coll.findAndModify(q, null, null, false, u, true, true);
        return (Integer) obj.get(key);
    } catch (MongoException e) {
        throw new MongoOperationException(e);
    }
}

From source file:com.ebay.oss.bark.domain.DataAsset.java

License:Apache License

@SuppressWarnings({ "unchecked", "deprecation" })
public DataAsset(DBObject o) {
    this.set_id(Long.parseLong(o.get("_id").toString()));
    this.setAssetHDFSPath((String) o.get("assetHDFSPath"));
    this.setAssetName((String) o.get("assetName"));
    this.setAssetType((String) o.get("assetType"));

    this.setOwner((String) o.get("owner"));
    this.setPlatform((String) o.get("platform"));
    this.setSystem((String) o.get("system"));

    //      this.setPartitions((List<PartitionFormat>) o.get("partitions")); // this doesn't work
    if (o.get("partitions") != null) {
        List<PartitionFormat> partitionlist = new ArrayList<PartitionFormat>();
        List<DBObject> tlist = (List<DBObject>) o.get("partitions");
        for (DBObject temp : tlist) {
            partitionlist.add(new PartitionFormat(temp.get("name").toString(), temp.get("format").toString()));
        }//  w w w  .ja v a  2  s .c  o  m
        this.setPartitions(partitionlist);
    }

    //      this.setSchema((List<DataSchema>) o.get("schema"));
    if (o.get("schema") != null) {
        List<DBObject> tlist = (List<DBObject>) o.get("schema");
        List<DataSchema> list = new ArrayList<DataSchema>();
        for (DBObject temp : tlist) {
            list.add(new DataSchema(temp.get("name").toString(), temp.get("type").toString(),
                    temp.get("desc").toString(), temp.get("sample").toString()));
        }
        this.setSchema(list);
    }

    if (!o.containsField("timestamp")) {
        this.setTimestamp(new Date());
    } else {
        this.setTimestamp(new Date(o.get("timestamp").toString()));
    }
}

From source file:com.ebay.oss.bark.repo.DqJobRepoImpl.java

License:Apache License

@Override
protected DqJob toEntity(DBObject o) {
    DqJob entity = new DqJob();
    entity.setId((String) o.get("_id"));
    entity.setModelList((String) o.get("modelList"));
    entity.setJobType(NumberUtils.parseInt(o.get("jobType")));
    entity.setStatus(NumberUtils.parseInt(o.get("status")));
    entity.setStarttime(NumberUtils.parseLong(o.get("starttime")));
    entity.setContent((String) o.get("content"));
    entity.setEndtime(NumberUtils.parseLong(o.get("endtime")));
    entity.setValue(NumberUtils.parseLong(o.get("value")));

    return entity;
}

From source file:com.ebay.oss.bark.repo.DqMetricsRepoImpl.java

License:Apache License

@Override
public DqMetricsValue getLatestByAssetId(String assetId) {
    DBCursor temp = dbCollection.find(new BasicDBObject("assetId", assetId));
    List<DBObject> all = temp.toArray();
    long latest = 0L;
    DBObject latestObject = null;/*www.  ja v  a  2s .c  o  m*/
    for (DBObject o : all) {
        if (Long.parseLong(o.get("timestamp").toString()) - latest > 0) {
            latest = Long.parseLong(o.get("timestamp").toString());
            latestObject = o;
        }
    }

    if (latestObject == null) {
        return null;
    }
    return new DqMetricsValue(latestObject.get("metricName").toString(),
            Long.parseLong(latestObject.get("timestamp").toString()),
            Float.parseFloat(latestObject.get("value").toString()));
}

From source file:com.ebay.oss.bark.repo.DqModelRepoImpl.java

License:Apache License

@Override
public DqModel findCountModelByAssetID(long assetID) {

    DBCursor cursor = dbCollection.find(new BasicDBObject("assetId", assetID));
    for (DBObject tempDBObject : cursor) {
        if (tempDBObject.get("modelType").equals(ModelType.VALIDITY)) {
            String content = tempDBObject.get("modelContent").toString();
            String[] contents = content.split("\\|");
            if (contents[2].equals(ValidityType.TOTAL_COUNT + "")) {
                return toEntity(tempDBObject);
            }//from ww  w  . j a  va2 s. c o m
        }
    }

    return null;

}