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;
}
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);
});
});
}
Aggregations