List of usage examples for com.mongodb WriteResult getN
public int getN()
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 ww w . j a v a 2 s .co 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.dalapi.persistence.impl.embed.AbstractFieldEmbedCommand.java
License:Apache License
@Override public void execute(PersistenceContext context) { String entityId = entity.getId(); rootMetaClass = getRootEntityMetaClass(context); BitSet arrayBits = helper.checkArrayOnPath(entityId, rootMetaClass); String parentId = helper.getParentId(entityId); rootQuery = buildGetQuery(arrayBits, parentId, rootMetaClass); // get the root entity for further embed path computing DBObject rootGetFields = buildGetRootFields(entityId, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootGetFields); if (rootObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, getOperation() + " on embed field, parenet document with id " + parentId + "doesn't exist!"); }/*from w w w . j a v a2 s .c om*/ embedPath = getEmbedPath(arrayBits, entityId, rootObject, rootMetaClass); // new query with version buildModifyQuery(rootQuery, entity); DBObject modifyQuery = buildModifyQuery(rootQuery, entity); DBObject modifyBody = buildModifyBody(arrayBits, rootObject, rootMetaClass); WriteResult result = MongoExecutor.update(context, rootMetaClass, modifyQuery, modifyBody); if (result.getN() == 0) { // something happens between get and replace throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails for " + entity.getId() + " in class " + rootMetaClass.getName() + " and embed class " + getEntity().getType()); } }
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.j a 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); }/* w w w .j a v a 2 s . co 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()); } }
From source file:com.ebay.cloud.cms.dalapi.persistence.impl.embed.EmbedModifyCommand.java
License:Apache License
@Override public void execute(PersistenceContext context) { String entityId = entity.getId(); MetaClass rootMetaClass = getRootEntityMetaClass(entityId, context); BitSet arrayBits = helper.checkArrayOnPath(entityId, rootMetaClass); String parentId = helper.getParentId(entity.getId()); BitSet parentBits = helper.checkArrayOnPath(parentId, rootMetaClass); String id = parentId;/*from w w w . j av a2 s . c o m*/ if (parentBits.cardinality() == 0) { id = entity.getId(); } // query root object with all intermediate _id DBObject rootQuery = buildGetQuery(parentBits, parentId, rootMetaClass); DBObject rootFields = buildGetRootFields(id, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootFields); if (rootObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Modify, can not find embed document with Id: " + entityId); } // update the embed object DBObject modifyQuery = buildModifyQuery(rootQuery, entity); DBObject modifyBody = buildModifyBody(arrayBits, rootObject, rootMetaClass); WriteResult result = MongoExecutor.update(context, rootMetaClass, modifyQuery, modifyBody); if (result.getN() == 0) { // TODO: need to send get query so we can tell WHY getN == 0 throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails for " + entityId); } }
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; 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()); }// w ww. j a v a 2s .com 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 w w .jav a 2s . co 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.mongo.MongoRepositoryServiceImpl.java
License:Apache License
@Override public void updateRepository(Repository repo) { CheckConditions.checkNotNull(repo, "repository can not be null"); CheckConditions.checkNotNull(repo.getRepositoryName(), "repository name can not be null"); String repositoryName = repo.getRepositoryName(); try {// ww w . ja va 2s. com metadataLock.lock(); DBObject qryDbo = new BasicDBObject(); qryDbo.put(Repository.REPOSITORY_FIELD_NAME, repositoryName); // only support admin change now DBObject updateDbo = new BasicDBObject(); DBObject setDbo = new BasicDBObject(); if (repo.getRepositoryAdmin() != null) { setDbo.put(Repository.REPOSITORY_FIELD_ADMIN_NAME, repo.getRepositoryAdmin()); setDbo.put(Repository.REPOSITORY_FIELD_TYPE_NAME, repo.getAccessType().name()); } if (repo.getOptions() != null) { setDbo.put(Repository.REPOSITORY_FIELD_OPTIONS_NAME, repositoryOptionConverter.toBson(repo.getOptions())); } if (!setDbo.keySet().isEmpty()) { updateDbo.put("$set", setDbo); WriteResult result = repoCollection.update(qryDbo, updateDbo); if (result.getN() != 1) { throw new RepositoryNotExistsException(repositoryName); } } refreshRepositoryCache(); } catch (InterruptedException e) { logger.info("lock interrupted for createRepository " + repositoryName); throw new MetaDataException(MetaErrCodeEnum.LOCK_INTERRUPTED, "lock interrupted for createRepository " + repositoryName, e); } finally { metadataLock.unlock(); } }
From source file:com.epam.ta.reportportal.core.admin.ServerAdminHandlerImpl.java
License:Open Source License
public OperationCompletionRS deleteEmailSettings(String profileId) { WriteResult result = mongoOperations.updateFirst(query(Criteria.where("_id").is(profileId)), Update.update("serverEmailDetails", null), ServerSettings.class); BusinessRule.expect(result.getN(), not(equalTo(0))).verify(ErrorType.SERVER_SETTINGS_NOT_FOUND, profileId); return new OperationCompletionRS( "Server Settings with profile '" + profileId + "' is successfully updated."); }
From source file:com.epam.ta.reportportal.database.dao.ReportPortalRepositoryImpl.java
License:Open Source License
@Override public void partialUpdate(T t) { ID id = getEntityInformation().getId(t); if (null == id) { throw new IllegalArgumentException("ID property should not be null"); }//from w ww .jav a 2 s.c o m Update update = new Update(); final MongoPersistentEntity<?> persistentEntity = mongoOperations.getConverter().getMappingContext() .getPersistentEntity(getEntityInformation().getJavaType()); persistentEntity.doWithProperties((PropertyHandler<MongoPersistentProperty>) persistentProperty -> { if (!persistentEntity.isIdProperty(persistentProperty)) { Object value = Accessible.on(t).field(persistentProperty.getField()).getValue(); if (null != value) { update.set(persistentProperty.getFieldName(), value); } } }); WriteResult writeResult = mongoOperations.updateFirst( query(where(persistentEntity.getIdProperty().getFieldName()).is(id)), update, getEntityInformation().getCollectionName()); if (1 != writeResult.getN()) { throw new IncorrectResultSizeDataAccessException(1, writeResult.getN()); } }