Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

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

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:bd.DVenta.java

@Override
public ArrayList listar(HashMap map) {
    ArrayList datos = new ArrayList();
    CVenta x = new CVenta();
    conecion con = new conecion(table);
    BasicDBObject id = new BasicDBObject(map);
    DBCursor cursor = con.get_colletion().find(id);

    try {//w  w w.j  ava2s.co  m
        while (cursor.hasNext()) {
            x = new CVenta();
            x.set_datos((HashMap) cursor.next().toMap());
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();

    return datos;
}

From source file:biopolis.headless.BiopolisSegmentationManagement.java

public java.util.Date updateTTL() throws BiopolisGeneralException {
    java.util.Date date = new java.util.Date();
    DBCursor cur = dbcol.find(new BasicDBObject("_id", id));
    if (!cur.hasNext()) {
        throw new BiopolisGeneralException("Search trashed");
    }//from   w  w  w  .j a  v a 2 s  .co  m
    BasicDBObject setter = new BasicDBObject("$set", new BasicDBObject("biopolisttl", date));
    dbcol.update(new BasicDBObject("_id", id), setter);
    dbcol.update(new BasicDBObject("biopolishandle", this.biopiolisid), setter);
    return date;
}

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);//from  w  ww  .  j a  v a 2s  . 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);
    }/*ww  w.jav  a 2s. 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");
        }/*from   w ww .  j a  va 2 s  .  co 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();
        }//w  w w . j av  a  2  s. com
    }
    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();/*ww  w .  j  a  va 2  s. c o  m*/
    }

    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");
        }/*from   w w w  .ja  va2  s.  co  m*/
        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 w ww  .  ja va2s .c o 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  www. ja v a2s  .  c om*/
 * @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;
}