Example usage for com.mongodb WriteResult getN

List of usage examples for com.mongodb WriteResult getN

Introduction

In this page you can find the example usage for com.mongodb WriteResult getN.

Prototype

public int getN() 

Source Link

Document

Gets the "n" field, which contains the number of documents affected in the write operation.

Usage

From source file:com.ebay.cloud.cms.dal.persistence.flatten.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,
                "Embed Delete, parenet document doesn't exist!  " + parentId);
    }//from  w  w  w. j a  va2  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() + "! entity is " + entity.toString());
    }
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.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  a v  a  2 s. co  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 + "! entity is " + entity.toString());
    }

}

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

License:Apache License

@Override
public void execute(PersistenceContext context) {
    DBObject queryObject = buildModifyQuery();

    DBObject modifyBody = buildModifyBody();

    WriteResult result = MongoExecutor.update(context, getEntity().getMetaClass(), queryObject, modifyBody);
    if (result.getN() == 0) {
        throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT,
                MessageFormat.format("Field not modified!! Query:{0}, Body:{1}", queryObject, modifyBody));
    }//from w ww.ja  va 2  s . c o m
}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.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: " + entity.toString());
        }// w  w w  .ja v a  2 s. co m
        Object currentVersion = getResult.get(InternalFieldEnum.VERSION.getDbName());
        throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "current version is " + currentVersion
                + ", but version in repalce body is " + version + "! entity is " + entity.toString());
    }
}

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());
        }/*from w  ww.j  a  v  a2 s.  c  om*/

        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.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, context);

    // 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  va 2  s  .  c  o m
    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() + "! entity is " + entity.toString());
    }
}

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  a  v a  2s .  co  m
    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.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 w  w  w .  j  a  v a  2 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() + "! entity is " + entity.toString());
    }
}

From source file:com.ebay.cloud.cms.dal.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  ww .  j av  a  2 s.  co  m
    if (parentBits.cardinality() == 0) {
        id = entity.getId();
    }

    // query root object with all intermediate _id
    DBObject rootQuery = buildGetQuery(parentBits, parentId, rootMetaClass, context);
    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 + "! entity is " + entity.toString());
    }

}

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;
    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());
        }//from w  ww. j  a  va2s .  c  om
        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());
    }
}