List of usage examples for com.mongodb DBObject get
Object get(String key);
From source file:com.ebay.cloud.cms.dal.persistence.impl.embed.EmbedModifyCommand.java
License:Apache License
protected DBObject buildModifyBody(BitSet arrayBits, DBObject rootObject, MetaClass rootMetaClass) { BasicDBObject embedObject = (BasicDBObject) EmbedDBObjectFilter.filter(entity.getId(), rootObject, rootMetaClass, null, helper); setEmbedObjectValue(embedObject);// ww w . j a v a2 s. com BasicDBObject setModifyObject = new BasicDBObject(); BasicDBObject obj = (BasicDBObject) rootObject.get(embedFieldName); if (obj == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Modify, can not find embed field with Id: " + this.entity.getId()); } obj.put(FieldProperty.TIMESTAMP.getDbName(), new Date()); setModifyObject.put(embedFieldName, obj); BasicDBObject modifyBody = new BasicDBObject(); modifyBody.put("$set", setModifyObject); // increase version on root document BasicDBObject versionObject = new BasicDBObject(); versionObject.put(InternalFieldEnum.VERSION.getDbName(), 1); modifyBody.put("$inc", versionObject); buildRootUpdateObject(entity, null, modifyBody, rootMetaClass); return modifyBody; }
From source file:com.ebay.cloud.cms.dal.persistence.impl.root.RootFieldModifyCommand.java
License:Apache License
private void buildSetFieldBody(DBObject modifyBody) { MetaField field = getField();/*from ww w.j ava2 s .c o m*/ BasicDBObject enityObject = (BasicDBObject) getEntity().getNode(); BasicDBObject fieldObject = (BasicDBObject) enityObject.get(field.getDbName()); if (fieldObject != null) { BasicDBObject set = (BasicDBObject) modifyBody.get("$set"); set.put(field.getDbName(), fieldObject); } }
From source file:com.ebay.cloud.cms.dal.persistence.impl.root.RootFieldModifyCommand.java
License:Apache License
private void buildArrayBody(DBObject modifyBody) { MetaField field = getField();/*from w w w . j a va 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("$inc"); 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.dal.persistence.impl.root.RootFieldModifyCommand.java
License:Apache License
private void buildJsonBody(DBObject modifyBody) { BasicDBObject set = (BasicDBObject) modifyBody.get("$set"); MetaField field = getField();/*from w w w. j a va2 s .c om*/ BasicDBObject enityObject = (BasicDBObject) getEntity().getNode(); BasicDBObject fieldObject = (BasicDBObject) enityObject.get(field.getDbName()); if (fieldObject != null) { BasicDBObject givenValue = (BasicDBObject) (fieldObject).get(V); if (givenValue != null) { for (String key : givenValue.keySet()) { set.put(field.getValueDbName() + DOT + key, givenValue.get(key)); } // field properties // no length here BasicDBObject setDbo = (BasicDBObject) modifyBody.get("$set"); setDbo.put(field.getDbName() + DOT + FieldProperty.TIMESTAMP.getDbName(), getEntity().getFieldTimestamp(field.getName())); } } }
From source file:com.ebay.cloud.cms.dal.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 a 2 s.c o m 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 modify body is " + version + "! entity is " + entity.toString()); } }
From source file:com.ebay.cloud.cms.dal.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 + "! entity is " + entity.toString()); }//from w w w . java 2 s. 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.dal.search.impl.query.EmbedSearchQuery.java
License:Apache License
private DBObject trimOperator(DBObject match, String operator) { if (match == null) { return null; }/* ww w . j av a2 s .com*/ return match.containsField(operator) ? (DBObject) match.get(operator) : match; }
From source file:com.ebay.cloud.cms.dalapi.persistence.impl.embed.AbstractEmbedCommand.java
License:Apache License
protected void buildRootUpdateObject(BsonEntity entity, Integer newVersion, DBObject updateBody, MetaClass rootMetaClass) {//from w w w .ja v a2s.c om BasicDBObject setObject = (BasicDBObject) updateBody.get("$set"); if (setObject == null) { setObject = new BasicDBObject(); } // populate the data to the root entity for the internal fields if (newVersion != null) { setObject.put(InternalFieldEnum.VERSION.getDbName(), newVersion); } setObject.put(InternalFieldEnum.MODIFIER.getDbName(), entity.getModifier()); setObject.put(InternalFieldEnum.LASTMODIFIED.getDbName(), entity.getLastModified()); setObject.put(InternalFieldEnum.STATUS.getDbName(), StatusEnum.ACTIVE.toString()); setObject.put(InternalFieldEnum.BRANCH.getDbName(), entity.getBranchId()); setObject.put(InternalFieldEnum.TYPE.getDbName(), rootMetaClass.getName()); String rootId = helper.getRootId(entity.getId()); setObject.put(InternalFieldEnum.ID.getDbName(), rootId); setInternalField(entity, setObject, InternalFieldEnum.COMMENT); setInternalField(entity, setObject, InternalFieldEnum.USER); updateBody.put("$set", setObject); }
From source file:com.ebay.cloud.cms.dalapi.persistence.impl.embed.EmbedCreateCommand.java
License:Apache License
@Override public void execute(PersistenceContext context) { MetaClass rootMetaClass = getRootEntityMetaClass(entity.getId(), context); String parentId = helper.getParentId(entity.getId()); BitSet parentBits = helper.checkArrayOnPath(parentId, rootMetaClass); // query root object with parent id DBObject rootQuery = buildGetQuery(parentBits, parentId, rootMetaClass); String id = parentId;//from w w w .ja v a 2s . c om if (parentBits.cardinality() == 0) { id = entity.getId(); } DBObject rootFields = buildGetRootFields(id, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootFields); if (rootObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Create, parent document doesn't exist! " + parentId); } // add embed document into parent document DBObject createQuery = buildModifyQuery(rootQuery, entity); int newVersion = (Integer) rootObject.get(InternalFieldEnum.VERSION.getDbName()) + 1; String parentPath = getUpdatePath(parentBits, parentId, rootObject, rootMetaClass); MetaRelationship lastField = helper.getLastMetaField(entity.getId(), rootMetaClass); DBObject createBody = buildCreateBody(lastField, parentPath, entity, newVersion, rootMetaClass, rootObject, parentId); WriteResult result = MongoExecutor.update(context, rootMetaClass, createQuery, createBody); if (result.getN() == 0) { // something happens between get and replace throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails"); } }
From source file:com.ebay.cloud.cms.dalapi.persistence.impl.embed.EmbedDeleteCommand.java
License:Apache License
@Override public void execute(PersistenceContext context) { MetaClass rootMetaClass = getRootEntityMetaClass(entityId, context); String parentId = helper.getParentId(entityId); BitSet parentBits = helper.checkArrayOnPath(entityId, rootMetaClass); //TODO : adjacent query/update in code level, should always hit PRIMARY // query root object with parent id DBObject rootQuery = buildGetQuery(parentBits, parentId, rootMetaClass); DBObject rootFields = buildGetFields(parentBits, parentId, false, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootFields); if (rootObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Delete, parenet document doesn't exist! " + parentId); }//from ww w.j a v a2 s. c o m // add embed document into parent document DBObject createQuery = buildModifyQuery(rootQuery, entity); int newVersion = (Integer) rootObject.get(InternalFieldEnum.VERSION.getDbName()) + 1; String parentPath = getUpdatePath(parentBits, parentId, rootObject, rootMetaClass); MetaRelationship lastMetaField = helper.getLastMetaField(entityId, rootMetaClass); DBObject deleteBody = buildDeleteBody(lastMetaField, parentPath, entityId, newVersion, rootMetaClass); WriteResult result = MongoExecutor.update(context, rootMetaClass, createQuery, deleteBody); if (result.getN() == 0) { // something happens between get and replace throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails for " + entityId + " in class " + metaClass.getName()); } }