Search in sources :

Example 6 with RecordNotFoundException

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

the class DiskMapRecords method removeMapKeys.

@Override
public void removeMapKeys(String collection, String name, ColumnField mapField, ArrayList<ColumnField> mapKeys) throws FailedDBOperationException {
    JSONObject record = null;
    try {
        record = lookupEntireRecord(collection, name);
    } catch (RecordNotFoundException e) {
    }
    LOGGER.log(Level.FINE, "Record before:{0}", record);
    if (record == null) {
        DatabaseConfig.getLogger().log(Level.FINE, "removeMapKeys failed. record is null");
        throw new FailedDBOperationException(collection, name, "Record not found.");
    }
    if (mapField != null && mapKeys != null) {
        try {
            JSONObject json = record.getJSONObject(mapField.getName());
            LOGGER.log(Level.FINE, "Json before:{0}", json);
            for (int i = 0; i < mapKeys.size(); i++) {
                String fieldName = mapKeys.get(i).getName();
                LOGGER.log(Level.FINE, "Removing: {0}", fieldName);
                JSONDotNotation.removeWithDotNotation(fieldName, json);
            }
            LOGGER.log(Level.FINE, "Json after:{0}", json);
            record.put(mapField.getName(), json);
            LOGGER.log(Level.FINE, "Record after:{0}", record);
        } catch (JSONException e) {
            LOGGER.log(Level.SEVERE, "Problem updating json: {0}", e.getMessage());
        }
    }
    getMap(collection).put(name, record);
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 7 with RecordNotFoundException

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

the class DiskMapRecords method updateIndividualFields.

@Override
public void updateIndividualFields(String collection, String name, ColumnField valuesMapField, ArrayList<ColumnField> valuesMapKeys, ArrayList<Object> valuesMapValues) throws FailedDBOperationException {
    LOGGER.log(Level.FINE, "Update fields {0}/{1}", new Object[] { name, valuesMapKeys });
    JSONObject record;
    try {
        record = lookupEntireRecord(collection, name);
    } catch (RecordNotFoundException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "updateIndividualFields failed: {0}", e.getMessage());
        throw new FailedDBOperationException(collection, name, "Record not found.");
    }
    LOGGER.log(Level.FINE, "Record before:{0}", record);
    if (record == null) {
        throw new FailedDBOperationException(collection, name, "Record is null.");
    }
    if (valuesMapField != null && valuesMapKeys != null) {
        try {
            JSONObject json = record.getJSONObject(valuesMapField.getName());
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String fieldName = valuesMapKeys.get(i).getName();
                switch(valuesMapKeys.get(i).type()) {
                    case LIST_STRING:
                        JSONDotNotation.putWithDotNotation(json, fieldName, valuesMapValues.get(i));
                        break;
                    case USER_JSON:
                        JSONDotNotation.putWithDotNotation(json, fieldName, JSONParse(valuesMapValues.get(i)));
                        break;
                    default:
                        LOGGER.log(Level.WARNING, "Ignoring unknown format: {0}", valuesMapKeys.get(i).type());
                        break;
                }
            }
            LOGGER.log(Level.FINE, "Json after:{0}", json);
            record.put(valuesMapField.getName(), json);
            LOGGER.log(Level.FINE, "Record after:{0}", record);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Problem updating json: {0}", e.getMessage());
            throw new FailedDBOperationException(collection, name, "Unable to parse json " + e.getMessage());
        }
    }
    getMap(collection).put(name, record);
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 8 with RecordNotFoundException

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

the class MongoRecords method lookupEntireRecord.

private JSONObject lookupEntireRecord(String collectionName, String guid, boolean explain) throws RecordNotFoundException, FailedDBOperationException {
    long startTime = System.currentTimeMillis();
    db.requestStart();
    try {
        String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject(primaryKey, guid);
        DBCursor cursor = collection.find(query);
        if (explain) {
            System.out.println(cursor.explain().toString());
        }
        if (cursor.hasNext()) {
            DBObject obj = cursor.next();
            // arun: optimized for the common case of Map
            @SuppressWarnings("unchecked") JSONObject json = obj instanceof Map ? DiskMapRecords.recursiveCopyMap((Map<String, ?>) obj) : new JSONObject(obj.toString());
            // instrumentation
            DelayProfiler.updateDelay("lookupEntireRecord", startTime);
            // older style
            int lookupTime = (int) (System.currentTimeMillis() - startTime);
            if (lookupTime > 20) {
                DatabaseConfig.getLogger().log(Level.FINE, "{0} mongoLookup Long delay {1}", new Object[] { dbName, lookupTime });
            }
            return json;
        } else {
            throw new RecordNotFoundException(guid);
        }
    } catch (JSONException e) {
        DatabaseConfig.getLogger().log(Level.WARNING, "{0} Unable to parse JSON: {1}", new Object[] { dbName, e.getMessage() });
        return null;
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} lookupEntireRecord 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) 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) DBCursor(com.mongodb.DBCursor) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) Map(java.util.Map) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap)

Example 9 with RecordNotFoundException

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

the class NoSQLTest method test_06_CheckForRecordGone.

/**
   *
   */
@Test
public void test_06_CheckForRecordGone() {
    try {
        JSONObject json = instance.lookupEntireRecord(collection, guid);
        fail("Record should not exist: " + json);
    } catch (RecordNotFoundException | FailedDBOperationException e) {
    }
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) Test(org.junit.Test)

Example 10 with RecordNotFoundException

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

the class NoSQLTest method test_03_LookupEntireRecord.

/**
   *
   */
@Test
public void test_03_LookupEntireRecord() {
    JSONObject json = new JSONObject();
    try {
        json.put(field, "some value");
    } catch (JSONException e) {
        fail("Problem creating json: " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    JSONObject expected = new JSONObject();
    try {
        expected.put(NameRecord.NAME.getName(), guid);
        expected.put(NameRecord.VALUES_MAP.getName(), valuesMap);
    } catch (JSONException e) {
        fail("Problem creating json expected value: " + e);
    }
    try {
        JSONAssert.assertEquals(expected, instance.lookupEntireRecord(collection, guid), JSONCompareMode.STRICT);
    } catch (RecordNotFoundException | FailedDBOperationException | JSONException e) {
        fail("Problem during LookupEntireRecord: " + e);
    }
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) Test(org.junit.Test)

Aggregations

RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)16 JSONObject (org.json.JSONObject)15 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)14 JSONException (org.json.JSONException)12 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)10 Test (org.junit.Test)6 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)5 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)4 HashMap (java.util.HashMap)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBCollection (com.mongodb.DBCollection)2 DBObject (com.mongodb.DBObject)2 MongoException (com.mongodb.MongoException)2 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)2 DBCursor (com.mongodb.DBCursor)1 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)1 GNSRecordMap (edu.umass.cs.gnsserver.gnsapp.recordmap.GNSRecordMap)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1