Search in sources :

Example 36 with QueryResponseImpl

use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.

the class RegistryIdPostIngestPluginTest method testInit.

@Test
public void testInit() throws Exception {
    Metacard metacard = getDefaultMetacard();
    QueryResponseImpl response = new QueryResponseImpl(null, Collections.singletonList(new ResultImpl(metacard)), 1L);
    when(security.runAsAdminWithException(any(PrivilegedExceptionAction.class))).thenAnswer(invocation -> ((PrivilegedExceptionAction) invocation.getArguments()[0]).run());
    when(security.runWithSubjectOrElevate(any(Callable.class))).thenAnswer(invocation -> ((Callable) invocation.getArguments()[0]).call());
    when(framework.query(any(QueryRequest.class))).thenReturn(response);
    registryIdPostIngestPlugin.init();
    assertThat(registryIdPostIngestPlugin.getRegistryIds().size(), equalTo(1));
    assertThat(registryIdPostIngestPlugin.getRegistryIds().iterator().next(), equalTo("regId"));
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequest(ddf.catalog.operation.QueryRequest) ResultImpl(ddf.catalog.data.impl.ResultImpl) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 37 with QueryResponseImpl

use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.

the class Search method failSource.

public synchronized void failSource(String sourceId, Exception cause) {
    QueryResponseImpl failedResponse = new QueryResponseImpl(null);
    failedResponse.closeResultQueue();
    failedResponse.setHits(0);
    failedResponse.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, cause));
    failedResponse.getProperties().put("elapsed", -1L);
    updateStatus(sourceId, failedResponse);
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) ProcessingDetailsImpl(ddf.catalog.operation.impl.ProcessingDetailsImpl)

Example 38 with QueryResponseImpl

use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.

the class FederationStrategyTest method testFederateTwoSourcesOffsetTwoPageSizeThree.

/**
     * Verify that a modified version of the mockQuery passed is used by the sources.
     * <p>
     * Special results handling done by OffsetResultsHandler.
     */
