use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class SystemInformation method populatePartitionGroups.
private static void populatePartitionGroups(Integer hostId, VoltTable vt) {
try {
byte[] bytes = VoltDB.instance().getHostMessenger().getZK().getData(CoreZK.hosts_host + hostId, false, new Stat());
String hostInfo = new String(bytes, StandardCharsets.UTF_8);
JSONObject obj = new JSONObject(hostInfo);
vt.addRow(hostId, "PLACEMENTGROUP", obj.getString("group"));
} catch (KeeperException | InterruptedException | JSONException e) {
vt.addRow(hostId, "PLACEMENTGROUP", "NULL");
}
Set<Integer> buddies = VoltDB.instance().getCartograhper().getHostIdsWithinPartitionGroup(hostId);
String[] strIds = buddies.stream().sorted().map(i -> String.valueOf(i)).toArray(String[]::new);
vt.addRow(hostId, "PARTITIONGROUP", String.join(",", strIds));
}
use of org.json_voltpatches.JSONObject 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.JSONObject in project voltdb by VoltDB.
the class TestJdbcDatabaseMetaDataGenerator method testGetTables.
public void testGetTables() throws Exception {
String schema = "create table Table1 (Column1 varchar(10) not null, Column2 integer);" + "partition table Table1 on column Column1;" + "create table Table2 (Column1 integer);" + "create view View1 (Column1, num) as select Column1, count(*) from Table1 group by Column1;" + "create view View2 (Column2, num) as select Column2, count(*) from Table1 group by Column2;" + "create stream Export1 (Column1 integer);" + "create stream Export2 export to target foo (Column1 integer);" + "create procedure sample as select * from Table1;";
VoltCompiler c = compileForDDLTest2(schema);
System.out.println(c.getCatalog().serialize());
JdbcDatabaseMetaDataGenerator dut = new JdbcDatabaseMetaDataGenerator(c.getCatalog(), null, new InMemoryJarfile(testout_jar));
VoltTable tables = dut.getMetaData("tables");
System.out.println(tables);
assertEquals(10, tables.getColumnCount());
assertEquals(6, tables.getRowCount());
assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Table1"));
assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("TABLE"));
assertTrue(tables.get("REMARKS", VoltType.STRING).equals("{\"partitionColumn\":\"COLUMN1\"}"));
assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Table2"));
assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("TABLE"));
assertEquals(null, tables.get("REMARKS", VoltType.STRING));
assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "View1"));
assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("VIEW"));
assertTrue(tables.get("REMARKS", VoltType.STRING).equals(new JSONObject("{\"partitionColumn\":\"COLUMN1\",\"sourceTable\":\"TABLE1\"}").toString()));
assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "View2"));
assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("VIEW"));
assertTrue(tables.get("REMARKS", VoltType.STRING).equals(new JSONObject("{\"partitionColumn\":\"COLUMN1\",\"sourceTable\":\"TABLE1\"}").toString()));
assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Export1"));
assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("EXPORT"));
assertTrue(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "Export2"));
assertTrue(tables.get("TABLE_TYPE", VoltType.STRING).equals("EXPORT"));
assertFalse(VoltTableTestHelpers.moveToMatchingRow(tables, "TABLE_NAME", "NotATable"));
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class PlannerTestAideDeCamp method compile.
/**
* Compile and cache the statement and plan and return the final plan graph.
*/
private List<AbstractPlanNode> compile(String sql, int paramCount, String joinOrder, boolean inferPartitioning, boolean forceSingle, DeterminismMode detMode) {
String stmtLabel = "stmt-" + String.valueOf(compileCounter++);
Statement catalogStmt = proc.getStatements().add(stmtLabel);
catalogStmt.setSqltext(sql);
catalogStmt.setSinglepartition(forceSingle);
// determine the type of the query
QueryType qtype = QueryType.SELECT;
catalogStmt.setReadonly(true);
if (sql.toLowerCase().startsWith("insert")) {
qtype = QueryType.INSERT;
catalogStmt.setReadonly(false);
}
if (sql.toLowerCase().startsWith("update")) {
qtype = QueryType.UPDATE;
catalogStmt.setReadonly(false);
}
if (sql.toLowerCase().startsWith("delete")) {
qtype = QueryType.DELETE;
catalogStmt.setReadonly(false);
}
catalogStmt.setQuerytype(qtype.getValue());
// name will look like "basename-stmt-#"
String name = catalogStmt.getParent().getTypeName() + "-" + catalogStmt.getTypeName();
DatabaseEstimates estimates = new DatabaseEstimates();
TrivialCostModel costModel = new TrivialCostModel();
StatementPartitioning partitioning;
if (inferPartitioning) {
partitioning = StatementPartitioning.inferPartitioning();
} else if (forceSingle) {
partitioning = StatementPartitioning.forceSP();
} else {
partitioning = StatementPartitioning.forceMP();
}
String procName = catalogStmt.getParent().getTypeName();
QueryPlanner planner = new QueryPlanner(sql, stmtLabel, procName, db, partitioning, hsql, estimates, false, StatementCompiler.DEFAULT_MAX_JOIN_TABLES, costModel, null, joinOrder, detMode);
CompiledPlan plan = null;
planner.parse();
plan = planner.plan();
assert (plan != null);
// Partitioning optionally inferred from the planning process.
if (partitioning.isInferred()) {
catalogStmt.setSinglepartition(partitioning.isInferredSingle());
}
// We will need to update the system catalogs with this new information
for (int i = 0; i < plan.parameters.length; ++i) {
StmtParameter catalogParam = catalogStmt.getParameters().add(String.valueOf(i));
ParameterValueExpression pve = plan.parameters[i];
catalogParam.setJavatype(pve.getValueType().getValue());
catalogParam.setIsarray(pve.getParamIsVector());
catalogParam.setIndex(i);
}
List<PlanNodeList> nodeLists = new ArrayList<>();
nodeLists.add(new PlanNodeList(plan.rootPlanGraph));
if (plan.subPlanGraph != null) {
nodeLists.add(new PlanNodeList(plan.subPlanGraph));
}
// Now update our catalog information
// HACK: We're using the node_tree's hashCode() as it's name. It would be really
// nice if the Catalog code give us an guid without needing a name first...
String json = null;
try {
JSONObject jobj = new JSONObject(nodeLists.get(0).toJSONString());
json = jobj.toString(4);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
System.exit(-1);
return null;
}
//
try {
BuildDirectoryUtils.writeFile("statement-plans", name + "_json.txt", json, true);
BuildDirectoryUtils.writeFile("statement-plans", name + ".dot", nodeLists.get(0).toDOTString("name"), true);
} catch (Exception e) {
e.printStackTrace();
}
List<AbstractPlanNode> plannodes = new ArrayList<>();
for (PlanNodeList nodeList : nodeLists) {
plannodes.add(nodeList.getRootPlanNode());
}
m_currentPlan = plan;
return plannodes;
}
use of org.json_voltpatches.JSONObject 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