Search in sources :

Example 41 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class MongoRecords method removeMapKeys.

@Override
public void removeMapKeys(String collectionName, String name, ColumnField mapField, ArrayList<ColumnField> mapKeys) throws FailedDBOperationException {
    String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    DBCollection collection = db.getCollection(collectionName);
    BasicDBObject query = new BasicDBObject(primaryKey, name);
    BasicDBObject updates = new BasicDBObject();
    if (mapField != null && mapKeys != null) {
        for (int i = 0; i < mapKeys.size(); i++) {
            String fieldName = mapField.getName() + "." + mapKeys.get(i).getName();
            updates.append(fieldName, 1);
        }
    }
    if (updates.keySet().size() > 0) {
        try {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} <============>unset{1}<============>", new Object[] { dbName, updates.toString() });
            collection.update(query, new BasicDBObject("$unset", updates));
        } catch (MongoException e) {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} removeMapKeys failed: {1}", new Object[] { dbName, e.getMessage() });
            throw new FailedDBOperationException(collectionName, updates.toString(), "Original mongo exception:" + e.getMessage());
        }
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) 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 42 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class MongoRecords method updateEntireRecord.

@Override
public void updateEntireRecord(String collectionName, String guid, ValuesMap valuesMap) throws FailedDBOperationException {
    BasicDBObject updates = new BasicDBObject();
    try {
        updates.append(NameRecord.VALUES_MAP.getName(), JSON.parse(valuesMap.toString()));
    } catch (Exception e) {
        throw new FailedDBOperationException(collectionName, guid, "Unable to parse json" + e.getMessage());
    }
    doUpdate(collectionName, guid, updates);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 43 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.

the class MongoRecords method lookupSomeFields.

@Override
public HashMap<ColumnField, Object> lookupSomeFields(String collectionName, String guid, ColumnField nameField, ColumnField valuesMapField, ArrayList<ColumnField> valuesMapKeys) throws RecordNotFoundException, FailedDBOperationException {
    if (guid == null) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} GUID is null: {1}", new Object[] { dbName, guid });
        throw new RecordNotFoundException(guid);
    }
    db.requestStart();
    try {
        String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject(primaryKey, guid);
        BasicDBObject projection = new BasicDBObject().append("_id", 0);
        if (valuesMapField != null && valuesMapKeys != null) {
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String fieldName = valuesMapField.getName() + "." + valuesMapKeys.get(i).getName();
                projection.append(fieldName, 1);
            }
        }
        DBObject dbObject = collection.findOne(query, projection);
        if (dbObject == null) {
            throw new RecordNotFoundException(guid);
        }
        HashMap<ColumnField, Object> hashMap = new HashMap<>();
        // put the name in the hashmap!! very important!!
        hashMap.put(nameField, guid);
        if (valuesMapField != null && valuesMapKeys != null) {
            // first we pull all the user values from the dbObject and put in a bson object
            // FIXME: Why not convert this to a JSONObject right now? We know that's what it is.
            BasicDBObject bson = (BasicDBObject) dbObject.get(valuesMapField.getName());
            DatabaseConfig.getLogger().log(Level.FINER, "{0} @@@@@@@@ {1}", new Object[] { dbName, bson });
            // then we run thru each userkey in the valuesMapKeys and pull the
            // value put stuffing it into the values map
            ValuesMap valuesMap = new ValuesMap();
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String userKey = valuesMapKeys.get(i).getName();
                if (containsFieldDotNotation(userKey, bson) == false) {
                    DatabaseConfig.getLogger().log(Level.FINE, "{0} DBObject doesn't contain {1}", new Object[] { dbName, userKey });
                    continue;
                }
                try {
                    switch(valuesMapKeys.get(i).type()) {
                        case USER_JSON:
                            Object value = getWithDotNotation(userKey, bson);
                            DatabaseConfig.getLogger().log(Level.FINE, "{0} Object is {1}", new Object[] { dbName, value.toString() });
                            valuesMap.put(userKey, value);
                            break;
                        case LIST_STRING:
                            valuesMap.putAsArray(userKey, JSONUtils.JSONArrayToResultValue(new JSONArray(getWithDotNotation(userKey, bson).toString())));
                            break;
                        default:
                            DatabaseConfig.getLogger().log(Level.SEVERE, "{0} ERROR: Error: User keys field {1} is not a known type: {2}", new Object[] { dbName, userKey, valuesMapKeys.get(i).type() });
                            break;
                    }
                } catch (JSONException e) {
                    DatabaseConfig.getLogger().log(Level.SEVERE, "{0} Error parsing json: {1}", new Object[] { dbName, e.getMessage() });
                    e.printStackTrace();
                }
            }
            hashMap.put(valuesMapField, valuesMap);
        }
        return hashMap;
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} lookupSomeFields failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
    } finally {
        db.requestDone();
    }
}
Also used : MongoException(com.mongodb.MongoException) HashMap(java.util.HashMap) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 44 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException 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 45 with FailedDBOperationException

use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException 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)

Aggregations

FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)49 JSONException (org.json.JSONException)36 JSONObject (org.json.JSONObject)36 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)22 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)18 BasicDBObject (com.mongodb.BasicDBObject)13 MongoException (com.mongodb.MongoException)13 DBCollection (com.mongodb.DBCollection)12 DBObject (com.mongodb.DBObject)12 IOException (java.io.IOException)11 JSONArray (org.json.JSONArray)9 Test (org.junit.Test)9 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)7 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)7 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)7 DBCursor (com.mongodb.DBCursor)6 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)6 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)6 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)5 RandomString (edu.umass.cs.gnscommon.utils.RandomString)4