Example usage for com.mongodb BasicDBList get

List of usage examples for com.mongodb BasicDBList get

Introduction

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

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value at an index.

Usage

From source file:org.tymoonnext.bot.module.clhs.CLHS.java

License:GNU General Public License

@Override
public ResultSet get(String volume, int from, int to, String user)
        throws SourceException, InexistentVolumeException {
    try {/* w  w  w  . j av a  2  s. com*/
        DataModel mod = DataModel.getFirst("clhs", new BasicDBObject("symbol", volume));
        if (mod == null)
            throw new InexistentVolumeException("No results for '" + volume + "'.");
        BasicDBList pages = mod.get("pages");

        if (to > pages.size())
            to = pages.size();
        if (to == -1)
            to = pages.size();
        if (from > to)
            from = to;

        Result[] res = new Result[to - from];
        for (int i = 0; i < res.length; i++) {
            res[i] = new Result(pages.get(i + from).toString());
        }
        return new ResultSet(res, pages.size());
    } catch (MongoException ex) {
        Commons.log.log(Level.WARNING, "[CLHS] Failed to retrieve record from the clhs collection!", ex);
        throw new SourceException("Error in MongoDB", ex);
    }
}

From source file:org.waveprotocol.box.server.persistence.mongodb.MongoDbDeltaStoreUtil.java

License:Apache License

public static TransformedWaveletDelta deserializeTransformedWaveletDelta(DBObject dbObject)
        throws PersistenceException {

    ParticipantId author = deserializeParicipantId((DBObject) dbObject.get(FIELD_AUTHOR));
    HashedVersion resultingVersion = deserializeHashedVersion((DBObject) dbObject.get(FIELD_RESULTINGVERSION));
    long applicationTimestamp = (Long) dbObject.get(FIELD_APPLICATIONTIMESTAMP);

    BasicDBList dbOps = (BasicDBList) dbObject.get(FIELD_OPS);
    ImmutableList.Builder<WaveletOperation> operations = ImmutableList.builder();

    int numOperations = dbOps.size();

    // Code analog to ProtoDeltaStoreDataSerializer.deserialize
    for (int i = 0; i < numOperations; i++) {

        WaveletOperationContext context;
        if (i == numOperations - 1) {
            context = new WaveletOperationContext(author, applicationTimestamp, 1, resultingVersion);
        } else {//from  w ww.j  av a 2s  . co m
            context = new WaveletOperationContext(author, applicationTimestamp, 1);
        }
        operations.add(deserializeWaveletOperation((DBObject) dbOps.get(i), context));
    }

    return new TransformedWaveletDelta(author, resultingVersion, applicationTimestamp, operations.build());
}

From source file:org.waveprotocol.box.server.persistence.mongodb.MongoDbStoreUtil.java

License:Apache License

public static TransformedWaveletDelta deserializeTransformedWaveletDelta(DBObject dbObject)
        throws PersistenceException {

    ParticipantId author = deserializeParicipantId((DBObject) dbObject.get(FIELD_AUTHOR));
    HashedVersion resultingVersion = deserializeHashedVersion((DBObject) dbObject.get(FIELD_RESULTINGVERSION));
    long applicationTimestamp = (Long) dbObject.get(FIELD_APPLICATIONTIMESTAMP);

    BasicDBList dbOps = (BasicDBList) dbObject.get(FIELD_OPS);
    ImmutableList.Builder<WaveletOperation> operations = ImmutableList.builder();

    int numOperations = dbOps.size();

    // Code analog to ProtoDeltaStoreDataSerializer.deserialize
    for (int i = 0; i < numOperations; i++) {

        WaveletOperationContext context;
        if (i == numOperations - 1) {
            context = new WaveletOperationContext(author, applicationTimestamp, resultingVersion.getVersion(),
                    resultingVersion);//  ww w.j  a v  a 2 s.co  m
        } else {
            context = new WaveletOperationContext(author, applicationTimestamp, resultingVersion.getVersion());
        }
        operations.add(deserializeWaveletOperation((DBObject) dbOps.get(i), context));
    }

    return new TransformedWaveletDelta(author, resultingVersion, applicationTimestamp, operations.build());
}

From source file:rapture.lock.mongodb.MongoLockHandler.java

License:Open Source License

