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.RouterNode.java

License:Apache License

@Override
protected void populateChildren() {
    CommandResult res = mongo.getDB("admin").command("listShards");
    shards = (BasicDBList) res.get("shards");
    if (shards == null) {
        return;/*ww w .  ja v  a2 s. co m*/
    }

    for (Object obj : shards) {
        try {
            DBObject shard = (DBObject) obj;
            String shardName = (String) shard.get("_id");
            String hosts = (String) shard.get("host");
            String repl = null;
            int slash = hosts.indexOf('/');
            if (slash >= 0) {
                repl = hosts.substring(0, slash);
                hosts = hosts.substring(slash + 1);
            }

            String[] hostList = hosts.split(",");
            ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
            for (String host : hostList) {
                int colon = host.indexOf(':');
                if (colon >= 0) {
                    addrs.add(new ServerAddress(host.substring(0, colon),
                            Integer.parseInt(host.substring(colon + 1))));
                } else {
                    addrs.add(new ServerAddress(host));
                }
            }

            if (repl != null || addrs.size() > 1) {
                addChild(new ReplSetNode(repl, addrs, mongo.getMongoClientOptions(), shardName));
            } else {
                addChild(new ServerNode(addrs.get(0), mongo.getMongoClientOptions(), false, false));
            }
        } catch (Exception e) {
            getLogger().log(Level.WARNING, null, e);
        }
    }

    // add config servers
    try {
        res = mongo.getDB("admin").command("getCmdLineOpts");
        String configStr = (String) ((BasicDBObject) res.get("parsed")).get("configdb");
        String[] configsvrs = configStr.split(",");
        for (String host : configsvrs) {
            int colon = host.indexOf(':');
            ServerAddress addr;
            if (colon >= 0) {
                addr = new ServerAddress(host.substring(0, colon), Integer.parseInt(host.substring(colon + 1)));
            } else {
                addr = new ServerAddress(host);
            }
            addChild(new ServerNode(addr, mongo.getMongoClientOptions(), false, true));
        }
    } catch (Exception e) {
        getLogger().log(Level.WARNING, null, e);
    }
}

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

License:Apache License

String[] getShardNames() {
    if (!shards.isEmpty()) {
        String[] items = new String[shards.size()];
        for (int i = 0; i < shards.size(); ++i) {
            DBObject shard = (DBObject) shards.get(i);
            items[i] = (shard.get("_id")).toString();
        }//from ww w.  jav a  2 s . c o  m
        return items;
    }
    return null;
}

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

License:Apache License

public void rsRemove(ButtonBase button) throws Exception {
    ReplSetNode replset = (ReplSetNode) getServerNode().getParentNode();
    final DBCollection col = replset.getMongoClient().getDB("local").getCollection("system.replset");
    DBObject config = col.findOne();

    BasicDBList members = (BasicDBList) config.get("members");
    int i = 0;//from w  w  w  . j av a2 s  .co m
    String myhost = getServerNode().getServerAddress().getHost() + ":"
            + getServerNode().getServerAddress().getPort();
    for (; i < members.size(); ++i) {
        if (myhost.equals(((DBObject) members.get(i)).get("host")))
            break;
    }

    if (i == members.size()) {
        throw new Exception("No such server in configuration");
    }

    members.remove(i);
    ReplSetPanel.reconfigure(replset, config);
}

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

License:Apache License

public void rsReconfigure(ButtonBase button) throws Exception {
    ReplSetNode replset = (ReplSetNode) getServerNode().getParentNode();
    final DBCollection col = replset.getMongoClient().getDB("local").getCollection("system.replset");
    DBObject config = col.findOne();

    BasicDBList members = (BasicDBList) config.get("members");
    int i = 0;// w  ww.  j a v a2 s  .c  o m
    String myhost = getServerNode().getServerAddress().getHost() + ":"
            + getServerNode().getServerAddress().getPort();
    for (; i < members.size(); ++i) {
        if (myhost.equals(((DBObject) members.get(i)).get("host")))
            break;
    }

    if (i == members.size()) {
        throw new Exception("No such server in configuration");
    }

    ReplicaDialog dia = UMongo.instance.getGlobalStore().getReplicaDialog();
    BasicDBObject oldConf = (BasicDBObject) members.get(i);
    dia.updateFromReplicaConfig(oldConf);
    if (!dia.show())
        return;
    BasicDBObject conf = dia.getReplicaConfig(oldConf.getInt("_id"));
    members.put(i, conf);

    ReplSetPanel.reconfigure(replset, config);
}

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

