Example usage for com.mongodb DBCursor next

List of usage examples for com.mongodb DBCursor next

Introduction

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

Prototype

@Override
public DBObject next() 

Source Link

Document

Returns the object the cursor is at and moves the cursor ahead by one.

Usage

From source file:biopolis.headless.BiopolisSegmentationManagement.java

public void iterationPhase(DBCursor cur) throws SQLException, BiopolisGeneralException {
    java.util.Date date = this.updateTTL();
    while (cur.hasNext()) {
        Long name = (Long) cur.next().get("biopolisdata");
        BasicDBObject place = new BasicDBObject();
        place.put("biopolishandle", this.biopiolisid);
        place.put("biopolisdata", name);
        place.put("biopolisttl", date);
        dbcol.insert(place);// w w  w .  j a v a 2  s.  co  m
    }
    this.occurs++;
}

From source file:biopolis.headless.BiopolisSegmentationManagement.java

public List<Long> search(BiopolisSegmentQuery seg) throws BiopolisGeneralException {
    byte[] bytes = Base64.decodeBase64(seg.biopolisid);
    ObjectId someid = new ObjectId(bytes);
    DBCursor cur = dbcol.find(new BasicDBObject("_id", someid));
    if (!cur.hasNext()) {
        throw new BiopolisGeneralException("Search not exists with id " + seg.biopolisid);
    }//from w w w .  j  av a  2 s .c o  m
    BasicDBObject setter = new BasicDBObject("$set", new BasicDBObject("biopolisttl", new java.util.Date()));
    dbcol.update(new BasicDBObject("_id", someid), setter);
    dbcol.update(new BasicDBObject("biopolisid", seg.biopolisid), setter);

    List<Long> resus = new ArrayList<Long>();
    cur = dbcol.find(new BasicDBObject("biopolisid", seg.biopolisid));
    cur = cur.sort(new BasicDBObject("biopolisdata", 1));

    int count = 0;
    while (cur.hasNext()) {
        System.out.println("scan " + seg.from);
        DBObject obj = cur.next();
        if ((count >= seg.from) && (count < seg.from + this.segSZ)) {
            System.out.println("add");
            Long i = (Long) obj.get("biopolisdata");
            resus.add(i);
        }
        count++;
    }
    System.out.println(resus.size());
    return resus;
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static List<Integer> getIsisCcFields(final String mstName, final DBCollection coll) throws IOException {
    assert mstName != null;
    assert coll != null;

    final List<Integer> lst = new ArrayList<Integer>();
    final BasicDBObject query = new BasicDBObject(MST_FIELD, mstName);
    final DBCursor cursor = coll.find(query);

    if (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final BasicDBList flds = (BasicDBList) obj.get(CC_TAGS_FIELD);
        if (flds.isEmpty()) {
            throw new IOException("Missing CCs field");
        }// w w  w.  j a va2  s . c  o m
        for (Object tag : flds) {
            lst.add((Integer) tag);
        }
    } else {
        throw new IOException("Missing collection: " + coll.getName());
    }
    cursor.close();

    return lst;
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static boolean removeOldDocs(final DBCollection coll) {
    assert coll != null;

    final Date now = new Date();
    final DBCursor cursor = coll.find();
    boolean ret = true;

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final Date auxDate = obj.getDate(LAST_UPDATE_FIELD);
        if ((auxDate == null) || (now.getTime() - auxDate.getTime()) > 60 * 60 * 1000) {
            final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED);
            ret = ret && wr.getCachedLastError().ok();
        }//from   ww  w  .  jav  a2  s  .  c om
    }
    return ret;
}

From source file:br.bireme.scl.CopyMongoDb.java

License:Open Source License

