List of usage examples for com.mongodb DBObject get
Object get(String key);
From source file:com.ebay.cloud.cms.dal.persistence.flatten.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()); }/*w w w . j a va 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 + "! entity is " + entity.toString()); } //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.persistence.impl.embed.AbstractEmbedCommand.java
License:Apache License
protected void getArrayOffsetPath(BitSet arrayBits, String embedId, DBObject getObject, MetaClass rootMetaClass, List<String> updatePathList) { DBObject currentObject = getObject; List<String> idList = helper.generateAncestorIds(embedId); List<String> dbFieldList = helper.getEmbedPathSegs(embedId, rootMetaClass); for (int index = 0; index < dbFieldList.size(); index++) { String dbFieldName = dbFieldList.get(index); updatePathList.add(dbFieldName); updatePathList.add(MetaField.VALUE_KEY); DBObject fieldObject = (DBObject) currentObject.get(dbFieldName); if (arrayBits.get(index)) { @SuppressWarnings("unchecked") List<DBObject> refValues = (List<DBObject>) fieldObject.get(MetaField.VALUE_KEY); String idVal = idList.get(index); int offset = findOffsetById(refValues, idVal); currentObject = refValues.get(offset); updatePathList.add(String.valueOf(offset)); } else {/* w w w. j a v a 2 s . com*/ currentObject = (DBObject) fieldObject.get(MetaField.VALUE_KEY); } } }
From source file:com.ebay.cloud.cms.dal.persistence.impl.embed.AbstractEmbedCommand.java
License:Apache License
protected void buildRootUpdateObject(BsonEntity entity, Integer newVersion, DBObject updateBody, MetaClass rootMetaClass) {/*from w w w .j ava 2 s .c o m*/ 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(), new Date()); 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.dal.persistence.impl.embed.AbstractFieldEmbedCommand.java
License:Apache License
protected DBObject buildSetBody(DBObject rootObject) { BasicDBObject setModifyObject = new BasicDBObject(); BasicDBObject obj = (BasicDBObject) rootObject.get(embedFieldName); if (obj == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, getOperation() + ", can not find embed field with Id: " + this.entity.getId()); }/*from w w w.j ava2 s. c om*/ 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.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, context); String id = parentId;/*from w w w . j av 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" + "! entity is " + entity.toString()); } }
From source file:com.ebay.cloud.cms.dal.persistence.impl.embed.EmbedCreateCommand.java
License:Apache License
public DBObject buildCreateBody(MetaRelationship lastField, String parentPath, BsonEntity entity, int newVersion, MetaClass rootMetaClass, DBObject rootObject, String parentId) { BasicDBObject embedParentObject = (BasicDBObject) EmbedDBObjectFilter.filter(parentId, rootObject, rootMetaClass, null, helper); if (embedParentObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Create, can not find embed field with Id: " + parentId); }/*from w w w. ja v a 2s. c o m*/ embedParentObject.remove("_id"); if (lastField.getCardinality() == CardinalityEnum.Many) { BasicDBObject obj = (BasicDBObject) embedParentObject.get(lastField.getDbName()); BasicDBList valueList = null; if (obj != null) { valueList = (BasicDBList) obj.get(MetaField.VALUE_KEY); } if (valueList == null) { if (obj == null) { obj = new BasicDBObject(); embedParentObject.put(lastField.getDbName(), obj); } valueList = new BasicDBList(); valueList.add(entity.getNode()); obj.put(FieldProperty.LENGTH.getDbName(), 1); obj.put(FieldProperty.TIMESTAMP.getDbName(), new Date()); obj.put(MetaField.VALUE_KEY, valueList); } else { int size = valueList.size(); valueList.add(entity.getNode()); obj.put(FieldProperty.LENGTH.getDbName(), size + 1); obj.put(FieldProperty.TIMESTAMP.getDbName(), new Date()); obj.put(MetaField.VALUE_KEY, valueList); } } else { DBObject vObj = new BasicDBObject(); vObj.put(MetaField.VALUE_KEY, entity.getNode()); embedParentObject.put(lastField.getDbName(), vObj); } BasicDBObject setModifyObject = new BasicDBObject(); BasicDBObject obj = (BasicDBObject) rootObject.get(embedFieldName); if (obj == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Create, can not find embed field with Id: " + this.entity.getId()); } 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.embed.EmbedDBObjectFilter.java
License:Apache License
@SuppressWarnings("unchecked") private static DBObject traverse(DBObject currObject, String embedId, List<String> pathList, int index, Collection<String> fields) { if (index == pathList.size()) { if (compareEntityId(embedId, currObject)) { return filterFields(currObject, fields); }/*w ww . j a v a 2 s .c om*/ return null; } String dbFieldName = pathList.get(index); DBObject fieldObject = (DBObject) currObject.get(dbFieldName); if (fieldObject == null) { return null; } Object fieldValue = fieldObject.get(MetaField.VALUE_KEY); if (fieldValue instanceof List) { List<DBObject> listValue = (List<DBObject>) fieldValue; for (DBObject childObject : listValue) { DBObject res = traverse(childObject, embedId, pathList, index + 1, fields); if (res != null) { return res; } } } else if (fieldValue instanceof DBObject) { DBObject childObject = (DBObject) fieldValue; return traverse(childObject, embedId, pathList, index + 1, fields); } return null; }
From source file:com.ebay.cloud.cms.dal.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, context); 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.ja v a 2 s .c om // 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() + "! entity is " + entity.toString()); } }
From source file:com.ebay.cloud.cms.dal.persistence.impl.embed.EmbedFieldModifyCommand.java
License:Apache License
@Override @SuppressWarnings({ "rawtypes", "unchecked" }) protected DBObject buildModifyBody(BitSet arrayBits, DBObject rootObject, MetaClass rootMetaClass) { BasicDBObject embedObject = (BasicDBObject) EmbedDBObjectFilter.filter(entity.getId(), rootObject, rootMetaClass, null, helper); MetaField field = getField();/*ww w . j a va 2 s.co m*/ if (field.getCardinality() == CardinalityEnum.Many) { BasicDBObject enityObject = (BasicDBObject) getEntity().getNode(); BasicDBObject fieldObject = (BasicDBObject) enityObject.get(field.getDbName()); if (fieldObject != null) { List givenValue = (List) fieldObject.get(V); if (givenValue != null) { // merge with exsiting fields FIXME:: keep the same with RootFieldModifyCommand of using delta List targetFilterList = new ArrayList(); BasicDBObject existingFieldObject = (BasicDBObject) embedObject.get(field.getDbName()); if (existingFieldObject != null) { BasicDBList list = (BasicDBList) existingFieldObject.get(V); if (list != null) { targetFilterList.addAll(list); } } targetFilterList.addAll(givenValue); BasicDBList valueList = new BasicDBList(); valueList.addAll(targetFilterList); DBObject obj = (DBObject) embedObject.get(field.getDbName()); if (obj == null) { obj = new BasicDBObject(); embedObject.put(field.getDbName(), obj); } obj.put(V, valueList); obj.put(FieldProperty.LENGTH.getDbName(), targetFilterList.size()); obj.put(FieldProperty.TIMESTAMP.getDbName(), new Date()); } } } else if (field.getDataType().equals(DataTypeEnum.JSON)) { // incremental $set // buildJsonBody(parentPath, modifyBody); BasicDBObject enityObject = (BasicDBObject) getEntity().getNode(); BasicDBObject fieldObject = (BasicDBObject) enityObject.get(field.getDbName()); if (fieldObject != null) { DBObject obj = (DBObject) embedObject.get(field.getDbName()); if (obj == null) { obj = new BasicDBObject(); embedObject.put(field.getDbName(), obj); } DBObject valueObj = (DBObject) obj.get(V); if (valueObj == null) { valueObj = new BasicDBObject(); obj.put(V, valueObj); } BasicDBObject givenValue = (BasicDBObject) (fieldObject).get(V); if (givenValue != null) { for (String key : givenValue.keySet()) { valueObj.put(key, givenValue.get(key)); } valueObj.put(FieldProperty.TIMESTAMP.getDbName(), getEntity().getFieldTimestamp(field.getName())); } } } else { // non-array: replace the whole field BasicDBObject enityObject = (BasicDBObject) getEntity().getNode(); BasicDBObject fieldObject = (BasicDBObject) enityObject.get(field.getDbName()); embedObject.put(field.getDbName(), fieldObject); // buildSetFieldBody(parentPath, modifyBody); } embedObject.put(InternalFieldEnum.MODIFIER.getDbName(), entity.getModifier()); embedObject.put(InternalFieldEnum.LASTMODIFIED.getDbName(), entity.getLastModified()); return buildSetBody(rootObject); }
From source file:com.ebay.cloud.cms.dal.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, context); DBObject getField = buildGetFields(arrayBits, entityId, true, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, getQuery, getField); if (rootObject == null) { return;//from w w w . ja v a 2s . c om } // 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); } }