Search in sources :

Example 6 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class RemoveAllCommand method executeRemoveAllFromStore.

private Object executeRemoveAllFromStore() throws Exception {
    CatalogFacade catalog = getCatalog();
    QueryRequest firstQuery = getIntendedQuery(filterBuilder, true);
    QueryRequest subsequentQuery = getIntendedQuery(filterBuilder, false);
    long totalAmountDeleted = 0;
    long start = System.currentTimeMillis();
    SourceResponse response;
    try {
        response = catalog.query(firstQuery);
    } catch (UnsupportedQueryException e) {
        firstQuery = getAlternateQuery(filterBuilder, true);
        subsequentQuery = getAlternateQuery(filterBuilder, false);
        response = catalog.query(firstQuery);
    }
    if (response == null) {
        printErrorMessage("No response from Catalog.");
        return null;
    }
    if (needsAlternateQueryAndResponse(response)) {
        firstQuery = getAlternateQuery(filterBuilder, true);
        subsequentQuery = getAlternateQuery(filterBuilder, false);
        response = catalog.query(firstQuery);
    }
    String totalAmount = getTotalAmount(response.getHits());
    while (response.getResults().size() > 0) {
        // Add metacard ids to string array
        List<String> ids = response.getResults().stream().filter(Objects::nonNull).map(Result::getMetacard).filter(Objects::nonNull).map(Metacard::getId).collect(Collectors.toList());
        // Delete the records
        DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[ids.size()]));
        DeleteResponse deleteResponse = catalog.delete(request);
        int amountDeleted = deleteResponse.getDeletedMetacards().size();
        totalAmountDeleted += amountDeleted;
        console.print(String.format(PROGRESS_FORMAT, totalAmountDeleted, totalAmount));
        console.flush();
        // Break out if there are no more records to delete
        if (amountDeleted < batchSize || batchSize < 1) {
            break;
        }
        // Re-query when necessary
        response = catalog.query(subsequentQuery);
    }
    long end = System.currentTimeMillis();
    String info = String.format(" %d file(s) removed in %3.3f seconds%n", totalAmountDeleted, (end - start) / MS_PER_SECOND);
    LOGGER.info(info);
    LOGGER.info(totalAmountDeleted + " files removed using cache:removeAll command");
    console.println();
    console.print(info);
    return null;
}
Also used : DeleteResponse(ddf.catalog.operation.DeleteResponse) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) Objects(java.util.Objects) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade)

Example 7 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class MigrateCommand method query.

