Example usage for com.mongodb DBCollection setWriteConcern

List of usage examples for com.mongodb DBCollection setWriteConcern

Introduction

In this page you can find the example usage for com.mongodb DBCollection setWriteConcern.

Prototype

public void setWriteConcern(final WriteConcern writeConcern) 

Source Link

Document

Set the WriteConcern for this collection.

Usage

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

protected void writeFile(InputStream ins, MFile mf, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    db.requestStart();/*  w w w.j av  a 2 s  .  c  o m*/
    GridFSInputFile gfsInput;
    gfsInput = new GridFS(db, tableName).createFile(ins);
    DBCollection col = db.getCollection(tableName + ".files");
    col.setWriteConcern(WriteConcern.SAFE);
    gfsInput.setFilename(mf.getFilename());
    gfsInput.put(FIELD_STORAGE_KEY, mf.getStorageKey());
    gfsInput.put(FIELD_DOC_ID, mf.getDocid());
    gfsInput.put(FILED_VERSION_ID, mf.getVid());
    gfsInput.put(FIELD_APP_ID, mf.getAppid());
    gfsInput.put(FILED_IS_PUB, getIsPubStatus(mf.getIsPub()));
    gfsInput.setContentType(mf.getContentType());
    gfsInput.save();
    db.requestDone();
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

protected void writeTrivialFile(InputStream ins, MTrivialFile mtf, String tableName) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    db.requestStart();/*from w  w w.  j  a v  a 2 s.  c  o  m*/
    GridFSInputFile gfsInput;
    DBCollection col = db.getCollection(tableName + ".files");
    col.setWriteConcern(WriteConcern.SAFE);
    gfsInput = new GridFS(db, tableName).createFile(ins);
    gfsInput.setFilename(mtf.getFileName());
    gfsInput.put(FIELD_STORAGE_KEY, mtf.getStorageKey());
    gfsInput.put(FIELD_SPACE_NAME, mtf.getSpaceName());
    gfsInput.setContentType(mtf.getContentType());
    gfsInput.save();
    db.requestDone();
}

From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

