use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class AbstractFederationStrategy method federate.
@Override
public QueryResponse federate(List<Source> sources, final QueryRequest queryRequest) {
final String methodName = "federate";
LOGGER.trace("ENTERING: {}", methodName);
if (LOGGER.isDebugEnabled()) {
for (Source source : sources) {
if (source != null) {
LOGGER.debug("source to query: {}", source.getId());
}
}
}
Query originalQuery = queryRequest.getQuery();
int offset = originalQuery.getStartIndex();
final int pageSize = originalQuery.getPageSize();
// limit offset to max value
if (offset > this.maxStartIndex) {
offset = this.maxStartIndex;
}
final QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, null);
Map<Source, Future<SourceResponse>> futures = new HashMap<Source, Future<SourceResponse>>();
Query modifiedQuery = getModifiedQuery(originalQuery, sources.size(), offset, pageSize);
QueryRequest modifiedQueryRequest = new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), queryRequest.getSourceIds(), queryRequest.getProperties());
// Do NOT call source.isAvailable() when checking sources
for (final Source source : sources) {
if (source != null) {
if (!futures.containsKey(source)) {
LOGGER.debug("running query on source: {}", source.getId());
try {
for (PreFederatedQueryPlugin service : preQuery) {
try {
modifiedQueryRequest = service.process(source, modifiedQueryRequest);
} catch (PluginExecutionException e) {
LOGGER.info("Error executing PreFederatedQueryPlugin: ", e);
}
}
} catch (StopProcessingException e) {
LOGGER.info("Plugin stopped processing: ", e);
}
futures.put(source, queryExecutorService.submit(new CallableSourceResponse(source, modifiedQueryRequest.getQuery(), modifiedQueryRequest.getProperties())));
} else {
LOGGER.info("Duplicate source found with name {}. Ignoring second one.", source.getId());
}
}
}
QueryResponseImpl offsetResults = null;
// OffsetResultHandler does.
if (offset > 1 && sources.size() > 1) {
offsetResults = new QueryResponseImpl(queryRequest, null);
queryExecutorService.submit(new OffsetResultHandler(queryResponseQueue, offsetResults, pageSize, offset));
}
queryExecutorService.submit(createMonitor(queryExecutorService, futures, queryResponseQueue, modifiedQueryRequest.getQuery()));
QueryResponse queryResponse = null;
if (offset > 1 && sources.size() > 1) {
queryResponse = offsetResults;
LOGGER.debug("returning offsetResults");
} else {
queryResponse = queryResponseQueue;
LOGGER.debug("returning returnResults: {}", queryResponse);
}
try {
for (PostFederatedQueryPlugin service : postQuery) {
try {
queryResponse = service.process(queryResponse);
} catch (PluginExecutionException e) {
LOGGER.info("Error executing PostFederatedQueryPlugin: ", e);
}
}
} catch (StopProcessingException e) {
LOGGER.info("Plugin stopped processing: ", e);
}
LOGGER.debug("returning Query Results: {}", queryResponse);
LOGGER.trace("EXITING: {}.federate", CLASS_NAME);
return queryResponse;
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class MockFederationStrategy method federate.
@Override
public QueryResponse federate(List<Source> sources, QueryRequest query) throws FederationException {
LOGGER.debug("entry");
QueryResponseImpl resp = new QueryResponseImpl(query);
for (Source src : sources) {
try {
resp.addResults(src.query(query).getResults(), false);
} catch (UnsupportedQueryException e) {
LOGGER.error("Doh!", e);
}
}
resp.closeResultQueue();
List<Result> results = resp.getResults();
resp.setHits(results.size());
LOGGER.debug("exit");
return resp;
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class CatalogMetricsTest method catalogExceptionMetric.
@Test
public void catalogExceptionMetric() throws Exception {
QueryResponse response = new QueryResponseImpl(new QueryRequestImpl(new QueryImpl(idFilter)));
Set<ProcessingDetails> details = response.getProcessingDetails();
details.addAll(new HashSet<ProcessingDetails>() {
{
add(new ProcessingDetailsImpl("source1", new UnsupportedQueryException()));
add(new ProcessingDetailsImpl("source2", new SourceUnavailableException()));
add(new ProcessingDetailsImpl("source3", new FederationException()));
add(new ProcessingDetailsImpl("source4", new Exception()));
}
});
underTest.process(response);
assertThat(underTest.exceptions.getCount(), is(4L));
assertThat(underTest.unsupportedQueryExceptions.getCount(), is(1L));
assertThat(underTest.sourceUnavailableExceptions.getCount(), is(1L));
assertThat(underTest.federationExceptions.getCount(), is(1L));
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class CatalogMetricsTest method catalogResultCountMetric.
@Test
public void catalogResultCountMetric() throws Exception {
QueryRequest query = new QueryRequestImpl(new QueryImpl(idFilter));
QueryResponse response = new QueryResponseImpl(query, new ArrayList(), 50);
underTest.process(response);
assertThat(underTest.resultCount.getCount(), is(1L));
assertThat(underTest.resultCount.getSnapshot().getMean(), is(50.0));
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class CatalogFrameworkImplTest method testUpdateStorage.
/**
* Tests that the framework properly passes an update request to the local provider.
*/
@Test
public void testUpdateStorage() throws Exception {
List<ContentItem> contentItems = new ArrayList<>();
MetacardImpl metacard = new MetacardImpl();
metacard.setId(null);
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("blah".getBytes());
}
};
ContentItemImpl contentItem = new ContentItemImpl(uuidGenerator.generateUuid(), byteSource, "application/octet-stream", "blah", 0L, metacard);
contentItems.add(contentItem);
CreateResponse response = framework.create(new CreateStorageRequestImpl(contentItems, null));
Metacard insertedCard = response.getCreatedMetacards().get(0);
List<ContentItem> updatedContentItems = new ArrayList<>();
updatedContentItems.add(new ContentItemImpl(insertedCard.getId(), byteSource, "application/octet-stream", insertedCard));
UpdateStorageRequest request = new UpdateStorageRequestImpl(updatedContentItems, null);
List<Result> mockFederationResults = Stream.of(insertedCard).map(m -> {
Result mockResult = mock(Result.class);
when(mockResult.getMetacard()).thenReturn(m);
return mockResult;
}).collect(Collectors.toList());
QueryResponseImpl queryResponse = new QueryResponseImpl(mock(QueryRequest.class), mockFederationResults, 1);
when(mockFederationStrategy.federate(anyList(), anyObject())).thenReturn(queryResponse);
// send update to framework
List<Update> returnedCards = framework.update(request).getUpdatedMetacards();
assertThat(returnedCards, hasSize(1));
final Metacard newMetacard = returnedCards.get(0).getNewMetacard();
assertThat(newMetacard.getId(), notNullValue());
assertThat(newMetacard.getResourceURI().toString(), is(contentItem.getUri()));
assertThat(newMetacard.getResourceSize(), is(Long.toString(byteSource.size())));
assertThat(response.getCreatedMetacards(), hasSize(storageProvider.size()));
// make sure that the event was posted correctly
assertThat(eventAdmin.wasEventPosted(), is(true));
}
Aggregations