@Test
public void testFederateTwoSourcesOffsetTwoPageSizeThree() throws Exception {
    LOGGER.debug("testFederate_TwoSources_OffsetTwo_PageSizeThree()");
    // Test Setup
    Query mockQuery = mock(QueryImpl.class);
    // Offset of 2
    when(mockQuery.getStartIndex()).thenReturn(2);
    // Page size of 3
    when(mockQuery.getPageSize()).thenReturn(3);
    QueryRequest queryRequest = mock(QueryRequest.class);
    when(queryRequest.getQuery()).thenReturn(mockQuery);
    ArgumentCaptor<QueryRequest> argument1 = ArgumentCaptor.forClass(QueryRequest.class);
    ArgumentCaptor<QueryRequest> argument2 = ArgumentCaptor.forClass(QueryRequest.class);
    /**
         * When using a modified mockQuery to mockQuery the sources, the desired offset and page size are
         * NOT used. So, the results returned by each source start at index 1 and end at (offset +
         * pageSize - 1).
         *
         * Number of results returned by each source = offset + pageSize - 1 4 = 2 + 3 - 1
         */
    Result mockSource1Result1 = mock(Result.class);
    Result mockSource1Result2 = mock(Result.class);
    Result mockSource1Result3 = mock(Result.class);
    Result mockSource1Result4 = mock(Result.class);
    SourceResponse mockSource1Response = mock(SourceResponse.class);
    List<Result> mockSource1Results = Arrays.asList(mockSource1Result1, mockSource1Result2, mockSource1Result3, mockSource1Result4);
    when(mockSource1Response.getResults()).thenReturn(mockSource1Results);
    Source mockSource1 = mock(Source.class);
    when(mockSource1.query(any(QueryRequest.class))).thenReturn(mockSource1Response);
    when(mockSource1.getId()).thenReturn("####### MOCK SOURCE 1.3 #######");
    /**
         * When using a modified mockQuery to mockQuery the sources, the desired offset and page size are
         * NOT used. So, the results returned by each source start at index 1 and end at (offset +
         * pageSize - 1).
         *
         * Number of results returned by each source = offset + pageSize - 1 4 = 2 + 3 - 1
         */
    Result mockSource2Result1 = mock(Result.class);
    Result mockSource2Result2 = mock(Result.class);
    Result mockSource2Result3 = mock(Result.class);
    Result mockSource2Result4 = mock(Result.class);
    SourceResponse mockSource2Response = mock(SourceResponse.class);
    List<Result> mockSource2Results = Arrays.asList(mockSource2Result1, mockSource2Result2, mockSource2Result3, mockSource2Result4);
    when(mockSource2Response.getResults()).thenReturn(mockSource2Results);
    Source mockSource2 = mock(Source.class);
    when(mockSource2.query(any(QueryRequest.class))).thenReturn(mockSource2Response);
    when(mockSource2.getId()).thenReturn("####### MOCK SOURCE 2.3 #######");
    // Two sources
    List<Source> sources = new ArrayList<Source>(2);
    sources.add(mockSource1);
    sources.add(mockSource2);
    Result mockSortedResult1 = mock(Result.class);
    Result mockSortedResult2 = mock(Result.class);
    Result mockSortedResult3 = mock(Result.class);
    Result mockSortedResult4 = mock(Result.class);
    Result mockSortedResult5 = mock(Result.class);
    Result mockSortedResult6 = mock(Result.class);
    Result mockSortedResult7 = mock(Result.class);
    Result mockSortedResult8 = mock(Result.class);
    List<Result> mockSortedResults = Arrays.asList(mockSortedResult1, mockSortedResult2, mockSortedResult3, mockSortedResult4, mockSortedResult5, mockSortedResult6, mockSortedResult7, mockSortedResult8);
    QueryResponseImpl mockOriginalResults = Mockito.mock(QueryResponseImpl.class);
    // Return true for the number of mockSortedResults
    Mockito.when(mockOriginalResults.hasMoreResults()).thenReturn(true, true, true, true, true, true, true, true, false);
    Mockito.when(mockOriginalResults.getResults()).thenReturn(mockSortedResults);
    // Returns the sorted results from both sources (4 + 4 = 8)
    Mockito.when(mockOriginalResults.take()).thenReturn(mockSortedResult1, mockSortedResult2, mockSortedResult3, mockSortedResult4, mockSortedResult5, mockSortedResult6, mockSortedResult7, mockSortedResult8);
    QueryResponseImpl offsetResultQueue = new QueryResponseImpl(queryRequest, null);
    PowerMockito.whenNew(QueryResponseImpl.class).withArguments(queryRequest, (Map<String, Serializable>) null).thenReturn(mockOriginalResults, offsetResultQueue);
    SortedFederationStrategy strategy = new SortedFederationStrategy(executor, new ArrayList<PreFederatedQueryPlugin>(), new ArrayList<PostFederatedQueryPlugin>());
    // Run Test
    QueryResponse federatedResponse = strategy.federate(sources, queryRequest);
    // Make sure we've finished running the test
    killAndWaitForExecutor();
    // Verification
    assertNotNull(federatedResponse);
    verify(mockSource1).query(argument1.capture());
    // The modified query should have a start index of 1 and an end index of offset + pageSize -
    // 1
    assertEquals(1, argument1.getValue().getQuery().getStartIndex());
    assertEquals(4, argument1.getValue().getQuery().getPageSize());
    verify(mockSource2).query(argument2.capture());
    assertThat(mockQuery, not(argument2.getValue().getQuery()));
    // The modified query should have a start index of 1 and an end index of offset + pageSize -
    // 1
    assertEquals(1, argument2.getValue().getQuery().getStartIndex());
    assertEquals(4, argument2.getValue().getQuery().getPageSize());
    /**
         * Verify three results (page size) are returned. The sorted results returned by the sources
         * do NOT have the offset and page size taken into account, so the offset and page size are
         * applied to the sorted results in the OffsetResultHandler.
         *
         * Offset of 2 (start at result 2) and page size of 3 (end at result 4).
         */
    LOGGER.debug("mockSortedResult1: {}", mockSortedResult1);
    LOGGER.debug("mockSortedResult2: {}", mockSortedResult2);
    LOGGER.debug("mockSortedResult3: {}", mockSortedResult3);
    LOGGER.debug("mockSortedResult4: {}", mockSortedResult4);
    assertEquals(3, federatedResponse.getResults().size());
    assertEquals(mockSortedResult2, federatedResponse.getResults().get(0));
    assertEquals(mockSortedResult3, federatedResponse.getResults().get(1));
    assertEquals(mockSortedResult4, federatedResponse.getResults().get(2));
    for (Result result : federatedResponse.getResults()) {
        LOGGER.debug("federated response result: {}", result);
    }
}
Also used : Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) PreFederatedQueryPlugin(ddf.catalog.plugin.PreFederatedQueryPlugin) ArrayList(java.util.ArrayList) Source(ddf.catalog.source.Source) PostFederatedQueryPlugin(ddf.catalog.plugin.PostFederatedQueryPlugin) Result(ddf.catalog.data.Result) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Map(java.util.Map) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with QueryResponseImpl

