Search in sources :

Example 11 with FailedDBOperationException

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();
}
Also used : BulkWriteOperation(com.mongodb.BulkWriteOperation) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) DBObject(com.mongodb.DBObject) 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) BulkWriteResult(com.mongodb.BulkWriteResult)

Example 12 with FailedDBOperationException

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();
    }
}
Also used : DBCollection(com.mongodb.DBCollection) 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 13 with FailedDBOperationException

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();
    }
}
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 14 with FailedDBOperationException

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);
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) SelectRequestPacket(edu.umass.cs.gnsserver.gnsapp.packet.SelectRequestPacket) IOException(java.io.IOException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 15 with FailedDBOperationException

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);
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) 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