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:net.ymate.platform.persistence.mongodb.impl.MongoGridFSSession.java

License:Apache License

public List<GridFSDBFile> find(Query query, OrderBy orderBy, Page page) {
    DBCursor _cursor = __dbCollection.find(query.toBson());
    if (orderBy != null) {
        _cursor.sort(orderBy.toBson());// ww w .  j a v a 2s  .  co  m
    }
    if (page != null && page.page() > 0 && page.pageSize() > 0) {
        _cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
    }
    List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
    while (_cursor.hasNext()) {
        _results.add((GridFSDBFile) _cursor.next());
    }
    _cursor.close();
    return _results;
}

From source file:net.ymate.platform.persistence.mongodb.support.MongoEntitySupport.java

License:Apache License

public static <T> List<T> randerToEntities(Class<T> entity, DBCursor cursor) {
    List<T> _resultList = new ArrayList<T>();
    while (cursor.hasNext()) {
        DBObject _object = cursor.next();
        _resultList.add(randerToEntity(entity, _object));
    }/*from w  w w . j  a  v a 2s  . co  m*/
    cursor.close();
    return _resultList;
}

From source file:NexT.db.mongo.DataModel.java

License:GNU General Public License

/**
 * Attempt to retrieve records from the specified collection using the
 * given query. If no records were found, null is returned instead.
 * @param collection The collection to query
 * @param query A DBObject representing the query parameters.
 * @param from From offset.//from   ww w .j  a  v a 2 s . c om
 * @param limit Max. number of entries.
 * @return Null if no documents were found or an array of records.
 * @throws MongoException Thrown if the MongoWrapper has not been
 * initialized yet or if there was an error reading the objects from the db.
 */
public static DataModel[] getData(String collection, DBObject query, int from, int limit)
        throws MongoException {
    MongoWrapper wrapper = MongoWrapper.getInstance();
    if (wrapper == null)
        throw new MongoException("MongoWrapper has not been initiated.");
    DBCollection col = wrapper.getCollection(collection);
    DBCursor cursor = col.find(query).skip(from);
    if (limit != -1)
        cursor = cursor.limit(limit);
    List<DBObject> modelsList;
    DataModel[] models;
    try {
        modelsList = cursor.toArray();
        models = new DataModel[modelsList.size()];
        for (int i = 0; i < models.length; i++) {
            models[i] = new DataModel(col, modelsList.get(i));
        }
    } catch (Exception ex) {
        throw new MongoException("Error while retrieving Objects", ex);
    } finally {
        cursor.close();
    }
    if (models.length == 0)
        return null;
    else
        return models;
}

From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoDB.java

License:Open Source License

/**
 * Closes the specified cursor./*from  w ww .j  a  v  a2 s.co  m*/
 */
