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());
}
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());
}
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);
}
}
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);
}
}
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);
}
Aggregations