Example usage for com.mongodb DBCollection getFullName

List of usage examples for com.mongodb DBCollection getFullName

Introduction

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

Prototype

public String getFullName() 

Source Link

Document

Get the full name of a collection, with the database name as a prefix.

Usage

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

License:Apache License

public void rename(ButtonBase button) {
    final CollectionNode colNode = getCollectionNode();
    final DBCollection col = colNode.getCollection();
    // select parent since this is getting renamed
    UMongo.instance.displayNode(colNode.getDbNode());

    final String name = getStringFieldValue(Item.newName);
    final boolean dropTarget = getBooleanFieldValue(Item.dropTarget);

    DBObject cmd = BasicDBObjectBuilder.start().add("renameCollection", col.getFullName())
            .add("to", col.getDB().getName() + "." + name).add("dropTarget", dropTarget).get();
    new DbJobCmd(col.getDB().getSisterDB("admin"), cmd, null, null).addJob();
}

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

License:Apache License

public void mapReduce(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    String map = getStringFieldValue(Item.mrMap);
    String reduce = getStringFieldValue(Item.mrReduce);
    String finalize = getStringFieldValue(Item.mrFinalize);
    String stype = getStringFieldValue(Item.mrType);
    final OutputType type = OutputType.valueOf(stype.toUpperCase());
    String out = getStringFieldValue(Item.mrOut);
    if (type != OutputType.INLINE && (out.isEmpty())) {
        new InfoDialog(id, null, null, "Output collection cannot be empty if type is not inline.").show();
        return;//w  w w.  j  a v  a 2 s. c o m
    }

    String outDB = getStringFieldValue(Item.mrOutDB);
    DBObject query = ((DocBuilderField) getBoundUnit(Item.mrQuery)).getDBObject();
    int limit = getIntFieldValue(Item.mrLimit);
    final MapReduceCommand cmd = new MapReduceCommand(col, map, reduce, out, type, query);
    DBObject sort = ((DocBuilderField) getBoundUnit(Item.mrSort)).getDBObject();
    if (sort != null) {
        cmd.setSort(sort);
    }
    if (!outDB.isEmpty()) {
        cmd.setOutputDB(outDB);
    }
    if (!finalize.isEmpty()) {
        cmd.setFinalize(finalize);
    }
    if (limit > 0) {
        cmd.setLimit(limit);
    }

    if (getBooleanFieldValue(Item.mrJSMode)) {
        cmd.addExtraOption("jsMode", true);
    }

    final BasicDBObject cmdobj = (BasicDBObject) cmd.toDBObject();
    if (getBooleanFieldValue(Item.mrOutSharded)) {
        ((BasicDBObject) cmdobj.get("out")).put("sharded", true);
    }
    if (getBooleanFieldValue(Item.mrNonAtomic)) {
        ((BasicDBObject) cmdobj.get("out")).put("nonAtomic", true);
    }

    new DbJob() {
        MapReduceOutput output;

        @Override
        public Object doRun() {
            //                output = col.mapReduce(cmd);

            // if type in inline, then query options like slaveOk is fine
            CommandResult res = null;
            if (type == MapReduceCommand.OutputType.INLINE) {
                res = col.getDB().command(cmdobj, col.getOptions());
                return res;
            }

            res = col.getDB().command(cmdobj);
            res.throwOnError();
            output = new MapReduceOutput(col, cmdobj, res);
            return output;
        }

        @Override
        public void wrapUp(Object res) {
            if (output != null) {
                if (cmd.getOutputType() == OutputType.INLINE) {
                    res = output.results();
                } else {
                    // spawn a find
                    doFind(output.getOutputCollection(), null);
                    res = output.getRaw();
                }
            }
            super.wrapUp(res);
        }

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

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

        @Override
        public DBObject getRoot(Object result) {
            return cmdobj;
        }

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

        @Override
        DBObject getCommand() {
            return cmdobj;
        }

        @Override
        DB getDB() {
            return col.getDB();
        }
    }.addJob();
}

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

License:Apache License

static void doFind(final DBCollection col, final DBObject query, final DBObject fields, final DBObject sort,
        final int skip, final int limit, final int batchSize, final boolean explain, final DBObject hint,
        final int options) {
    new DbJob() {
        @Override//from  ww w .j a v a2  s  . c om
        public Object doRun() {
            // this does not actually block, may not need dbjob
            DBCursor cur = col.find(query, fields).skip(skip).batchSize(batchSize).addOption(options);
            if (sort != null) {
                cur.sort(sort);
            }
            if (limit > 0) {
                cur.limit(limit);
            }
            if (hint != null) {
                cur.hint(hint);
            }
            if (explain) {
                return cur.explain();
            }

            // force cursor to start
            cur.hasNext();
            return cur;
        }

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

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

        @Override
        public DBObject getRoot(Object result) {
            if (result == null || !(result instanceof DBCursor)) {
                return null;
            }
            DBCursor res = (DBCursor) result;
            BasicDBObject obj = new BasicDBObject("cursorId", res.getCursorId());
            obj.put("query", res.getQuery());
            obj.put("fields", res.getKeysWanted());
            obj.put("options", res.getOptions());
            obj.put("readPreference", res.getReadPreference().toDBObject());
            obj.put("numSeen", res.numSeen());
            obj.put("numGetMores", res.numGetMores());
            // want skip, limit, batchsize
            return obj;
        }
    }.addJob();
}

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

