Search in sources :

Example 46 with DBCursor

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);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 47 with DBCursor

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;
}
Also used : StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) HistoricItem(org.openhab.core.persistence.HistoricItem) NumberItem(org.openhab.core.library.items.NumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HistoricItem(org.openhab.core.persistence.HistoricItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) PercentType(org.openhab.core.library.types.PercentType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 48 with DBCursor

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());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) 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 49 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 50 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)

Aggregations

DBCursor (com.mongodb.DBCursor)87 BasicDBObject (com.mongodb.BasicDBObject)70 DBObject (com.mongodb.DBObject)54 DBCollection (com.mongodb.DBCollection)42 ArrayList (java.util.ArrayList)20 JSONObject (org.json.JSONObject)15 MongoException (com.mongodb.MongoException)11 DB (com.mongodb.DB)10 Test (org.junit.Test)9 BasicDBList (com.mongodb.BasicDBList)8 HashMap (java.util.HashMap)8 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)6 JSONArray (org.json.JSONArray)5 MongoClient (com.mongodb.MongoClient)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 Function (com.google.common.base.Function)3 Date (java.util.Date)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 WorkItem (com.example.entities.WorkItem)2