private static void copyDB(final String from_host, final String to_host, final String from_db,
        final String to_db, final String from_port, final String to_port, final boolean appendCollections,
        final boolean displayAllIds) throws UnknownHostException, IOException {
    assert from_host != null;
    assert to_host != null;
    assert from_db != null;
    assert to_db != null;
    assert from_port != null;
    assert to_port != null;

    final int MAX_LOOP_SIZE = 15000; // MongoException$CursorNotFound
    final MongoClient fromClient = new MongoClient(from_host, Integer.parseInt(from_port));
    final MongoClient toClient = new MongoClient(to_host, Integer.parseInt(to_port));
    final DB fromDb = fromClient.getDB(from_db);
    final DB toDb = toClient.getDB(to_db);

    if (!appendCollections) {
        toDb.dropDatabase();//from   w  w w  . j a  v  a2 s . c  om
    }

    final Set<String> colls = fromDb.getCollectionNames();

    for (String cname : colls) {
        if (cname.equals("system.indexes")) {
            continue;
        }

        final DBCollection fromColl = fromDb.getCollection(cname);
        final DBCollection toColl = toDb.getCollection(cname);

        DBCursor cursor = fromColl.find();
        int curr = 0;

        System.out.println("Copying collection: " + cname);

        while (cursor.hasNext()) {
            if (curr % MAX_LOOP_SIZE == 0) {
                if (curr > 0) {
                    cursor.close();
                    cursor = fromColl.find().skip(curr);
                    if (!cursor.hasNext()) {
                        throw new IOException("hasNext() failed");
                    }
                }
            }
            final DBObject doc = cursor.next();
            final WriteResult ret = toColl.save(doc, WriteConcern.ACKNOWLEDGED);

            if (!ret.getCachedLastError().ok()) {
                System.err.println("write error doc id=" + doc.get("_id"));
            }
            if (++curr % 1000 == 0) {
                System.out.println("+++" + curr);
            }
            if (displayAllIds) {
                System.out.println(" id=" + doc.get("_id"));
            }
        }
        cursor.close();

        System.out.println();
    }
}

From source file:br.bireme.scl.Gizmo.java

License:Open Source License

Collection<Element> getNotExportedElements(final DBCollection coll, final DBCursor cursor) throws IOException {
    assert coll != null;
    assert cursor != null;

    final Collection<Element> col = new ArrayList<Element>();

    while (cursor.hasNext()) {
        final BasicDBObject obj = (BasicDBObject) cursor.next();
        final String id = obj.getString(ID_FIELD);
        final BasicDBList lst = (BasicDBList) obj.get(ELEM_LST_FIELD);
        if (lst == null) {
            throw new NullPointerException("Elem list espected");
        }//  w w  w  . j  ava2 s. com
        final BasicDBObject lelem = (BasicDBObject) lst.get(0);
        if (lelem == null) {
            throw new NullPointerException("Elem element espected");
        }
        if (!lelem.getBoolean(EXPORTED_FIELD)) {
            final Element elem = new Element(id, lelem.getString(BROKEN_URL_FIELD),
                    lelem.getString(PRETTY_BROKEN_URL_FIELD), lelem.getString(FIXED_URL_FIELD),
                    obj.getString(MST_FIELD), lelem.getDate(LAST_UPDATE_FIELD).toString(),
                    lelem.getString(USER_FIELD), null, false);
            col.add(elem);

            lelem.put(EXPORTED_FIELD, true);
            final WriteResult res = coll.save(obj, WriteConcern.ACKNOWLEDGED);

            if (!res.getCachedLastError().ok()) {
                throw new IOException("write doc[" + obj.getString(ID_FIELD) + "] failed");
            }
        }
    }

    return col;
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static Set<String> getCenters(final DBCollection coll) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }/*from   www  . ja va2s . co  m*/
    final Set<String> set = new TreeSet<String>();
    final DBCursor cursor = coll.find();

    while (cursor.hasNext()) {
        final BasicDBList lst = (BasicDBList) cursor.next().get(CENTER_FIELD);
        set.add((String) lst.get(0));
    }
    cursor.close();

    return set;
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

