Search in sources :

Example 1 with AnalyticsChunkRow

use of com.couchbase.client.core.msg.analytics.AnalyticsChunkRow 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)

Aggregations

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 AnalyticsRequest (com.couchbase.client.core.msg.analytics.AnalyticsRequest)1 AnalyticsResponse (com.couchbase.client.core.msg.analytics.AnalyticsResponse)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 SQLException (java.sql.SQLException)1 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1