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:org.semispace.semimeter.dao.mongo.SemiMeterDaoMongo.java

License:Apache License

private void hourlySumsMemory(final Map<String, GroupedResult> result, final DBObject query) {
    DBCursor dbResult = mongoTemplate.getCollection("meter").find(query);
    try {//from   ww  w. j a  v  a2 s.  c  o  m
        while (dbResult.hasNext()) {
            DBObject row = dbResult.next();
            DBObject day = (DBObject) row.get("day");
            DBObject hours = (DBObject) day.get("hours");
            String type = (String) row.get("type");
            for (String hourString : hours.keySet()) {
                DBObject hourObject = (DBObject) hours.get(hourString);
                DBObject minutes = (DBObject) hourObject.get("minutes");
                for (String minuteString : minutes.keySet()) {
                    DBObject minuteObject = (DBObject) minutes.get(minuteString);
                    Integer count = (Integer) minuteObject.get("count");
                    GroupedResult gr;
                    if (result.containsKey(minuteString)) {
                        gr = result.get(minuteString);
                    } else {
                        gr = new GroupedResult();
                        Long ts = Long.valueOf(minuteString);
                        String time = df.format(new Date(ts));
                        gr.setKey(time);
                        gr.setKeyName("minute");
                        gr.setCount(0);
                        result.put(minuteString, gr);
                    }
                    gr.setCount(gr.getCount() + count);
                    if (gr.getSplitCounts().containsKey(type)) {
                        gr.getSplitCounts().put(type, gr.getSplitCounts().get(type) + count);
                    } else {
                        gr.getSplitCounts().put(type, count);
                    }
                }
            }
        }
    } finally {
        dbResult.close();
    }

}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
// TODO use skip and maxItems
public Stream<Entity<String>> getNames(Node node, int skip, int maxItems) {
    String nodeId = node.getNodeId();
    String nodeVersion = node.getVersionLabel();

    Collection<Entity<String>> ret = new LinkedList<>();

    QueryBuilder queryBuilder = QueryBuilder.start("n").is(nodeId).and("v").is(nodeVersion);
    DBObject query = queryBuilder.get();

    BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("nm", 1);
    DBObject orderBy = orderByBuilder.get();

    DBCursor cursor = entitiesData.find(query).sort(orderBy);
    try {/*from w  ww  .  ja va 2  s  .  c o m*/
        for (DBObject dbObject : cursor) {
            String name = (String) dbObject.get("nm");
            int count = (Integer) dbObject.get("c");
            String type = map.get("nm");
            Entity<String> entity = new Entity<>(EntityType.valueOf(type), name, count);
            ret.add(entity);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return ret.stream();
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
// TODO use skip and maxItems
public Stream<Entity<String>> getOrgs(Node node, int skip, int maxItems) {
    String nodeId = node.getNodeId();
    String nodeVersion = node.getVersionLabel();

    Collection<Entity<String>> ret = new LinkedList<>();

    QueryBuilder queryBuilder = QueryBuilder.start("n").is(nodeId).and("v").is(nodeVersion);
    DBObject query = queryBuilder.get();

    BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("o", 1);
    DBObject orderBy = orderByBuilder.get();

    DBCursor cursor = entitiesData.find(query).sort(orderBy);
    try {//from w  ww  .j av a 2  s.  c  o m
        for (DBObject dbObject : cursor) {
            String org = (String) dbObject.get("o");
            int count = (Integer) dbObject.get("c");
            String type = map.get("nm");
            Entity<String> entity = new Entity<>(EntityType.valueOf(type), org, count);
            ret.add(entity);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return ret.stream();
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
public Stream<Node> matchingNodes(EntityType type, String name) {
    List<Node> nodes = new LinkedList<>();

    QueryBuilder queryBuilder = QueryBuilder.start("t").is(type).and("nm").is(name);
    DBObject query = queryBuilder.get();

    BasicDBObjectBuilder orderByBuilder = BasicDBObjectBuilder.start("c", 1);
    DBObject orderBy = orderByBuilder.get();

    DBCursor cursor = entitiesData.find(query).sort(orderBy);
    try {/*  w ww . j  a  va 2 s . com*/
        for (DBObject dbObject : cursor) {
            String nodeId = (String) dbObject.get("n");
            long nodeInternalId = (Long) dbObject.get("ni");
            Long nodeVersion = (Long) dbObject.get("v");
            Node node = Node.build().nodeInternalId(nodeInternalId).nodeId(nodeId).nodeVersion(nodeVersion);
            nodes.add(node);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return nodes.stream();
}

From source file:org.sglover.entities.dao.mongo.MongoEntitiesDAO.java

License:Open Source License

@Override
public Entities getEntities(Node node) {
    String nodeId = node.getNodeId();
    String nodeVersion = node.getVersionLabel();

    Entities entities = Entities.empty(nodeId, nodeVersion);

    QueryBuilder queryBuilder = QueryBuilder.start("n").is(nodeId).and("v").is(nodeVersion);

    Set<String> types = new HashSet<>();
    types.add("names");
    types.add("orgs");
    queryBuilder.and("t").in(types);

    DBObject query = queryBuilder.get();

    DBCursor cursor = entitiesData.find(query);
    try {//w  ww.j  a  v a2 s. c  o m
        for (DBObject dbObject : cursor) {
            Entity<String> entity = getEntity(dbObject);
            entities.addEntity(entity);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return entities;
}

From source file:org.sglover.entities.dao.mongo.MongoSimilarityDAO.java

License:Open Source License

@Override
public List<Similarity> getSimilar(Node node) {
    List<Similarity> nodes = new LinkedList<>();

    List<DBObject> ors = new LinkedList<>();
    ors.add(QueryBuilder.start("n1").is(node.getNodeId()).and("v1").is(node.getNodeVersion()).get());
    ors.add(QueryBuilder.start("n2").is(node.getNodeId()).and("v2").is(node.getNodeVersion()).get());

    DBObject query = QueryBuilder.start().or(ors.toArray(new DBObject[0])).get();

    DBObject orderBy = BasicDBObjectBuilder.start("s", -1).get();

    DBCursor cursor = similarityData.find(query).sort(orderBy);
    try {//from   www.j a  va  2 s  .c  om
        for (DBObject dbObject : cursor) {
            String node1Id = (String) dbObject.get("n1");
            String node1Version = (String) dbObject.get("v1");
            String node2Id = (String) dbObject.get("n2");
            String node2Version = (String) dbObject.get("v2");
            double similarity = (Double) dbObject.get("s");

            if (node1Id.equals(node.getNodeId())) {
                if (!node2Id.equals(node.getNodeId())) {
                    Node n = Node.build().nodeId(node2Id).versionLabel(node2Version);
                    Similarity s = new Similarity(n, similarity);
                    nodes.add(s);
                }
            } else {
                if (node2Id.equals(node.getNodeId())) {
                    Node n = Node.build().nodeId(node1Id).versionLabel(node1Version);
                    Similarity s = new Similarity(n, similarity);
                    nodes.add(s);
                }
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return nodes;
}

From source file:org.sglover.entities.dao.mongo.MongoUserTrackingDAO.java

License:Open Source License

@Override
public List<ViewedNode> viewedNodes(String username, long timeDelta) {
    List<ViewedNode> viewedNodes = new LinkedList<>();

    long time = System.currentTimeMillis() - timeDelta;

    DBObject query = QueryBuilder.start("ic").is(true).and("u").is(username).and("t").greaterThanEquals(time)
            .get();/*w  w  w.  j av a 2s. co  m*/
    DBCursor cursor = data.find(query);

    try {
        for (DBObject dbObject : cursor) {
            String nodeId = (String) dbObject.get("n");
            long nodeInternalId = (Long) dbObject.get("ni");
            String nodeVersion = (String) dbObject.get("v");
            long timestamp = (Long) dbObject.get("t");
            ViewedNode viewedNode = new ViewedNode(username, nodeId, nodeInternalId, nodeVersion, timestamp);
            viewedNodes.add(viewedNode);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return viewedNodes;
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

/**
 * Internal method using callback to do queries against the datastore that requires reading a collection of objects.
 * It will take the following steps//from  w  w  w .jav  a  2 s  .  c  o  m
 * <ol>
 * <li>Execute the given {@link ConnectionCallback} for a {@link DBCursor}.</li>
 * <li>Prepare that {@link DBCursor} with the given {@link CursorPreparer} (will be skipped if {@link CursorPreparer}
 * is {@literal null}</li>
 * <li>Iterate over the {@link DBCursor} and applies the given {@link DbObjectCallback} to each of the
 * {@link DBObject}s collecting the actual result {@link List}.</li>
 * <ol>
 * 
 * @param <T>
 * @param collectionCallback the callback to retrieve the {@link DBCursor} with
 * @param preparer the {@link CursorPreparer} to potentially modify the {@link DBCursor} before ireating over it
 * @param objectCallback the {@link DbObjectCallback} to transform {@link DBObject}s into the actual domain type
 * @param collectionName the collection to be queried
 * @return
 */
private <T> List<T> executeFindMultiInternal(CollectionCallback<DBCursor> collectionCallback,
        CursorPreparer preparer, DbObjectCallback<T> objectCallback, String collectionName) {

    try {

        DBCursor cursor = null;

        try {

            cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));

            if (preparer != null) {
                cursor = preparer.prepare(cursor);
            }

            List<T> result = new ArrayList<T>();

            while (cursor.hasNext()) {
                DBObject object = cursor.next();
                result.add(objectCallback.doWith(object));
            }

            return result;

        } finally {

            if (cursor != null) {
                cursor.close();
            }
        }
    } catch (RuntimeException e) {
        throw potentiallyConvertRuntimeException(e, exceptionTranslator);
    }
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

private void executeQueryInternal(CollectionCallback<DBCursor> collectionCallback, CursorPreparer preparer,
        DocumentCallbackHandler callbackHandler, String collectionName) {

    try {//  w  w  w .  j  a  v  a2s.c  o  m

        DBCursor cursor = null;

        try {
            cursor = collectionCallback.doInCollection(getAndPrepareCollection(getDb(), collectionName));

            if (preparer != null) {
                cursor = preparer.prepare(cursor);
            }

            while (cursor.hasNext()) {
                DBObject dbobject = cursor.next();
                callbackHandler.processDocument(dbobject);
            }

        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

    } catch (RuntimeException e) {
        throw potentiallyConvertRuntimeException(e, exceptionTranslator);
    }
}

From source file:org.surfing.service.persistence.Persistence.java

@GET
@Produces("text/html")
@Path("/list/{collection}")
public String update(@PathParam("collection") String collection) {
    DBCursor cursor = db.getCollection(collection).find();
    String lazyReturn = "<html><body>";
    try {/*from  w  w  w.j  a  v a 2s.com*/
        while (cursor.hasNext()) {
            lazyReturn += "<p>" + cursor.next() + "</p>";
        }
    } finally {
        cursor.close();
    }
    lazyReturn += "</body></html>";
    return lazyReturn;
}