Search in sources :

Example 36 with JSONArray

use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.

the class SnapshotDaemon method retrievePerPartitionTransactionIds.

/*
     * If this cluster has per partition transactions ids carried over from
     * previous instances, retrieve them from ZK and pass them to snapshot save so that it can
     * include them in the snapshot
     */
private JSONArray retrievePerPartitionTransactionIds() {
    JSONArray retval = new JSONArray();
    try {
        ByteBuffer values = ByteBuffer.wrap(m_zk.getData(VoltZK.perPartitionTxnIds, false, null));
        int numKeys = values.getInt();
        for (int ii = 0; ii < numKeys; ii++) {
            retval.put(values.getLong());
        }
    } catch (KeeperException.NoNodeException e) {
    /*doesn't have to exist*/
    } catch (Exception e) {
        VoltDB.crashLocalVoltDB("Failed to retrieve per partition transaction ids for snapshot", false, e);
    }
    return retval;
}
Also used : JSONArray(org.json_voltpatches.JSONArray) ByteBuffer(java.nio.ByteBuffer) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) JSONException(org.json_voltpatches.JSONException) NodeExistsException(org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) ExecutionException(java.util.concurrent.ExecutionException)

Example 37 with JSONArray

use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.

the class ParameterSet method paramFromPossibleJSON.

static Object paramFromPossibleJSON(Object value) throws JSONException, IOException {
    if (value instanceof JSONObject) {
        JSONObject jsonObj = (JSONObject) value;
        return VoltTable.fromJSONObject(jsonObj);
    }
    if (value instanceof JSONArray) {
        JSONArray array = (JSONArray) value;
        Object[] retval = new Object[array.length()];
        for (int i = 0; i < array.length(); i++) {
            Object valueAtIndex = array.get(i);
            retval[i] = paramFromPossibleJSON(valueAtIndex);
        }
        return retval;
    }
    return value;
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONArray(org.json_voltpatches.JSONArray) JSONObject(org.json_voltpatches.JSONObject)

Example 38 with JSONArray

use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.

the class VoltTable method fromJSONObject.

/**
     * <p>Construct a table from a JSON object. Only parses VoltDB VoltTable JSON format.</p>
     *
     * @param json String containing JSON-formatted table data.
     * @return Constructed <code>VoltTable</code> instance.
     * @throws JSONException on JSON-related error.
     * @throws IOException if thrown by our JSON library.
     */
public static VoltTable fromJSONObject(JSONObject json) throws JSONException, IOException {
    // extract the schema and creat an empty table
    JSONArray jsonCols = json.getJSONArray(JSON_SCHEMA_KEY);
    ColumnInfo[] columns = new ColumnInfo[jsonCols.length()];
    for (int i = 0; i < jsonCols.length(); i++) {
        JSONObject jsonCol = jsonCols.getJSONObject(i);
        String name = jsonCol.getString(JSON_NAME_KEY);
        VoltType type = VoltType.get((byte) jsonCol.getInt(JSON_TYPE_KEY));
        columns[i] = new ColumnInfo(name, type);
    }
    VoltTable t = new VoltTable(columns);
    // set the status byte
    byte status = (byte) json.getInt(JSON_STATUS_KEY);
    t.setStatusCode(status);
    // load the row data
    JSONArray data = json.getJSONArray(JSON_DATA_KEY);
    for (int i = 0; i < data.length(); i++) {
        JSONArray jsonRow = data.getJSONArray(i);
        assert (jsonRow.length() == jsonCols.length());
        Object[] row = new Object[jsonRow.length()];
        for (int j = 0; j < jsonRow.length(); j++) {
            row[j] = jsonRow.get(j);
            if (row[j] == JSONObject.NULL)
                row[j] = null;
            VoltType type = columns[j].type;
            // convert strings to numbers
            if (row[j] != null) {
                switch(type) {
                    case BIGINT:
                    case INTEGER:
                    case SMALLINT:
                    case TINYINT:
                    case TIMESTAMP:
                        if (row[j] instanceof String) {
                            row[j] = Long.parseLong((String) row[j]);
                        }
                        assert (row[j] instanceof Number);
                        break;
                    case DECIMAL:
                        String decVal;
                        if (row[j] instanceof String)
                            decVal = (String) row[j];
                        else
                            decVal = row[j].toString();
                        if (decVal.compareToIgnoreCase("NULL") == 0)
                            row[j] = null;
                        else
                            row[j] = VoltDecimalHelper.deserializeBigDecimalFromString(decVal);
                        break;
                    case FLOAT:
                        if (row[j] instanceof String) {
                            row[j] = Double.parseDouble((String) row[j]);
                        }
                        assert (row[j] instanceof Number);
                        break;
                    default:
                }
            }
        }
        t.addRow(row);
    }
    return t;
}
Also used : JSONObject(org.json_voltpatches.JSONObject) JSONArray(org.json_voltpatches.JSONArray) JSONObject(org.json_voltpatches.JSONObject) JSONString(org.json_voltpatches.JSONString)

Example 39 with JSONArray

use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.

the class AbstractPlanNode method loadSchemaFromJSONObject.

protected static NodeSchema loadSchemaFromJSONObject(JSONObject jobj, String jsonKey) throws JSONException {
    NodeSchema nodeSchema = new NodeSchema();
    JSONArray jarray = jobj.getJSONArray(jsonKey);
    int size = jarray.length();
    for (int i = 0; i < size; ++i) {
        nodeSchema.addColumn(SchemaColumn.fromJSONObject(jarray.getJSONObject(i)));
    }
    return nodeSchema;
}
Also used : JSONArray(org.json_voltpatches.JSONArray)

Example 40 with JSONArray

use of org.json_voltpatches.JSONArray in project voltdb by VoltDB.

the class AbstractPlanNode method loadIntArrayMemberFromJSON.

/**
     * @param jobj
     * @param key
     * @return
     * @throws JSONException
     */
int[] loadIntArrayMemberFromJSON(JSONObject jobj, String key) throws JSONException {
    if (jobj.isNull(key)) {
        return null;
    }
    JSONArray jarray = jobj.getJSONArray(key);
    int numElems = jarray.length();
    int[] result = new int[numElems];
    for (int ii = 0; ii < numElems; ++ii) {
        result[ii] = jarray.getInt(ii);
    }
    return result;
}
Also used : JSONArray(org.json_voltpatches.JSONArray)

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