Example usage for com.mongodb DBObject put

List of usage examples for com.mongodb DBObject put

Introduction

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

Prototype

Object put(String key, Object v);

Source Link

Document

Sets a name/value pair in this object.

Usage

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

License:Apache License

public void addJSFunction(final ButtonBase button) {
    final DB db = getDbNode().getDb();
    final DBCollection col = db.getCollection("system.js");
    final String name = getStringFieldValue(Item.addJSFunctionName);
    final String code = getStringFieldValue(Item.addJSFunctionCode);
    CollectionPanel.doFind(col, new BasicDBObject("_id", name));

    new DbJob() {

        @Override//from w w w.j  a  v  a2s  . c  om
        public Object doRun() throws Exception {
            DBObject obj = new BasicDBObject("_id", name);
            obj.put("value", new Code(code));
            return col.insert(obj);
        }

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

        @Override
        public String getShortName() {
            return "Add JS Function";
        }

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

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

License:Apache License

@Override
protected void commitComponentCustom(BoxPanel comp) {
    Div fields = (Div) getBoundUnit(Item.fields);
    DBObject doc = createDBObject();
    if (fields.hasChildren()) {
        for (Object child : fields.getChildren()) {
            if (child instanceof DocFieldText) {
                DocFieldText text = (DocFieldText) child;
                doc.put(text.getKey(), text.getValue());
            }//from   w  w w  .  j  av a2s.c  o  m
        }
    }
    value = doc;
}

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

License:Apache License

protected void addField(String key, Object val) {
    DBObject doc = (DBObject) value;
    doc.put(key, val);
}

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

License:Apache License

public DBObject readObject() throws IOException {
    if (first) {//from   w  w w  . j  ava 2s  . co m
        if (format != Format.BSON) {
            if (is == null) {
                FileReader fr = new FileReader(file);
                br = new BufferedReader(fr);
            } else {
                br = new BufferedReader(new InputStreamReader(is));
            }
            if (format == Format.CSV) {
                fields = br.readLine();
                if (fields != null) {
                    filter = fields.split(delimiter);
                    // field names are never quoted
                    for (int i = 0; i < filter.length; ++i) {
                        filter[i] = filter[i].trim();
                    }
                }
            }
        } else {
            if (is == null) {
                is = new FileInputStream(file);
            }
            callback = new DefaultDBCallback(null);
            decoder = new BasicBSONDecoder();
        }

        if (format == Format.JSON_ARRAY) {
            String line = br.readLine();
            BasicDBList list = (BasicDBList) JSON.parse(line);
            iterator = list.iterator();
        }

        first = false;
    }

    if (format == Format.JSON_ARRAY) {
        if (iterator == null || !iterator.hasNext()) {
            return null;
        }
        return (DBObject) iterator.next();
    }

    DBObject obj = null;
    if (format != Format.BSON) {
        String line = br.readLine();

        if (line == null) {
            return null;
        }

        if (format == Format.JSON_SINGLE_DOC) {
            // keep reading all lines
            String line2 = null;
            while ((line2 = br.readLine()) != null) {
                line += line2;
            }
        }

        if (format == Format.CSV) {
            List<String> values = splitByCommasNotInQuotes(line);
            if (template == null) {
                obj = new BasicDBObject();
                // set each field defined
                for (int i = 0; i < filter.length; ++i) {
                    String val = values.get(i);
                    // string values are always quoted
                    obj.put(filter[i], JSON.parse(val));
                }
            } else {
                obj = (BasicDBObject) template.copy();
                fillInTemplate(obj, values);
            }
        } else {
            obj = (DBObject) JSON.parse(line);
        }
    } else {
        // BSON is binary
        callback.reset();
        try {
            decoder.decode(is, callback);
        } catch (IOException e) {
            // most likely EOF
            return null;
        }
        obj = (DBObject) callback.get();

        //                // read length
        //                byte[] buf = new byte[4096];
        //                int n = fis.read(buf, 0, 4);
        //                if (n <= 0) {
        //                    return null;
        //                }
        //                int len = Bits.readInt(buf);
        //
        //                ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //                baos.write(buf, 0, 4);
        //                int toread = len;
        //                while (toread > 0) {
        //                    n = fis.read(buf, 0, Math.min(toread, buf.length));
        //                    if (n <= 0) {
        //                        break;
        //                    }
        //                    baos.write(buf, 0, n);
        //                    toread -= n;
        //                }
        //                if (baos.size() != len)
        //                    throw new IOException("Lenght of read object " + baos.size() + " does not match expected size " + len);
        //                obj = new BasicDBObject((BasicBSONObject) BSON.decode(baos.toByteArray()));
    }

    return obj;
}

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

License:Apache License

private void fillInTemplate(DBObject obj, List<String> values) {
    for (String field : obj.keySet()) {
        Object val = obj.get(field);
        if (val instanceof BasicDBObject) {
            fillInTemplate((BasicDBObject) val, values);
        } else if (val instanceof BasicDBList) {
            fillInTemplate((BasicDBList) val, values);
        } else if (val instanceof String) {
            String str = (String) val;
            if (str.startsWith("$")) {
                str = str.substring(1);//from   w  w  w.j a  va 2  s. c  o  m
                int slash = str.indexOf("/");
                String ref = str;
                String type = null;
                if (slash > 0) {
                    ref = str.substring(0, slash);
                    type = str.substring(slash + 1);
                }

                // find field index
                int index = 0;
                while (index < filter.length && !filter[index].equals(ref)) {
                    ++index;
                }
                if (index >= filter.length) {
                    continue;
                }
                String value = values.get(index);

                try {
                    if (type == null || "JSON".equals(type)) {
                        // this is typically used for quoted Strings
                        obj.put(field, JSON.parse(value));
                    } else if ("String".equals(type)) {
                        obj.put(field, value);
                    } else if ("Date".equals(type)) {
                        Long time = Long.valueOf(value);
                        obj.put(field, new Date(time));
                    } else if ("Boolean".equals(type)) {
                        obj.put(field, Boolean.valueOf(value));
                    } else if ("Integer".equals(type)) {
                        obj.put(field, Integer.valueOf(value));
                    } else if ("Long".equals(type)) {
                        obj.put(field, Long.valueOf(value));
                    } else if ("Double".equals(type)) {
                        obj.put(field, Double.valueOf(value));
                    }
                } catch (Exception ex) {
                    Logger.getLogger(DocumentDeserializer.class.getName()).log(Level.WARNING, null, ex);
                }
            } else {
                // this is a static value
                obj.put(field, val);
            }
        } else {
            // this is a static value
            obj.put(field, val);
        }
    }
}

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

License:Apache License

@Override
public Object getParameters() {
    DBObject cmd = new BasicDBObject();
    cmd.put("near", ((DocBuilderField) getBoundUnit(Item.near)).getDBObject());
    cmd.put("distanceField", getStringFieldValue(Item.distanceField));
    cmd.put("maxDistance", getDoubleFieldValue(Item.maxDistance));
    double distanceMult = getDoubleFieldValue(Item.distanceMultiplier);
    if (distanceMult > 0) {
        cmd.put("distanceMultiplier", distanceMult);
    }/*  ww w. j  a  v  a  2 s  . co  m*/
    DBObject query = ((DocBuilderField) getBoundUnit(Item.query)).getDBObject();
    if (query != null) {
        cmd.put("query", query);
    }
    boolean spherical = getBooleanFieldValue(Item.spherical);
    if (spherical) {
        cmd.put("spherical", spherical);
    }
    DBObject search = ((DocBuilderField) getBoundUnit(Item.query)).getDBObject();
    if (search != null) {
        cmd.put("query", search);
    }
    String includeLocs = getStringFieldValue(Item.includeLocs);
    if (includeLocs != null && !includeLocs.isEmpty()) {
        cmd.put("includeLocs", includeLocs);
    }
    boolean unique = getBooleanFieldValue(Item.uniqueDocs);
    if (unique) {
        cmd.put("uniqueDocs", unique);
    }

    return cmd;
}

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

License:Apache License

public void drop(ButtonBase button) {
    final IndexNode indexNode = getIndexNode();
    DBObject cmd = new BasicDBObject("deleteIndexes", indexNode.getCollectionNode().getCollection().getName());
    cmd.put("index", indexNode.getName());
    new DbJobCmd(indexNode.getCollectionNode().getDbNode().getDb(), cmd, null, indexNode.getCollectionNode(),
            null).addJob();/* w ww .j a v a2 s . com*/
}

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 ww  .  j a v a2s  .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.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();//from w  w  w . j  av a2  s  .com
    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
        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.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;/*from  w  ww.  ja v a 2 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();
}