use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class ActiveNonBlockingRunner method main.
/**
* Test throughput with multithread worker with multiple script engine
* @param args
* @throws JSONException
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws JSONException, InterruptedException, ExecutionException {
int numThread = 10;
final ActiveNonBlockingRunner[] runners = new ActiveNonBlockingRunner[numThread];
for (int i = 0; i < numThread; i++) {
runners[i] = new ActiveNonBlockingRunner(null, null);
}
final ThreadPoolExecutor executor = new ThreadPoolExecutor(numThread, numThread, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
executor.prestartAllCoreThreads();
ArrayList<Future<String>> tasks = new ArrayList<Future<String>>();
String guid = "zhaoyu";
String field = "gao";
String noop_code = "";
try {
noop_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/noop.js")));
} catch (IOException e) {
e.printStackTrace();
}
ValuesMap value = new ValuesMap();
value.put("string", "hello world");
ActiveMessage msg = new ActiveMessage(guid, field, noop_code, value.toString(), 0, 500);
int n = 1000000;
long t1 = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
tasks.add(executor.submit(new SimpleTask(runners[0], msg)));
}
for (Future<String> task : tasks) {
task.get();
}
long elapsed = System.currentTimeMillis() - t1;
System.out.println("It takes " + elapsed + "ms, and the average latency for each operation is " + (elapsed * 1000.0 / n) + "us");
System.out.println("The throughput is " + n * 1000.0 / elapsed);
/**
* Test runner's protected method
*/
ActiveNonBlockingRunner runner = new ActiveNonBlockingRunner(null, null);
String chain_code = null;
try {
//chain_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/permissionTest.js")));
chain_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/permissionTest.js")));
//chain_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/testLoad.js")));
} catch (IOException e) {
e.printStackTrace();
}
try {
runner.runCode(guid, field, chain_code, value.toString(), 0, 0);
// fail here
assert (false) : "The code should not be here";
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class NameResolution method lookupGuidField.
/**
* Lookup the field or fields in the guid.
* Returns a JSONObject containing the fields and values
* or null if the domainName doesn't exist.
* @param addr
* @param id
* @param domain - the HRN of the guid
* @param field - the field to lookup (mutually exclusive with fieldNames)
* @param fields - the fields to lookup (mutually exclusive with fieldNames)
* @param handler
* @return a JSONObject containing the fields and values or null
*/
public static JSONObject lookupGuidField(String addr, int id, String domain, String field, ArrayList<String> fields, ClientRequestHandlerInterface handler) {
/**
* Querying multiple types together is allowed in DNS protocol, but practically not supported.
* Therefore, no need for us to implement support for multi-type query.
*/
/**
* 1. Lookup guid for the domain name
*/
String guid = null;
final ValuesMap result;
try {
result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, domain, HRN_GUID, handler.getApp(), false);
if (result != null) {
guid = result.getString(HRN_GUID);
}
} catch (FailedDBOperationException | JSONException e) {
NameResolution.getLogger().log(Level.FINE, "No guid for {0}: {1}", new Object[] { domain, e });
return null;
}
/**
* 2. Lookup the record
*/
JSONObject value = null;
if (guid != null) {
// Generate a DNS header for local read
InternalRequestHeader header = new InternalRequestHeader() {
@Override
public long getOriginatingRequestID() {
return id;
}
@Override
public String getOriginatingGUID() {
try {
return result.getString(HRN_GUID);
} catch (JSONException e) {
return null;
}
}
@Override
public int getTTL() {
return InternalRequestHeader.DEFAULT_TTL;
}
@Override
public boolean hasBeenCoordinatedOnce() {
// DNS request does not need coordination
return false;
}
@Override
public String getSourceAddress() {
return addr;
}
};
try {
value = NSFieldAccess.lookupFieldsLocalNoAuth(header, guid, fields, ColumnFieldType.USER_JSON, handler);
} catch (FailedDBOperationException e) {
NameResolution.getLogger().log(Level.FINE, "Fetching record failed for {0}: {1}", new Object[] { domain, e });
}
} else {
NameResolution.getLogger().log(Level.FINE, "No guid for {0} is found", new Object[] { domain });
}
return value;
}
use of edu.umass.cs.gnsserver.utils.ValuesMap 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.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class DiskMapRecords method lookupSomeFields.
@Override
public HashMap<ColumnField, Object> lookupSomeFields(String collection, String name, ColumnField nameField, ColumnField valuesMapField, ArrayList<ColumnField> valuesMapKeys) throws RecordNotFoundException, FailedDBOperationException {
JSONObject record = lookupEntireRecord(collection, name);
// LOGGER.log(Level.FINE, "Full record " + record.toString());
HashMap<ColumnField, Object> hashMap = new HashMap<>();
hashMap.put(nameField, name);
if (valuesMapField != null && valuesMapKeys != null) {
try {
JSONObject readValuesMap = record.getJSONObject(valuesMapField.getName());
// LOGGER.log(Level.FINE, "Read valuesMap " + readValuesMap.toString());
ValuesMap valuesMapOut = new ValuesMap();
for (int i = 0; i < valuesMapKeys.size(); i++) {
String userKey = valuesMapKeys.get(i).getName();
if (JSONDotNotation.containsFieldDotNotation(userKey, readValuesMap) == false) {
// LOGGER.fine("valuesMap doesn't contain " + userKey);
continue;
}
try {
switch(valuesMapKeys.get(i).type()) {
case USER_JSON:
Object value = JSONDotNotation.getWithDotNotation(userKey, readValuesMap);
LOGGER.log(Level.FINE, "Object is {0}", new Object[] { value.toString() });
valuesMapOut.put(userKey, value);
break;
case LIST_STRING:
valuesMapOut.putAsArray(userKey, JSONUtils.JSONArrayToResultValue(new JSONArray(JSONDotNotation.getWithDotNotation(userKey, readValuesMap).toString())));
break;
default:
LOGGER.log(Level.SEVERE, "ERROR: Error: User keys field {0} is not a known type:{1}", new Object[] { userKey, valuesMapKeys.get(i).type() });
break;
}
} catch (JSONException e) {
LOGGER.log(Level.SEVERE, "Error parsing json: {0}", e.getMessage());
}
}
hashMap.put(valuesMapField, valuesMapOut);
} catch (JSONException e) {
LOGGER.log(Level.SEVERE, "Problem getting values map: {0}", e.getMessage());
}
}
return hashMap;
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class NSGroupAccess method lookupFieldInGroupGuid.
/**
* Returns the values of a field in a set of guids contained in a group guid.
* The result is return as an array of values.
*
* @param header
* @param groupGuid
* @param field
* @param gnsApp
* @return a ValuesMap containing the field with an array of the values
* @throws FailedDBOperationException
* @throws JSONException
*/
public static ValuesMap lookupFieldInGroupGuid(InternalRequestHeader header, String groupGuid, String field, GNSApplicationInterface<String> gnsApp) throws FailedDBOperationException, JSONException {
JSONArray resultArray = new JSONArray();
for (Object guidObject : lookupMembers(header, groupGuid, false, gnsApp.getRequestHandler())) {
String guid = (String) guidObject;
ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldAnywhere(header, guid, field, gnsApp);
if (valuesMap != null && valuesMap.has(field)) {
resultArray.put(valuesMap.get(field));
}
}
ClientSupportConfig.getLogger().log(Level.FINE, "Group result for {0}/{1} = {2}", new Object[] { groupGuid, field, resultArray.toString() });
ValuesMap result = new ValuesMap();
result.put(field, resultArray);
return result;
}
Aggregations