Search in sources :

Example 16 with QueryResponseImpl

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;
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result)

Example 17 with QueryResponseImpl

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));
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) ArrayList(java.util.ArrayList) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 18 with QueryResponseImpl

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;
}
Also used : Query(ddf.catalog.operation.Query) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequest(ddf.catalog.operation.QueryRequest) PreFederatedQueryPlugin(ddf.catalog.plugin.PreFederatedQueryPlugin) HashMap(java.util.HashMap) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Source(ddf.catalog.source.Source) PostFederatedQueryPlugin(ddf.catalog.plugin.PostFederatedQueryPlugin) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Future(java.util.concurrent.Future) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException)

Example 19 with QueryResponseImpl

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;
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) Source(ddf.catalog.source.Source) Result(ddf.catalog.data.Result)

Example 20 with QueryResponseImpl

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());
    }
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Aggregations

QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)49 Result (ddf.catalog.data.Result)32 QueryRequest (ddf.catalog.operation.QueryRequest)31 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)27 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)25 QueryResponse (ddf.catalog.operation.QueryResponse)24 ResultImpl (ddf.catalog.data.impl.ResultImpl)23 Metacard (ddf.catalog.data.Metacard)22 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)20 Query (ddf.catalog.operation.Query)15 SourceResponse (ddf.catalog.operation.SourceResponse)15 Source (ddf.catalog.source.Source)15 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)14 HashMap (java.util.HashMap)14 FederationStrategy (ddf.catalog.federation.FederationStrategy)12 StopProcessingException (ddf.catalog.plugin.StopProcessingException)12 FederationException (ddf.catalog.federation.FederationException)10 ByteSource (com.google.common.io.ByteSource)9 ValidationQueryFactory (ddf.catalog.cache.solr.impl.ValidationQueryFactory)9