/**
 * Obtem uma lista com objetos IdUrl obtidos da base de dados MongoDb
 * @param coll coleo onde esto as urls//from w  ww.j  av a2  s . c o  m
 * @param centerIds filtro dos centros colaboradores desejados. Nunca  nulo
 * @param filter se no nulo filtra as urls com um cc especfico
 * @param from indice inicial da lista a ser recuperado. Comea de 1.
 * @param count numero de elementos a serem devolvidos
 * @param ascendingOrder se retorna por ordem de data ascendente ou descendente
 * @return lista de objetos IdUrl
 */
private static List<IdUrl> getCenterUrls(final DBCollection coll, final Set<String> centerIds,
        final String filter, final int from, final int count, final boolean ascendingOrder) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }
    if (centerIds == null) {
        throw new NullPointerException("centerIds");
    }
    if (centerIds.isEmpty()) {
        throw new IllegalArgumentException("empty centerIds");
    }
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    //final Set<IdUrl> lst = new TreeSet<IdUrl>();
    final List<IdUrl> lst = new ArrayList<IdUrl>();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    final BasicDBObject sort = new BasicDBObject(DATE_FIELD, ascendingOrder ? 1 : -1);

    if (filter == null) {
        final BasicDBList cclst = new BasicDBList();

        for (String centerId : centerIds) {
            cclst.add(centerId);
        }
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        final BasicDBObject query = new BasicDBObject(CENTER_FIELD, in);
        //final DBCursor cursor = coll.find(query).skip(from - 1).limit(count);
        final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
        while (cursor.hasNext()) {
            final DBObject doc = cursor.next();
            final BasicDBList ccsLst = (BasicDBList) doc.get(CENTER_FIELD);
            final Set<String> ccs = new TreeSet<String>();

            for (Object cc : ccsLst) {
                ccs.add((String) cc);
            }

            final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD),
                    ccs, format.format((Date) (doc.get(DATE_FIELD))), (String) doc.get(MST_FIELD));
            lst.add(iu);
        }
        cursor.close();
    } else {
        final BasicDBObject query = new BasicDBObject(CENTER_FIELD, filter);
        final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);

        while (cursor.hasNext()) {
            final DBObject doc = cursor.next();
            final Set<String> ccs = new TreeSet<String>();
            ccs.add(filter);

            final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD),
                    ccs, format.format((Date) doc.get(DATE_FIELD)), (String) doc.get(MST_FIELD));
            lst.add(iu);
        }
        cursor.close();
    }
    return lst;
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static SearchResult getDocuments(final DBCollection coll, final String docMast, final String docId,
        final String docUrl, final Set<String> centerIds, final boolean decreasingOrder, final int from,
        final int count) {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//from  ww w  .  ja  va 2s. com
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    final List<IdUrl> lst = new ArrayList<IdUrl>();
    final BasicDBObject query = new BasicDBObject();

    if (docMast != null) {
        query.append(MST_FIELD, docMast);
    }
    if (docId != null) {
        final Pattern pat = Pattern.compile("^" + docId.trim() + "_\\d+");
        query.append(ID_FIELD, pat);
    }
    if (docUrl != null) {
        query.append(BROKEN_URL_FIELD, docUrl.trim());
    }
    if (centerIds != null) {
        final BasicDBList cclst = new BasicDBList();
        for (String centerId : centerIds) {
            cclst.add(centerId);
        }
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        query.append(CENTER_FIELD, in);
    }
    final BasicDBObject sort = new BasicDBObject(DATE_FIELD, decreasingOrder ? -1 : 1);
    final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
    final int size = cursor.count();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    while (cursor.hasNext()) {
        final DBObject doc = cursor.next();
        final BasicDBList ccsLst = (BasicDBList) doc.get(CENTER_FIELD);
        final Set<String> ccs = new TreeSet<String>();

        for (Object cc : ccsLst) {
            ccs.add((String) cc);
        }
        final IdUrl iu = new IdUrl((String) doc.get(ID_FIELD), (String) doc.get(PRETTY_BROKEN_URL_FIELD), ccs,
                format.format((Date) (doc.get(DATE_FIELD))), (String) doc.get(MST_FIELD));
        lst.add(iu);
    }
    cursor.close();

    return new SearchResult(size, lst);
}

