Example usage for com.mongodb DBCursor sort

List of usage examples for com.mongodb DBCursor sort

Introduction

In this page you can find the example usage for com.mongodb DBCursor sort.

Prototype

public DBCursor sort(final DBObject orderBy) 

Source Link

Document

Sorts this cursor's elements.

Usage

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;
        }/*from   w  w  w.j a  v a 2 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 {/*from w ww  .j  a  v a2s  .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);
    }
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized ArrayList<Selection> getSelections(ObjectId xpId) {
    if (selection == null) {
        return null;
    }// ww w  . ja  v a2 s.c om
    BasicDBObject query = new BasicDBObject("experiment_id", xpId);
    DBCursor cursor = selection.find(query);
    cursor.sort(new BasicDBObject("name", 1));
    ArrayList<Selection> res = new ArrayList<Selection>(cursor.size());
    while (cursor.hasNext()) {
        Selection s = (Selection) cursor.next();
        s.init();
        res.add(s);
    }
    cursor.close();
    return res;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized BasicDBObject[] getObjectsArray(ObjectId nucleusId, int channelIdx,
        ArrayList<String> fields) {
    BasicDBObject query = new BasicDBObject("nucleus_id", nucleusId).append("channelIdx", channelIdx);
    DBCursor cursor;
    if (fields == null || fields.isEmpty()) {
        cursor = object3D.find(query);/*from  ww  w.j a v  a 2  s  .c  o  m*/
    } else {
        BasicDBObject f = new BasicDBObject();
        for (String s : fields) {
            f.append(s, 1);
        }
        cursor = object3D.find(query, f);
    }
    cursor.sort(new BasicDBObject("idx", 1));
    BasicDBObject[] res = new BasicDBObject[cursor.size()];
    int count = 0;
    while (cursor.hasNext()) {
        res[count] = (BasicDBObject) cursor.next();
    }
    cursor.close();
    return res;
}

From source file:tourapi.TourAPI.java

/**
 * @param args the command line arguments
 *//*from  ww  w .  jav a 2s  . c  om*/

private tour_Information findwithDB(String from) {

    tour_Information returntour = new tour_Information();
    String to = null;

    try {
        MongoClient mongoClient = getMongoClient();
        DB db = mongoClient.getDB("my_database");
        DBCollection coll = db.getCollection("ML_Result");
        WriteConcern w = new WriteConcern(1, 0);
        mongoClient.setWriteConcern(w);

        // string?  select
        BasicDBObject query = new BasicDBObject();
        query.put("from", from);

        DBCursor cursor = coll.find(query);
        cursor.sort(new BasicDBObject("num", -1));

        int num = -1;
        System.out.println("from 0: " + from);
        if (cursor.hasNext())// ??  
        {
            DBObject mapObj = cursor.next();
            String temp = mapObj.get("to").toString();
            num = Integer.parseInt(temp);
            //num = ((Number) mapObj.get("to")).intValue();
            cursor.close();

        } else {
            //System.out.println("from2 : "+from);
            returntour.myURL = null;
        }
        System.out.println(coll.getCount());
        // close resources
        if (num != -1) {
            db = mongoClient.getDB("my_database");
            coll = db.getCollection("TB_titleID");
            w = new WriteConcern(1, 0);// ? ,   2000 // ? 2 ??  ? ?
            mongoClient.setWriteConcern(w);

            // string?  select
            query = new BasicDBObject();
            //
            query.put("ID", num);
            //query.put("to",to);
            cursor = coll.find(query);
            if (cursor.hasNext())// ?? 
            {
                System.out.println("num : " + num);
                DBObject mapObj = cursor.next();
                returntour.myURL = mapObj.get("url").toString();
                returntour.myLocation = Integer.parseInt(mapObj.get("num").toString());
                cursor.close();
            } else {
                returntour.myURL = null;
                returntour.myLocation = 0;
            }
        }
        mongoClient.close();
    } catch (Exception e) {
        System.err.println("findwithDB Exception");
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return returntour;
}

From source file:v7cr.vaadin.DBCollectionContainer.java

License:Open Source License

public DBCollectionContainer(SchemaDefinition schema, DBCollection collection, DBObject filter, String sortBy,
        boolean ascending) {
    this.collection = collection;
    DBObject _null = new BasicDBObject();
    if (filter == null)
        filter = _null;/* ww  w .  j  a v a  2s.c om*/
    DBCursor cursor = collection.find(filter, _null);
    if (sortBy != null) {
        cursor = cursor.sort(new BasicDBObject(sortBy, ascending ? 1 : -1));
    }
    _ids = initIds(cursor);
    initPropertyIds(schema);
}