use of com.mongodb.DBCursor in project apex-malhar by apache.
the class MongoDBOutputOperator method initLastWindowInfo.
/**
* init last completed windowId information with operatorId, read from maxWindowTable.
* If the table is empty, insert a default value document
*/
public void initLastWindowInfo() {
maxWindowCollection = db.getCollection(maxWindowTable);
BasicDBObject query = new BasicDBObject();
query.put(operatorIdColumnName, operatorId);
// query.put(applicationIdName, "0");
DBCursor cursor = maxWindowCollection.find(query);
if (cursor.hasNext()) {
Object obj = cursor.next().get(windowIdColumnName);
lastWindowId = (Long) obj;
} else {
BasicDBObject doc = new BasicDBObject();
doc.put(windowIdColumnName, (long) 0);
// doc.put(applicationIdName, 0);
doc.put(operatorIdColumnName, operatorId);
maxWindowCollection.save(doc);
}
logger.debug("last windowid: {}", lastWindowId);
}
use of com.mongodb.DBCursor in project openhab1-addons by openhab.
the class MongoDBPersistenceService method query.
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
if (!initialized) {
return Collections.emptyList();
}
if (!isConnected()) {
connectToDatabase();
}
if (!isConnected()) {
return Collections.emptyList();
}
String name = filter.getItemName();
Item item = getItem(name);
List<HistoricItem> items = new ArrayList<HistoricItem>();
DBObject query = new BasicDBObject();
if (filter.getItemName() != null) {
query.put(FIELD_ITEM, filter.getItemName());
}
if (filter.getState() != null && filter.getOperator() != null) {
String op = convertOperator(filter.getOperator());
Object value = convertValue(filter.getState());
query.put(FIELD_VALUE, new BasicDBObject(op, value));
}
if (filter.getBeginDate() != null) {
query.put(FIELD_TIMESTAMP, new BasicDBObject("$gte", filter.getBeginDate()));
}
if (filter.getEndDate() != null) {
query.put(FIELD_TIMESTAMP, new BasicDBObject("$lte", filter.getEndDate()));
}
Integer sortDir = (filter.getOrdering() == Ordering.ASCENDING) ? 1 : -1;
DBCursor cursor = this.mongoCollection.find(query).sort(new BasicDBObject(FIELD_TIMESTAMP, sortDir)).skip(filter.getPageNumber() * filter.getPageSize()).limit(filter.getPageSize());
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
final State state;
if (item instanceof NumberItem) {
state = new DecimalType(obj.getDouble(FIELD_VALUE));
} else if (item instanceof DimmerItem) {
state = new PercentType(obj.getInt(FIELD_VALUE));
} else if (item instanceof SwitchItem) {
state = OnOffType.valueOf(obj.getString(FIELD_VALUE));
} else if (item instanceof ContactItem) {
state = OpenClosedType.valueOf(obj.getString(FIELD_VALUE));
} else if (item instanceof RollershutterItem) {
state = new PercentType(obj.getInt(FIELD_VALUE));
} else if (item instanceof ColorItem) {
state = new HSBType(obj.getString(FIELD_VALUE));
} else if (item instanceof DateTimeItem) {
Calendar cal = Calendar.getInstance();
cal.setTime(obj.getDate(FIELD_VALUE));
state = new DateTimeType(cal);
} else {
state = new StringType(obj.getString(FIELD_VALUE));
}
items.add(new MongoDBItem(name, state, obj.getDate(FIELD_TIMESTAMP)));
}
return items;
}
use of com.mongodb.DBCursor in project GNS by MobilityFirst.
the class MongoRecords method selectRecordsWithin.
private MongoRecordCursor selectRecordsWithin(String collectionName, ColumnField valuesMapField, String key, String value, boolean explain) throws FailedDBOperationException {
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
BasicDBList box = parseJSONArrayLocationStringIntoDBList(value);
String fieldName = valuesMapField.getName() + "." + key;
BasicDBObject shapeClause = new BasicDBObject("$box", box);
BasicDBObject withinClause = new BasicDBObject("$geoWithin", shapeClause);
BasicDBObject query = new BasicDBObject(fieldName, withinClause);
DBCursor cursor = null;
try {
cursor = collection.find(query);
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecordsWithin 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 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());
}
Aggregations