use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class QueryOperations method replaceSourceId.
/**
* Replaces the site name(s) of {@link FederatedSource}s in the {@link QueryResponse} with the
* fanout's site name to keep info about the {@link FederatedSource}s hidden from the external
* client.
*
* @param queryResponse the original {@link QueryResponse} from the query request
* @return the updated {@link QueryResponse} with all site names replaced with fanout's site
* name
*/
public QueryResponse replaceSourceId(QueryResponse queryResponse) {
LOGGER.trace("ENTERING: replaceSourceId()");
List<Result> results = queryResponse.getResults();
QueryResponseImpl newResponse = new QueryResponseImpl(queryResponse.getRequest(), queryResponse.getProperties());
for (Result result : results) {
MetacardImpl newMetacard = new MetacardImpl(result.getMetacard());
newMetacard.setSourceId(this.getId());
ResultImpl newResult = new ResultImpl(newMetacard);
// Copy over scores
newResult.setDistanceInMeters(result.getDistanceInMeters());
newResult.setRelevanceScore(result.getRelevanceScore());
newResponse.addResult(newResult, false);
}
newResponse.setHits(queryResponse.getHits());
newResponse.closeResultQueue();
LOGGER.trace("EXITING: replaceSourceId()");
return newResponse;
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class CachingFederationStrategyTest method testOffsetResultHandler.
@Test
public void testOffsetResultHandler() throws Exception {
QueryResponseImpl originalResults = mock(QueryResponseImpl.class);
QueryResponseImpl offsetResultQueue = mock(QueryResponseImpl.class);
int pageSize = 1, offset = 1;
Result mockResult = mock(Result.class);
MetacardImpl mockMetacard = mock(MetacardImpl.class);
List<Result> results = new ArrayList<>();
CachingFederationStrategy.OffsetResultHandler offsetResultHandler = new CachingFederationStrategy.OffsetResultHandler(originalResults, offsetResultQueue, pageSize, offset);
when(originalResults.hasMoreResults()).thenReturn(true);
when(mockResult.getMetacard()).thenReturn(mockMetacard);
when(mockMetacard.getId()).thenReturn("mock metacard");
when(originalResults.take()).thenReturn(mockResult);
doAnswer(invocation -> results.add(mockResult)).when(offsetResultQueue).addResult(any(Result.class), any(Boolean.class));
offsetResultHandler.run();
assertThat(results.size(), is(1));
assertThat(results.get(0).getMetacard().getId(), is("mock metacard"));
verify(offsetResultQueue, atLeastOnce()).addResult(any(Result.class), any(Boolean.class));
}
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 FanoutCatalogFrameworkTest method testReplaceDistance.
@Test
public void testReplaceDistance() {
QueryRequest request = new QueryRequestImpl(null);
List<Result> results = new ArrayList<Result>();
MetacardImpl newCard1 = new MetacardImpl();
ResultImpl result1 = new ResultImpl(newCard1);
result1.setRelevanceScore(RELEVANCE_SCORE);
result1.setDistanceInMeters(DISTANCE_SCORE);
MetacardImpl newCard2 = new MetacardImpl();
ResultImpl result2 = new ResultImpl(newCard2);
result2.setRelevanceScore(RELEVANCE_SCORE);
result2.setDistanceInMeters(DISTANCE_SCORE);
results.add(result1);
results.add(result2);
QueryResponse response = new QueryResponseImpl(request, results, 2);
QueryResponse newResponse = framework.getQueryOperations().replaceSourceId(response);
assertNotNull(newResponse);
List<Result> newResults = newResponse.getResults();
assertNotNull(newResults);
assertEquals(2, newResults.size());
Metacard card = new MetacardImpl();
// Make sure the relevance and distance score was copied over
for (Result newResult : newResults) {
card = newResult.getMetacard();
assertNotNull(card);
assertEquals(RELEVANCE_SCORE, newResult.getRelevanceScore());
assertEquals(DISTANCE_SCORE, newResult.getDistanceInMeters());
}
}
Aggregations