Example usage for com.mongodb WriteConcern NORMAL

List of usage examples for com.mongodb WriteConcern NORMAL

Introduction

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

Prototype

WriteConcern NORMAL

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

Click Source Link

Document

Write operations that use this write concern will return as soon as the message is written to the socket.

Usage

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

License:Apache License

private WriteConcern getWriteConcernFromKeyword(String keyword) throws T2DBException {
    WriteConcern wc = null;//from w  w w .  j av a2s  .c om
    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:co.dewald.log.MongoDBAppender.java

License:Open Source License

/**
 * @return initialised state./*  w w  w.ja  va 2 s  .  c om*/
 */
protected boolean init() {
    if (mongo != null)
        return true;

    try {
        mongo = new MongoClient(host);
        db = mongo.getDB(database);
        logs = db.getCollection(collection);

        mongo.setWriteConcern(WriteConcern.NORMAL);
        logs.createIndex(index); // timestamp is indexed in descending order

        return true;
    } catch (final UnknownHostException e) {
        System.out.println("BAD: Nothing can be logged to MongoDB!");
        e.printStackTrace();
        return false;
    }
}

From source file:com.andreig.jetty.MongoDB.java

License:GNU General Public License

public static Mongo get() {

    if (db == null) {
        try {//from   w w  w  . j  av a 2 s .c  om
            List<ServerAddress> addrs = str2addresses(Config.mongo_servers);
            MongoOptions opts = new MongoOptions();
            opts.autoConnectRetry = true;
            int thrno = Config.server_threadsno;
            if (thrno < 100)
                opts.connectionsPerHost = thrno;
            else
                opts.connectionsPerHost = 100;
            opts.threadsAllowedToBlockForConnectionMultiplier = 10;
            opts.maxWaitTime = 10000; // millisecs
            db = new Mongo(addrs, opts);
            write_concern = Config.mongo_safeoperations ? WriteConcern.FSYNC_SAFE : WriteConcern.NORMAL;
            log.info("getDB():" + db);
        } catch (UnknownHostException e) {
            log.severe("Bad host " + e);
            db = null;
        }
    }

    return db;

}

From source file:com.cyslab.craftvm.rest.mongo.MongoDB.java

License:GNU General Public License

public static Mongo get() {

    if (db == null) {
        try {//from  w w w.  ja  v  a  2  s . c om
            List<ServerAddress> addrs = str2addresses(Config.mongo_servers);
            MongoOptions opts = new MongoOptions();
            opts.autoConnectRetry = true;
            int thrno = Config.server_threadsno;
            if (thrno < 100)
                opts.connectionsPerHost = thrno;
            else
                opts.connectionsPerHost = 100;
            opts.threadsAllowedToBlockForConnectionMultiplier = 10;
            opts.maxWaitTime = 10000; // millisecs
            db = new Mongo(addrs, opts);
            write_concern = Config.mongo_safeoperations ? WriteConcern.FSYNC_SAFE : WriteConcern.NORMAL;
            log.info("getDB():" + db);
        } catch (UnknownHostException e) {
            log.error("Bad host " + e);
            db = null;
        }
    }

    return db;

}

From source file:com.edgytech.umongo.RouterPanel.java

License:Apache License

public void regenConfigDB(ButtonBase button) throws UnknownHostException {
    MongoClient cmongo = getRouterNode().getMongoClient();
    String servers = getStringFieldValue(Item.regenServers);
    final String db = getStringFieldValue(Item.regenDB);
    final String col = getStringFieldValue(Item.regenCollection);
    final String ns = db + "." + col;
    final DBObject shardKey = ((DocBuilderField) getBoundUnit(Item.regenShardKey)).getDBObject();
    final boolean unique = getBooleanFieldValue(Item.regenKeyUnique);
    final BasicDBObject result = new BasicDBObject();
    result.put("ns", ns);
    result.put("shardKey", shardKey);
    result.put("unique", unique);

    // create direct mongo for each replica set
    String[] serverList = servers.split("\n");
    List<ServerAddress> list = new ArrayList<ServerAddress>();
    String txt = "";
    String primaryShard = null;/*from   w  w  w.  j a  v a 2  s  . c  o m*/
    final BasicDBObject shardList = new BasicDBObject();
    HashMap<MongoClient, String> mongoToShard = new HashMap<MongoClient, String>();
    sLoop: for (String server : serverList) {
        server = server.trim();
        String[] tokens = server.split("/");
        if (tokens.length != 2) {
            new InfoDialog(null, "Error", null, "Server format must be like 'hostname:port/shard', one by line")
                    .show();
            return;
        }
        server = tokens[0];
        String shard = tokens[1];
        if (primaryShard == null) {
            primaryShard = shard;
        }
        ServerAddress addr = new ServerAddress(server);

        // filter out if replset already exists
        for (MongoClient replset : mongoToShard.keySet()) {
            if (replset.getServerAddressList().contains(addr)) {
                continue sLoop;
            }
        }

        list.clear();
        list.add(addr);
        MongoClient mongo = new MongoClient(list);
        //            UMongo.instance.addMongoClient(mongo, null);
        // make request to force server detection
        mongo.getDatabaseNames();
        mongoToShard.put(mongo, shard);

        String desc = null;
        if (!mongo.getDatabaseNames().contains(db) || !mongo.getDB(db).getCollectionNames().contains(col)) {
            desc = "Collection not present!";
        } else {
            // try to see if shard key has index
            DBObject index = mongo.getDB(db).getCollection("system.indexes")
                    .findOne(new BasicDBObject("key", shardKey));
            if (index != null) {
                desc = "shard key found";
            } else {
                desc = "shard key NOT found!";
            }
        }
        txt += mongo.toString() + " shard=" + shard + " - " + desc + "\n";
        BasicDBObject shardObj = new BasicDBObject("servers", mongo.toString());
        shardObj.put("status", desc);
        if (shardList.containsField(shard)) {
            new InfoDialog(null, "Error", null, "Duplicate Shard name " + shard).show();
            return;
        }
        shardList.put(shard, shardObj);
    }
    result.put("shards", shardList);

    FormDialog dia = (FormDialog) getBoundUnit(Item.regenRSList);
    dia.setStringFieldValue(Item.regenRSListArea, txt);
    if (!dia.show()) {
        return;
    }

    DB config = cmongo.getDB("config");

    // add database record
    BasicDBObject doc = new BasicDBObject("_id", db);
    doc.put("partitioned", true);
    doc.put("primary", primaryShard);
    config.getCollection("databases").save(doc);

    // add collection record
    doc = new BasicDBObject("_id", ns);
    doc.put("lastmod", new Date());
    doc.put("dropped", false);
    doc.put("key", shardKey);
    doc.put("unique", unique);
    config.getCollection("collections").save(doc);

    final DBCollection chunks = config.getCollection("chunks");
    long count = chunks.count(new BasicDBObject("ns", ns));
    if (count > 0) {
        dia = (FormDialog) getBoundUnit(Item.regenDeleteChunks);
        if (dia.show()) {
            chunks.remove(new BasicDBObject("ns", ns));
        } else {
            return;
        }
    }

    // add temp collection to sort chunks with shard key
    final DBCollection tmpchunks = config.getCollection("_tmpchunks_" + col);
    tmpchunks.drop();
    // should be safe environment, and dup keys should be ignored
    tmpchunks.setWriteConcern(WriteConcern.NORMAL);
    // can use shardKey as unique _id
    //        tmpchunks.ensureIndex(shardKey, "shardKey", true);

    // create filter for shard fields
    final DBObject shardKeyFilter = new BasicDBObject();
    //        final DBObject shardKeyDescend = new BasicDBObject();
    boolean hasId = false;
    for (String key : shardKey.keySet()) {
        shardKeyFilter.put(key, 1);
        if (key.equals("_id")) {
            hasId = true;
        }
    }
    if (!hasId) {
        shardKeyFilter.put("_id", 0);
    }

    dia = (FormDialog) getBoundUnit(Item.regenConfirm);
    if (!dia.show()) {
        return;
    }

    // now fetch all records from each shard
    final AtomicInteger todo = new AtomicInteger(mongoToShard.size());
    for (Map.Entry<MongoClient, String> entry : mongoToShard.entrySet()) {
        final MongoClient mongo = entry.getKey();
        final String shard = entry.getValue();
        new DbJob() {

            @Override
            public Object doRun() throws Exception {
                BasicDBObject shardObj = (BasicDBObject) shardList.get(shard);
                long count = mongo.getDB(db).getCollection(col).count();
                shardObj.put("count", count);
                DBCursor cur = mongo.getDB(db).getCollection(col).find(new BasicDBObject(), shardKeyFilter);
                long i = 0;
                int inserted = 0;
                long start = System.currentTimeMillis();
                while (cur.hasNext() && !isCancelled()) {
                    BasicDBObject key = (BasicDBObject) cur.next();
                    setProgress((int) ((++i * 100.0f) / count));
                    try {
                        BasicDBObject entry = new BasicDBObject("_id", key);
                        entry.put("_shard", shard);
                        tmpchunks.insert(entry);
                        ++inserted;
                    } catch (Exception e) {
                        getLogger().log(Level.WARNING, e.getMessage(), e);
                    }
                }

                if (isCancelled()) {
                    shardObj.put("cancelled", true);
                }
                shardObj.put("inserted", inserted);
                shardObj.put("scanTime", System.currentTimeMillis() - start);
                todo.decrementAndGet();
                return null;
            }

            @Override
            public String getNS() {
                return tmpchunks.getFullName();
            }

            @Override
            public String getShortName() {
                return "Scanning " + shard;
            }

            @Override
            public boolean isDeterminate() {
                return true;
            }
        }.addJob();

    }

    new DbJob() {

        @Override
        public Object doRun() throws Exception {
            // wait for all shards to be done
            long start = System.currentTimeMillis();
            while (todo.get() > 0 && !isCancelled()) {
                Thread.sleep(2000);
            }

            if (isCancelled()) {
                result.put("cancelled", true);
                return result;
            }

            // find highest current timestamp
            DBCursor cur = chunks.find().sort(new BasicDBObject("lastmod", -1)).batchSize(-1);
            BasicDBObject chunk = (BasicDBObject) (cur.hasNext() ? cur.next() : null);
            BSONTimestamp ts = (BSONTimestamp) (chunk != null ? chunk.get("lastmod") : null);

            // now infer chunk ranges
            long count = tmpchunks.count();
            result.put("uniqueKeys", count);
            int numChunks = 0;
            cur = tmpchunks.find().sort(new BasicDBObject("_id", 1));
            BasicDBObject prev = (BasicDBObject) cur.next();
            BasicDBObject next = null;
            // snap prev to minkey
            BasicDBObject theid = (BasicDBObject) prev.get("_id");
            for (String key : shardKey.keySet()) {
                theid.put(key, new MinKey());
            }
            String currentShard = prev.getString("_shard");

            int i = 1;
            while (cur.hasNext()) {
                next = (BasicDBObject) cur.next();
                setProgress((int) ((++i * 100.0f) / count));
                String newShard = next.getString("_shard");
                if (newShard.equals(currentShard))
                    continue;

                // add chunk
                ts = getNextTimestamp(ts);
                chunk = getChunk(ns, shardKey, prev, next, ts);
                chunks.insert(chunk);
                prev = next;
                currentShard = prev.getString("_shard");
                ++numChunks;
            }

            // build max
            next = new BasicDBObject();
            for (String key : shardKey.keySet()) {
                next.put(key, new MaxKey());
            }
            next = new BasicDBObject("_id", next);
            ts = getNextTimestamp(ts);
            chunk = getChunk(ns, shardKey, prev, next, ts);
            chunks.insert(chunk);
            ++numChunks;
            result.put("numChunks", numChunks);
            result.put("totalTime", System.currentTimeMillis() - start);
            return result;
        }

        @Override
        public String getNS() {
            return chunks.getFullName();
        }

        @Override
        public String getShortName() {
            return "Creating Chunks";
        }

        @Override
        public boolean isDeterminate() {
            return true;
        }
    }.addJob();
}

From source file:com.linuxbox.enkive.audit.mongodb.MongoAuditService.java

License:Open Source License

public MongoAuditService(Mongo mongo, DBCollection collection) {
    this.mongo = mongo;
    this.auditCollection = collection;

    // see comments on def'n of CALL_ENSURE_INDEX_ON_INIT to see why it's
    // done conditionally
    if (CALL_ENSURE_INDEX_ON_INIT) {
        // see class com.linuxbox.enkive.MongoDBIndexManager
    }/* w  w  w. j  av a  2 s  .com*/

    // TODO: do we (will we) need a who, what, when index, so we can select
    // by who/what and sort by when?

    if (CONFIRM_AUDIT_LOG_WRITES) {
        auditCollection.setWriteConcern(WriteConcern.FSYNC_SAFE);
    } else {
        auditCollection.setWriteConcern(WriteConcern.NORMAL);
    }

    final int indexCount = auditCollection.getIndexInfo().size();
    // we expect 4 -- our 3 plus the default index on ObjectID
    if (indexCount > 4) {
        if (LOGGER.isWarnEnabled())
            LOGGER.warn(
                    "the MongoAuditService may have extra indices (which could impact performance); expect 4 but have "
                            + indexCount);
    }
}

From source file:com.nesscomputing.mongo.MongoWriter.java

License:Apache License

protected void flushToMongo(final List<Callable<DBObject>> dbObjects) {
    LOG.trace("Starting write of %d elements...", dbObjects.size());

    final DBCollection collection = dbCollection.get();
    if (collection != null) {
        final WriteResult writeResult = collection.insert(Lists.transform(dbObjects, CALLABLE_FUNCTION),
                WriteConcern.NORMAL);
        final CommandResult cmdResult = writeResult.getLastError();
        if (cmdResult.ok()) {
            opsSent.addAndGet(dbObjects.size());
        } else {//from   www. j  av a  2s . c om
            LOG.warn("Command returned %s", cmdResult.getErrorMessage());
            opsLost.addAndGet(dbObjects.size());
        }
        LOG.trace("Wrote %d put ops to Mongo dbCollection %s.", dbObjects.size(), collectionName);
    } else {
        LOG.warn("dbCollection is null, probably shutting down!");
    }
}

From source file:com.softwear.plugins.mongodb.MongoDBScriptObject.java

License:BSD License

public WriteConcern js_getMongoWriteConcern(String _type) {
    WriteConcern wc = WriteConcern.UNACKNOWLEDGED;
    if (_type.equals("none") || _type.equals("unacknowledge")) {
        wc = WriteConcern.UNACKNOWLEDGED;
    } else if (_type.equals("normal")) {
        wc = WriteConcern.NORMAL;
    } else if (_type.equals("safe")) {
        wc = WriteConcern.SAFE;//  w w  w.ja  v a  2  s .c  o  m
    } else if (_type.equals("replicas_safe")) {
        wc = WriteConcern.REPLICAS_SAFE;
    } else if (_type.equals("fsync_safe")) {
        wc = WriteConcern.FSYNC_SAFE;
    }
    return wc;
}

From source file:com.staticvillage.recommender.indexer.MongoDBIndexer.java

License:Apache License

@Override
public void addBean(Place bean) throws IndexerException {
    DBCollection dbCollection = instanceDB.getCollection(collection);
    dbCollection.insert(toDBObject(bean), WriteConcern.NORMAL);
}

From source file:HAL.libraries.blackboard_client.BlackboardClient.java

License:Open Source License

/**
 * Inserts a document into the currently selected collection.
 * Does not wait for the server to perform write to disk, nor does it check for errors other than networks errors.
 * //w  w w  .  java 2 s.c  o  m
 * @param obj DBObject representing the document to be inserted.
 * @return ObjectId of the inserted object.
 * @throws InvalidDBNamespaceException No collection has been selected.
 * @throws GeneralMongoException A MongoException occurred.
 **/
public ObjectId insertDocumentUnsafe(DBObject obj) throws InvalidDBNamespaceException, GeneralMongoException {
    if (currentCollection == null) {
        throw new InvalidDBNamespaceException("No collection has been selected.");
    }
    try {
        WriteConcern oldConcern = currentCollection.getWriteConcern();
        currentCollection.setWriteConcern(WriteConcern.NORMAL);
        currentCollection.insert(obj);
        currentCollection.setWriteConcern(oldConcern);
    } catch (MongoException mongoException) {
        throw new GeneralMongoException("An error occurred attempting to insert.", mongoException);
    }
    return ObjectId.massageToObjectId(obj.get("_id"));
}