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:act.server.MongoDB.java

License:Open Source License

private void mergeIntoDB(long id, Chemical c) {
    Chemical oldc = getChemicalFromChemicalUUID(id);
    Chemical mergedc = c.createNewByMerge(oldc);

    if (mergedc == null) {
        // whoa! inconsistent values on unmergables, so recover

        System.err.println("\n\n\n\n\n\n\n\n\n\n");
        System.err.println("---- Conflicting uuid or name or smiles or inchi or inchikey or pubchem_id:");
        System.err.println("---- NEW\t " + c);
        System.err.println("---- OLD\t " + oldc);
        System.err.println("---- Keeping OLD entry");
        System.err.println("\n\n\n\n\n\n\n\n\n\n");

        return;//from w  ww .ja v a2 s  .c o  m
    }

    BasicDBObject withID = new BasicDBObject();
    withID.put("_id", id);
    this.dbChemicals.remove(withID, WriteConcern.SAFE); // remove the old entry oldc from the collection
    submitToActChemicalDB(mergedc, id); // now that the old entry is removed, we can simply add
}

From source file:ch.agent.crnickl.mongodb.MongoDB.java

License:Apache License

private WriteConcern getWriteConcernFromKeyword(String keyword) throws T2DBException {
    WriteConcern wc = null;//from   ww  w .  j a va 2  s . c  o  m
    if (keyword == null)
        keyword = "SAFE";
    WriteConcernKeyword k = null;
    try {
        k = WriteConcernKeyword.valueOf(keyword);
    } catch (IllegalArgumentException e) {
        throw T2DBMMsg.exception(e, J.J81020, keyword);
    }
    switch (k) {
    case NONE:
        wc = WriteConcern.NONE;
        break;
    case NORMAL:
        wc = WriteConcern.NORMAL;
        break;
    case SAFE:
        wc = WriteConcern.SAFE;
        break;
    case MAJORITY:
        wc = WriteConcern.MAJORITY;
        break;
    case FSYNC_SAFE:
        wc = WriteConcern.FSYNC_SAFE;
        break;
    case JOURNAL_SAFE:
        wc = WriteConcern.JOURNAL_SAFE;
        break;
    case REPLICAS_SAFE:
        wc = WriteConcern.REPLICAS_SAFE;
        break;
    default:
        throw new RuntimeException("bug: " + k.name());
    }
    if (wc != WriteConcern.SAFE)
        throw T2DBMMsg.exception(J.J81021, keyword);
    return wc;
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForChroniclesAndSeries.java

License:Apache License

/**
 * Delete a chronicle from the database. Also delete its attribute values
 * and possibly other dependent objects. The chronicle update policy is
 * supposed to forbid deleting when there are dependent chronicles or
 * series, but to allow cascading delete of attribute values. Throw an
 * exception if the operation cannot be done.
 * /*from  ww  w . j  a  va2s .  c o m*/
 * @param chronicle a chronicle
 * @param policy a chronicle updating policy
 * @throws T2DBException
 */
public void deleteChronicle(UpdatableChronicle chronicle, ChronicleUpdatePolicy policy) throws T2DBException {
    boolean done = false;
    Throwable cause = null;
    Surrogate s = chronicle.getSurrogate();
    MongoDatabase db = (MongoDatabase) s.getDatabase();
    try {
        check(Permission.MODIFY, chronicle);
        policy.willDelete(chronicle);
        Chronicle original = getChronicle(s);
        getMongoDB(s).getChronicles().remove(asQuery(s.getId()), WriteConcern.SAFE);
        db.sleep();
        try {
            policy.willDelete(chronicle);
        } catch (T2DBException e) {
            // Oops! referential integrity broken!
            createChronicle(original, true);
            throw e;
        }
        deleteAttributes(chronicle);
        done = true;
    } catch (Exception e) {
        cause = e;
    } finally {
    }
    if (!done || cause != null) {
        throw T2DBMsg.exception(cause, E.E40110, chronicle.getName(true));
    }
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForChroniclesAndSeries.java

License:Apache License

/**
 * Delete an attribute value from a chronicle.
 * Throw an exception if the operation cannot be done.
 * //from w  ww  .  j a v a2 s .  c  om
 * @param chronicle a chronicle
 * @param def an attribute definition
 * @throws T2DBException
 */
public void deleteAttribute(Chronicle chronicle, AttributeDefinition<?> def) throws T2DBException {
    try {
        check(Permission.MODIFY, chronicle);
        getMongoDB(chronicle.getSurrogate()).getAttributes().remove(mongoObject(MongoDatabase.FLD_ATTR_CHRON,
                getId(chronicle), MongoDatabase.FLD_ATTR_PROP, getId(def.getProperty())), WriteConcern.SAFE);
    } catch (Exception e) {
        throw T2DBMsg.exception(E.E40114, chronicle.getName(true), def.getNumber());
    }
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForChroniclesAndSeries.java

License:Apache License

/**
 * Delete a series. The policy typically forbids to delete a series which is
 * not empty. Throw an exception if the operation cannot be done.
 * /* w  w w.  j a  v  a  2  s  . c o  m*/
 * @param series
 *            a series
 * @param policy
 *            a chronicle update policy
 * @throws T2DBException
 */
public void deleteSeries(UpdatableSeries<?> series, ChronicleUpdatePolicy policy) throws T2DBException {
    boolean done = false;
    Throwable cause = null;
    Surrogate s = series.getSurrogate();
    MongoDatabase db = (MongoDatabase) s.getDatabase();
    try {
        check(Permission.MODIFY, series);
        policy.willDelete(series);
        done = policy.deleteSeries(series);
        Series<?> original = db.getReadMethodsForChronicleAndSeries().getSeries(s);
        getMongoDB(db).getSeries().remove(asQuery(s.getId()), WriteConcern.SAFE);
        db.sleep();
        try {
            policy.willDelete(series);
        } catch (T2DBException e) {
            // Oops! referential integrity broken!
            createSeries(original, true);
            throw e;
        }
        done = true;
    } catch (Exception e) {
        cause = e;
    }
    if (!done || cause != null)
        throw T2DBMsg.exception(cause, E.E50112, series.getName(true));
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForProperty.java

License:Apache License

/**
 * Delete the property./* w w w.  j  ava  2 s  . c om*/
 * If deleting fails throw an exception.
 * 
 * @param prop a property
 * @param policy a schema updating policy
 * @throws T2DBException
 */
public <T> void deleteProperty(Property<T> prop, SchemaUpdatePolicy policy) throws T2DBException {
    boolean done = false;
    Throwable cause = null;
    Surrogate s = prop.getSurrogate();
    MongoDatabase database = (MongoDatabase) s.getDatabase();
    try {
        check(Permission.MODIFY, prop);
        // dangerous update! see comment in MongoDatabase.sleep
        policy.willDelete(prop);
        Property<T> original = database.getReadMethodsForProperty().getProperty(s);
        DBCollection coll = getMongoDB(s).getProperties();
        coll.remove(asQuery(s.getId()), WriteConcern.SAFE);
        database.sleep();
        try {
            policy.willDelete(original);
        } catch (T2DBException e) {
            createProperty(original);
            throw e;
        }
        done = true;
    } catch (Exception e) {
        cause = e;
    } finally {
    }
    if (!done || cause != null)
        throw T2DBMsg.exception(cause, E.E20115, prop.getName());
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java

License:Apache License

/**
 * Delete a schema from the database.//from  www  .j  a v  a 2 s  .  c  om
 * Throw an exception if the operation cannot be done.
 * 
 * @param schema a schema
 * @param policy a schema udpdating policy
 * @throws T2DBException
 */
public void deleteSchema(UpdatableSchema schema, SchemaUpdatePolicy policy) throws T2DBException {
    boolean done = false;
    Throwable cause = null;
    Surrogate s = schema.getSurrogate();
    MongoDatabase database = (MongoDatabase) s.getDatabase();
    try {
        check(Permission.MODIFY, schema);
        policy.willDelete(schema);
        UpdatableSchema original = database.getReadMethodsForSchema().getSchema(s);
        DBCollection coll = getMongoDB(s).getSchemas();
        coll.remove(asQuery(s.getId()), WriteConcern.SAFE);
        database.sleep();
        try {
            policy.willDelete(schema);
        } catch (T2DBException e) {
            // Oops! referential integrity broken!
            createSchema(original);
            throw e;
        }
        done = true;
    } catch (Exception e) {
        cause = e;
    } finally {
    }
    if (!done || cause != null) {
        throw T2DBMsg.exception(cause, E.E30123, schema.getName());
    }
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java

License:Apache License

/**
 * Update the basic schema setup in the database.
 * Throw an exception if the operation cannot be done.
 * //w  w w . j a va 2 s  .  co  m
 * @param schema a schema
 * @param policy 
 * @return true if the schema was updated
 * @throws T2DBException
 */
public boolean updateSchema(UpdatableSchema schema, SchemaUpdatePolicy policy) throws T2DBException {
    boolean done = false;
    Throwable cause = null;
    Surrogate s = schema.getSurrogate();
    MongoDatabase database = (MongoDatabase) s.getDatabase();
    try {
        check(Permission.MODIFY, schema);
        UpdatableSchema original = database.getReadMethodsForSchema().getSchema(s);
        Schema base = schema.getBase();
        if (base != null && !base.equals(original.getBase()))
            check(Permission.READ, base);
        DBCollection coll = getMongoDB(s).getSchemas();
        // full replace (no need to set _id in full update) 
        com.mongodb.DBObject operation = mongoObject(MongoDatabase.FLD_SCHEMA_NAME, schema.getName(),
                MongoDatabase.FLD_SCHEMA_BASE, getIdOrZero(schema.getBase()), MongoDatabase.FLD_SCHEMA_ATTRIBS,
                attributeDefinitions(schema.getAttributeDefinitions()), MongoDatabase.FLD_SCHEMA_SERIES,
                seriesDefinitions(schema.getSeriesDefinitions()));
        coll.update(asQuery(s.getId()), operation, false, false, WriteConcern.SAFE);
        database.sleep();
        try {
            policy.willUpdate(schema);
        } catch (T2DBException e) {
            // Oops! referential integrity broken!
            operation = mongoObject(MongoDatabase.FLD_SCHEMA_NAME, original.getName(),
                    MongoDatabase.FLD_SCHEMA_BASE, getId(original.getBase()), MongoDatabase.FLD_SCHEMA_ATTRIBS,
                    original.getAttributeDefinitions(), MongoDatabase.FLD_SCHEMA_SERIES,
                    original.getSeriesDefinitions());
            coll.update(asQuery(s.getId()), operation);
            throw e;
        }
        done = true;
    } catch (Exception e) {
        cause = e;
    } finally {
    }
    if (cause != null)
        throw T2DBMsg.exception(cause, E.E30122, schema.getName());

    return done;
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForValueType.java

License:Apache License

/**
 * Delete a value type from the database.
 * Throw an exception if the operation fails.
 * /*from   ww  w .  j  a  v  a2 s  .  com*/
 * @param vt a value type
 * @param policy a schema udpdating policy
 * @throws T2DBException
 */
public <T> void deleteValueType(ValueType<T> vt, SchemaUpdatePolicy policy) throws T2DBException {
    boolean done = false;
    Throwable cause = null;
    Surrogate s = vt.getSurrogate();
    MongoDatabase database = (MongoDatabase) s.getDatabase();
    try {
        check(Permission.MODIFY, vt);
        policy.willDelete(vt);
        ValueType<T> original = database.getReadMethodsForValueType().getValueType(s);
        DBCollection coll = getMongoDB(s).getValueTypes();
        coll.remove(asQuery(s.getId()), WriteConcern.SAFE); // ??? REPLICA_SAFE --> MongoException("norepl");
        database.sleep();
        try {
            policy.willDelete(original);
        } catch (T2DBException e) {
            // Oops! referential integrity broken!
            createValueType(original);
            throw e;
        }
        done = true;
    } catch (Exception e) {
        cause = e;
    } finally {
    }
    if (!done || cause != null) {
        throw T2DBMsg.exception(cause, E.E10145, vt.getName());
    }
}

From source file:ch.agent.crnickl.mongodb.WriteMethodsForValueType.java

License:Apache License

/**
 * Update a value type in the database./*w  w w  .j  a v a 2 s.  c o m*/
 * Throw an exception if the operation cannot be done.
 * Updating a value type is an expensive operation, because in case 
 * 
 * @param vt a value type
 * @param policy a schema udpdating policy
 * @throws T2DBException
 */
public <T> void updateValueType(ValueType<T> vt, SchemaUpdatePolicy policy) throws T2DBException {
    boolean done = false;
    Throwable cause = null;
    Surrogate s = vt.getSurrogate();
    MongoDatabase database = (MongoDatabase) s.getDatabase();
    try {
        check(Permission.MODIFY, vt);
        DBCollection coll = getMongoDB(s).getValueTypes();

        if (vt.isRestricted() && vt.getValues().size() > 0) {
            ValueType<T> original = database.getReadMethodsForValueType().getValueType(s);
            Set<T> deleted = deletedValues(original, vt);
            com.mongodb.DBObject operation = operation(Operator.SET, MongoDatabase.FLD_VT_NAME, vt.getName(),
                    MongoDatabase.FLD_VT_VALUES, valueDescriptionsAsMap(vt));

            if (deleted.size() > 0) {
                // dangerous update! see comment in MongoDatabase.sleep
                Iterator<T> it = deleted.iterator();
                while (it.hasNext()) {
                    policy.willDelete(vt, it.next());
                }
                coll.update(asQuery(s.getId()), operation, false, false, WriteConcern.SAFE);
                database.sleep();
                try {
                    it = deleted.iterator();
                    while (it.hasNext()) {
                        policy.willDelete(vt, it.next());
                    }
                } catch (T2DBException e) {
                    // restore state and throw exception
                    operation = operation(Operator.SET, MongoDatabase.FLD_VT_NAME, original.getName(),
                            MongoDatabase.FLD_VT_VALUES, valueDescriptionsAsMap(original));
                    coll.update(asQuery(s.getId()), operation);
                    throw e;
                }
            } else {
                coll.update(asQuery(s.getId()), operation);
            }
        } else
            coll.update(asQuery(s.getId()), operation(Operator.SET, MongoDatabase.FLD_VT_NAME, vt.getName()));
        done = true;
    } catch (Exception e) {
        cause = e;
    } finally {
    }
    if (!done || cause != null)
        throw T2DBMsg.exception(cause, E.E10146, vt.getName());
}