Example usage for com.mongodb.client MongoCursor close

List of usage examples for com.mongodb.client MongoCursor close

Introduction

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

Prototype

@Override
    void close();

Source Link

Usage

From source file:org.gennai.gungnir.metastore.MongoDbMetaStore.java

License:Apache License

@Override
public List<UserEntity> findUserAccounts() throws MetaStoreException {
    try {/*from  ww w .  ja v  a 2 s  .  co m*/
        List<UserEntity> users = Lists.newArrayList();

        MongoCursor<Document> cursor = userAccountCollection.find().iterator();
        try {
            while (cursor.hasNext()) {
                Document doc = cursor.next();
                UserEntity user = new UserEntity();
                user.setId(doc.getObjectId("_id").toString());
                user.setName(doc.getString("name"));
                user.setPassword(doc.get("password", Binary.class).getData());
                user.setCreateTime(doc.getDate("createTime"));
                user.setLastModifyTime(doc.getDate("lastModifyTime"));

                users.add(user);
            }
        } finally {
            cursor.close();
        }

        return users;
    } catch (MongoException e) {
        LOG.error("Failed to find user accounts", e);
        throw new MetaStoreException("Failed to find user accounts", e);
    }
}

From source file:org.gennai.gungnir.metastore.MongoDbMetaStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<Schema> findSchemas(UserEntity owner) throws MetaStoreException {
    try {/*from  w ww. j a v  a2 s . co m*/
        List<Schema> schemas = Lists.newArrayList();

        MongoCursor<Document> cursor = schemaCollection.find(eq("owner", owner.getId())).iterator();
        try {
            while (cursor.hasNext()) {
                Document doc = cursor.next();

                Schema schema = (Schema) Utils.deserialize(doc.get("desc", Binary.class).getData());
                schema.setId(doc.getObjectId("_id").toString());
                schema.setTopologies(doc.get("topologies", ArrayList.class));
                schema.setOwner(owner);
                schema.setCreateTime(doc.getDate("createTime"));
                schema.setComment(doc.getString("comment"));

                schemas.add(schema);
            }
        } finally {
            cursor.close();
        }

        return schemas;
    } catch (MongoException e) {
        LOG.error("Failed to find schemas", e);
        throw new MetaStoreException("Failed to find schemas", e);
    }
}

From source file:org.gennai.gungnir.metastore.MongoDbMetaStore.java

License:Apache License

@Override
public List<GungnirTopology> findTopologies(UserEntity owner) throws MetaStoreException {
    try {//  ww w.  j av  a2  s  .co m
        List<GungnirTopology> topologies = Lists.newArrayList();

        MongoCursor<Document> cursor = topologyCollection.find(eq("owner", owner.getId())).iterator();
        try {
            while (cursor.hasNext()) {
                Document doc = cursor.next();
                GungnirTopology topology = (GungnirTopology) Utils
                        .deserialize(doc.get("desc", Binary.class).getData());
                topology.setId(doc.getObjectId("_id").toString());
                topology.setName(doc.getString("name"));
                topology.setOwner(owner);
                topology.setStatus(TopologyStatus.valueOf(doc.getString("status")));
                topology.setCreateTime(doc.getDate("createTime"));
                topology.setComment(doc.getString("comment"));

                topologies.add(topology);
            }
        } finally {
            cursor.close();
        }

        return topologies;
    } catch (MongoException e) {
        LOG.error("Failed to find topologies", e);
        throw new MetaStoreException("Failed to find topologies", e);
    }
}

From source file:org.gennai.gungnir.metastore.MongoDbMetaStore.java

License:Apache License

@Override
public List<GungnirTopology> findTopologies(UserEntity owner, TopologyStatus status) throws MetaStoreException {
    try {//from  w  ww  .  j  a  va 2  s. co  m
        List<GungnirTopology> topologies = Lists.newArrayList();

        MongoCursor<Document> cursor = topologyCollection
                .find(and(eq("owner", owner.getId()), eq("status", status.toString()))).iterator();
        try {
            while (cursor.hasNext()) {
                Document doc = cursor.next();
                GungnirTopology topology = (GungnirTopology) Utils
                        .deserialize(doc.get("desc", Binary.class).getData());
                topology.setId(doc.getObjectId("_id").toString());
                topology.setName(doc.getString("name"));
                topology.setStatus(TopologyStatus.valueOf(doc.getString("status")));
                topology.setOwner(owner);
                topology.setCreateTime(doc.getDate("createTime"));
                topology.setComment(doc.getString("comment"));

                topologies.add(topology);
            }
        } finally {
            cursor.close();
        }

        return topologies;
    } catch (MongoException e) {
        LOG.error("Failed to find topologies", e);
        throw new MetaStoreException("Failed to find topologies", e);
    }
}

From source file:org.gennai.gungnir.topology.processor.MongoFetchProcessor.java

License:Apache License

