Search in sources :

Example 1 with RowStream

use of io.vertx.sqlclient.RowStream in project raml-module-builder by folio-org.

the class PostgresClientIT method streamGetResultException.

@Test
public void streamGetResultException(TestContext context) {
    createTableWithPoLines(context);
    ResultInfo resultInfo = new ResultInfo();
    context.assertNotNull(vertx);
    RowStream<Row> sqlRowStream = new MySQLRowStream();
    StringBuilder events = new StringBuilder();
    Async async = context.async();
    PostgresClientStreamResult<Object> streamResult = new PostgresClientStreamResult(resultInfo);
    Transaction transaction = null;
    postgresClient.doStreamRowResults(sqlRowStream, Object.class, transaction, new QueryHelper("table_name"), streamResult, context.asyncAssertSuccess(sr -> {
        sr.handler(streamHandler -> {
            events.append("[handler]");
        });
        sr.endHandler(x -> {
            events.append("[endHandler]");
            throw new NullPointerException("null");
        });
        sr.exceptionHandler(x -> {
            events.append("[exception]");
            context.assertEquals("SQLRowStream exception", x.getMessage());
            async.complete();
        });
    }));
    async.await(1000);
    context.assertEquals("[exception]", events.toString());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestContext(io.vertx.ext.unit.TestContext) RowStream(io.vertx.sqlclient.RowStream) Arrays(java.util.Arrays) PgNotification(io.vertx.pgclient.PgNotification) TransactionRollbackException(io.vertx.sqlclient.TransactionRollbackException) VertxUtils(org.folio.rest.tools.utils.VertxUtils) Tuple(io.vertx.sqlclient.Tuple) UpdateSection(org.folio.rest.persist.Criteria.UpdateSection) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) RowIterator(io.vertx.sqlclient.RowIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqlResult(io.vertx.sqlclient.SqlResult) After(org.junit.After) JsonObject(io.vertx.core.json.JsonObject) Offset(org.folio.rest.persist.Criteria.Offset) Collector(java.util.stream.Collector) Transaction(io.vertx.sqlclient.Transaction) AfterClass(org.junit.AfterClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RowImpl(io.vertx.pgclient.impl.RowImpl) Set(java.util.Set) UUID(java.util.UUID) FieldException(org.folio.cql2pgjson.exception.FieldException) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) CQL2PgJSON(org.folio.cql2pgjson.CQL2PgJSON) Base64(java.util.Base64) List(java.util.List) Stream(java.util.stream.Stream) Criterion(org.folio.rest.persist.Criteria.Criterion) Results(org.folio.rest.persist.interfaces.Results) Facet(org.folio.rest.jaxrs.model.Facet) RowDesc(io.vertx.sqlclient.impl.RowDesc) Async(io.vertx.ext.unit.Async) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) BeforeClass(org.junit.BeforeClass) FacetField(org.folio.rest.persist.facets.FacetField) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) Criteria(org.folio.rest.persist.Criteria.Criteria) CoreMatchers.not(org.hamcrest.CoreMatchers.not) RunWith(org.junit.runner.RunWith) Timeout(io.vertx.ext.unit.junit.Timeout) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Function(java.util.function.Function) TotaledResults(org.folio.rest.persist.PostgresClient.TotaledResults) ArrayList(java.util.ArrayList) PreparedStatement(io.vertx.sqlclient.PreparedStatement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Poline(org.folio.rest.persist.helpers.Poline) PrepareOptions(io.vertx.sqlclient.PrepareOptions) SqlConnection(io.vertx.sqlclient.SqlConnection) Limit(org.folio.rest.persist.Criteria.Limit) QueryHelper(org.folio.rest.persist.PostgresClient.QueryHelper) RowSet(io.vertx.sqlclient.RowSet) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AsyncResult(io.vertx.core.AsyncResult) LinkedList(java.util.LinkedList) DatabaseMetadata(io.vertx.sqlclient.spi.DatabaseMetadata) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Before(org.junit.Before) Files(java.nio.file.Files) Query(io.vertx.sqlclient.Query) Promise(io.vertx.core.Promise) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) PgPool(io.vertx.pgclient.PgPool) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) PreparedQuery(io.vertx.sqlclient.PreparedQuery) JsonArray(io.vertx.core.json.JsonArray) PgConnection(io.vertx.pgclient.PgConnection) Rule(org.junit.Rule) Paths(java.nio.file.Paths) Row(io.vertx.sqlclient.Row) LocalRowSet(org.folio.rest.persist.helpers.LocalRowSet) Handler(io.vertx.core.Handler) SimplePojo(org.folio.rest.persist.helpers.SimplePojo) Collections(java.util.Collections) InputStream(java.io.InputStream) QueryHelper(org.folio.rest.persist.PostgresClient.QueryHelper) Transaction(io.vertx.sqlclient.Transaction) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Row(io.vertx.sqlclient.Row) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Test(org.junit.Test)

Example 2 with RowStream

use of io.vertx.sqlclient.RowStream in project raml-module-builder by folio-org.

the class PostgresClient method doStreamRowResults.

/**
 * @param transaction the transaction to close, null if not to close
 */
