use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.
the class StreamSnapshotRequestConfig method parseStreamPairs.
private static Multimap<Long, Long> parseStreamPairs(JSONObject jsData) {
ArrayListMultimap<Long, Long> streamPairs = ArrayListMultimap.create();
if (jsData != null) {
try {
JSONObject sp = jsData.getJSONObject("streamPairs");
Iterator<String> it = sp.keys();
while (it.hasNext()) {
String key = it.next();
long sourceHSId = Long.valueOf(key);
JSONArray destJSONArray = sp.getJSONArray(key);
for (int i = 0; i < destJSONArray.length(); i++) {
long destHSId = destJSONArray.getLong(i);
streamPairs.put(sourceHSId, destHSId);
}
}
} catch (JSONException e) {
SNAP_LOG.warn("Failed to parse stream pair information", e);
}
}
return streamPairs;
}
use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.
the class SnapshotRequestConfig method getTablesToInclude.
private static Table[] getTablesToInclude(JSONObject jsData, Database catalogDatabase) {
final List<Table> tables = SnapshotUtil.getTablesToSave(catalogDatabase);
Set<String> tableNamesToInclude = null;
Set<String> tableNamesToExclude = null;
if (jsData != null) {
JSONArray tableNames = jsData.optJSONArray("tables");
if (tableNames != null) {
tableNamesToInclude = Sets.newHashSet();
for (int i = 0; i < tableNames.length(); i++) {
try {
final String s = tableNames.getString(i).trim().toUpperCase();
if (!s.isEmpty()) {
tableNamesToInclude.add(s);
}
} catch (JSONException e) {
SNAP_LOG.warn("Unable to parse tables to include for snapshot", e);
}
}
}
JSONArray excludeTableNames = jsData.optJSONArray("skiptables");
if (excludeTableNames != null) {
tableNamesToExclude = Sets.newHashSet();
for (int i = 0; i < excludeTableNames.length(); i++) {
try {
final String s = excludeTableNames.getString(i).trim().toUpperCase();
if (!s.isEmpty()) {
tableNamesToExclude.add(s);
}
} catch (JSONException e) {
SNAP_LOG.warn("Unable to parse tables to exclude for snapshot", e);
}
}
}
}
if (tableNamesToInclude != null && tableNamesToInclude.isEmpty()) {
// Stream snapshot may specify empty snapshot sometimes.
tables.clear();
} else {
ListIterator<Table> iter = tables.listIterator();
while (iter.hasNext()) {
Table table = iter.next();
if ((tableNamesToInclude != null && !tableNamesToInclude.remove(table.getTypeName())) || (tableNamesToExclude != null && tableNamesToExclude.remove(table.getTypeName()))) {
// If the table index is not in the list to include or
// is in the list to exclude, remove it
iter.remove();
}
}
}
if (tableNamesToInclude != null && !tableNamesToInclude.isEmpty()) {
throw new IllegalArgumentException("The following tables were specified to include in the snapshot, but are not present in the database: " + Joiner.on(", ").join(tableNamesToInclude));
}
if (tableNamesToExclude != null && !tableNamesToExclude.isEmpty()) {
throw new IllegalArgumentException("The following tables were specified to exclude from the snapshot, but are not present in the database: " + Joiner.on(", ").join(tableNamesToExclude));
}
return tables.toArray(new Table[0]);
}
use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.
the class MiscUtils method formatHostMetadataFromJSON.
public static String formatHostMetadataFromJSON(String json) {
try {
JSONObject obj = new JSONObject(json);
StringBuilder sb = new StringBuilder();
JSONArray interfaces = (JSONArray) obj.get("interfaces");
for (int ii = 0; ii < interfaces.length(); ii++) {
sb.append(interfaces.getString(ii));
if (ii + 1 < interfaces.length()) {
sb.append(" ");
}
}
sb.append(" ");
sb.append(obj.getString("clientPort")).append(',');
sb.append(obj.getString("adminPort")).append(',');
sb.append(obj.getString("httpPort"));
return sb.toString();
} catch (Exception e) {
hostLog.warn("Unable to format host metadata " + json, e);
}
return "";
}
use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.
the class TestJSONInterface method testUsers.
public void testUsers() throws Exception {
try {
String simpleSchema = "CREATE TABLE foo (\n" + " bar BIGINT NOT NULL,\n" + " PRIMARY KEY (bar)\n" + ");";
File schemaFile = VoltProjectBuilder.writeStringToTempFile(simpleSchema);
String schemaPath = schemaFile.getPath();
schemaPath = URLEncoder.encode(schemaPath, "UTF-8");
VoltProjectBuilder builder = new VoltProjectBuilder();
builder.addSchema(schemaPath);
builder.addPartitionInfo("foo", "bar");
builder.addProcedures(DelayProc.class);
builder.setHTTPDPort(8095);
builder.setUseDDLSchema(true);
boolean success = builder.compile(Configuration.getPathToCatalogForTest("json.jar"));
assertTrue(success);
VoltDB.Configuration config = new VoltDB.Configuration();
config.m_pathToCatalog = config.setPathToCatalogForTest("json.jar");
config.m_pathToDeployment = builder.getPathToDeployment();
server = new ServerThread(config);
server.start();
server.waitForInitialization();
//Get users
String json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
assertEquals(json, "[]");
getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo", null, null, null, 404, "application/json");
//Put users
ObjectMapper mapper = new ObjectMapper();
UsersType.User user = new UsersType.User();
user.setName("foo");
user.setPassword("foo");
String map = mapper.writeValueAsString(user);
Map<String, String> params = new HashMap<>();
params.put("user", map);
putUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo/", null, null, null, 201, "application/json", params);
//Get users
json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
JSONArray jarray = new JSONArray(json);
assertEquals(jarray.length(), 1);
JSONObject jobj = jarray.getJSONObject(0);
assertTrue(jobj.getString("id").contains("/deployment/users/foo"));
assertTrue(jobj.getString("roles").equalsIgnoreCase("null"));
//Post users
user.setRoles("foo");
map = mapper.writeValueAsString(user);
params.put("user", map);
postUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo/", null, null, null, 200, "application/json", params);
//Get users
json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
jarray = new JSONArray(json);
assertEquals(jarray.length(), 1);
jobj = jarray.getJSONObject(0);
assertTrue(jobj.getString("roles").equals("foo"));
//Delete users
deleteUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/foo/", null, null, null, 204, "application/json");
//Get users
json = getUrlOverJSON(protocolPrefix + "localhost:8095/deployment/users/", null, null, null, 200, "application/json");
assertEquals(json, "[]");
} finally {
if (server != null) {
server.shutdown();
server.join();
}
server = null;
}
}
use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.
the class PlanNodeTree method loadPlanNodesFromJSONArrays.
/**
* Load plan nodes from the "PLAN_NODE" array. All the nodes are from
* a substatement with the id = stmtId
* @param stmtId
* @param jArray - PLAN_NODES
* @param db
* @throws JSONException
*/
private void loadPlanNodesFromJSONArrays(int stmtId, JSONArray jArray, Database db) {
List<AbstractPlanNode> planNodes = new ArrayList<AbstractPlanNode>();
int size = jArray.length();
try {
for (int i = 0; i < size; i++) {
JSONObject jobj = jArray.getJSONObject(i);
String nodeTypeStr = jobj.getString("PLAN_NODE_TYPE");
PlanNodeType nodeType = PlanNodeType.get(nodeTypeStr);
AbstractPlanNode apn = nodeType.getPlanNodeClass().newInstance();
apn.loadFromJSONObject(jobj, db);
planNodes.add(apn);
}
//link children and parents
for (int i = 0; i < size; i++) {
JSONObject jobj = jArray.getJSONObject(i);
if (jobj.has("CHILDREN_IDS")) {
AbstractPlanNode parent = planNodes.get(i);
JSONArray children = jobj.getJSONArray("CHILDREN_IDS");
for (int j = 0; j < children.length(); j++) {
AbstractPlanNode child = getNodeofId(children.getInt(j), planNodes);
parent.addAndLinkChild(child);
}
}
}
m_planNodesListMap.put(stmtId, planNodes);
} catch (JSONException | InstantiationException | IllegalAccessException e) {
System.err.println(e);
e.printStackTrace();
}
}
Aggregations