use of com.couchbase.client.core.msg.analytics.AnalyticsResponse in project couchbase-jdbc-driver by couchbaselabs.
the class AnalyticsProtocol method fetchResult.
@Override
public JsonParser fetchResult(QueryServiceResponse response, SubmitStatementOptions options) throws SQLException {
int p = response.handle.lastIndexOf("/");
if (p < 0) {
throw new SQLNonTransientConnectionException("Protocol error - could not extract deferred ID");
}
String handlePath = response.handle.substring(p);
Core core = connectionHandle.core();
CoreContext ctx = core.context();
AnalyticsRequest request = new AnalyticsRequest(getTimeout(options), ctx, ctx.environment().retryStrategy(), ctx.authenticator(), null, AnalyticsRequest.NO_PRIORITY, true, UUID.randomUUID().toString(), "", null, null, null, QUERY_RESULT_ENDPOINT_PATH + handlePath, HttpMethod.GET);
core.send(request);
try {
AnalyticsResponse analyticsResponse = block(request.response());
PipedOutputStream pos = new PipedOutputStream();
InputStream is = new PipedInputStream(pos);
rowExecutor.submit(() -> {
try {
final AtomicBoolean first = new AtomicBoolean(true);
Stream<AnalyticsChunkRow> rows = analyticsResponse.rows().toStream();
pos.write('[');
rows.forEach(row -> {
try {
if (!first.compareAndSet(true, false)) {
pos.write(',');
}
pos.write(row.data());
} catch (IOException e) {
throw new RuntimeException("Failed to parse JSON row", e);
}
});
pos.write(']');
} catch (Exception e) {
throw new RuntimeException("Failure during streaming rows", e);
} finally {
try {
pos.close();
} catch (IOException e) {
// ignored.
}
}
});
return driverContext.getGenericObjectReader().getFactory().createParser(is);
} catch (JsonProcessingException e) {
throw getErrorReporter().errorInProtocol(e);
} catch (IOException e) {
throw getErrorReporter().errorInConnection(e);
} catch (Exception e) {
throw getErrorReporter().errorInConnection(e.getMessage());
}
}
Aggregations