Example usage for com.mongodb DBCursor close

List of usage examples for com.mongodb DBCursor close

Introduction

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

Prototype

@Override
    public void close() 

Source Link

Usage

From source file:bd.DVenta.java

@Override
public ArrayList listar() {
    ArrayList datos = new ArrayList();
    CVenta x = new CVenta();
    conecion con = new conecion(table);
    DBCursor cursor = con.get_colletion().find();
    try {/*from   ww  w  . j ava2 s  .co m*/
        while (cursor.hasNext()) {
            x = new CVenta();
            BasicDBObject agg = (BasicDBObject) cursor.next();
            x.set_datos((HashMap) agg.toMap());
            x.setId((String) agg.getString("_id"));
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    return datos;
}

From source file:bd.DVenta.java

@Override
public Object buscar_id(String id_find) {
    ArrayList datos = new ArrayList();
    CVenta x = new CVenta();
    conecion con = new conecion(table);
    BasicDBObject id = new BasicDBObject("_id", new ObjectId(id_find));
    DBCursor cursor = con.get_colletion().find(id);

    try {/* w  ww.  j  a v  a2  s.  c  om*/
        while (cursor.hasNext()) {
            x = new CVenta();
            x.set_datos((HashMap) cursor.next().toMap());
            datos.add(x);
        }
    } finally {
        cursor.close();
    }
    con.end();
    if (datos.size() == 0)
        return null;
    return datos.get(0);
}

From source file:bd.DVenta.java

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

    try {//from w  w  w .j a v a2s  .  c o  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: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 a  v  a  2  s. com*/
        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: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");
        }//  www.  java2s .com
        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.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  .ja  v  a 2 s.  co  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> loadRecords(final String host, final int port, final String user, final String password)
        throws IOException {
    assert host != null;
    assert port > 0;

    final MongoClient mongoClient = new MongoClient(host, port);
    final DB db = mongoClient.getDB(SOCIAL_CHECK_DB);

    if ((user != null) && (!user.trim().isEmpty())) {
        final boolean auth = db.authenticate(user, password.toCharArray());
        if (!auth) {
            throw new IllegalArgumentException("invalid user/password");
        }/*from  w  ww.  ja  v  a  2 s  .  c  om*/
    }

    final DBCollection coll = db.getCollection(HISTORY_COL);
    final String fldName = ELEM_LST_FIELD + ".0." + EXPORTED_FIELD;
    final BasicDBObject query = new BasicDBObject(fldName, false);
    final DBCursor cursor = coll.find(query);
    final Collection<Element> col = getNotExportedElements(coll, cursor);

    cursor.close();

    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.j a  v  a 2 s.  com
    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 w  w  .  j  av a2s. 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 www.  j  av  a  2s .c  om
    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);
}