Search in sources :

Example 1 with RecordNotFoundException

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

the class NoSQLRecordsThroughputTest method testlookupMultipleSystemAndUserFields.

private static void testlookupMultipleSystemAndUserFields(String node, String guid, String field) {
    //NoSQLRecords instance = new MongoRecords<String>(node);
    NoSQLRecords instance = new DiskMapRecords(node);
    GNSRecordMap<String> recordMap = new GNSRecordMap<String>(instance, COLLECTION_NAME);
    JSONObject json = new JSONObject();
    try {
        json.put(field, "some value");
    } catch (JSONException e) {
        System.out.println("Problem creating json " + e);
    }
    ValuesMap valuesMap = new ValuesMap(json);
    NameRecord nameRecord = new NameRecord(recordMap, guid, valuesMap);
    try {
        instance.insert(COLLECTION_NAME, guid, nameRecord.toJSONObject());
    } catch (JSONException e) {
        System.out.println("Problem writing json " + e);
    } catch (FailedDBOperationException e) {
        System.out.println("Problem adding " + json.toString() + " as value of " + guid + ": " + e);
    } catch (RecordExistsException e) {
        System.out.println(guid + " record already exists in database. Try something else." + e);
    }
    // and try to read it as fast as possible
    try {
        ArrayList<ColumnField> userFields = new ArrayList<>(Arrays.asList(new ColumnField(field, ColumnFieldType.USER_JSON)));
        int frequency = 10000;
        reset();
        do {
            Map<ColumnField, Object> map = instance.lookupSomeFields(COLLECTION_NAME, guid, NameRecord.NAME, NameRecord.VALUES_MAP, userFields);
            if (incrCount() % frequency == 0) {
                System.out.println(map);
                System.out.println(DelayProfiler.getStats());
                System.out.println("op/s = " + Format.formatTime(getCount() * 1000.0 / (System.currentTimeMillis() - initTime)));
                if (getCount() > frequency * 20) {
                    System.out.println("**********************resetting************************");
                    reset();
                }
            }
        } while (true);
    } catch (FailedDBOperationException | RecordNotFoundException e) {
        System.out.println("Lookup failed: " + e);
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) GNSRecordMap(edu.umass.cs.gnsserver.gnsapp.recordmap.GNSRecordMap) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Example 2 with RecordNotFoundException

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

the class ActiveCodeHandler method handleActiveCode.

/**
   * This interface is used for the class out of activecode package to trigger active code.
   * It requires the parameters for running active code such as guid, field, and value.
   * It runs the requests and returns the processed result to the caller.
   *
   *
   * @param header header is needed for depth query
   * @param guid
   * @param field
   * @param action the actions in {@code ActiveCode}
   * @param value
   * @param db db is needed for fetching active code to run
   * @return the processed result as an JSONObject, the original value is returned if there is an error with code execution
   * @throws InternalRequestException
   */
public static JSONObject handleActiveCode(InternalRequestHeader header, String guid, String field, String action, JSONObject value, BasicRecordMap db) throws InternalRequestException {
    if (Config.getGlobalBoolean(GNSConfig.GNSC.DISABLE_ACTIVE_CODE)) {
        return value;
    }
    if (header != null) {
        // this is a depth query, and we do not call the code again, as it will form a infinite loop if not.
        if (guid.equals(header.getOriginatingGUID()) && header.getTTL() < InternalRequestHeader.DEFAULT_TTL) {
            return value;
        }
    } else {
        // without a header, the code can misbehave without any regulation, therefore we return the value immediately if no header presents
        return value;
    }
    long t = System.nanoTime();
    ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO handles:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
    /**
     * Only execute active code for user field
     * FIXME:
     * <p>
     * Read can be a single-field read or multi-field read.
     * If it's a single-field read, then the field can not be a internal field.
     * If it's a multi-field read, then there may be some field is internal.
     * <p>
     * Write has no field value, but if there should not be an internal
     * field in the JSONObject value.
     */
    if ((action.equals(ActiveCode.READ_ACTION) && field != null && InternalField.isInternalField(field)) || (action.equals(ActiveCode.WRITE_ACTION) && ((value != null && containInternalField(value)) || (field != null && InternalField.isInternalField(field))))) {
        ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO no need to handle:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
        return value;
    }
    JSONObject newResult = value;
    if (field == null || !InternalField.isInternalField(field)) {
        //FIXME: Seems like this field lookup all could be replaced by something 
        // like NSFieldAccess.lookupJSONFieldLocalNoAuth
        NameRecord activeCodeNameRecord = null;
        try {
            activeCodeNameRecord = NameRecord.getNameRecordMultiUserFields(db, guid, ColumnFieldType.USER_JSON, ActiveCode.getCodeField(action));
        } catch (RecordNotFoundException | FailedDBOperationException | IllegalArgumentException e) {
            e.printStackTrace();
            return value;
        }
        ValuesMap codeMap = null;
        try {
            codeMap = activeCodeNameRecord.getValuesMap();
        } catch (FieldNotFoundException e) {
            e.printStackTrace();
            return value;
        }
        if (codeMap != null && value != null) {
            String code;
            try {
                code = codeMap.getString(ActiveCode.getCodeField(action));
            } catch (JSONException | IllegalArgumentException e) {
                return value;
            }
            // Prepare values for query
            String accessorGuid = header == null ? guid : header.getOriginatingGUID();
            if (header.getSourceAddress() != null) {
                try {
                    value.put(SOURCE_IP_FIELD, header.getSourceAddress());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            // Run code
            newResult = runCode(header, code, guid, accessorGuid, action, value, header.getTTL());
            // Strip the appended fields
            if (newResult.has(SOURCE_IP_FIELD)) {
                newResult.remove(SOURCE_IP_FIELD);
            }
        } else if (codeMap == null) {
            ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO no code to run:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
        }
    }
    ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO The result after executing active code is {0}", new Object[] { newResult });
    DelayProfiler.updateDelayNano("activeTotal", t);
    return newResult;
}
Also used : ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Example 3 with RecordNotFoundException

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

the class NSAccessSupport method getMataDataForACLCheck.

/**
   * @param guid
   * @param basicRecordMap
   * @return meta data
   */
protected static JSONObject getMataDataForACLCheck(String guid, BasicRecordMap basicRecordMap) {
    /**
	   *  For the rest of ACL and signature check, let's first retrieve
	   *  the entire record, then check ACL.
	   */
    JSONObject json = null;
    /**
	   * 1. Fetch the entire record of guid
	   */
    try {
        long startTime = System.nanoTime();
        /**
		   * An entire record retrieved from DB is a JSON
		   */
        json = basicRecordMap.lookupEntireRecord(guid);
        DelayProfiler.updateDelayNano("lookupEntireRecordForACLCheck", startTime);
    } catch (FailedDBOperationException | RecordNotFoundException e) {
        /**
		   * If the entire record can not be retrieved, then there is no way for us to check
		   * ACL and signature
		   */
        return null;
    }
    if (json == null) {
        // The entire record is null, so we did not find the record
        return null;
    }
    JSONObject metaData = null;
    try {
        metaData = json.getJSONObject(GNSProtocol.META_DATA_FIELD.toString());
    } catch (JSONException e) {
        return null;
    }
    return metaData;
}
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 4 with RecordNotFoundException

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

the class NSFieldAccess method lookupFieldLocalNoAuth.

private static ValuesMap lookupFieldLocalNoAuth(String guid, String field, ColumnFieldType returnFormat, BasicRecordMap database) throws FailedDBOperationException {
    NameRecord nameRecord = null;
    ClientSupportConfig.getLogger().log(Level.FINE, "XXXXXXXXXXXXXXXXXXXXX LOOKUP_FIELD_LOCALLY: {0} : {1}", new Object[] { guid, field });
    // Try to look up the value in the database
    try {
        // Check for the case where we're returning all the fields the entire record.
        if (GNSProtocol.ENTIRE_RECORD.toString().equals(field)) {
            ClientSupportConfig.getLogger().log(Level.FINE, "ALL FIELDS: Format={0}", new Object[] { returnFormat });
            // need everything so just grab all the fields
            nameRecord = NameRecord.getNameRecord(database, guid);
        // Otherwise if field is specified we're just looking up that single field.
        } else if (field != null) {
            ClientSupportConfig.getLogger().log(Level.FINE, "Field={0} Format={1}", new Object[] { field, returnFormat });
            long t = System.nanoTime();
            // otherwise grab the field the user wanted
            nameRecord = NameRecord.getNameRecordMultiUserFields(database, guid, returnFormat, field);
            if (Util.oneIn(100)) {
                DelayProfiler.updateDelayNano("getNameRecordMultiUserFields", t);
            }
        }
        if (nameRecord != null) {
            ClientSupportConfig.getLogger().log(Level.FINE, "VALUES MAP={0}", new Object[] { nameRecord.getValuesMap().toString() });
            return nameRecord.getValuesMap();
        }
    } catch (RecordNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Record not found for name: {0} Key = {1}", new Object[] { guid, field });
    } catch (FieldNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Field not found {0} : {1}", new Object[] { guid, e });
    }
    return null;
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) JSONObject(org.json.JSONObject)

Example 5 with RecordNotFoundException

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

the class NSFieldAccess method lookupFieldsLocalNoAuth.

/**
   *
   * @param header
   * @param guid
   * @param fields
   * @param returnFormat
   * @param handler
   * @return a values map
   * @throws FailedDBOperationException
   */
public static ValuesMap lookupFieldsLocalNoAuth(InternalRequestHeader header, String guid, List<String> fields, ColumnFieldType returnFormat, ClientRequestHandlerInterface handler) throws FailedDBOperationException {
    // Try to look up the value in the database
    try {
        ClientSupportConfig.getLogger().log(Level.FINE, "Fields={0} Format={1}", new Object[] { fields, returnFormat });
        String[] fieldArray = new String[fields.size()];
        fieldArray = fields.toArray(fieldArray);
        // Grab the fields the user wanted
        NameRecord nameRecord = NameRecord.getNameRecordMultiUserFields(handler.getApp().getDB(), guid, returnFormat, fieldArray);
        if (nameRecord != null) {
            // active code handling
            ValuesMap valuesMap = nameRecord.getValuesMap();
            //if (!Config.getGlobalBoolean(GNSConfig.GNSC.DISABLE_ACTIVE_CODE)) {
            try {
                JSONObject result = ActiveCodeHandler.handleActiveCode(header, guid, null, ActiveCode.READ_ACTION, valuesMap, handler.getApp().getDB());
                valuesMap = result != null ? new ValuesMap(result) : valuesMap;
            } catch (InternalRequestException e) {
                e.printStackTrace();
            }
            //}
            return valuesMap;
        }
    } catch (RecordNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Record not found for name: {0}", guid);
    } catch (FieldNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Field not found for {0} : {1}", new Object[] { guid, e });
    }
    return null;
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) JSONObject(org.json.JSONObject)

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