Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

In this page you can find the example usage for com.mongodb DBObject get.

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

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

License:Apache License

@Override
protected void updateComponentCustom(JPanel comp) {
    try {//from   www .  ja va2 s  .  com
        DBObject index = getIndexInfo();
        setStringFieldValue(Item.name, (String) index.get("name"));
        setStringFieldValue(Item.ns, (String) index.get("ns"));
        ((DocField) getBoundUnit(Item.key)).setDoc((DBObject) index.get("key"));
        ((DocField) getBoundUnit(Item.info)).setDoc(index);
    } catch (Exception e) {
        UMongo.instance.showError(this.getClass().getSimpleName() + " update", e);
    }
}

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

License:Apache License

public static void addChildrenToTreeNode(DefaultMutableTreeNode node, DBObject obj) {
    for (String key : obj.keySet()) {
        Object val = obj.get(key);
        //            if (val == null) {
        //                continue;
        //            }

        DefaultMutableTreeNode child = new DefaultMutableTreeNode(new TreeNodeDocumentField(key, val));
        if (val instanceof DBObject) {
            addChildrenToTreeNode(child, (DBObject) val);
        } else if (val instanceof ObjectId) {
            // break it down
            ObjectId id = (ObjectId) val;
            child.add(new DefaultMutableTreeNode(
                    "Time: " + id.getTime() + " = " + new Date(id.getTime()).toString()));
            child.add(new DefaultMutableTreeNode("Machine: " + (id.getMachine() & 0xFFFFFFFFL)));
            child.add(new DefaultMutableTreeNode("Inc: " + (id.getInc() & 0xFFFFFFFFL)));
        }//from w  w  w  .  ja  v  a  2  s .c  om
        node.add(child);
    }
}

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

License:Apache License

public static DBObject getReplicaSetInfo(MongoClient mongo) {
    DB db = mongo.getDB("local");
    DBObject result = new BasicDBObject();
    DBCollection namespaces = db.getCollection("system.namespaces");
    String oplogName;//from   w  w  w.ja v  a  2  s . com
    if (namespaces.findOne(new BasicDBObject("name", "local.oplog.rs")) != null) {
        oplogName = "oplog.rs";
    } else if (namespaces.findOne(new BasicDBObject("name", "local.oplog.$main")) != null) {
        oplogName = "oplog.$main";
    } else {
        return null;
    }
    DBObject olEntry = namespaces.findOne(new BasicDBObject("name", "local." + oplogName));
    if (olEntry != null && olEntry.containsField("options")) {
        BasicDBObject options = (BasicDBObject) olEntry.get("options");
        long size = options.getLong("size");
        result.put("logSizeMB", Float.valueOf(String.format("%.2f", size / 1048576f)));
    } else {
        return null;
    }
    DBCollection oplog = db.getCollection(oplogName);
    int size = oplog.getStats().getInt("size");
    result.put("usedMB", Float.valueOf(String.format("%.2f", size / 1048576f)));

    DBCursor firstc = oplog.find().sort(new BasicDBObject("$natural", 1)).limit(1);
    DBCursor lastc = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1);
    if (!firstc.hasNext() || !lastc.hasNext()) {
        return null;
    }
    BasicDBObject first = (BasicDBObject) firstc.next();
    BasicDBObject last = (BasicDBObject) lastc.next();
    BSONTimestamp tsfirst = (BSONTimestamp) first.get("ts");
    BSONTimestamp tslast = (BSONTimestamp) last.get("ts");
    if (tsfirst == null || tslast == null) {
        return null;
    }

    int ftime = tsfirst.getTime();
    int ltime = tslast.getTime();
    int timeDiffSec = ltime - ftime;
    result.put("timeDiff", timeDiffSec);
    result.put("timeDiffHours", Float.valueOf(String.format("%.2f", timeDiffSec / 3600f)));
    result.put("tFirst", new Date(ftime * 1000l));
    result.put("tLast", new Date(ltime * 1000l));
    result.put("now", new Date());
    return result;
}

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

