Search in sources :

Example 21 with JSONArray

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;
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONArray(org.json_voltpatches.JSONArray) JSONException(org.json_voltpatches.JSONException)

Example 22 with JSONArray

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]);
}
Also used : Table(org.voltdb.catalog.Table) JSONArray(org.json_voltpatches.JSONArray) JSONException(org.json_voltpatches.JSONException)

Example 23 with JSONArray

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 "";
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONArray(org.json_voltpatches.JSONArray) URISyntaxException(java.net.URISyntaxException) BindException(java.net.BindException) IOException(java.io.IOException) LicenseException(org.voltdb.licensetool.LicenseException)

Example 24 with JSONArray

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;
    }
}
Also used : Configuration(org.voltdb.VoltDB.Configuration) HashMap(java.util.HashMap) JSONArray(org.json_voltpatches.JSONArray) JSONObject(org.json_voltpatches.JSONObject) VoltProjectBuilder(org.voltdb.compiler.VoltProjectBuilder) Configuration(org.voltdb.VoltDB.Configuration) UsersType(org.voltdb.compiler.deploymentfile.UsersType) File(java.io.File) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 25 with JSONArray

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();
    }
}
Also used : PlanNodeType(org.voltdb.types.PlanNodeType) JSONObject(org.json_voltpatches.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json_voltpatches.JSONArray) JSONException(org.json_voltpatches.JSONException) JSONString(org.json_voltpatches.JSONString)

Aggregations

JSONArray (org.json_voltpatches.JSONArray)44 JSONObject (org.json_voltpatches.JSONObject)29 JSONException (org.json_voltpatches.JSONException)13 File (java.io.File)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)4 JSONString (org.json_voltpatches.JSONString)4 ByteBuffer (java.nio.ByteBuffer)3 HashSet (java.util.HashSet)3 ImmutableList (com.google_voltpatches.common.collect.ImmutableList)2 InetSocketAddress (java.net.InetSocketAddress)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 ZipFile (java.util.zip.ZipFile)2 HttpResponse (org.apache.http.HttpResponse)2 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)2 InstanceId (org.voltcore.utils.InstanceId)2 Configuration (org.voltdb.VoltDB.Configuration)2 ClientResponse (org.voltdb.client.ClientResponse)2