use of ddf.catalog.util.impl.QueryFunction in project ddf by codice.
the class CswEndpoint method queryCsw.
private CswRecordCollection queryCsw(GetRecordsType request) throws CswException {
if (LOGGER.isDebugEnabled()) {
try {
Writer writer = new StringWriter();
try {
Marshaller marshaller = CswQueryFactory.getJaxBContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
JAXBElement<GetRecordsType> jaxbElement = new ObjectFactory().createGetRecords(request);
marshaller.marshal(jaxbElement, writer);
} catch (JAXBException e) {
LOGGER.debug("Unable to marshall {} to XML", GetRecordsType.class, e);
}
LOGGER.debug(writer.toString());
} catch (Exception e) {
LOGGER.debug("Unable to create debug message for getRecordsType", e);
}
}
QueryType query = (QueryType) request.getAbstractQuery().getValue();
CswRecordCollection response = new CswRecordCollection();
response.setRequest(request);
response.setOutputSchema(request.getOutputSchema());
response.setMimeType(request.getOutputFormat());
response.setElementName(query.getElementName());
response.setElementSetType((query.getElementSetName() != null) ? query.getElementSetName().getValue() : null);
response.setResultType((ResultType) ObjectUtils.defaultIfNull(request.getResultType(), ResultType.HITS));
if (ResultType.HITS.equals(request.getResultType()) || ResultType.RESULTS.equals(request.getResultType())) {
QueryRequest queryRequest = queryFactory.getQuery(request);
try {
queryRequest = queryFactory.updateQueryRequestTags(queryRequest, request.getOutputSchema());
LOGGER.debug("Attempting to execute paged query: {}", queryRequest);
AtomicLong hitCount = new AtomicLong(0);
QueryFunction qf = qr -> {
SourceResponse sr = framework.query(qr);
hitCount.compareAndSet(0, sr.getHits());
return sr;
};
ResultIterable results = ResultIterable.resultIterable(qf, queryRequest, request.getMaxRecords().intValue());
List<Result> resultList = results.stream().collect(Collectors.toList());
// The hitCount Atomic is used here instead of just defaulting
// to the size of the resultList because the size of the resultList
// can be limited to request.getMaxRecords().intValue() which would
// lead to an incorrect response for hits.
// hitCount is set within the QueryFunction and will correspond to
// all responses.
long totalHits = hitCount.get();
totalHits = totalHits != 0 ? totalHits : resultList.size();
QueryResponse queryResponse = new QueryResponseImpl(queryRequest, resultList, totalHits);
response.setSourceResponse(queryResponse);
} catch (UnsupportedQueryException | CatalogQueryException e) {
LOGGER.debug("Unable to query", e);
throw new CswException(e);
}
}
return response;
}
Aggregations