use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.

the class FifoFederationStrategy method federate.

@Override
public QueryResponse federate(List<Source> sources, final QueryRequest queryRequest) {
    Query originalQuery = queryRequest.getQuery();
    int offset = originalQuery.getStartIndex();
    // limit offset to max value
    if (offset > this.maxStartIndex) {
        offset = this.maxStartIndex;
    }
    final int pageSize = originalQuery.getPageSize();
    QueryResponseImpl queryResponse = 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());
    executeSourceQueries(sources, futures, modifiedQueryRequest);
    int resultsToSkip = 0;
    if (offset > 1 && sources.size() > 1) {
        resultsToSkip = offset - 1;
    }
    queryExecutorService.submit(new FifoQueryMonitor(queryExecutorService, futures, queryResponse, modifiedQueryRequest.getQuery(), resultsToSkip));
    return executePostFederationPlugins(queryResponse);
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Query(ddf.catalog.operation.Query) SourceResponse(ddf.catalog.operation.SourceResponse) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Future(java.util.concurrent.Future) Source(ddf.catalog.source.Source)

Example 40 with QueryResponseImpl

use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.

the class SolrMetacardClientImpl method query.

@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
    if (request == null || request.getQuery() == null) {
        return new QueryResponseImpl(request, new ArrayList<Result>(), true, 0L);
    }
    SolrQuery query = getSolrQuery(request, filterDelegateFactory.newInstance(resolver));
    long totalHits;
    List<Result> results = new ArrayList<>();
    try {
        QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
        totalHits = solrResponse.getResults().getNumFound();
        SolrDocumentList docs = solrResponse.getResults();
        for (SolrDocument doc : docs) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("SOLR DOC: {}", doc.getFieldValue(Metacard.ID + SchemaFields.TEXT_SUFFIX));
            }
            ResultImpl tmpResult;
            try {
                tmpResult = createResult(doc);
            } catch (MetacardCreationException e) {
                throw new UnsupportedQueryException("Could not create metacard(s).", e);
            }
            results.add(tmpResult);
        }
    } catch (SolrServerException | IOException | SolrException e) {
        throw new UnsupportedQueryException("Could not complete solr query.", e);
    }
    SourceResponse sourceResponse = new SourceResponseImpl(request, results, totalHits);
    return sourceResponse;
}
Also used : MetacardCreationException(ddf.catalog.data.MetacardCreationException) SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Result(ddf.catalog.data.Result) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrException(org.apache.solr.common.SolrException)

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