private void chunkedUpload(int chunkIndex, int chunkCount, InputStream data, int dataLength, MFile mf,
        String tableName, int chunkSize) {
    DB db = options.getCollection(TABLE_TEMP_KEY).getDB();
    db.requestStart();/* w  ww.ja v a 2  s. c  o m*/
    MyGridFS gfs = new MyGridFS(db, tableName);
    DBCollection col = db.getCollection(tableName + ".files");
    col.setWriteConcern(WriteConcern.SAFE);
    MyGridFSInputFile inf = new MyGridFSInputFile(gfs);
    inf.setFilename(mf.getFilename());
    inf.setId(mf.getStorageKey());
    inf.put(FIELD_STORAGE_KEY, mf.getStorageKey());
    inf.put(FIELD_DOC_ID, mf.getDocid());
    inf.put(FILED_VERSION_ID, mf.getVid());
    inf.put(FIELD_APP_ID, mf.getAppid());
    inf.put(FILED_IS_PUB, getIsPubStatus(mf.getIsPub()));
    inf.setContentType(mf.getContentType());
    inf.setChunkSize(chunkSize);
    inf.setContentType(mf.getContentType());
    inf.setMD5(mf.getMd5());
    inf.setLength(mf.getLength());
    int numBytes = 0;
    int tmpRead = 0;
    byte[] buf = new byte[BUF_SIZE];
    byte[] bigBuf = new byte[dataLength];
    try {
        while (EOF != (tmpRead = data.read(buf))) {
            System.arraycopy(buf, 0, bigBuf, numBytes, tmpRead);
            numBytes += tmpRead;
        }
        if (dataLength != numBytes) {
            System.out.println(
                    "Missing Data expected data-size=" + dataLength + ", actual read data-size=" + numBytes);
            return;
        }
        inf.setInputStream(new ByteArrayInputStream(bigBuf, 0, numBytes));
        inf.setSavedChunk(false);
        inf.setCurrentChunkNumber(chunkIndex);
        inf.saveChunks(chunkSize);
        System.out.println("Chunk " + chunkIndex + " upload finish, bytes " + numBytes);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
    if (chunkIndex == (chunkCount - 1)) {
        inf.setSavedChunk(true);
        inf.save();
    }
    db.requestDone();
}

From source file:com.ebay.cloud.cms.dal.persistence.PersistenceContext.java

License:Apache License

public DBCollection getDBCollection(MetaClass metadata) {
    DB db = getDB();/* ww w  .  ja v a2s  . co m*/
    String collectionName = getDBCollectionName(metadata);
    DBCollection dbCol = db.getCollection(collectionName);
    dbCol.setReadPreference(consistentPolicy.getReadPreference());
    dbCol.setWriteConcern(consistentPolicy.getWriteConcern());
    return dbCol;
}

From source file:com.ebay.cloud.cms.metadata.mongo.MongoMetadataServiceImpl.java

License:Apache License

@Override
public int getCollectionCount(String dbCollectionName) {
    CheckConditions.checkNotNull(dbCollectionName, "Collection name can't be null!");
    Integer count = cacheManager.getCountFromCache(dbCollectionName);
    if (count == null) {
        DBCollection col = this.mongo.getDB(repo.getRepositoryName()).getCollection(dbCollectionName);
        // read from primary only
        col.setReadPreference(ReadPreference.primary());
        col.setWriteConcern(WriteConcern.SAFE);

        count = (Integer) col.getStats().get("count");
        if (count == null) {
            count = 0;/*ww  w. j  av a 2  s.c  o  m*/
        } else {
            cacheManager.putCountToCache(dbCollectionName, count);
        }
    }
    return count;
}

From source file:com.ebay.cloud.cms.metadata.mongo.MongoRepositoryServiceImpl.java

License:Apache License

public static void prepareSystemDB(Mongo mongo) {
    DBCollection repoCollection = mongo.getDB(CMSConsts.SYS_DB).getCollection(CMSConsts.REPOSITORY_COLL);

    MongoUtils.ensureIndex(repoCollection, Repository.REPOSITORY_FIELD_NAME, true, false);

    repoCollection.setWriteConcern(WriteConcern.SAFE);
}

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

License:Apache License

public void readWriteOptions(ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    OptionDialog od = UMongo.instance.getGlobalStore().getOptionDialog();
    od.update(col.getOptions(), col.getWriteConcern(), col.getReadPreference());
    if (!od.show()) {
        return;// www  .  java  2 s .  c  o m
    }
    col.setOptions(od.getQueryOptions());
    col.setWriteConcern(od.getWriteConcern());
    col.setReadPreference(od.getReadPreference());
    refresh();
}

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

License:Apache License

public void update(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.upQuery)).getDBObject();
    final BasicDBObject update = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.upUpdate)).getDBObject();
    final boolean upsert = getBooleanFieldValue(Item.upUpsert);
    final boolean multi = getBooleanFieldValue(Item.upMulti);
    final boolean safe = getBooleanFieldValue(Item.upSafe);
    col.setWriteConcern(WriteConcern.SAFE);

    new DbJob() {
        @Override/*from  w w  w  . j  ava  2 s.co  m*/
        public Object doRun() {
            if (safe) {
                long count = col.getCount(query);
                long toupdate = count > 0 ? 1 : 0;
                if (multi) {
                    toupdate = count;
                }
                String text = "Proceed with updating " + toupdate + " of " + count + " documents?";
                ConfirmDialog confirm = new ConfirmDialog(null, "Confirm Update", null, text);
                if (!confirm.show()) {
                    return null;
                }
            }
            return col.update(query, (DBObject) update.copy(), upsert, multi);
        }

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

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

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject obj = new BasicDBObject("query", query);
            obj.put("update", update);
            obj.put("upsert", upsert);
            obj.put("multi", multi);
            return obj;
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }
    }.addJob();
}

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;// ww  w.  j av a 2s  .c  om
    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:org.alfresco.cacheserver.dao.mongo.MongoWebSocketDAO.java

License:Open Source License

protected DBCollection getCollection(DB db, String collectionName, WriteConcern writeConcern) {
    if (!db.collectionExists(collectionName)) {
        DBObject options = new BasicDBObject();
        db.createCollection(collectionName, options);
    }//from   w w w.j a  v  a  2 s. com
    DBCollection collection = db.getCollection(collectionName);
    collection.setWriteConcern(writeConcern);

    return collection;
}