License:Apache License

void resetForNew(DB config, String ns) {
    xmlLoadCheckpoint();//from  w ww  .j  a  v  a  2  s .  c o  m

    DBObject collection = config.getCollection("collections").findOne(new BasicDBObject("_id", ns));
    DBObject shardKey = (DBObject) collection.get("key");
    ((DocBuilderField) getBoundUnit(Item.min)).setDBObject(shardKey);
    ((DocBuilderField) getBoundUnit(Item.max)).setDBObject(shardKey);
    ((DynamicComboBox) getBoundUnit(Item.tag)).items = getExistingTags(config);
    updateComponent();
}

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

License:Apache License

static String[] getExistingTags(DB config) {
    DBCursor cur = config.getCollection("shards").find();
    HashSet<String> tags = new HashSet<String>();
    while (cur.hasNext()) {
        DBObject shard = cur.next();
        if (shard.containsField("tags")) {
            BasicDBList list = (BasicDBList) shard.get("tags");
            for (Object tag : list) {
                tags.add((String) tag);
            }// w  ww. j av  a  2 s  . co m
        }
    }
    return tags.toArray(new String[tags.size()]);
}

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

License:Apache License

void logActivity(DBObject obj) {
    synchronized (this) {
        try {//from ww  w . j  a  v  a2s  .co  m
            if ("Auth".equals(obj.get("name"))) {
                // dont log auth
                return;
            }

            activityLogWriter.write(MongoUtils.getJSON(obj));
            activityLogWriter.write("\n");
            activityLogWriter.flush();
        } catch (Exception ex) {
            getLogger().log(Level.WARNING, null, ex);
        }
    }
}

From source file:com.effektif.mongo.MongoHelper.java

License:Apache License

public static Map<String, Object> toMap(DBObject dbObject) {
    if (dbObject == null) {
        return null;
    }//from ww w .  j a  v  a 2 s  .co m
    Map<String, Object> map = new HashMap<>();
    for (String key : dbObject.keySet()) {
        Object value = dbObject.get(key);
        if (value instanceof DBObject) {
            value = toMap((DBObject) value);
        }
        map.put(key, value);
    }
    return map;
}

From source file:com.effektif.mongo.MongoJobStore.java

License:Apache License

public Iterator<String> getWorkflowInstanceIdsToLockForJobs() {
    DBObject query = buildLockNextJobQuery().push(WORKFLOW_INSTANCE_ID).append("$exists", true).pop().get();
    // TODO use MongoQuery filterOrganization(query, JobFields.organizationId);
    DBObject retrieveFields = new BasicDBObject(WORKFLOW_INSTANCE_ID, true);
    DBCursor jobsDueHavingProcessInstance = jobsCollection.find("jobs-having-process-instance", query,
            retrieveFields);/* w w  w.j a  va  2s  .  com*/
    List<String> processInstanceIds = new ArrayList<>();
    while (jobsDueHavingProcessInstance.hasNext()) {
        DBObject partialJob = jobsDueHavingProcessInstance.next();
        Object processInstanceId = partialJob.get(WORKFLOW_INSTANCE_ID);
        processInstanceIds.add(processInstanceId.toString());
    }
    return processInstanceIds.iterator();
}

From source file:com.eharmony.matching.seeking.translator.mongodb.MongoQueryTranslator.java

License:Apache License

@SuppressWarnings("unchecked")
protected DBObject merge(DBObject[] queries) {
    if (queries.length == 1) {
        return queries[0];
    }/*from www  . j  a  v  a2 s  .c  o  m*/
    DBObject merged = object();
    Set<DBObject> ands = new LinkedHashSet<DBObject>();
    String andSymbol = MongoOperator.AND.symbol();
    // multimap => map<key,Collection<values>> 
    Map<String, Collection<DBObject>> map = multimap(queries).asMap();
    for (Entry<String, Collection<DBObject>> entry : map.entrySet()) {
        String key = entry.getKey();
        // merge multiple ANDs
        if (andSymbol.equals(key)) {
            for (DBObject o : entry.getValue()) {
                ands.addAll((Set<DBObject>) o.get(andSymbol));
            }
        } else if (entry.getValue().size() == 1) {
            DBObject single = entry.getValue().iterator().next();
            merged.put(key, single.get(key));
        } else {
            for (DBObject o : entry.getValue()) {
                ands.add(o);
            }
        }
    }
    // add ANDs
    if (ands.size() > 0) {
        merged.put(andSymbol, ands);
    }
    return merged;
}