use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class WindowFunctionPlanNode method loadFromJSONObject.
/**
* Deserialize a PartitionByPlanNode from JSON. Since we don't need the
* sort directions, and we don't serialize them in toJSONString, then we
* can't in general get them here.
*/
@Override
public void loadFromJSONObject(JSONObject jobj, Database db) throws JSONException {
helpLoadFromJSONObject(jobj, db);
JSONArray jarray = jobj.getJSONArray(Members.AGGREGATE_COLUMNS.name());
int size = jarray.length();
for (int i = 0; i < size; i++) {
// We only expect one of these for now.
assert (i == 0);
JSONObject tempObj = jarray.getJSONObject(i);
m_aggregateTypes.add(ExpressionType.get(tempObj.getString(Members.AGGREGATE_TYPE.name())));
m_aggregateOutputColumns.add(tempObj.getInt(Members.AGGREGATE_OUTPUT_COLUMN.name()));
m_aggregateExpressions.add(AbstractExpression.loadFromJSONArrayChild(null, tempObj, Members.AGGREGATE_EXPRESSIONS.name(), null));
}
m_partitionByExpressions = AbstractExpression.loadFromJSONArrayChild(null, jobj, Members.PARTITIONBY_EXPRESSIONS.name(), null);
m_orderByExpressions = new ArrayList<>();
AbstractExpression.loadSortListFromJSONArray(m_orderByExpressions, null, jobj);
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class HTTPUtils method responseFromJSON.
public static Response responseFromJSON(String jsonStr) throws JSONException, IOException {
Response response = new Response();
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray resultsJson = jsonObj.getJSONArray("results");
response.results = new VoltTable[resultsJson.length()];
for (int i = 0; i < response.results.length; i++) {
JSONObject tableJson = resultsJson.getJSONObject(i);
response.results[i] = VoltTable.fromJSONObject(tableJson);
System.out.println(response.results[i].toString());
}
if (jsonObj.isNull("status") == false) {
response.status = (byte) jsonObj.getInt("status");
}
if (jsonObj.isNull("appstatus") == false) {
response.appStatus = (byte) jsonObj.getInt("appstatus");
}
if (jsonObj.isNull("statusstring") == false) {
response.statusString = jsonObj.getString("statusstring");
}
if (jsonObj.isNull("appstatusstring") == false) {
response.appStatusString = jsonObj.getString("appstatusstring");
}
if (jsonObj.isNull("exception") == false) {
response.exception = jsonObj.getString("exception");
}
return response;
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class SnapshotUtil method CRCCheck.
/**
*
* This isn't just a CRC check. It also loads the file and returns it as
* a JSON object.
* Check if the CRC of the snapshot digest. Note that this only checks if
* the CRC at the beginning of the digest file matches the CRC of the digest
* file itself.
*
* @param f
* The snapshot digest file object
* @return The table list as a string
* @throws IOException
* If CRC does not match
*/
public static JSONObject CRCCheck(File f, VoltLogger logger) throws IOException {
final FileInputStream fis = new FileInputStream(f);
try {
final BufferedInputStream bis = new BufferedInputStream(fis);
ByteBuffer crcBuffer = ByteBuffer.allocate(4);
if (4 != bis.read(crcBuffer.array())) {
logger.warn("EOF while attempting to read CRC from snapshot digest " + f + " on host " + CoreUtils.getHostnameOrAddress());
return null;
}
final int crc = crcBuffer.getInt();
final InputStreamReader isr = new InputStreamReader(bis, StandardCharsets.UTF_8);
CharArrayWriter caw = new CharArrayWriter();
while (true) {
int nextChar = isr.read();
if (nextChar == -1) {
break;
}
//digests
if (nextChar == '\n') {
break;
}
caw.write(nextChar);
}
/*
* Try and parse the contents as a JSON object. If it succeeds then assume
* it is a the new version of the digest file. It is unlikely the old version
* will successfully parse as JSON because it starts with a number
* instead of an open brace.
*/
JSONObject obj = null;
try {
obj = new JSONObject(caw.toString());
} catch (JSONException e) {
//assume it is the old format
}
/*
* Convert the old style file to a JSONObject so it can be presented
* via a consistent interface.
*/
if (obj == null) {
String tableList = caw.toString();
byte[] tableListBytes = tableList.getBytes(StandardCharsets.UTF_8);
PureJavaCrc32 tableListCRC = new PureJavaCrc32();
tableListCRC.update(tableListBytes);
tableListCRC.update("\n".getBytes(StandardCharsets.UTF_8));
final int calculatedValue = (int) tableListCRC.getValue();
if (crc != calculatedValue) {
logger.warn("CRC of snapshot digest " + f + " did not match digest contents");
return null;
}
String[] tableNames = tableList.split(",");
long txnId = Long.valueOf(tableNames[0]);
obj = new JSONObject();
try {
obj.put("version", 0);
obj.put("txnId", txnId);
for (int ii = 1; ii < tableNames.length; ii++) {
obj.append("tables", tableNames[ii]);
}
} catch (JSONException e) {
logger.warn("Exception parsing JSON of digest " + f, e);
return null;
}
return obj;
} else {
/*
* Verify the CRC and then return the data as a JSON object.
*/
String tableList = caw.toString();
byte[] tableListBytes = tableList.getBytes(StandardCharsets.UTF_8);
PureJavaCrc32 tableListCRC = new PureJavaCrc32();
tableListCRC.update(tableListBytes);
final int calculatedValue = (int) tableListCRC.getValue();
if (crc != calculatedValue) {
logger.warn("CRC of snapshot digest " + f + " did not match digest contents");
return null;
}
return obj;
}
} catch (Exception e) {
logger.warn("Exception while parsing snapshot digest " + f, e);
return null;
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
}
}
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class StreamSnapshotRequestConfig method parseStreams.
private ImmutableList<Stream> parseStreams(JSONObject jsData) {
ImmutableList.Builder<Stream> builder = ImmutableList.builder();
try {
JSONArray streamArray = jsData.getJSONArray("streams");
for (int i = 0; i < streamArray.length(); i++) {
JSONObject streamObj = streamArray.getJSONObject(i);
Integer newPartition = null;
if (!streamObj.isNull("newPartition")) {
newPartition = Integer.parseInt(streamObj.getString("newPartition"));
}
Stream config = new Stream(parseStreamPairs(streamObj), newPartition);
builder.add(config);
}
} catch (JSONException e) {
SNAP_LOG.warn("Failed to parse stream snapshot request config", e);
}
return builder.build();
}
use of org.json_voltpatches.JSONObject in project voltdb by VoltDB.
the class TestCartographer method testSPMasterChange.
@Test
public void testSPMasterChange() throws Exception {
ZooKeeper zk = getClient(0);
VoltZK.createPersistentZKNodes(zk);
LeaderCache spwriter = new LeaderCache(zk, VoltZK.iv2masters);
HostMessenger hm = mock(HostMessenger.class);
when(hm.getZK()).thenReturn(m_messengers.get(0).getZK());
Cartographer dut = new Cartographer(hm, 0, false);
// Startup partitions
spwriter.start(true);
spwriter.put(0, 0l);
verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
reset(hm);
spwriter.put(1, 1l);
verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
reset(hm);
spwriter.put(2, 2l);
verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
reset(hm);
// now change master for part 0
spwriter.put(0, 3l);
ArgumentCaptor<Long> hsIdCaptor = ArgumentCaptor.forClass(Long.class);
ArgumentCaptor<BinaryPayloadMessage> bpmCaptor = ArgumentCaptor.forClass(BinaryPayloadMessage.class);
verify(hm, timeout(10000)).send(hsIdCaptor.capture(), bpmCaptor.capture());
JSONObject jsObj = new JSONObject(new String(bpmCaptor.getValue().m_payload, "UTF-8"));
System.out.println("BPM: " + jsObj.toString());
final int partitionId = jsObj.getInt(Cartographer.JSON_PARTITION_ID);
final long initiatorHSId = jsObj.getLong(Cartographer.JSON_INITIATOR_HSID);
assertEquals(0, partitionId);
assertEquals(3, initiatorHSId);
spwriter.shutdown();
}
Aggregations