private List<List<Object>> find(Document execQuery) {
    List<List<Object>> valuesList = Lists.newArrayList();
    FindIterable<Document> find = collection.find(execQuery).projection(fetchFields);
    if (sort != null) {
        find.sort(sort);//from   w w  w. j  a  v a 2  s  . c o  m
    }
    if (limit != null) {
        find.limit(limit);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Fetch from '{}.{}' query {}", dbName, collectionName, execQuery);
    }

    MongoCursor<Document> cursor = find.iterator();
    try {
        while (cursor.hasNext()) {
            Document doc = cursor.next();

            List<Object> values = Lists.newArrayListWithCapacity(fetchFieldNames.length);
            for (String fieldName : fetchFieldNames) {
                values.add(toValue(doc.get(fieldName)));
            }
            valuesList.add(values);
        }
    } finally {
        cursor.close();
    }
    return valuesList;
}

From source file:org.helm.rest.MongoDB.java

public JSONObject List(String table, String cols, BsonDocument where, BsonDocument sortby, int page,
        int countperpage) {
    if (page < 1)
        page = 1;/*from www .  j av  a2 s.c o  m*/
    if (countperpage < 1)
        countperpage = 10;

    long count;
    FindIterable iter;
    MongoCollection coll = db.getCollection(table);
    if (where == null) {
        count = coll.count();
        iter = coll.find();
    } else {
        count = coll.count(where);
        iter = coll.find(where);
    }

    if (sortby != null)
        iter = iter.sort(sortby);

    if (cols != null) {
        String[] ss = cols.split(",");
        Document fields = new Document("_id", false);
        for (int i = 0; i < ss.length; ++i) {
            fields.append(ss[i].trim().toLowerCase(), true);
        }
        iter = iter.projection(fields);
    }

    long mod = count % countperpage;
    long pages = (count - mod) / countperpage + (mod == 0 ? 0 : 1);

    if (page > 1)
        iter = iter.skip((page - 1) * countperpage);
    iter = iter.limit(countperpage);
    MongoCursor cur = iter.iterator();

    JSONObject ret = new JSONObject();
    ret.put("page", page);
    ret.put("pages", pages);
    ret.put("rows", ResultSet2Json(cur));

    cur.close();
    return ret;
}

From source file:org.iotivity.cloud.accountserver.db.MongoDB.java

License:Open Source License

/**
 * API for selecting records from DB table.
 * /*from w w w  .  j  a v  a2 s. c o  m*/
 * @param tableName
 *            table name for the record to be selected
 * @param doc
 *            document filter to be selected
 * @return record list according to the filter document
 */
public ArrayList<HashMap<String, Object>> selectRecord(String tableName, Document doc) {

    if (tableName == null || doc == null)
        return null;

    MongoCollection<Document> collection = db.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find(doc).iterator();

    ArrayList<HashMap<String, Object>> recordList = new ArrayList<HashMap<String, Object>>();

    try {

        while (cursor.hasNext()) {
            Document selectedDoc = cursor.next();
            recordList.add(convertDocumentToHashMap(selectedDoc));
        }

    } finally {

        cursor.close();
    }

    return recordList;
}

From source file:org.iotivity.cloud.accountserver.db.MongoDB.java

License:Open Source License

private void showRecord(String tableName) {

    MongoCollection<Document> collection = db.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find().iterator();

    Log.i("<" + tableName + ">");

    HashMap<String, Object> records = null;
    int index = 0;
    while (cursor.hasNext()) {

        Document doc = cursor.next();
        records = convertDocumentToHashMap(doc);

        Log.i("[" + index + "] " + records.toString());
        index++;/*from   ww  w  .  j  a  v  a2 s. c o  m*/
    }

    cursor.close();
}

From source file:org.iotivity.cloud.rdserver.db.MongoDB.java

License:Open Source License

/**
 * API for selecting records from DB table.
 * //from ww w.  j  a  v  a  2 s.  c  o m
 * @param tableName
 *            table name for the record to be selected
 * @param doc
 *            document filter to be selected
 * @return record list according to the filter document
 */
public ArrayList<HashMap<String, Object>> selectRecord(String tableName, Document doc) {

    if (tableName == null || doc == null)
        return null;

    MongoCollection<Document> collection = db.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find(doc).iterator();

    ArrayList<HashMap<String, Object>> recordList = new ArrayList<>();

    try {

        while (cursor.hasNext()) {
            Document selectedDoc = cursor.next();
            recordList.add(convertDocumentToHashMap(selectedDoc));
        }

    } finally {

        cursor.close();
    }

    return recordList;
}

From source file:org.iotivity.cloud.rdserver.MongoDB.java

License:Open Source License

/**
 * API for finding resources matched filterValue of filterKey in collection
 *
 * @param filterKey// www. java2 s  . co m
 *            field name in collection
 * @param filterValue
 *            field value about field name
 * @param tablename
 *            collection name
 * @return ArrayList<PublishPayloadFormat> - array list of resource
 *         information
 */
public ArrayList<PublishPayloadFormat> readResource(String filterKey, String filterValue, String tablename) {
    MongoCollection<Document> collection = db.getCollection(tablename);
    ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
    MongoCursor<Document> cursor = collection.find(Filters.eq(filterKey, filterValue)).iterator();
    try {
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            resourceFormatList.add(convertDocumentToResourceFormat(doc));
        }
    } finally {
        cursor.close();
    }

    return resourceFormatList;
}