@Override
public Boolean forceReleaseLock(String lockName) {
    log.debug("Mongo break lock");
    Document query = getLockQuery(lockName);
    Document current = getLockCollection().find(query).first();
    if (current != null) {
        BasicDBList locks = (BasicDBList) current.get(LOCKS);
        Document first = (Document) locks.get(0);
        String holder = first.get(CTX).toString();
        breakLock(holder, lockName);//from   ww w . j  av  a  2  s.c  o  m
        return true;
    }
    return false;
}

From source file:service.PrivatePostConverter.java

public static PrivateDiscussion toPrivateDiscussion(DBObject object) {

    int i = 0;/*from   ww  w . jav a 2s .  c o m*/

    PrivateDiscussion pb = new PrivateDiscussion(object.get("userId").toString(),
            object.get("topic").toString(), object.get("content").toString());

    pb.setid(new ObjectId(object.get("_id").toString()));

    pb.setStatus(new Boolean(object.get("status").toString()));

    DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
    try {
        pb.setDate(formatter.parse(object.get("date").toString()));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    BasicDBList tagObject = (BasicDBList) object.get("tags");
    ArrayList<String> tags = null;
    if (tagObject != null) {
        tags = new ArrayList<String>();
        for (i = 0; i < tagObject.size(); i++) {
            tags.add(((BasicDBObject) tagObject.get(i)).get("tag").toString());
        }
    }
    pb.setTags(tags);

    HashMap<String, Integer> votes = null;
    BasicDBList voteObject = (BasicDBList) object.get("votes");
    if (voteObject != null) {
        votes = new HashMap<String, Integer>();
        for (i = 0; i < voteObject.size(); i++) {
            votes.put(((BasicDBObject) voteObject.get(i)).get("username").toString(),
                    (Integer) ((BasicDBObject) voteObject.get(i)).get("vote"));
        }
    }
    pb.setVotes(votes);

    ArrayList<Comment> comments = null;
    BasicDBList commentsObject = (BasicDBList) object.get("comments");
    if (commentsObject != null) {
        comments = new ArrayList<Comment>();
        for (i = 0; i < commentsObject.size(); i++) {
            comments.add(CommentConverter.toComment((BasicDBObject) commentsObject.get(i)));
        }
    }
    pb.setComments(comments);

    pb.setSpam(new Boolean(object.get("isSpam").toString()));

    HashMap<String, String> spamHistory = null;
    BasicDBList spamHistoryObject = (BasicDBList) object.get("spamHistory");
    if (spamHistoryObject != null) {
        spamHistory = new HashMap<String, String>();
        for (i = 0; i < spamHistoryObject.size(); i++) {
            spamHistory.put(((BasicDBObject) spamHistoryObject.get(i)).get("username").toString(),
                    ((BasicDBObject) spamHistoryObject.get(i)).get("reason").toString());
        }
    }
    pb.setSpamHistory(spamHistory);

    ArrayList<String> category = null;
    BasicDBList catObject = (BasicDBList) object.get("category");
    if (catObject != null) {
        category = new ArrayList<String>();
        for (i = 0; i < catObject.size(); i++) {
            category.add(((BasicDBObject) catObject.get(i)).get("cat").toString());
        }
    }
    pb.setCategory(category);

    ArrayList<String> userlist = null;
    BasicDBList userlistObject = (BasicDBList) object.get("userlist");
    if (userlistObject != null) {
        userlist = new ArrayList<String>();
        for (i = 0; i < userlistObject.size(); i++) {
            userlist.add(((BasicDBObject) userlistObject.get(i)).get("username").toString());
        }
    }
    pb.setUserlist(userlist);

    return pb;
}

From source file:tango.dataStructure.Experiment.java

License:Open Source License

public void setStructures(BasicDBList structures, boolean keepProcessingChains) {
    if (keepProcessingChains) {
        for (int i = 0; i < structures.size(); i++) {
            if (i < this.structures.size()) {
                BasicDBObject p = this.getProcessingChain(i);
                ((BasicDBObject) structures.get(i)).append("processingChain", p);
            }//from ww w  . j  a  v a  2 s. c  o  m
        }
    }
    this.data.append("structures", structures);
    this.structures = structures;
}

From source file:tango.dataStructure.Selection.java

License:Open Source License

public void init() {
    if (!this.containsField("nuclei")) {
        nuclei = new BasicDBObject();
        this.append("nuclei", nuclei);
    } else {/*from w  ww.  j a  v a2  s  .c  o  m*/
        Object o = this.get("nuclei");
        if (o instanceof BasicDBList) {
            IJ.log("Warning: Selection: " + this.getName() + " bad structure: level 1");
            nuclei = new BasicDBObject();
            this.append("nuclei", nuclei);
            BasicDBList l = (BasicDBList) o;
            boolean warn2 = false;
            for (String s : l.keySet()) {
                Object n = l.get(s);
                if (n instanceof BasicDBObject)
                    nuclei.append(s, (BasicDBObject) n);
                else if (n instanceof BasicDBList) {
                    nuclei.append(s, toBDO((BasicDBList) n));
                    if (!warn2) {
                        IJ.log("Warning: Selection: " + this.getName() + "nucleus: " + s
                                + " bad structure: level 2");
                        warn2 = true;
                    }
                }
            }
        } else if (o instanceof BasicDBObject) {
            nuclei = (BasicDBObject) o;
        } else {
            nuclei = new BasicDBObject();
            this.append("nuclei", nuclei);
        }
    }
}

From source file:tango.dataStructure.Selection.java

License:Open Source License

static BasicDBObject toBDO(BasicDBList list) {
    BasicDBObject res = new BasicDBObject();
    for (String s : list.keySet())
        res.append(s, list.get(s));
    return res;//from  w w  w  .  j  a  v  a2s  .  c  o  m
}

From source file:tango.gui.DataManager.java

License:Open Source License

private void extractData() {
    HashMap<MultiKey, TreeSet<String>> newC2CKeys = new HashMap<MultiKey, TreeSet<String>>();
    TreeSet<String> NucKeysToAdd = new TreeSet<String>();
    DBCursor cur = mc.getXPNuclei(xp.getName());
    cur.sort(new BasicDBObject("field_id", 1).append("idx", 1));
    int nbNuc = cur.count();
    IJ.log("extract data nb nuc:" + nbNuc);
    objectMes = new HashMap<Integer, TreeMap<MultiKey3D, String>>(ojectKeys.size());
    nbObjects = new TreeMap<MultiKey2D, int[]>();
    nucTags = new TreeMap<MultiKey2D, Integer>();
    nucIds = new TreeMap<MultiKey2D, String>();
    for (int i : ojectKeys.keySet()) {
        if (i < 0) {
            continue;
        }// ww  w  .  j  a  v  a2 s . c  om
        objectMes.put(i, new TreeMap<MultiKey3D, String>());
    }
    if (!ojectKeys.containsKey(0)) {
        objectMes.put(0, new TreeMap<MultiKey3D, String>());
        ojectKeys.put(0, new TreeSet<String>());
    }
    o2oMes = new HashMap<MultiKey, TreeMap<MultiKey4D, String>>();
    for (MultiKey dk : c2cKeys.keySet()) {
        o2oMes.put(dk, new TreeMap<MultiKey4D, String>());
        newC2CKeys.put(dk, new TreeSet<String>());
    }
    while (cur.hasNext()) {
        BasicDBObject nuc = (BasicDBObject) cur.next();
        if (nuc.getInt("tag", 0) < 0) {
            continue; // exclude negative tags
        }
        ObjectId nucId = (ObjectId) nuc.get("_id");
        int nucIdx = nuc.getInt("idx");
        String fieldName = mc.getField((ObjectId) nuc.get("field_id")).getString("name");
        int[] nbPart = new int[channelNames.length];
        //mesure objects
        for (int i = 0; i < channelNames.length; i++) {
            TreeMap<MultiKey3D, String> omes = objectMes.get(i);
            TreeSet<String> keys = ojectKeys.get(i);
            DBCursor cursor = mc.getObjectsCursor(nucId, i);
            cursor.sort(new BasicDBObject("idx", 1));
            nbPart[i] = cursor.count();
            if (keys != null && !keys.isEmpty()) {
                while (cursor.hasNext()) {
                    BasicDBObject o = (BasicDBObject) cursor.next();
                    //IJ.log("o="+o);
                    //IJ.log("omes="+omes);
                    //IJ.log("f="+fieldName+" "+nucIdx+" "+o.getInt("idx")+" "+keys);
                    for (String k : keys) {
                        //IJ.log("k="+k+" "+o.getString(k));
                        if (o.getString(k) != null) {
                            omes.put(new MultiKey3D(fieldName, nucIdx, o.getInt("idx"), k),
                                    o.get(k).toString());
                        }
                    }
                }
            }
            cursor.close();
        }
        String s = "";
        for (int i : nbPart) {
            s += i + ";";
        }
        //IJ.log("nb objects:" + s);
        MultiKey2D k2D = new MultiKey2D(fieldName, nucIdx, "nbParts");
        nbObjects.put(k2D, nbPart);
        nucTags.put(k2D, nuc.getInt("tag", 0));
        nucIds.put(k2D, nuc.getString("_id"));
        //C2C
        TreeMap<MultiKey3D, String> nucMes = objectMes.get(0);
        for (MultiKey dk : c2cKeys.keySet()) {
            if (dk.getKey(0) < 0) {
                continue;
            }
            int size = (dk.getKey(0) != dk.getKey(1)) ? nbPart[dk.getKey(0)] * nbPart[dk.getKey(1)]
                    : nbPart[dk.getKey(0)] * (nbPart[dk.getKey(0)] - 1) / 2;
            BasicDBObject mes = mc.getMeasurementStructure(nucId, dk.getKeys(), true);
            //IJ.log("get mes:" + dk + " mes");
            TreeMap<MultiKey4D, String> o2oMesDk = o2oMes.get(dk);
            TreeSet<String> keys = c2cKeys.get(dk);
            TreeSet<String> newKeys = newC2CKeys.get(dk);

            for (String k : keys) {
                Object o = mes.get(k);
                if (o instanceof BasicDBList) {
                    BasicDBList list = ((BasicDBList) o);
                    if (list.size() == size) {
                        int count = 0;
                        if (dk.getKey(0) != dk.getKey(1)) {
                            for (int p1 = 1; p1 <= nbPart[dk.getKey(0)]; p1++) {
                                for (int p2 = 1; p2 <= nbPart[dk.getKey(1)]; p2++) {
                                    o2oMesDk.put(new MultiKey4D(fieldName, nucIdx, p1, p2, k),
                                            list.get(count).toString());
                                    count++;
                                }
                            }
                        } else {
                            for (int p1 = 1; p1 < nbPart[dk.getKey(0)]; p1++) {
                                for (int p2 = p1 + 1; p2 <= nbPart[dk.getKey(1)]; p2++) {
                                    o2oMesDk.put(new MultiKey4D(fieldName, nucIdx, p1, p2, k),
                                            list.get(count).toString());
                                    count++;

                                }
                            }
                        }
                        newKeys.add(k);
                    }
                } else if (o instanceof Number || o instanceof String) {
                    String newKey = channelNames[dk.getKey(0)] + "." + channelNames[dk.getKey(1)] + "." + k;
                    nucMes.put(new MultiKey3D(fieldName, nucIdx, 1, newKey), o.toString());
                    NucKeysToAdd.add(newKey);
                }
            }
        }
    }
    cur.close();
    this.ojectKeys.get(0).addAll(NucKeysToAdd);
    this.c2cKeys = newC2CKeys;
}

From source file:tango.gui.DataManager.java

License:Open Source License

private void writeC2CMisc(File output, MultiKey dk, String key, String delimiter) {
    try {//  w  w w .  ja  v a2 s .c om
        DBCursor cur = mc.getXPNuclei(xp.getName());
        cur.sort(new BasicDBObject("field_id", 1).append("idx", 1));
        int nbNuc = cur.count();
        FileWriter fstream = new FileWriter(output);
        BufferedWriter out = new BufferedWriter(fstream);
        String headers = "nucId" + delimiter + "field" + delimiter + "nuc.idx" + delimiter + "idx" + delimiter
                + key;
        out.write(headers);
        out.newLine();
        while (cur.hasNext()) {
            BasicDBObject nuc = (BasicDBObject) cur.next();
            ObjectId nucId = (ObjectId) nuc.get("_id");
            int nucIdx = nuc.getInt("idx");
            String fieldName = mc.getField((ObjectId) nuc.get("field_id")).getString("name");
            String line = nucId + delimiter + fieldName + delimiter + nucIdx + delimiter;
            //C2C
            BasicDBObject mes = mc.getMeasurementStructure(nucId, dk.getKeys(), true);
            Object o = mes.get(key);
            if (o instanceof BasicDBList) {
                BasicDBList list = ((BasicDBList) o);
                for (int i = 0; i < list.size(); i++) {
                    out.write(line + i + delimiter + list.get(i));
                    out.newLine();
                }
            } else if (o instanceof Number || o instanceof String) {
                out.write(line + "1" + delimiter + o.toString());
                out.newLine();
            }
        }
        out.close();
        cur.close();
    } catch (Exception e) {
        exceptionPrinter.print(e, "extract key: " + key, Core.GUIMode);
    }
}