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:com.ifeng.vdn.report.rest.data.mongodb.ApplicationConfiguration.java

License:Apache License

@Override
public Mongo mongo() throws Exception {
    MongoClient mongo = new MongoClient();
    mongo.setWriteConcern(WriteConcern.SAFE);
    return mongo;
}

From source file:com.ikanow.infinit.e.data_model.utils.MongoTransactionLock.java

License:Apache License

protected synchronized boolean getToken() {
    DBCollection cachedCollection = _collections.get();

    boolean bDefinitelyHaveControl = false;
    String hostname = null;//from  ww w .j  av a  2  s . com
    String oneUp = null;

    // 1] Get Lock Object (create one an assert control if it doesn't exist)

    BasicDBObject lockObj = (BasicDBObject) cachedCollection.findOne();
    if (null == lockObj) { // Currently the DB is unlocked
        hostname = getHostname();
        oneUp = Long.toString(1000000L * (new Date().getTime() % 10000));
        // (ie a randomish start number)

        lockObj = new BasicDBObject(id_, _lockId);
        lockObj.put(hostname_, hostname);
        lockObj.put(oneUp_, oneUp);
        lockObj.put(lastUpdated_, new Date());

        //logger.debug("Creating a new aggregation lock object: " + lockObj.toString());

        try {
            cachedCollection.insert(lockObj, WriteConcern.SAFE);
            // (will fail if another harvester gets there first)
            bDefinitelyHaveControl = true;
        } catch (Exception e) { // Someone else has created it in the meantime
            lockObj = (BasicDBObject) cachedCollection.findOne();
        }

    } //TESTED

    // (So by here lockObj is always non-null)

    // 2] Do I have control?

    if (bDefinitelyHaveControl) {
        _bHaveControl = true;
        _nLastCheck = 0;
    } else {
        hostname = lockObj.getString(hostname_);
        oneUp = lockObj.getString(oneUp_);
        _bHaveControl = getHostname().equals(hostname);
    }
    // 3] If not, has the lock object been static for >= 1 minute

    if (!_bHaveControl) { // Don't currently have control
        long nNow = new Date().getTime();
        if (0 == _nLastCheck) {
            _nLastCheck = nNow;
        }

        if ((nNow - _nLastCheck) > 60000) { // re-check every minute
            if (_savedHostname.equals(hostname) && _savedOneUp.equals(oneUp)) { // Now I have control...
                //logger.debug("I am taking control from: " + hostname + ", " + oneUp);

                if (updateToken(true)) { // Try to grab control:                  
                    _bHaveControl = true;
                } else { // (else someone else snagged control just carry on)
                    _nLastCheck = 0; // (reset clock again anyway)
                }

            } //(if lock has remained static)
        } //(end if >=1 minutes has passed)

    } //(end if have don't have control)

    // 4] Update saved state 

    _savedHostname = hostname;
    _savedOneUp = oneUp;

    return _bHaveControl;
}

From source file:com.ineunet.knife.persist.mongo.MongoDBServerImpl.java

License:Apache License

public MongoDBServerImpl() throws Exception {
    String url = ConfigFactory.getKnifeConfig().get(ConfigKeysDB.mongo_db_addr, "localhost");
    this.dbName = ConfigFactory.getKnifeConfig().get(ConfigKeysDB.mongo_db_defaultdb, "knife");
    String[] addrs = url.split(",");
    List<ServerAddress> sa = new ArrayList<ServerAddress>();
    for (String ad : addrs) {
        String[] tmp = ad.split(":");
        if (tmp.length == 1) {
            sa.add(new ServerAddress(tmp[0], defaultPort));
        } else {/*from ww w  .j a  v  a2 s.com*/
            sa.add(new ServerAddress(tmp[0], Integer.parseInt(tmp[1])));
        }
    }

    this.mongo = new Mongo(sa);
    mongo.setWriteConcern(WriteConcern.SAFE);
}

From source file:com.jagornet.dhcp.db.MongoLeaseManager.java

License:Open Source License

public void init() throws Exception {
    mongoClient = new Mongo(getMongoServer());
    mongoClient.setWriteConcern(WriteConcern.SAFE); // throw exceptions on failed write
    database = mongoClient.getDB("jagornet-dhcpv6");
    log.info("Connected to jagornet-dhcpv6 via Mongo client: " + mongoClient.toString());
    dhcpLeases = database.getCollection("DHCPLEASE");
    dhcpLeases.ensureIndex(new BasicDBObject("ipAddress", 1), "pkey", true);
    dhcpLeases.ensureIndex(new BasicDBObject("duid", 1).append("iatype", 1).append("iaid", 1), "tuple", false);
    dhcpLeases.ensureIndex("duid");
    dhcpLeases.ensureIndex("iatype");
    dhcpLeases.ensureIndex("state");
    dhcpLeases.ensureIndex("validEndTime");
}

From source file:com.joyfulmongo.db.javadriver.JFDBCollection.java

License:Apache License