@Override
protected SourceResponse query(CatalogFacade framework, Filter filter, int startIndex, long querySize) {
    QueryImpl query = new QueryImpl(filter);
    query.setRequestsTotalResultsCount(true);
    query.setPageSize((int) querySize);
    query.setSortBy(new SortByImpl(Metacard.MODIFIED, SortOrder.DESCENDING));
    QueryRequest queryRequest = new QueryRequestImpl(query);
    query.setStartIndex(startIndex);
    SourceResponse response;
    try {
        LOGGER.debug("Querying with startIndex: {}", startIndex);
        response = framework.query(queryRequest);
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        printErrorMessage(String.format("Received error from Frameworks: %s%n", e.getMessage()));
        return null;
    }
    if (response.getProcessingDetails() != null && !response.getProcessingDetails().isEmpty()) {
        for (SourceProcessingDetails details : response.getProcessingDetails()) {
            LOGGER.debug("Got Issues: {}", details.getWarnings());
        }
        return null;
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) SourceProcessingDetails(ddf.catalog.operation.SourceProcessingDetails)

Example 8 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class ReplicateCommandTest method verifyReplicate.

private void verifyReplicate(int actualMaxMetacards, String sortBy) throws Exception {
    ArgumentCaptor<QueryRequest> argument = ArgumentCaptor.forClass(QueryRequest.class);
    verify(catalogFramework, times((int) Math.ceil((double) actualMaxMetacards / (double) replicateCommand.batchSize))).query(argument.capture());
    QueryRequest request = argument.getValue();
    assertThat(request, notNullValue());
    Query query = request.getQuery();
    assertThat(query, notNullValue());
    assertThat(query.getPageSize(), is(replicateCommand.batchSize));
    if (replicateCommand.multithreaded == 1) {
        assertThat(query.getStartIndex(), is((((int) ((double) (actualMaxMetacards - 1) / (double) replicateCommand.batchSize)) * replicateCommand.batchSize + 1)));
    }
    assertThat(query.getSortBy().getPropertyName().getPropertyName(), is(sortBy));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) Query(ddf.catalog.operation.Query)

Example 9 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class ReplicateCommandTest method setUp.

@Before
public void setUp() throws UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
    catalogFramework = mock(CatalogFramework.class);
    replicateCommand = new ReplicateCommand() {

        @Override
        public String getInput(String message) throws IOException {
            return "sourceId1";
        }
    };
    replicateCommand.catalogFramework = catalogFramework;
    replicateCommand.filterBuilder = new GeotoolsFilterBuilder();
    when(mockSession.getKeyboard()).thenReturn(mockIS);
    when(catalogFramework.getSourceIds()).thenReturn(SOURCE_IDS);
    when(catalogFramework.query(isA(QueryRequest.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        QueryRequest request = (QueryRequest) args[0];
        QueryResponse mockQueryResponse = mock(QueryResponse.class);
        when(mockQueryResponse.getHits()).thenReturn(Long.valueOf(HITS));
        when(mockQueryResponse.getResults()).thenReturn(getResultList(Math.min(replicateCommand.batchSize, HITS - request.getQuery().getStartIndex() + 1)));
        return mockQueryResponse;
    });
    when(catalogFramework.create(isA(CreateRequest.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        CreateRequest request = (CreateRequest) args[0];
        when(mockCreateResponse.getCreatedMetacards()).thenReturn(request.getMetacards());
        return mockCreateResponse;
    });
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) CreateRequest(ddf.catalog.operation.CreateRequest) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) Before(org.junit.Before)

Example 10 with QueryRequest

use of ddf.catalog.operation.QueryRequest in project ddf by codice.

the class FederationStrategyTest method testFederateOneSourceOffsetTwoPageSizeTwo.

/**
     * Verify that the original query is used by the source.
     * <p>
     * No special results handling done by OffsetResultsHandler.
     */
@Test
public void testFederateOneSourceOffsetTwoPageSizeTwo() throws Exception {
    LOGGER.debug("testFederate_OneSource_OffsetTwo_PageSizeTwo()");
    // Offset of 2
    when(mockQuery.getStartIndex()).thenReturn(2);
    // Page size of 2
    when(mockQuery.getPageSize()).thenReturn(2);
    QueryRequest queryRequest = mock(QueryRequest.class);
    when(queryRequest.getQuery()).thenReturn(mockQuery);
    // ArgumentCaptor<QueryRequest> argument = ArgumentCaptor.forClass(QueryRequest.class);
    /**
         * When using the original query to query the source, the desired offset and page size are
         * used. So, the results returned by the source already have the offset and page size taken
         * into account.
         */
    Result mockResult1 = mock(Result.class);
    Result mockResult2 = mock(Result.class);
    SourceResponse mockSourceResponse = mock(SourceResponse.class);
    List<Result> results = Arrays.asList(mockResult1, mockResult2);
    when(mockSourceResponse.getResults()).thenReturn(results);
    Source mockSource1 = mock(Source.class);
    when(mockSource1.query(any(QueryRequest.class))).thenReturn(mockSourceResponse);
    when(mockSource1.getId()).thenReturn("####### MOCK SOURCE 1.1 #######");
    // Only one source
    List<Source> sources = new ArrayList<Source>(1);
    sources.add(mockSource1);
    SortedFederationStrategy strategy = new SortedFederationStrategy(executor, new ArrayList<PreFederatedQueryPlugin>(), new ArrayList<PostFederatedQueryPlugin>());
    // Run Test
    QueryResponse federatedResponse = strategy.federate(sources, queryRequest);
    // Verification
    assertNotNull(federatedResponse);
    LOGGER.debug("Federated response result size: {}", federatedResponse.getResults().size());
    /**
         * Verify two results (page size) are returned. The results returned by the source already
         * have the offset and page size taken into account, so we can verify that the lists match.
         */
    assertEquals(2, federatedResponse.getResults().size());
    assertEquals(mockResult1, federatedResponse.getResults().get(0));
    assertEquals(mockResult2, federatedResponse.getResults().get(1));
    LOGGER.debug("mockResult1: {}", mockResult1);
    LOGGER.debug("mockResult2: {}", mockResult2);
    for (Result result : federatedResponse.getResults()) {
        LOGGER.debug("result: {}", result);
    }
}
Also used : 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) QueryResponse(ddf.catalog.operation.QueryResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

QueryRequest (ddf.catalog.operation.QueryRequest)153 Test (org.junit.Test)98 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)86 QueryImpl (ddf.catalog.operation.impl.QueryImpl)66 QueryResponse (ddf.catalog.operation.QueryResponse)57 ArrayList (java.util.ArrayList)41 SourceResponse (ddf.catalog.operation.SourceResponse)39 Metacard (ddf.catalog.data.Metacard)33 Result (ddf.catalog.data.Result)31 Filter (org.opengis.filter.Filter)31 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)30 Query (ddf.catalog.operation.Query)29 Source (ddf.catalog.source.Source)24 FederationException (ddf.catalog.federation.FederationException)20 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)15 InputStream (java.io.InputStream)15 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)14 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)13 SortByImpl (ddf.catalog.filter.impl.SortByImpl)11 HashMap (java.util.HashMap)11