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