public void create(JSONObject creates) {
    JFDBObject newDBObj = new JFDBObject(colName, creates);

    DBObject dbObject = newDBObj.getDBObject();

    dbCollection.ensureIndex(Constants.Props.objectId.toString());
    ensure2DIndexIfExist(creates);/*  w ww.java 2s. c  o m*/

    WriteResult result = dbCollection.insert(dbObject, WriteConcern.SAFE);

    recordWriteResult("create", result);
}

From source file:com.joyfulmongo.db.javadriver.JFDBCollection.java

License:Apache License

public void update(JSONObject query, JSONObject updates) {
    JSONObject extraInstruction = null;/*from  w  ww  .  j a  v  a  2  s  .  co  m*/

    List<String> modifiers = findModifiers(updates);
    for (String modifier : modifiers) {
        if (extraInstruction == null) {
            extraInstruction = new JSONObject();
        }
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("UPDATE modifier=" + modifier + " " + extraInstruction);
        }
        Object o = updates.get(modifier);
        extraInstruction.put(modifier, o);
    }

    for (String modifier : modifiers) {
        updates.remove(modifier);
    }

    this.ensure2DIndexIfExist(updates);

    JFDBObject queryObj = new JFDBObject(colName, query);
    DBObject queryDBObject = queryObj.getDBObject();

    WriteResult result = null;
    {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("UPDATE =" + " " + updates);
        }
        JSONObject updateInstruction = new JSONObject();
        updateInstruction.put("$set", updates);
        JFDBObject jfObj = new JFDBObject(colName, updateInstruction);
        DBObject updateDBObject = jfObj.getDBObject();

        result = dbCollection.update(queryDBObject, updateDBObject, false, false, WriteConcern.SAFE);
    }

    if (extraInstruction != null) {
        JFDBObject jfObj = new JFDBObject(colName, extraInstruction);
        DBObject updateInstructionObject = jfObj.getDBObject();
        result = dbCollection.update(queryDBObject, updateInstructionObject, false, false, WriteConcern.SAFE);
    }

    recordWriteResult("update", result);
}

From source file:com.joyfulmongo.db.javadriver.JFDBCollection.java

License:Apache License

public void delete(JSONObject obj) {
    String objectId = (String) obj.get(Constants.Props.objectId.toString());
    if (objectId == null || objectId.length() == 0) {
        throw new IllegalArgumentException("ObjectId can not be null or empty.");
    } else {/* w ww. j av a2s.c  om*/
        DBObject queryCondition = new BasicDBObject();

        queryCondition.put(Constants.Props.objectId.toString(), objectId);

        WriteResult result = dbCollection.remove(queryCondition, WriteConcern.SAFE);

        recordWriteResult("delete", result);
    }
}

From source file:com.joyfulmongo.db.javadriver.MongoCollection.java

License:Apache License

public void create(JSONObject creates) {
    MongoObject newDBObj = new MongoObject(colName, creates);

    DBObject dbObject = newDBObj.getDBObject();

    dbCollection.ensureIndex(Constants.Props.objectId.toString());
    ensure2DIndexIfExist(creates);//from ww  w . ja  v a  2s  . c  o m

    WriteResult result = dbCollection.insert(dbObject, WriteConcern.SAFE);

    recordWriteResult("create", result);
}

From source file:com.joyfulmongo.db.javadriver.MongoCollection.java

License:Apache License

public void update(JSONObject query, JSONObject updates) {
    JSONObject extraInstruction = null;//from   w ww . jav a 2  s  . c  om

    List<String> modifiers = findModifiers(updates);
    for (String modifier : modifiers) {
        if (extraInstruction == null) {
            extraInstruction = new JSONObject();
        }
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("UPDATE modifier=" + modifier + " " + extraInstruction);
        }
        Object o = updates.get(modifier);
        extraInstruction.put(modifier, o);
    }

    for (String modifier : modifiers) {
        updates.remove(modifier);
    }

    this.ensure2DIndexIfExist(updates);

    MongoObject queryObj = new MongoObject(colName, query);
    DBObject queryDBObject = queryObj.getDBObject();

    WriteResult result = null;
    {
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("UPDATE =" + " " + updates);
        }
        JSONObject updateInstruction = new JSONObject();
        updateInstruction.put("$set", updates);
        MongoObject jfObj = new MongoObject(colName, updateInstruction);
        DBObject updateDBObject = jfObj.getDBObject();

        result = dbCollection.update(queryDBObject, updateDBObject, false, false, WriteConcern.SAFE);
    }

    if (extraInstruction != null) {
        MongoObject jfObj = new MongoObject(colName, extraInstruction);
        DBObject updateInstructionObject = jfObj.getDBObject();
        result = dbCollection.update(queryDBObject, updateInstructionObject, false, false, WriteConcern.SAFE);
    }

    recordWriteResult("update", result);
}

From source file:com.malsolo.mongodb.humongous.config.Application.java

@Bean
public Mongo mongo() throws Exception {
    Mongo mongo = new MongoClient();
    mongo.setWriteConcern(WriteConcern.SAFE);
    return mongo;
}