Search in sources :

Example 1 with AnalyticsRequest

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());
    }
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) AnalyticsChunkRow(com.couchbase.client.core.msg.analytics.AnalyticsChunkRow) SQLException(java.sql.SQLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) InvalidDefinitionException(com.fasterxml.jackson.databind.exc.InvalidDefinitionException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AnalyticsResponse(com.couchbase.client.core.msg.analytics.AnalyticsResponse) AnalyticsRequest(com.couchbase.client.core.msg.analytics.AnalyticsRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Core(com.couchbase.client.core.Core)

Example 2 with AnalyticsRequest

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;
}
Also used : ReducedAnalyticsErrorContext(com.couchbase.client.core.error.context.ReducedAnalyticsErrorContext) AnalyticsRequest(com.couchbase.client.core.msg.analytics.AnalyticsRequest) JsonObject(com.couchbase.client.java.json.JsonObject) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 3 with AnalyticsRequest

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;
}
Also used : ReducedAnalyticsErrorContext(com.couchbase.client.core.error.context.ReducedAnalyticsErrorContext) AnalyticsRequest(com.couchbase.client.core.msg.analytics.AnalyticsRequest) JsonObject(com.couchbase.client.java.json.JsonObject) Duration(java.time.Duration) ConnectionStringUtil.asConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString) ConnectionStringUtil.checkConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.checkConnectionString) ConnectionString(com.couchbase.client.core.util.ConnectionString) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Aggregations

AnalyticsRequest (com.couchbase.client.core.msg.analytics.AnalyticsRequest)3 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 ReducedAnalyticsErrorContext (com.couchbase.client.core.error.context.ReducedAnalyticsErrorContext)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 JsonObject (com.couchbase.client.java.json.JsonObject)2 Duration (java.time.Duration)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)1 AnalyticsChunkRow (com.couchbase.client.core.msg.analytics.AnalyticsChunkRow)1 AnalyticsResponse (com.couchbase.client.core.msg.analytics.AnalyticsResponse)1 ConnectionString (com.couchbase.client.core.util.ConnectionString)1 ConnectionStringUtil.asConnectionString (com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString)1 ConnectionStringUtil.checkConnectionString (com.couchbase.client.core.util.ConnectionStringUtil.checkConnectionString)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 InvalidDefinitionException (com.fasterxml.jackson.databind.exc.InvalidDefinitionException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1