use of com.couchbase.client.core.msg.analytics.AnalyticsRequest 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());
}
}
use of com.couchbase.client.core.msg.analytics.AnalyticsRequest in project couchbase-jvm-clients by couchbase.
the class AsyncScope method analyticsRequest.
/**
* Helper method to craft an analytics request.
*
* @param statement the statement to use.
* @param opts the built analytics options.
* @return the created analytics request.
*/
AnalyticsRequest analyticsRequest(final String statement, final AnalyticsOptions.Built opts) {
notNullOrEmpty(statement, "Statement", () -> new ReducedAnalyticsErrorContext(statement));
Duration timeout = opts.timeout().orElse(environment.timeoutConfig().analyticsTimeout());
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
JsonObject query = JsonObject.create();
query.put("statement", statement);
query.put("timeout", encodeDurationToMs(timeout));
query.put("query_context", AnalyticsRequest.queryContext(bucketName, scopeName));
opts.injectParams(query);
final byte[] queryBytes = query.toString().getBytes(StandardCharsets.UTF_8);
final String clientContextId = query.getString("client_context_id");
final RequestSpan span = environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ANALYTICS, opts.parentSpan().orElse(null));
AnalyticsRequest request = new AnalyticsRequest(timeout, core.context(), retryStrategy, core.context().authenticator(), queryBytes, opts.priority(), opts.readonly(), clientContextId, statement, span, bucketName, scopeName);
request.context().clientContext(opts.clientContext());
return request;
}
use of com.couchbase.client.core.msg.analytics.AnalyticsRequest in project couchbase-jvm-clients by couchbase.
the class AsyncCluster method analyticsRequest.
/**
* Helper method to craft an analytics request.
*
* @param statement the statement to use.
* @param opts the built analytics options.
* @return the created analytics request.
*/
AnalyticsRequest analyticsRequest(final String statement, final AnalyticsOptions.Built opts) {
notNullOrEmpty(statement, "Statement", () -> new ReducedAnalyticsErrorContext(statement));
Duration timeout = opts.timeout().orElse(environment.get().timeoutConfig().analyticsTimeout());
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.get().retryStrategy());
JsonObject query = JsonObject.create();
query.put("statement", statement);
query.put("timeout", encodeDurationToMs(timeout));
opts.injectParams(query);
final byte[] queryBytes = query.toString().getBytes(StandardCharsets.UTF_8);
final String clientContextId = query.getString("client_context_id");
final RequestSpan span = environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ANALYTICS, opts.parentSpan().orElse(null));
AnalyticsRequest request = new AnalyticsRequest(timeout, core.context(), retryStrategy, authenticator, queryBytes, opts.priority(), opts.readonly(), clientContextId, statement, span, null, null);
request.context().clientContext(opts.clientContext());
return request;
}
Aggregations