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.almende.eve.state.mongo.MongoState.java

License:Apache License

/**
 * updating the entire properties object at the same time, with force flag
 * to allow overwriting of updates/*  w  ww.j  av a 2 s.c  om*/
 * from other instances of the state
 * 
 * @param force
 * @throws UpdateConflictException
 *             | will not throw anything when $force flag is true
 */
private synchronized boolean updateProperties(final boolean force) throws UpdateConflictException {
    final Long now = System.nanoTime();
    /* write to database */
    final WriteResult result = (force)
            ? collection.update("{_id: #}", getId()).with("{$set: {properties: #, timestamp: #}}", properties,
                    now)
            : collection.update("{_id: #, timestamp: #}", getId(), timestamp)
                    .with("{$set: {properties: #, timestamp: #}}", properties, now);
    /* check results */
    final Boolean updatedExisting = (Boolean) result.getField("updatedExisting");
    if (result.getN() == 0 && result.getError() == null) {
        throw new UpdateConflictException(timestamp);
    } else if (result.getN() != 1) {
        throw new MongoException(result.getError() + " <--- " + properties);
    }
    timestamp = now;
    return updatedExisting;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public boolean deleteScope(String scopeName) {
    DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(ID_NAME, scopeName);
    WriteResult result = coll.remove(query);
    return (result.getN() == 1) ? true : false;
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public boolean deleteClientApp(String clientId) {
    DBCollection coll = db.getCollection(CLIENTS_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(CLIENTS_ID, clientId);
    WriteResult result = coll.remove(query);
    return (result.getN() == 1);
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public boolean deleteScope(String scopeName) {
    DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(CLIENTS_ID, scopeName);
    WriteResult result = coll.remove(query);
    return (result.getN() == 1);
}

From source file:com.bryanreinero.purchase.PurchaseDAO.java

License:Open Source License

@Override
public void setTransaction(Integer id, State state) throws TransactionException {
    DBObject query = new BasicDBObject("_id", id);
    DBObject update = new BasicDBObject("$set", new BasicDBObject("state", state.toString()));

    try {/*from w  w w  .jav a 2s .  c  om*/
        WriteResult result = transactions.update(query, update);

        if (!result.getLastError().ok())
            throw new TransactionException("Transaction update failure: " + result.getError());

        if (result.getN() == 0)
            throw new TransactionException(
                    "Transaction update failure: updated " + result.getN() + " transaction");

    } catch (MongoException e) {
        throw new TransactionException("Pricipal update failure", e);
    }

}

From source file:com.cedac.security.oauth2.provider.approval.MongoApprovalStore.java

License:Apache License

@Override
public boolean addApprovals(final Collection<Approval> approvals) {
    LOG.debug(APPROVAL, "Adding approvals: {}", approvals);

    boolean success = true;
    for (Approval approval : approvals) {
        DBObject query = new BasicDBObject(userIdFieldName, approval.getUserId())
                .append(clientIdFieldName, approval.getClientId()).append(scopeFieldName, approval.getScope());
        DBObject obj = getApprovalsCollection().findOne(query);
        if (obj == null) {
            obj = new BasicDBObject(userIdFieldName, approval.getUserId())
                    .append(clientIdFieldName, approval.getClientId())
                    .append(scopeFieldName, approval.getScope());
        }/*from  w w w. ja  v a 2  s . c  om*/
        obj.put(statusFieldName, approval.getStatus().name());
        obj.put(expiresAtFieldName, approval.getExpiresAt());
        obj.put(lastModifiedAtFieldName, approval.getLastUpdatedAt());

        LOG.trace(APPROVAL, "Saving approval {}", obj);

        WriteResult result = getApprovalsCollection().save(obj, writeConcern);

        LOG.trace(APPROVAL, "Approval save result is {}", result);

        success = success && result.getN() == 1;
    }
    return success;
}

From source file:com.cedac.security.oauth2.provider.approval.MongoApprovalStore.java

License:Apache License

@Override
public boolean revokeApprovals(Collection<Approval> approvals) {
    LOG.debug("Revoking approvals: {}", approvals);

    boolean success = true;
    for (Approval approval : approvals) {
        DBObject query = new BasicDBObject(userIdFieldName, approval.getUserId())
                .append(clientIdFieldName, approval.getClientId()).append(scopeFieldName, approval.getScope());
        DBObject result = getApprovalsCollection().findOne(query);
        if (result != null) {
            WriteResult writeResult;
            if (handleRevocationsAsExpiry) {
                LOG.trace(APPROVAL, "Handling revocation as expiry: updating approval {} field",
                        expiresAtFieldName);

                result.put(expiresAtFieldName, new Date());
                writeResult = getApprovalsCollection().save(result, writeConcern);
            } else {
                LOG.trace(APPROVAL, "Handling revocation as delete: removing approval {}", result);

                writeResult = getApprovalsCollection().remove(result, writeConcern);
            }/*from  w  w  w  .ja v a 2 s  .  c  o m*/
            success = success && writeResult.getN() == 1;
        } else {
            LOG.debug(APPROVAL, "No approval found for sample {}", query);
            success = false;
        }
    }
    return success;
}

From source file:com.cedac.security.oauth2.provider.approval.MongoApprovalStore.java

License:Apache License

public boolean purgeExpiredApprovals() {
    LOG.debug("Purging expired approvals from database");

    try {/*from   w w w .ja  v a2 s .  c o m*/
        DBObject query = new BasicDBObject(expiresAtFieldName, new BasicDBObject("$lte", new Date()));
        WriteResult result = getApprovalsCollection().remove(query, writeConcern);

        LOG.debug(APPROVAL, "{} expired approvals deleted", result.getN());
    } catch (DataAccessException ex) {
        LOG.error(APPROVAL, "Error purging expired approvals", ex);

        return false;
    }
    return true;
}

From source file:com.cedac.security.oauth2.provider.client.MongoClientDetailsService.java

License:Apache License

public void removeClientDetails(String clientId) throws NoSuchClientException {
    DBObject query = new BasicDBObject(clientIdFieldName, clientId);
    WriteResult result = getClientDetailsCollection().remove(query, writeConcern);
    if (result.getN() != 1) {
        throw new NoSuchClientException("No client found with id = " + clientId);
    }/*from www .  j  av  a  2  s .  c o m*/
}

From source file:com.dawsonsystems.session.MongoSessionManager.java

License:Apache License

public void processExpires() {
    BasicDBObject query = new BasicDBObject();

    long olderThan = System.currentTimeMillis() - (getMaxInactiveInterval() * 1000);

    log.fine("Looking for sessions less than for expiry in Mongo : " + olderThan);

    query.put("lastmodified", new BasicDBObject("$lt", olderThan));

    try {// w  w w  .java2  s .  co m
        WriteResult result = getCollection().remove(query);
        log.fine("Expired sessions : " + result.getN());
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error cleaning session in Mongo Session Store", e);
    }
}