License:Apache License

private void exportToFile(final DBCollection col, final DBObject query, final DBObject fields,
        final DBObject sort, final int skip, final int limit, final int batchSize) {
    ExportDialog dia = UMongo.instance.getGlobalStore().getExportDialog();
    if (!dia.show()) {
        return;/* w  ww .  j ava 2  s  .com*/
    }
    final DocumentSerializer ds = dia.getDocumentSerializer();
    final boolean continueOnError = dia.getBooleanFieldValue(ExportDialog.Item.continueOnError);
    new DbJob() {
        @Override
        public Object doRun() throws Exception {
            try {
                try {
                    DBCursor cur = col.find(query, fields);
                    if (skip > 0) {
                        cur.skip(skip);
                    }
                    if (batchSize != 0) {
                        cur.batchSize(batchSize);
                    }
                    if (sort != null) {
                        cur.sort(sort);
                    }
                    if (limit > 0) {
                        cur.limit(limit);
                    }
                    while (cur.hasNext() && !stopped) {
                        ds.writeObject(cur.next());
                    }
                } catch (Exception e) {
                    if (continueOnError) {
                        getLogger().log(Level.WARNING, null, e);
                    } else {
                        throw e;
                    }
                }
            } finally {
                ds.close();
            }
            return null;
        }

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

        @Override
        public String getShortName() {
            return "Export";
        }
    }.addJob();
}

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

License:Apache License

public void insert(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final BasicDBObject doc = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.insertDoc)).getDBObject();
    final int count = getIntFieldValue(Item.insertCount);
    final boolean bulk = getBooleanFieldValue(Item.insertBulk);

    new DbJob() {
        @Override/*  www.  ja v a2  s.  c o  m*/
        public Object doRun() throws IOException {
            WriteResult res = null;
            List<DBObject> list = new ArrayList<DBObject>();
            for (int i = 0; i < count; ++i) {
                BasicDBObject newdoc = (BasicDBObject) doc.copy();
                handleSpecialFields(newdoc);
                if (bulk) {
                    list.add(newdoc);
                    if (list.size() >= 1000) {
                        res = col.insert(list);
                        list.clear();
                    }
                } else {
                    res = col.insert(newdoc);
                }
            }
            if (bulk && !list.isEmpty()) {
                return col.insert(list);
            }
            return res;
        }

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

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

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject root = new BasicDBObject("doc", doc);
            root.put("count", count);
            root.put("bulk", bulk);
            return root;
        }

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

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

License:Apache License

public void save(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final BasicDBObject doc = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.saveDoc)).getDBObject();

    new DbJob() {
        @Override//w  w  w  . jav  a 2s  .c o  m
        public Object doRun() throws IOException {
            return col.save((DBObject) doc.copy());
        }

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

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

        @Override
        public DBObject getRoot(Object result) {
            return doc;
        }

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

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

License:Apache License

public void remove(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final BasicDBObject tmp = (BasicDBObject) ((DocBuilderField) getBoundUnit(Item.removeQuery)).getDBObject();
    final BasicDBObject doc = tmp != null ? tmp : new BasicDBObject();

    if (doc.isEmpty()) {
        ConfirmDialog confirm = (ConfirmDialog) getBoundUnit(Item.rmAllConfirm);
        if (!confirm.show()) {
            return;
        }/*  ww  w . ja  v a  2  s  . c o m*/
    }

    new DbJob() {
        @Override
        public Object doRun() throws IOException {
            return col.remove(doc);
        }

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

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

        @Override
        public DBObject getRoot(Object result) {
            return new BasicDBObject("query", doc);
        }

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

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

License:Apache License

public void createIndex(final ButtonBase button) {
    final CollectionNode node = getCollectionNode();
    final DBCollection col = getCollectionNode().getCollection();
    CreateIndexDialog dia = (CreateIndexDialog) button.getDialog();
    final DBObject keys = dia.getKeys();
    final DBObject opts = dia.getOptions();

    if (!UMongo.instance.getGlobalStore().confirmLockingOperation()) {
        return;//from  ww w. ja va2 s. c o m
    }

    new DbJob() {
        @Override
        public Object doRun() throws IOException {
            //                opts.put("key", keys);
            //                return col.getDB().getCollection("system.indexes").insert(opts);
            //                col.ensureIndex(keys, opts);
            col.createIndex(keys, opts);
            return new BasicDBObject("ok", 1);
        }

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

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

        @Override
        public void wrapUp(Object res) {
            super.wrapUp(res);
            node.structureComponent();
        }

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

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject obj = new BasicDBObject("keys", keys);
            obj.put("options", opts);
            return obj;
        }
    }.addJob();
}

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//  w  w  w.j  a  va2  s .  c  o 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.CollectionPanel.java

License:Apache License

public void doImport(final ButtonBase button) throws IOException {
    ImportDialog dia = UMongo.instance.getGlobalStore().getImportDialog();
    if (!dia.show()) {
        return;//  w  w  w  .j  a  v  a2  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();
}