use of io.syndesis.server.jsondb.JsonDBException in project syndesis by syndesisio.
the class SqlJsonDB method update.
@Override
public void update(String path, InputStream is) {
ArrayList<String> updatePaths = new ArrayList<>();
withTransaction(dbi -> {
try {
BatchManager mb = new BatchManager(dbi);
try (JsonParser jp = new JsonFactory().createParser(is)) {
JsonToken nextToken = jp.nextToken();
if (nextToken != JsonToken.START_OBJECT) {
throw new JsonParseException(jp, "Update did not contain a json object");
}
while (true) {
nextToken = jp.nextToken();
if (nextToken == JsonToken.END_OBJECT) {
break;
}
if (nextToken != JsonToken.FIELD_NAME) {
throw new JsonParseException(jp, "Expected a field name");
}
String key = suffix(path, "/") + jp.getCurrentName();
updatePaths.add(key);
String baseDBPath = JsonRecordSupport.convertToDBPath(key);
mb.deleteRecordsForSet(baseDBPath);
try {
JsonRecordSupport.jsonStreamToRecords(indexPaths, jp, baseDBPath, mb.createSetConsumer());
} catch (IOException e) {
throw new JsonDBException(e);
}
}
nextToken = jp.nextToken();
if (nextToken != null) {
throw new JsonParseException(jp, "Document did not terminate as expected.");
}
mb.flush();
}
} catch (IOException e) {
throw new JsonDBException(e);
}
});
if (bus != null) {
for (String updatePath : updatePaths) {
bus.broadcast("jsondb-updated", prefix(trimSuffix(updatePath, "/"), "/"));
}
}
}
Aggregations