use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class MongoRecords method bulkUpdate.
/**
*
* @param collectionName
* @param values
* @throws FailedDBOperationException
* @throws RecordExistsException
*/
public void bulkUpdate(String collectionName, Map<String, JSONObject> values) throws FailedDBOperationException, RecordExistsException {
//String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
DBCollection collection = db.getCollection(collectionName);
String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
db.requestEnsureConnection();
BulkWriteOperation unordered = collection.initializeUnorderedBulkOperation();
for (Map.Entry<String, JSONObject> entry : values.entrySet()) {
BasicDBObject query = new BasicDBObject(primaryKey, entry.getKey());
JSONObject value = entry.getValue();
if (value != null) {
DBObject document;
try {
document = (DBObject) JSON.parse(value.toString());
} catch (Exception e) {
throw new FailedDBOperationException(collectionName, "bulkUpdate", "Unable to parse json" + e.getMessage());
}
unordered.find(query).upsert().replaceOne(document);
} else {
unordered.find(query).removeOne();
}
}
// Maybe check the result?
BulkWriteResult result = unordered.execute();
}
use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class MongoRecords method contains.
@Override
public boolean contains(String collectionName, String guid) throws FailedDBOperationException {
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);
return cursor.hasNext();
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} contains 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 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.FailedDBOperationException in project GNS by MobilityFirst.
the class FieldAccess method selectQuery.
/**
* Sends a select request to the server to retrieve all the guid matching the query.
*
* @param header
* @param commandPacket
* @param reader
* @param query
* @param projection
* @param signature
* @param message
* @param handler
* @return a command response
* @throws InternalRequestException
*/
public static CommandResponse selectQuery(InternalRequestHeader header, CommandPacket commandPacket, String reader, String query, List<String> projection, String signature, String message, ClientRequestHandlerInterface handler) throws InternalRequestException {
if (Select.queryContainsEvil(query)) {
return new CommandResponse(ResponseCode.OPERATION_NOT_SUPPORTED, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.OPERATION_NOT_SUPPORTED.toString() + " Bad query operators in " + query);
}
JSONArray result;
try {
SelectRequestPacket packet = SelectRequestPacket.MakeQueryRequest(-1, reader, query, projection);
result = executeSelectHelper(header, commandPacket, packet, reader, signature, message, handler.getApp());
if (result != null) {
return new CommandResponse(ResponseCode.NO_ERROR, result.toString());
}
} catch (IOException | JSONException | FailedDBOperationException e) {
// FIXME: why silently fail?
}
return new CommandResponse(ResponseCode.NO_ERROR, EMPTY_JSON_ARRAY_STRING);
}
use of edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException in project GNS by MobilityFirst.
the class FieldAccess method lookupMultipleValues.
/**
* Reads the value of all the fields in a guid.
* Doesn't return internal system fields.
*
* @param header
* @param commandPacket
* @param guid
* @param reader
* @param signature
* @param message
* @param handler
* @param timestamp
* @return a command response
*/
public static CommandResponse lookupMultipleValues(InternalRequestHeader header, CommandPacket commandPacket, String guid, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, GNSProtocol.ENTIRE_RECORD.toString(), //fields
null, reader, signature, message, timestamp, handler.getApp());
if (errorCode.isExceptionOrError()) {
return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
}
String resultString;
ResponseCode responseCode;
try {
ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldLocally(header, guid, GNSProtocol.ENTIRE_RECORD.toString(), handler.getApp());
if (valuesMap != null) {
resultString = valuesMap.removeInternalFields().toString();
responseCode = ResponseCode.NO_ERROR;
} else {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.BAD_GUID_ERROR;
}
} catch (FailedDBOperationException e) {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.DATABASE_OPERATION_ERROR;
}
return new CommandResponse(responseCode, resultString);
}
Aggregations