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