Search in sources :

Example 11 with ResultInfo

use of org.folio.rest.jaxrs.model.ResultInfo in project raml-module-builder by folio-org.

the class PostgresClient method processResults.

/**
 * converts a result set into pojos - handles 3 types of queries:
 * 1. a regular query will return N rows, where each row contains Y columns. one of those columns is the jsonb
 * column which is mapped into a pojo. each row will also contain the count column (if count was requested for
 * the query), other fields , like updated date may also be returned if they were requested in the select.
 *    1a. note that there is an attempt to map external (non jsonb) columns to fields in the pojo. for example,
 *    a column called update_date will attempt to map its value to a field called updateDate in the pojo. however,
 *    for this to happen, the query must select the update_date -> select id,jsonb,update_date from ....
 * 2. a facet query returns 2 columns, a uuid and a jsonb column. the results of the query are returned as
 * id and json rows. facets are returned as jsonb values:
 * {"facetValues": [{"count": 542,"value": "11 ed."}], "type": "name"}
 * (along with a static '00000000-0000-0000-0000-000000000000' uuid)
 * the count for a facet query is returned in the following manner:
 * {"count": 501312} , with a static uuid as the facets
 * 3. audit queries - queries that query an audit table, meaning the clazz parameter passed in has a jsonb member.
 *
 * @param rs
 * @param total
 * @param clazz
 * @return
 */
<T> Results<T> processResults(RowSet<Row> rs, Integer total, int offset, int limit, Class<T> clazz) {
    long start = System.nanoTime();
    ResultsHelper<T> resultsHelper = new ResultsHelper<>(rs, total, clazz);
    deserializeResults(resultsHelper);
    ResultInfo resultInfo = new ResultInfo();
    resultsHelper.facets.forEach((k, v) -> resultInfo.getFacets().add(v));
    Integer totalRecords = getTotalRecords(resultsHelper.list.size(), resultsHelper.total, offset, limit);
    resultInfo.setTotalRecords(totalRecords);
    Results<T> results = new Results<>();
    results.setResults(resultsHelper.list);
    results.setResultInfo(resultInfo);
    statsTracker(PROCESS_RESULTS_STAT_METHOD, clazz.getSimpleName(), start);
    return results;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Results(org.folio.rest.persist.interfaces.Results) ResultInfo(org.folio.rest.jaxrs.model.ResultInfo)

Example 12 with ResultInfo

use of org.folio.rest.jaxrs.model.ResultInfo 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

ResultInfo (org.folio.rest.jaxrs.model.ResultInfo)12 Results (org.folio.rest.persist.interfaces.Results)11 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)10 JsonObject (io.vertx.core.json.JsonObject)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 AsyncResult (io.vertx.core.AsyncResult)9 Future (io.vertx.core.Future)9 Handler (io.vertx.core.Handler)9 Promise (io.vertx.core.Promise)9 Vertx (io.vertx.core.Vertx)9 JsonArray (io.vertx.core.json.JsonArray)9 PgConnection (io.vertx.pgclient.PgConnection)9 PgPool (io.vertx.pgclient.PgPool)9 PreparedStatement (io.vertx.sqlclient.PreparedStatement)9 Row (io.vertx.sqlclient.Row)9 RowIterator (io.vertx.sqlclient.RowIterator)9 RowSet (io.vertx.sqlclient.RowSet)9 RowStream (io.vertx.sqlclient.RowStream)9