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;
}
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;
}
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));
}
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;
});
}
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);
}
}
Aggregations