License:Apache License

/**
 * Checks key strings for invalid characters.
 *//*w w  w . j  a  v a2  s  . c o  m*/
public static void checkKeys(DBObject o) {
    if (o instanceof LazyDBObject || o instanceof LazyDBList)
        return;

    for (String s : o.keySet()) {
        validateKey(s);
        Object inner = o.get(s);
        if (inner instanceof DBObject) {
            checkKeys((DBObject) inner);
        } else if (inner instanceof Map) {
            checkKeys((Map<String, Object>) inner);
        }
    }
}

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

License:Apache License

void update(int options, WriteConcern wc, ReadPreference rp) {
    // reset/*from  w  ww.  ja  v  a2s .  c  o m*/
    xmlLoadCheckpoint();

    setBooleanFieldValue(Item.tailable, (options & Bytes.QUERYOPTION_TAILABLE) != 0);
    setBooleanFieldValue(Item.slaveOk, (options & Bytes.QUERYOPTION_SLAVEOK) != 0);
    setBooleanFieldValue(Item.opLogReplay, (options & Bytes.QUERYOPTION_OPLOGREPLAY) != 0);
    setBooleanFieldValue(Item.noTimeout, (options & Bytes.QUERYOPTION_NOTIMEOUT) != 0);
    setBooleanFieldValue(Item.awaitData, (options & Bytes.QUERYOPTION_AWAITDATA) != 0);
    setBooleanFieldValue(Item.exhaust, (options & Bytes.QUERYOPTION_EXHAUST) != 0);
    setBooleanFieldValue(Item.partial, (options & Bytes.QUERYOPTION_PARTIAL) != 0);

    Object w = wc.getWObject();
    int wInt = (Integer) (w instanceof Integer ? w : 0);
    String wStr = (String) (w instanceof String ? w : "");
    setIntFieldValue(Item.writeFactor, wInt);
    setStringFieldValue(Item.writePolicy, wStr);
    setIntFieldValue(Item.writeTimeout, wc.getWtimeout());
    //        setBooleanFieldValue(Item.fsync, wc.fsync());

    DBObject rpObj = rp.toDBObject();
    ComboBox readBox = (ComboBox) getBoundUnit(Item.rpPreference);
    ReadPref rpEnm = ReadPref.primary;
    if (rp != null)
        rpEnm = ReadPref.valueOf(rp.getName());
    readBox.value = rpEnm.ordinal();
    if (rpObj.containsField("tags")) {
        List tags = (List) rpObj.get("tags");
        if (tags.size() > 0) {
            ((DocBuilderField) getBoundComponentUnit(Item.rpTag)).setDBObject((DBObject) tags.get(0));
        }
    }
}

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

License:Apache License

@Override
protected void populateChildren() {
    // need to make a query to update server list
    try {//from w  w w . jav  a2  s.c o  m
        mongo.getDatabaseNames();
    } catch (Exception e) {
        getLogger().log(Level.WARNING, null, e);
    }

    // need to pull servers from configuration to see hidden
    //        List<ServerAddress> addrs = mongo.getServerAddressList();
    final DBCollection col = mongo.getDB("local").getCollection("system.replset");
    DBObject config = col.findOne();
    if (config == null) {
        getLogger().log(Level.WARNING, "No replica set configuration found");
        return;
    }

    BasicDBList members = (BasicDBList) config.get("members");
    for (int i = 0; i < members.size(); ++i) {
        String host = (String) ((DBObject) members.get(i)).get("host");
        try {
            // this will create new MongoClient instance, catch any exception
            addChild(new ServerNode(host, mongo.getMongoClientOptions(), true, false));
        } catch (Exception e) {
            getLogger().log(Level.WARNING, null, e);
        }
    }
}

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

License:Apache License