From source file:br.bireme.scl.MongoOperations.java

License:Open Source License

public static SearchResult2 getHistoryDocuments(final DBCollection coll, final Element elem, final int from,
        final int count) throws IOException, ParseException {
    if (coll == null) {
        throw new NullPointerException("coll");
    }//  ww w  .j  a va2 s .  c om
    if (elem == null) {
        throw new NullPointerException("elem");
    }
    if (from < 1) {
        throw new IllegalArgumentException("from[" + from + "] < 1");
    }
    if (count < 1) {
        throw new IllegalArgumentException("count[" + count + "] < 1");
    }
    final List<Element> lst = new ArrayList<Element>();
    final BasicDBObject query = new BasicDBObject();
    final String root = ELEM_LST_FIELD + ".0.";
    final String updated = root + LAST_UPDATE_FIELD;

    if (elem.getDbase() != null) {
        query.append(MST_FIELD, elem.getDbase().trim());
    }
    if (elem.getId() != null) {
        final Pattern pat = Pattern.compile("^" + elem.getId().trim() + "_\\d+");
        query.append(ID_FIELD, pat);
    }
    if (elem.getFurl() != null) {
        query.append(root + FIXED_URL_FIELD, elem.getFurl().trim());
    }
    if (!elem.getCcs().isEmpty()) {
        final BasicDBList cclst = new BasicDBList();
        for (String centerId : elem.getCcs()) {
            cclst.add(centerId.trim());
        }
        final String cc = root + CENTER_FIELD;
        final BasicDBObject in = new BasicDBObject("$in", cclst);
        query.append(cc, in);
    }
    if (elem.getDate() != null) {
        final SimpleDateFormat simple = new SimpleDateFormat("dd-MM-yyyy");
        final Date date = simple.parse(elem.getDate().trim());
        final BasicDBObject qdate = new BasicDBObject("$gte", date);
        query.append(updated, qdate);
    }
    if (elem.getUser() != null) {
        final String user = root + USER_FIELD;
        query.append(user, elem.getUser().trim());
    }

    final BasicDBObject sort = new BasicDBObject(updated, -1);
    final DBCursor cursor = coll.find(query).sort(sort).skip(from - 1).limit(count);
    final int size = cursor.count();
    final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    while (cursor.hasNext()) {
        final BasicDBObject hdoc = (BasicDBObject) cursor.next();
        final BasicDBList elst = (BasicDBList) hdoc.get(ELEM_LST_FIELD);
        final BasicDBObject hcurdoc = (BasicDBObject) elst.get(0);
        if (hcurdoc == null) {
            throw new IOException("document last element found.");
        }
        final BasicDBList ccLst = (BasicDBList) hcurdoc.get(CENTER_FIELD);
        final List<String> ccs = Arrays.asList(ccLst.toArray(new String[0]));
        final Element elem2 = new Element(hdoc.getString(ID_FIELD), hcurdoc.getString(BROKEN_URL_FIELD),
                hcurdoc.getString(PRETTY_BROKEN_URL_FIELD), hcurdoc.getString(FIXED_URL_FIELD),
                hdoc.getString(MST_FIELD), format.format((Date) (hcurdoc.get(LAST_UPDATE_FIELD))),
                hcurdoc.getString(USER_FIELD), ccs, hcurdoc.getBoolean(EXPORTED_FIELD));
        lst.add(elem2);
    }
    cursor.close();

    return new SearchResult2(size, lst.size(), lst);
}