Example usage for com.mongodb DBCollection drop

List of usage examples for com.mongodb DBCollection drop

Introduction

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

Prototype

public void drop() 

Source Link

Document

Drops (deletes) this collection from the database.

Usage

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

License:Apache License

public void doImport(final ButtonBase button) throws IOException {
    ImportDialog dia = UMongo.instance.getGlobalStore().getImportDialog();
    if (!dia.show()) {
        return;//from   w w  w .j  a va2  s  . c o m
    }
    final boolean dropCollection = dia.getBooleanFieldValue(ImportDialog.Item.dropCollection);
    final boolean continueOnError = dia.getBooleanFieldValue(ImportDialog.Item.continueOnError);
    final boolean upsert = dia.getBooleanFieldValue(ImportDialog.Item.upsert);
    final boolean bulk = dia.getBooleanFieldValue(ImportDialog.Item.bulk);
    String supsertFields = dia.getStringFieldValue(ImportDialog.Item.upsertFields);
    final String[] upsertFields = supsertFields != null ? supsertFields.split(",") : null;
    if (upsertFields != null) {
        for (int i = 0; i < upsertFields.length; ++i) {
            upsertFields[i] = upsertFields[i].trim();
        }
    }
    final DocumentDeserializer dd = dia.getDocumentDeserializer();
    final DBCollection col = getCollectionNode().getCollection();

    new DbJob() {
        @Override
        public Object doRun() throws Exception {
            try {
                if (dropCollection) {
                    col.drop();
                }
                DBObject obj = null;
                List<DBObject> batch = new ArrayList<DBObject>();
                while ((obj = dd.readObject()) != null) {
                    try {
                        if (upsert) {
                            if (upsertFields == null) {
                                col.save(obj);
                            } else {
                                BasicDBObject query = new BasicDBObject();
                                for (int i = 0; i < upsertFields.length; ++i) {
                                    String field = upsertFields[i];
                                    if (!obj.containsField(field)) {
                                        throw new Exception("Upsert field " + field + " not present in object "
                                                + obj.toString());
                                    }
                                    query.put(field, obj.get(field));
                                }
                                col.update(query, obj, true, false);
                            }
                        } else {
                            if (bulk) {
                                if (batch.size() > 1000) {
                                    col.insert(batch);
                                    batch.clear();
                                }
                                batch.add(obj);
                            } else {
                                col.insert(obj);
                            }
                        }
                    } catch (Exception e) {
                        if (continueOnError) {
                            getLogger().log(Level.WARNING, null, e);
                        } else {
                            throw e;
                        }
                    }
                }

                if (!batch.isEmpty()) {
                    col.insert(batch);
                }

            } finally {
                dd.close();
            }
            return null;
        }

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

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

        @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;//  w w w. j  ava2  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.hangum.tadpole.mongodb.core.test.MongoTestProfilling.java

License:Open Source License

/**
 * @param args//w ww .  ja v a 2  s.  c om
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    // ??       
    System.out.println("####[profilling  ]######################################################");
    CommandResult cr = db.command(new BasicDBObject("profile", 0));

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + JSONUtil.getPretty(cr.toString()));
    System.out.println("[size]" + cr.size());
    System.out.println("####[profilling  ]######################################################");

    //  ?          
    System.out.println(
            "####[profilling collections  ]######################################################");
    if (db.collectionExists("system.profile")) {
        DBCollection profileColl = db.getCollection("system.profile");
        profileColl.drop();
    }
    System.out.println(
            "####[profilling collections  ]######################################################");
    //  ?     

    // system.profile collection ? 
    System.out.println(
            "####[profilling collections ? ]######################################################");
    DBObject dbObject = (DBObject) JSON.parse("{capped:true, size:8000000}");
    DBCollection dbColl = db.createCollection("system.profile", dbObject);
    BasicDBObject newProfileColl = (BasicDBObject) dbColl.getStats().copy();
    System.out.println(
            "####[profilling collections ? ]######################################################");
    // system.profile collection ? 

    System.out.println("####[profilling ]######################################################");
    cr = db.command(new BasicDBObject("profile", 2));

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + JSONUtil.getPretty(cr.toString()));
    System.out.println("[size]" + cr.size());
    System.out.println("####[profilling ]######################################################");

    //      //#######################################################################################################
    //      //#######################################################################################################
    //      //#######################################################################################################      
    System.out.println("####[start profilling result]######################################################");
    DBCollection myColl = db.getCollection("system.profile");

    BasicDBObject query = new BasicDBObject();
    query.put("millis", new BasicDBObject("$gt", 4));

    DBCursor myCursor = myColl.find();
    while (myCursor.hasNext()) {
        DBObject dbObj = myCursor.next();
        System.out.println(dbObj.get("ts") + " - " + dbObj.get("ns") + " - " + dbObj.get("nscanned") + "/"
                + dbObj.get("nreturned") + " millis :" + dbObj.get("millis"));
    }
    System.out.println("####[end profilling result]######################################################");

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestReferenceCollection.java

License:Open Source License

/**
 * @param args/* w w w .j  av  a  2 s.  c  o  m*/
 */
public static void main(String[] args) throws Exception {

    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBObject colInformation = (DBObject) JSON.parse("{capped:true, size:100000}");
    DBCollection ref1Coll = db.getCollection(REF_1);
    if (ref1Coll != null)
        ref1Coll.drop();
    ref1Coll = db.createCollection(REF_1, colInformation);

    DBObject dbObjRef1 = (DBObject) JSON.parse("{ 'name' : 'cho'}");//"{'names': {'First': 'Gonza', 'Last': 'Vieira'}}");
    WriteResult wr = ref1Coll.insert(dbObjRef1);

    DBObject retDBObj = ref1Coll.findOne();
    createRef1Collection(db, retDBObj.get("_id"));

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestReferenceCollection.java

License:Open Source License

public static void createRef1Collection(DB db, Object objId) {
    DBObject colInformation = (DBObject) JSON.parse("{capped:true, size:100000}");
    DBCollection ref2Coll = db.getCollection(REF_2);
    if (ref2Coll != null)
        ref2Coll.drop();
    ref2Coll = db.createCollection(REF_2, colInformation);

    BasicDBObject insertObj = new BasicDBObject();
    insertObj.put(REF_1 + "_id", objId);

    DBObject addField = new BasicDBObject();
    addField.put("name", "Reference id");

    insertObj.putAll(addField);// www. j  ava  2 s . com

    //      DBObject dbObjRef2 = (DBObject) JSON.parse("{'ref1_id': 50f9437cf023f820730a3b42,  {'names': {'First': 'Gonza', 'Last': 'Vieira'}}}");
    ref2Coll.insert(insertObj);
}

From source file:com.hangum.tadpole.mongodb.core.test.UpdateEx.java

License:Open Source License

public static void dropCollection(String name) throws Exception {
    if (m == null)
        m = new Mongo(MangoDB_IP);
    DB db = m.getDB(DB_NAME);//from   www . ja  v  a 2  s .  c o  m
    DBCollection coll = db.getCollection(name);
    coll.drop();
}

From source file:com.ibm.sae.LoadDreamHomeDB.java

/***************************************************************/
 private static void loadCounters(DB db) {
     System.out.println("LoadDreamHomeDB:loadCounters begins");

     DBCollection coll = db.getCollection("dhCounterColl");
     System.out.println("LoadDreamHomeDB:loadCounters collection");
     coll.drop();

     System.out.println("LoadDreamHomeDB:loadCounters after drop");

     DBObject countersDoc = new BasicDBObject("_id", "notificationId").append("seq", 5000);

     WriteResult wr = coll.insert(countersDoc, WriteConcern.ACKNOWLEDGED);

     countersDoc = new BasicDBObject("_id", "clientId").append("seq", 4000);

     wr = coll.insert(countersDoc, WriteConcern.ACKNOWLEDGED);

     countersDoc = new BasicDBObject("_id", "propertyId").append("seq", 2000);

     wr = coll.insert(countersDoc, WriteConcern.ACKNOWLEDGED);

     countersDoc = new BasicDBObject("_id", "agentId").append("seq", 1000);

     wr = coll.insert(countersDoc, WriteConcern.ACKNOWLEDGED);

     countersDoc = new BasicDBObject("_id", "officeId").append("seq", 3000);

     wr = coll.insert(countersDoc, WriteConcern.ACKNOWLEDGED);

     System.out.println("LoadDreamHomeDB:loadNotificationCounters ends");
 }

From source file:com.ibm.sae.LoadDreamHomeDB.java

/***************************************************************/
 private static void loadClient(DB db, DBCollection coll) {
     System.out.println("LoadDreamHomeDB:loadClient begins");

     coll.drop();

     List<DBObject> comments = new ArrayList<DBObject>();
     List<DBObject> suggestedProperties = new ArrayList<DBObject>();

     DBObject commentDoc = new BasicDBObject("comment", "This is a beautiful home");

     comments.add(commentDoc);/*from ww w.  j a v  a2s  .c  o m*/

     DBObject propertyDoc = new BasicDBObject("propertyId", 1999).append("propertyState", 0)
             .append("askingPrice", 689900.45).append("rating", 0).append("comments", comments);

     suggestedProperties.add(propertyDoc);

     DBObject clientDoc = new BasicDBObject("clientId", 3999)
             .append("clientName", new BasicDBObject("clientFN", "Richard").append("clientLN", "Hendrix"))
             .append("clientInfo",
                     new BasicDBObject("address", "101 Valley Street").append("city", "Glendale")
                             .append("state", "California").append("zip", "67854"))
             .append("agentId", 1001).append("suggestedProperties", suggestedProperties)
             .append("minAskingPrice", 500000.00).append("maxAskingPrice", 700000.00);

     WriteResult wr = coll.insert(clientDoc, WriteConcern.ACKNOWLEDGED);

     System.out.println("LoadDreamHomeDB:loadClient ends");
 }

From source file:com.ibm.sae.LoadDreamHomeDB.java

/***************************************************************/
 private static void loadAgent(DB db, DBCollection coll) {
     System.out.println("LoadDreamHomeDB:loadAgent begins");

     coll.drop();

     List<DBObject> properties = new ArrayList<DBObject>();

     DBObject propertyDoc = new BasicDBObject("propertyId", 2000).append("clientId", 4000)
             .append("propertyState", 0);

     properties.add(propertyDoc);// w w w  .  j  ava  2 s. c o m

     DBObject agentDoc = new BasicDBObject("agentId", 999)
             .append("agentData", new BasicDBObject("agentFN", "Dinesh").append("agentLN", "Chugtai")
                     .append("agentLicense", "CAL-34917"))
             .append("officeId", 3000).append("properties", properties);

     WriteResult wr = coll.insert(agentDoc, WriteConcern.ACKNOWLEDGED);

     System.out.println("LoadDreamHomeDB:loadAgent ends");
 }

From source file:com.ibm.sae.LoadDreamHomeDB.java

/***************************************************************/
 private static void loadNotification(DB db, DBCollection coll) {
     System.out.println("LoadDreamHomeDB:loadNotification begins");

     coll.drop();

     DBObject notificationDoc = new BasicDBObject("notificationId", 4999).append("agentId", 1000)
             .append("clientId", 4000);
     WriteResult wr = coll.insert(notificationDoc, WriteConcern.ACKNOWLEDGED);

     System.out.println("LoadDreamHomeDB:loadNotification ends");
 }