public void closeCursor(DBCursor cursor) throws StorageException {
    if (cursor != null) {
        try {
            cursor.close();
        } catch (MongoException e) {
            throw new StorageException(e);
        }
    }
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * /*  w  ww. j  a va 2 s.co  m*/
 * @return
 */
public ArrayList<Club> getClubsFromDb() {

    ArrayList<Club> clubs = new ArrayList<>();
    DBCollection dbCollectionClubs = db.getCollection("clubs");

    DBCursor cursor = dbCollectionClubs.find();

    BasicDBObject mongoObject = new BasicDBObject();

    if (cursor == null)
        System.out.println("Cursor er null");

    try {
        while (cursor.hasNext()) {
            mongoObject = (BasicDBObject) cursor.next();
            clubs.add(new Club(mongoObject.getObjectId("_id"), mongoObject.getInt("id"),
                    mongoObject.getString("melwinId"), mongoObject.getString("name"),
                    mongoObject.getString("chiefInstructorMelwinId")));

        }
    } finally {
        cursor.close();
    }
    return clubs;
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * /*w ww . ja v  a 2  s  . c om*/
 * @return
 */
public ArrayList<License> getLicensesFromDb() {
    ArrayList<License> licenses = new ArrayList<>();

    DBCollection dbCollectionLicenses = db.getCollection("licenses");

    DBCursor cursor = dbCollectionLicenses.find();

    BasicDBObject mongoObject = new BasicDBObject();

    if (cursor == null)
        System.out.println("Cursor er null, licenses");

    try {
        while (cursor.hasNext()) {
            mongoObject = (BasicDBObject) cursor.next();
            licenses.add(new License(mongoObject.getObjectId("_id"), mongoObject.getInt("id"),
                    mongoObject.getString("melwinId"), mongoObject.getString("licenseName"), null, null));
        }
    } finally {
        cursor.close();
    }
    return licenses;
}

From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java

/**
 * //from  w ww . j av a  2  s.c  om
 * @param parachutistsFromMelwin
 */
public ArrayList<Parachutist> getParachutistsFromDB() {
    // HjelpeLister
    ArrayList<Club> clubs = getClubsFromDb();
    ArrayList<License> licenses = getLicensesFromDb();

    ArrayList<Parachutist> parachutistsInMongoDB = new ArrayList<>();

    DBCollection dbCollectionParachutists = db.getCollection("jumpers");

    DBCursor cursor = dbCollectionParachutists.find();

    BasicDBObject mongoObject = new BasicDBObject();

    try {
        int hoppteller = 0;
        while (cursor.hasNext()) {
            mongoObject = (BasicDBObject) cursor.next();
            hoppteller++;
            ArrayList<Club> memberClubs = new ArrayList<>();
            ArrayList<License> memberLicenses = new ArrayList<>();

            BasicDBList referenceClubs = (BasicDBList) mongoObject.get("memberclubs");
            BasicDBList referenceLicenses = (BasicDBList) mongoObject.get("licenses");

            for (Object clubReference : referenceClubs) {
                for (Club club : clubs) {
                    if ((int) clubReference == club.getId()) {
                        memberClubs.add(club);
                    }
                }
            }

            for (Object licenseReference : referenceLicenses) {
                for (License license : licenses) {
                    if ((int) licenseReference == license.getId()) {
                        memberLicenses.add(license);
                    }
                }

            }

            Parachutist parachutist = new Parachutist(mongoObject.getObjectId("_id"), mongoObject.getInt("id"),
                    mongoObject.getString("melwinId"), null, // nakKey
                    new ArrayList<Club>(), // clubs
                    new ArrayList<License>(), // licenses
                    mongoObject.getString("firstname"), mongoObject.getString("lastname"),
                    mongoObject.getDate("birthdate"), mongoObject.getString("gender"),
                    mongoObject.getString("street"), mongoObject.getString("postnumber"),
                    mongoObject.getString("postplace"), mongoObject.getString("mail"),
                    mongoObject.getString("phone"), mongoObject.getString("password"));

            parachutist.setMemberclubs(memberClubs);
            parachutist.setLicenses(memberLicenses);

            parachutistsInMongoDB.add(parachutist);

        }
    } finally {
        cursor.close();
    }
    return parachutistsInMongoDB;
}

From source file:org.alfresco.bm.api.v1.StatusAPI.java

License:Open Source License

@GET
@Path("/logs")
@Produces(MediaType.APPLICATION_JSON)/*  w w w  .  ja va  2  s. c o  m*/
public String getLogs(@QueryParam("driverId") String driverId, @QueryParam("test") String test,
        @QueryParam("run") String run, @DefaultValue("INFO") @QueryParam("level") String levelStr,
        @DefaultValue("0") @QueryParam("from") Long from,
        @DefaultValue("" + Long.MAX_VALUE) @QueryParam("to") Long to,
        @DefaultValue("0") @QueryParam("skip") int skip, @DefaultValue("50") @QueryParam("count") int count) {
    if (logger.isDebugEnabled()) {
        logger.debug("Inbound: " + "[driverId:" + driverId + ",test:" + test + ",run:" + run + ",level:"
                + levelStr + ",from:" + new Date(from) + ",to:" + new Date(to) + ",skip:" + skip + ",count:"
                + count + "]");
    }
    LogLevel level = LogLevel.INFO;
    try {
        level = LogLevel.valueOf(levelStr);
    } catch (Exception e) {
        // Just allow this
    }

    DBCursor cursor = null;
    try {
        String json = "[]";
        cursor = logService.getLogs(driverId, test, run, level, from, to, skip, count);
        if (cursor.count() > 0) {
            json = JSON.serialize(cursor);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Outbound: " + json);
        }
        return json;
    } catch (WebApplicationException e) {
        throw e;
    } catch (Exception e) {
        throwAndLogException(Status.INTERNAL_SERVER_ERROR, e);
        return null;
    } finally {
        if (cursor != null) {
            try {
                cursor.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Turn a cursor into an array of API-friendly objects
 *///w  ww  .j  a  v a2  s  .  c o m
protected List<FolderData> fromDBCursor(DBCursor cursor) {
    int count = cursor.count();
    try {
        List<FolderData> folderDatas = new ArrayList<FolderData>(count);
        while (cursor.hasNext()) {
            DBObject folderDataObj = cursor.next();
            FolderData folderData = fromDBObject(folderDataObj);
            folderDatas.add(folderData);
        }
        // Done
        return folderDatas;
    } finally {
        cursor.close();
    }
}

From source file:org.alfresco.bm.devicesync.dao.mongo.MongoMetricsService.java

License:Open Source License

@Override
public Stream<Metrics> getMetrics(int skip, int limit) {
    DBObject query = BasicDBObjectBuilder.start().get();
    DBObject orderBy = BasicDBObjectBuilder.start("timestampMS", 1).get();
    DBCursor cur = collection.find(query).sort(orderBy).skip(skip).limit(limit);
    Stream<Metrics> stream = StreamSupport.stream(cur.spliterator(), false).onClose(() -> cur.close()) // need to close cursor;
            .map(dbo -> Metrics.fromDBObject(dbo));
    return stream;
}