use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class GeoNamesCatalogIndexer method removeGeoNamesMetacardsFromCatalog.
private void removeGeoNamesMetacardsFromCatalog(String resource, ProgressCallback extractionCallback) throws UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
extractionCallback.updateProgress(0);
Optional<CatalogProvider> catalogProviderOptional = catalogProviders.stream().findFirst();
if (catalogProviderOptional.isPresent()) {
CatalogProvider catalogProvider = catalogProviderOptional.get();
if (StringUtils.isNotBlank(resource)) {
filter = filterBuilder.allOf(filterBuilder.attribute(GeoEntryAttributes.IMPORT_LOCATION).is().equalTo().text(resource), filter);
}
while (true) {
Query query = new QueryImpl(filter, 1, BATCH_SIZE, SortBy.NATURAL_ORDER, false, TimeUnit.SECONDS.toMillis(90));
QueryRequest queryRequest = new QueryRequestImpl(query);
LOGGER.trace("Removing existing geonames data with filter: {}", filter);
QueryResponse response = catalogFramework.query(queryRequest);
List<Serializable> metacardsToDelete = response.getResults().stream().map(Result::getMetacard).map(Metacard::getId).collect(Collectors.toList());
if (CollectionUtils.isEmpty(metacardsToDelete)) {
break;
}
LOGGER.trace("Deleting {} GeoNames metacards", metacardsToDelete.size());
removeMetacards(catalogProvider, extractionCallback, metacardsToDelete);
}
}
extractionCallback.updateProgress(50);
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class CswEndpointTest method getQueryResponseBatch.
private List<QueryResponse> getQueryResponseBatch(int batchSize, int total) {
Queue<Result> results = new ArrayDeque<>();
for (int i = 1; i <= total; i++) {
MetacardImpl metacard = new MetacardImpl();
metacard.setId(i + "");
results.add(new ResultImpl(metacard));
}
List<QueryResponse> queryResponses = new ArrayList<>();
while (!results.isEmpty()) {
List<Result> batchList = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
Result result = results.poll();
if (result == null) {
break;
}
batchList.add(result);
}
queryResponses.add(new QueryResponseImpl(null, batchList, total));
}
// Add one empty response list to the end
queryResponses.add(new QueryResponseImpl(null, Collections.emptyList(), 0));
return queryResponses;
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class CswEndpointTest method testDeleteBatching.
@Test
public void testDeleteBatching() throws Exception {
// configure query responses
queryResponseBatch = getQueryResponseBatch(500, 800);
QueryResponse[] qrRest = queryResponseBatch.subList(1, queryResponseBatch.size()).toArray(new QueryResponse[0]);
when(catalogFramework.query(any(QueryRequest.class))).thenReturn(queryResponseBatch.get(0), qrRest);
// configure delete responses
DeleteType deleteType = mock(DeleteType.class);
doReturn(CswConstants.CSW_RECORD).when(deleteType).getTypeName();
doReturn("").when(deleteType).getHandle();
QueryConstraintType queryConstraintType = new QueryConstraintType();
queryConstraintType.setCqlText("title = \"foo\"");
doReturn(queryConstraintType).when(deleteType).getConstraint();
when(deleteResponse.getDeletedMetacards()).thenReturn(populateMetacardList(1));
when(catalogFramework.delete(any(DeleteRequest.class))).thenAnswer((Answer<DeleteResponse>) invocation -> deleteResponse);
DeleteAction deleteAction = new DeleteActionImpl(deleteType, DefaultCswRecordMap.getPrefixToUriMapping());
CswTransactionRequest deleteRequest = new CswTransactionRequest();
deleteRequest.getDeleteActions().add(deleteAction);
TransactionResponseType response = csw.transaction(deleteRequest);
assertThat(response.getTransactionSummary().getTotalDeleted().intValue(), equalTo(800));
verify(catalogFramework, times(4)).query(any());
verify(catalogFramework, times(800)).delete(any());
}
use of ddf.catalog.operation.QueryResponse 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;
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class CswEndpoint method queryById.
private CswRecordCollection queryById(List<String> ids, String outputSchema) throws CswException {
QueryRequest queryRequest = queryFactory.getQueryById(ids);
try {
CswRecordCollection response = new CswRecordCollection();
response.setById(true);
queryRequest = queryFactory.updateQueryRequestTags(queryRequest, outputSchema);
QueryResponse queryResponse = framework.query(queryRequest);
response.setSourceResponse(queryResponse);
List<Metacard> metacards = new LinkedList<>();
for (Result result : queryResponse.getResults()) {
metacards.add(result.getMetacard());
}
response.setCswRecords(metacards);
return response;
} catch (FederationException | SourceUnavailableException | UnsupportedQueryException e) {
throw new CswException(e);
}
}
Aggregations