static public void reconfigure(final ReplSetNode rsNode, DBObject config) {
    final DBCollection col = rsNode.getMongoClient().getDB("local").getCollection("system.replset");
    DBObject oldConf = col.findOne();
    int version = ((Integer) oldConf.get("version")) + 1;
    config.put("version", version);

    // reconfig usually triggers an error as connections are bounced.. try to absorb it
    final DBObject cmd = new BasicDBObject("replSetReconfig", config);
    final DB admin = rsNode.getMongoClient().getDB("admin");

    new DbJob() {

        @Override//w w w  .jav  a 2 s  .  c o  m
        public Object doRun() {
            Object res = null;
            try {
                res = admin.command(cmd);
            } catch (MongoException.Network e) {
                res = new BasicDBObject("msg", "Operation was likely successful, but connection was bounced");
            }

            try {
                // sleep a bit since it takes time for driver to see change
                Thread.sleep(6000);
            } catch (InterruptedException ex) {
                getLogger().log(Level.WARNING, null, ex);
            }
            return res;
        }

        @Override
        public String getNS() {
            return null;
        }

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

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

        @Override
        public void wrapUp(Object res) {
            // try to restructure but changes arent seen for a few seconds
            super.wrapUp(res);
            rsNode.structureComponent();
        }
    }.addJob();
}

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

License:Apache License

public void addReplica(ButtonBase button) {
    final DBCollection col = getReplSetNode().getMongoClient().getDB("local").getCollection("system.replset");
    DBObject config = col.findOne();
    if (config == null) {
        new InfoDialog(null, "reconfig error", null, "No existing replica set configuration").show();
        return;//from w  w  w. java 2 s.co  m
    }

    BasicDBList members = (BasicDBList) config.get("members");
    int max = 0;
    for (int i = 0; i < members.size(); ++i) {
        int id = (Integer) ((DBObject) members.get(i)).get("_id");
        if (id > max)
            max = id;
    }

    ReplicaDialog dia = UMongo.instance.getGlobalStore().getReplicaDialog();
    if (!dia.show())
        return;
    BasicDBObject conf = dia.getReplicaConfig(max + 1);
    members.add(conf);
    reconfigure(getReplSetNode(), config);
}

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

License:Apache License

public void queryOplog(ButtonBase button) {
    final DBCollection oplog = getReplSetNode().getMongoClient().getDB("local").getCollection("oplog.rs");
    DBObject start = ((DocBuilderField) getBoundUnit(Item.qoStart)).getDBObject();
    DBObject end = ((DocBuilderField) getBoundUnit(Item.qoEnd)).getDBObject();
    DBObject extra = ((DocBuilderField) getBoundUnit(Item.qoQuery)).getDBObject();

    BasicDBObject query = new BasicDBObject();
    BasicDBObject range = new BasicDBObject();
    if (start != null)
        range.put("$gte", start.get("ts"));
    if (end != null)
        range.put("$lte", end.get("ts"));

    query.put("ts", range);
    if (extra != null)
        query.putAll(extra);/*from   w  ww . j  ava2 s.c  o m*/

    CollectionPanel.doFind(oplog, query, null, null, 0, 0, 0, false, null, Bytes.QUERYOPTION_OPLOGREPLAY);
}

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

License:Apache License

void refreshTagList() {
    String shardName = getReplSetNode().getShardName();
    if (shardName == null)
        return;/*ww  w  .  j a  v  a 2  s  .  c om*/

    ListArea list = (ListArea) getBoundUnit(Item.tagList);
    final DB db = ((RouterNode) getReplSetNode().getParentNode()).getMongoClient().getDB("config");
    DBObject shard = db.getCollection("shards").findOne(new BasicDBObject("_id", shardName));
    if (shard.containsField("tags")) {
        BasicDBList tags = (BasicDBList) shard.get("tags");
        if (tags.size() > 0) {
            String[] array = new String[tags.size()];
            int i = 0;
            for (Object tag : tags) {
                array[i++] = (String) tag;
            }
            list.items = array;
            list.structureComponent();
            return;
        }
    }
    list.items = null;
    list.structureComponent();
}