Example usage for com.mongodb WriteConcern SAFE

List of usage examples for com.mongodb WriteConcern SAFE

Introduction

In this page you can find the example usage for com.mongodb WriteConcern SAFE.

Prototype

WriteConcern SAFE

To view the source code for com.mongodb WriteConcern SAFE.

Click Source Link

Document

Write operations that use this write concern will wait for acknowledgement from the primary server before returning.

Usage

From source file:v7db.files.mongodb.SimpleGarbageCollector.java

License:Open Source License

static void purge(DBCollection contents, DBCollection references) throws MongoException, DecoderException {

    List<Object> refsToPurge = new ArrayList<Object>();
    Set<String> potentialGarbage = new HashSet<String>();
    Set<String> notGarbage = new HashSet<String>();

    for (DBObject x : references.find()) {
        if (x.containsField("purge")) {
            refsToPurge.add(x.get("_id"));
            for (Object r : BSONUtils.values(x, "refs")) {
                String h = Hex.encodeHexString((byte[]) r);
                potentialGarbage.add(h);
            }// www. j a v a2 s . c om
            for (Object r : BSONUtils.values(x, "refHistory")) {
                String h = Hex.encodeHexString((byte[]) r);
                potentialGarbage.add(h);
            }
        } else {
            for (Object r : BSONUtils.values(x, "refs")) {
                String h = Hex.encodeHexString((byte[]) r);
                notGarbage.add(h);
            }
            for (Object r : BSONUtils.values(x, "refHistory")) {
                String h = Hex.encodeHexString((byte[]) r);
                notGarbage.add(h);
            }
        }
    }

    potentialGarbage.removeAll(notGarbage);
    // TODO: bases must not be removed
    for (String g : potentialGarbage) {
        contents.remove(new BasicDBObject("_id", Hex.decodeHex(g.toCharArray())), WriteConcern.SAFE);
    }
    for (Object x : refsToPurge) {
        references.remove(new BasicDBObject("_id", x), WriteConcern.SAFE);
    }
}

From source file:v7db.files.mongodb.V7GridFS.java

License:Open Source License

private void insertMetaData(DBObject metaData) throws IOException {
    metaData.put("_version", 1);
    metaData.put("created_at", new Date());
    WriteResult result = files.insert(WriteConcern.SAFE, metaData);
    String error = result.getError();
    if (error != null)
        throw new IOException(error);
}

From source file:v7db.files.mongodb.Vermongo.java

License:Open Source License

/**
 * inserts a new object into the collection. The _version property must not
 * be present in the object, and will be set to 1 (integer).
 * // w w  w  .  java 2 s . c  om
 * @param object
 */
static void insert(DBCollection collection, DBObject object) {
    if (object.containsField(_VERSION))
        throw new IllegalArgumentException();

    object.put(_VERSION, 1);
    collection.insert(object, WriteConcern.SAFE);
}

From source file:v7db.files.mongodb.Vermongo.java

License:Open Source License

/**
 * updates an existing object. The object must have been the _version
 * property set to the version number of the base revision (i.e. the version
 * number to be replaced). If the current version in the DB does not have a
 * matching version number, the operation aborts with an
 * UpdateConflictException./*from w ww  .  jav a 2 s . c o  m*/
 * 
 * After the update is successful, _version in the object is updated to the
 * new version number.
 * 
 * The version that was replaced is moved into the collection's shadow
 * collection.
 * 
 * @param collection
 * @param object
 * @throws UpdateConflictException
 */
static void update(DBCollection collection, DBObject object) throws UpdateConflictException {
    if (!object.containsField(_VERSION))
        throw new IllegalArgumentException("the base version number needs to be included as _version");

    int baseVersion = (Integer) object.get(_VERSION);

    // load the base version
    {
        DBObject base = collection.findOne(new BasicDBObject("_id", getId(object)));
        if (base == null) {
            throw new IllegalArgumentException("document to update not found in collection");
        }
        Object bV = base.get(_VERSION);
        if (bV instanceof Integer) {
            if (baseVersion != (Integer) bV) {
                throw new UpdateConflictException(object, base);
            }
        } else {
            throw new UpdateConflictException(object, base);
        }
        // copy to shadow
        DBCollection shadow = getShadowCollection(collection);
        base.put("_id", new BasicDBObject("_id", getId(base)).append(_VERSION, baseVersion));
        WriteResult r = shadow.insert(base, WriteConcern.SAFE);

        // TODO: if already there, no error
        r.getLastError().throwOnError();
    }

    try {
        object.put(_VERSION, baseVersion + 1);
        DBObject found = collection
                .findAndModify(new BasicDBObject("_id", getId(object)).append(_VERSION, baseVersion), object);

        if (found == null) {
            // document has changed in the mean-time. get the latest version
            // again
            DBObject base = collection.findOne(new BasicDBObject("_id", getId(object)));
            if (base == null) {
                throw new IllegalArgumentException("document to update not found in collection");
            }
            throw new UpdateConflictException(object, base);
        }

    } catch (RuntimeException e) {
        object.put(_VERSION, baseVersion);
        throw e;
    }

}

From source file:v7db.files.mongodb.Vermongo.java

License:Open Source License

/**
 * deletes the object without checking for conflicts. An existing version is
 * moved to the shadow collection, along with a dummy version to mark the
 * deletion. This dummy version can contain optional meta-data (such as who
 * deleted the object, and when).//from w  w w .  j a  v  a2s.c  om
 */
static DBObject remove(DBCollection collection, Object id, BSONObject metaData) {
    DBObject base = collection.findOne(new BasicDBObject("_id", id));
    if (base == null)
        return null;

    // copy to shadow
    DBCollection shadow = getShadowCollection(collection);
    int version = getVersion(base);
    BasicDBObject revId = new BasicDBObject("_id", getId(base)).append(_VERSION, version);
    base.put("_id", revId);
    WriteResult r = shadow.insert(base, WriteConcern.SAFE);

    // TODO: if already there, no error
    r.getLastError().throwOnError();

    // add the dummy version
    BasicDBObject dummy = new BasicDBObject("_id", revId.append(_VERSION, version + 1)).append(_VERSION,
            "deleted:" + (version + 1));
    if (metaData != null)
        dummy.putAll(metaData);
    r = shadow.insert(dummy, WriteConcern.SAFE);
    // TODO: if already there, no error
    r.getLastError().throwOnError();

    collection.remove(new BasicDBObject("_id", id));
    return base;

}