<T> void doStreamRowResults(RowStream<Row> rowStream, Class<T> clazz, Transaction transaction, QueryHelper queryHelper, PostgresClientStreamResult<T> streamResult, Handler<AsyncResult<PostgresClientStreamResult<T>>> replyHandler) {
    ResultInfo resultInfo = streamResult.resultInfo();
    Promise<PostgresClientStreamResult<T>> promise = Promise.promise();
    ResultsHelper<T> resultsHelper = new ResultsHelper<>(clazz);
    boolean isAuditFlavored = isAuditFlavored(resultsHelper.clazz);
    Map<String, Method> externalColumnSetters = new HashMap<>();
    AtomicInteger resultCount = new AtomicInteger();
    rowStream.handler(r -> {
        try {
            // for first row, get column names
            if (resultsHelper.offset == 0) {
                List<String> columnNames = getColumnNames(r);
                collectExternalColumnSetters(columnNames, resultsHelper.clazz, isAuditFlavored, externalColumnSetters);
            }
            @SuppressWarnings("unchecked") T objRow = (T) deserializeRow(resultsHelper, externalColumnSetters, isAuditFlavored, r);
            if (!resultsHelper.facet) {
                resultCount.incrementAndGet();
                if (!promise.future().isComplete()) {
                    // end of facets (if any) .. produce result
                    resultsHelper.facets.forEach((k, v) -> resultInfo.getFacets().add(v));
                    promise.complete(streamResult);
                    replyHandler.handle(promise.future());
                }
                streamResult.fireHandler(objRow);
            }
            resultsHelper.offset++;
        } catch (Exception e) {
            streamResult.handler(null);
            log.error(e.getMessage(), e);
            if (!promise.future().isComplete()) {
                promise.complete(streamResult);
                replyHandler.handle(promise.future());
            }
            rowStream.close();
            closeIfNonNull(transaction).onComplete((AsyncResult<Void> voidRes) -> streamResult.fireExceptionHandler(e));
        }
    }).endHandler(v2 -> {
        rowStream.close();
        closeIfNonNull(transaction).onComplete(ignore -> {
            resultInfo.setTotalRecords(getTotalRecords(resultCount.get(), resultInfo.getTotalRecords(), queryHelper.offset, queryHelper.limit));
            try {
                if (!promise.future().isComplete()) {
                    promise.complete(streamResult);
                    replyHandler.handle(promise.future());
                }
                streamResult.fireEndHandler();
            } catch (Exception ex) {
                streamResult.fireExceptionHandler(ex);
            }
        });
    }).exceptionHandler(e -> {
        rowStream.close();
        closeIfNonNull(transaction).onComplete(ignore -> {
            if (!promise.future().isComplete()) {
                promise.complete(streamResult);
                replyHandler.handle(promise.future());
            }
            streamResult.fireExceptionHandler(e);
        });
    });
}
Also used : RowStream(io.vertx.sqlclient.RowStream) Arrays(java.util.Arrays) AES(org.folio.rest.security.AES) Tuple(io.vertx.sqlclient.Tuple) UpdateSection(org.folio.rest.persist.Criteria.UpdateSection) MetadataUtil(org.folio.rest.tools.utils.MetadataUtil) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) RowIterator(io.vertx.sqlclient.RowIterator) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectMapperTool(org.folio.dbschema.ObjectMapperTool) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) Transaction(io.vertx.sqlclient.Transaction) Method(java.lang.reflect.Method) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) FacetManager(org.folio.rest.persist.facets.FacetManager) UUID(java.util.UUID) Future(io.vertx.core.Future) PoolOptions(io.vertx.sqlclient.PoolOptions) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Criterion(org.folio.rest.persist.Criteria.Criterion) Results(org.folio.rest.persist.interfaces.Results) Buffer(io.vertx.core.buffer.Buffer) Pattern(java.util.regex.Pattern) SecretKey(javax.crypto.SecretKey) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FacetField(org.folio.rest.persist.facets.FacetField) TemplateException(freemarker.template.TemplateException) HashMap(java.util.HashMap) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo) Function(java.util.function.Function) PreparedStatement(io.vertx.sqlclient.PreparedStatement) ArrayList(java.util.ArrayList) SqlConnection(io.vertx.sqlclient.SqlConnection) RowSet(io.vertx.sqlclient.RowSet) AsyncResult(io.vertx.core.AsyncResult) PemTrustOptions(io.vertx.core.net.PemTrustOptions) LinkedList(java.util.LinkedList) PostgresTester(org.folio.util.PostgresTester) OpenSsl(io.netty.handler.ssl.OpenSsl) Iterator(java.util.Iterator) Envs(org.folio.rest.tools.utils.Envs) Promise(io.vertx.core.Promise) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) PgPool(io.vertx.pgclient.PgPool) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) HashedMap(org.apache.commons.collections4.map.HashedMap) MultiKeyMap(org.apache.commons.collections4.map.MultiKeyMap) TimeUnit(java.util.concurrent.TimeUnit) JsonArray(io.vertx.core.json.JsonArray) PgConnection(io.vertx.pgclient.PgConnection) Row(io.vertx.sqlclient.Row) PgConnectOptions(io.vertx.pgclient.PgConnectOptions) Option(java.lang.StackWalker.Option) Handler(io.vertx.core.Handler) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) SslMode(io.vertx.pgclient.SslMode) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) TemplateException(freemarker.template.TemplateException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Promise (io.vertx.core.Promise)2 Vertx (io.vertx.core.Vertx)2 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2 PgConnection (io.vertx.pgclient.PgConnection)2 PgPool (io.vertx.pgclient.PgPool)2 PreparedStatement (io.vertx.sqlclient.PreparedStatement)2 Row (io.vertx.sqlclient.Row)2 RowIterator (io.vertx.sqlclient.RowIterator)2 RowSet (io.vertx.sqlclient.RowSet)2 RowStream (io.vertx.sqlclient.RowStream)2 SqlConnection (io.vertx.sqlclient.SqlConnection)2 Transaction (io.vertx.sqlclient.Transaction)2 Tuple (io.vertx.sqlclient.Tuple)2 IOException (java.io.IOException)2