Search in sources :

Example 41 with DBCursor

use of com.mongodb.DBCursor in project GNS by MobilityFirst.

the class MongoRecords method selectRecordsNear.

private MongoRecordCursor selectRecordsNear(String collectionName, ColumnField valuesMapField, String key, String value, Double maxDistance, boolean explain) throws FailedDBOperationException {
    db.requestEnsureConnection();
    DBCollection collection = db.getCollection(collectionName);
    double maxDistanceInRadians = maxDistance / METERS_PER_DEGREE;
    BasicDBList tuple = new BasicDBList();
    try {
        JSONArray json = new JSONArray(value);
        tuple.add(json.getDouble(0));
        tuple.add(json.getDouble(1));
    } catch (JSONException e) {
        DatabaseConfig.getLogger().log(Level.SEVERE, "{0} Unable to parse JSON: {1}", new Object[] { dbName, e.getMessage() });
    }
    String fieldName = valuesMapField.getName() + "." + key;
    BasicDBObject nearClause = new BasicDBObject("$near", tuple).append("$maxDistance", maxDistanceInRadians);
    BasicDBObject query = new BasicDBObject(fieldName, nearClause);
    DBCursor cursor = null;
    try {
        cursor = collection.find(query);
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} selectNear failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, fieldName, "Original mongo exception:" + e.getMessage());
    }
    if (explain) {
        System.out.println(cursor.explain().toString());
    }
    return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 42 with DBCursor

use of com.mongodb.DBCursor in project GNS by MobilityFirst.

the class MongoRecords method selectRecordsQuery.

private MongoRecordCursor selectRecordsQuery(String collectionName, ColumnField valuesMapField, String query, List<String> projection, boolean explain) throws FailedDBOperationException {
    db.requestEnsureConnection();
    DBCollection collection = db.getCollection(collectionName);
    DBCursor cursor = null;
    try {
        if (projection == null || // in the projection
        (!projection.isEmpty() && projection.get(0).equals(GNSProtocol.ENTIRE_RECORD.toString()))) {
            cursor = collection.find(parseMongoQuery(query, valuesMapField));
        } else {
            cursor = collection.find(parseMongoQuery(query, valuesMapField), generateProjection(projection));
        }
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecordsQuery failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, query, "Original mongo exception:" + e.getMessage());
    }
    if (explain) {
        System.out.println(cursor.explain().toString());
    }
    return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 43 with DBCursor

use of com.mongodb.DBCursor in project camel by apache.

the class GridFsProducer method process.

public void process(Exchange exchange) throws Exception {
    String operation = endpoint.getOperation();
    if (operation == null) {
        operation = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_OPERATION, String.class);
    }
    if (operation == null || "create".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        Long chunkSize = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_CHUNKSIZE, Long.class);
        InputStream ins = exchange.getIn().getMandatoryBody(InputStream.class);
        GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true);
        if (chunkSize != null && chunkSize > 0) {
            gfsFile.setChunkSize(chunkSize);
        }
        final String ct = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        if (ct != null) {
            gfsFile.setContentType(ct);
        }
        String metaData = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_METADATA, String.class);
        DBObject dbObject = (DBObject) JSON.parse(metaData);
        gfsFile.setMetaData(dbObject);
        gfsFile.save();
        //add headers with the id and file name produced by the driver.
        exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, gfsFile.getFilename());
        exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_FILE_ID_PRODUCED, gfsFile.getId());
    } else if ("remove".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        endpoint.getGridFs().remove(filename);
    } else if ("findOne".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        GridFSDBFile file = endpoint.getGridFs().findOne(filename);
        if (file != null) {
            exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
            exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
            exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
            exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
            exchange.getIn().setBody(file.getInputStream(), InputStream.class);
        } else {
            throw new FileNotFoundException("No GridFS file for " + filename);
        }
    } else if ("listAll".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(new DBCursorFilenameReader(cursor), Reader.class);
    } else if ("count".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(cursor.count(), Integer.class);
    }
}
Also used : GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) InputStream(java.io.InputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 44 with DBCursor

use of com.mongodb.DBCursor in project jackrabbit-oak by apache.

the class DocumentNodeStoreHelper method getDocuments.

private static Iterable<NodeDocument> getDocuments(DocumentStore store) {
    if (store instanceof MongoDocumentStore) {
        // optimized implementation for MongoDocumentStore
        final MongoDocumentStore mds = (MongoDocumentStore) store;
        DBCollection dbCol = MongoDocumentStoreHelper.getDBCollection(mds, Collection.NODES);
        DBObject query = QueryBuilder.start(NodeDocument.HAS_BINARY_FLAG).is(NodeDocument.HAS_BINARY_VAL).get();
        DBCursor cursor = dbCol.find(query);
        return Iterables.transform(cursor, new Function<DBObject, NodeDocument>() {

            @Nullable
            @Override
            public NodeDocument apply(DBObject input) {
                return MongoDocumentStoreHelper.convertFromDBObject(mds, Collection.NODES, input);
            }
        });
    } else {
        return Utils.getSelectedDocuments(store, NodeDocument.HAS_BINARY_FLAG, NodeDocument.HAS_BINARY_VAL);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) MongoDocumentStore(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore) DBObject(com.mongodb.DBObject) Nullable(javax.annotation.Nullable)

Example 45 with DBCursor

use of com.mongodb.DBCursor in project jackrabbit-oak by apache.

the class MongoVersionGCSupport method getPossiblyDeletedDocs.

@Override
public CloseableIterable<NodeDocument> getPossiblyDeletedDocs(final long fromModified, final long toModified) {
    //_deletedOnce == true && _modified >= fromModified && _modified < toModified
    DBObject query = start(NodeDocument.DELETED_ONCE).is(Boolean.TRUE).put(NodeDocument.MODIFIED_IN_SECS).greaterThanEquals(NodeDocument.getModifiedInSecs(fromModified)).lessThan(NodeDocument.getModifiedInSecs(toModified)).get();
    DBCursor cursor = getNodeCollection().find(query).setReadPreference(ReadPreference.secondaryPreferred());
    cursor.batchSize(batchSize);
    return CloseableIterable.wrap(transform(cursor, new Function<DBObject, NodeDocument>() {

        @Override
        public NodeDocument apply(DBObject input) {
            return store.convertFromDBObject(NODES, input);
        }
    }), cursor);
}
Also used : Function(com.google.common.base.Function) DBCursor(com.mongodb.DBCursor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

DBCursor (com.mongodb.DBCursor)58 DBObject (com.mongodb.DBObject)49 BasicDBObject (com.mongodb.BasicDBObject)48 DBCollection (com.mongodb.DBCollection)24 HashMap (java.util.HashMap)9 MongoException (com.mongodb.MongoException)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)7 Function (com.google.common.base.Function)6 BasicDBList (com.mongodb.BasicDBList)6 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)6 JSONObject (org.json.JSONObject)6 QueryBuilder (com.mongodb.QueryBuilder)5 DB (com.mongodb.DB)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 Iterator (java.util.Iterator)3 Nonnull (javax.annotation.Nonnull)3 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)3 Mongo (com.mongodb.Mongo)2 GridFSDBFile (com.mongodb.gridfs.GridFSDBFile)2