use of ddf.catalog.operation.impl.QueryRequestImpl in project ddf by codice.
the class RemoveCommand method executeRemoveFromStore.
private Object executeRemoveFromStore() throws Exception {
CatalogFacade catalogProvider = getCatalog();
if (hasFilter()) {
QueryImpl query = new QueryImpl(getFilter());
query.setRequestsTotalResultsCount(true);
query.setPageSize(-1);
Map<String, Serializable> properties = new HashMap<>();
properties.put("mode", "native");
SourceResponse queryResponse = catalogProvider.query(new QueryRequestImpl(query, properties));
final List<String> idsFromFilteredQuery = queryResponse.getResults().stream().map(result -> result.getMetacard().getId()).collect(Collectors.toList());
if (ids == null) {
ids = idsFromFilteredQuery;
} else {
ids = ids.stream().filter(id -> idsFromFilteredQuery.contains(id)).collect(Collectors.toList());
}
}
final int numberOfMetacardsToRemove = ids.size();
if (numberOfMetacardsToRemove > 0) {
printSuccessMessage("Found " + numberOfMetacardsToRemove + " metacards to remove.");
} else {
printErrorMessage("No records found meeting filter criteria.");
return null;
}
DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[numberOfMetacardsToRemove]));
DeleteResponse response = catalogProvider.delete(request);
if (response.getDeletedMetacards().size() > 0) {
printSuccessMessage(ids + " successfully deleted.");
LOGGER.info(ids + " removed using catalog:remove command");
} else {
printErrorMessage(ids + " could not be deleted.");
LOGGER.info(ids + " could not be deleted using catalog:remove command");
}
return null;
}
use of ddf.catalog.operation.impl.QueryRequestImpl in project ddf by codice.
the class SearchCommand method executeSearchStore.
private Object executeSearchStore(Filter filter) throws Exception {
String formatString = "%1$-33s %2$-26s %3$-" + TITLE_MAX_LENGTH + "s %4$-" + EXCERPT_MAX_LENGTH + "s%n";
CatalogFacade catalogProvider = getCatalog();
QueryImpl query = new QueryImpl(filter);
query.setRequestsTotalResultsCount(true);
if (numberOfItems > -1) {
query.setPageSize(numberOfItems);
}
long start = System.currentTimeMillis();
SourceResponse response = catalogProvider.query(new QueryRequestImpl(query));
long end = System.currentTimeMillis();
int size = 0;
if (response.getResults() != null) {
size = response.getResults().size();
}
console.println();
console.printf(" %d result(s) out of %s%d%s in %3.3f seconds", (size), Ansi.ansi().fg(Ansi.Color.CYAN).toString(), response.getHits(), Ansi.ansi().reset().toString(), (end - start) / MS_PER_SECOND);
console.printf(formatString, "", "", "", "");
printHeaderMessage(String.format(formatString, ID, DATE, TITLE, EXCERPT));
for (Result result : response.getResults()) {
Metacard metacard = result.getMetacard();
String title = (metacard.getTitle() != null ? metacard.getTitle() : "N/A");
String excerpt = "N/A";
String modifiedDate = "";
if (searchPhrase != null) {
if (metacard.getMetadata() != null) {
XPathHelper helper = new XPathHelper(metacard.getMetadata());
String indexedText = helper.getDocument().getDocumentElement().getTextContent();
indexedText = indexedText.replaceAll("\\r\\n|\\r|\\n", " ");
String normalizedSearchPhrase = searchPhrase.replaceAll("\\*", "");
int index = -1;
if (caseSensitive) {
index = indexedText.indexOf(normalizedSearchPhrase);
} else {
index = indexedText.toLowerCase().indexOf(normalizedSearchPhrase.toLowerCase());
}
if (index != -1) {
int contextLength = (EXCERPT_MAX_LENGTH - normalizedSearchPhrase.length() - 8) / 2;
excerpt = "..." + indexedText.substring(Math.max(index - contextLength, 0), index);
excerpt = excerpt + Ansi.ansi().fg(Ansi.Color.GREEN).toString();
excerpt = excerpt + indexedText.substring(index, index + normalizedSearchPhrase.length());
excerpt = excerpt + Ansi.ansi().reset().toString();
excerpt = excerpt + indexedText.substring(index + normalizedSearchPhrase.length(), Math.min(indexedText.length(), index + normalizedSearchPhrase.length() + contextLength)) + "...";
}
}
}
if (metacard.getModifiedDate() != null) {
modifiedDate = new DateTime(metacard.getModifiedDate().getTime()).toString(DATETIME_FORMATTER);
}
console.printf(formatString, metacard.getId(), modifiedDate, title.substring(0, Math.min(title.length(), TITLE_MAX_LENGTH)), excerpt);
}
return null;
}
use of ddf.catalog.operation.impl.QueryRequestImpl 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.impl.QueryRequestImpl in project ddf by codice.
the class FederationStrategyTest method testQueryTimeout.
/**
* Tests that the framework properly times out using the default federation strategy.
*/
@Test
public void testQueryTimeout() throws Exception {
long queryDelay = 100;
UuidGenerator uuidGenerator = mock(UuidGenerator.class);
when(uuidGenerator.generateUuid()).thenReturn(UUID.randomUUID().toString());
MockDelayProvider provider = new MockDelayProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, new Date());
provider.setQueryDelayMillis(queryDelay);
// Mock register the provider in the container
SourcePollerRunner runner = new SourcePollerRunner();
SourcePoller poller = new SourcePoller(runner);
runner.bind(provider);
// Must have more than one thread or sleeps will block the monitor
SortedFederationStrategy fedStrategy = new SortedFederationStrategy(executor, new ArrayList<>(), new ArrayList<>());
FrameworkProperties props = new FrameworkProperties();
props.setCatalogProviders(Collections.singletonList(provider));
props.setFederationStrategy(fedStrategy);
props.setSourcePoller(poller);
props.setQueryResponsePostProcessor(mock(QueryResponsePostProcessor.class));
props.setFilterBuilder(new GeotoolsFilterBuilder());
props.setDefaultAttributeValueRegistry(new DefaultAttributeValueRegistryImpl());
OperationsSecuritySupport opsSecurity = new OperationsSecuritySupport();
MetacardFactory metacardFactory = new MetacardFactory(props.getMimeTypeToTransformerMapper(), uuidGenerator);
OperationsMetacardSupport opsMetacard = new OperationsMetacardSupport(props, metacardFactory);
Historian historian = new Historian();
historian.setHistoryEnabled(false);
SourceOperations sourceOperations = new SourceOperations(props);
QueryOperations queryOperations = new QueryOperations(props, sourceOperations, opsSecurity, opsMetacard);
OperationsStorageSupport opsStorage = new OperationsStorageSupport(sourceOperations, queryOperations);
OperationsCatalogStoreSupport opsCatStore = new OperationsCatalogStoreSupport(props, sourceOperations);
CreateOperations createOperations = new CreateOperations(props, queryOperations, sourceOperations, opsSecurity, opsMetacard, opsCatStore, opsStorage);
UpdateOperations updateOperations = new UpdateOperations(props, queryOperations, sourceOperations, opsSecurity, opsMetacard, opsCatStore, opsStorage);
DeleteOperations deleteOperations = new DeleteOperations(props, queryOperations, sourceOperations, opsSecurity, opsMetacard);
opsStorage.setHistorian(historian);
updateOperations.setHistorian(historian);
deleteOperations.setHistorian(historian);
deleteOperations.setOpsCatStoreSupport(opsCatStore);
CatalogFrameworkImpl framework = new CatalogFrameworkImpl(createOperations, updateOperations, deleteOperations, queryOperations, null, null, null);
sourceOperations.bind(provider);
List<Metacard> metacards = new ArrayList<Metacard>();
MetacardImpl newCard = new MetacardImpl();
newCard.setId(null);
metacards.add(newCard);
CreateResponse createResponse = null;
try {
try {
createResponse = framework.create(new CreateRequestImpl(metacards, null));
} catch (SourceUnavailableException e) {
long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10);
//this is a hack because the unit test is flaky and should be removed once a better test is possible
while (System.currentTimeMillis() < timeout) {
Thread.sleep(1000);
try {
createResponse = framework.create(new CreateRequestImpl(metacards, null));
break;
} catch (SourceUnavailableException e1) {
//ignore
}
}
}
if (createResponse == null) {
fail();
}
} catch (IngestException e) {
fail();
}
assertEquals(createResponse.getCreatedMetacards().size(), provider.size());
for (Metacard curCard : createResponse.getCreatedMetacards()) {
assertNotNull(curCard.getId());
}
QueryImpl query = new QueryImpl(filterFactory.equals(filterFactory.property(Metacard.ID), filterFactory.literal(createResponse.getCreatedMetacards().get(0).getId())));
query.setTimeoutMillis(SHORT_TIMEOUT);
query.setSortBy(new FilterFactoryImpl().sort(Result.RELEVANCE, SortOrder.ASCENDING));
QueryRequest fedQueryRequest = new QueryRequestImpl(query);
try {
QueryResponse response = framework.query(fedQueryRequest);
assertEquals("Timeout should happen before results return", 0, response.getHits());
} catch (UnsupportedQueryException e) {
fail();
} catch (FederationException e) {
LOGGER.error("Unexpected federation exception during test", e);
fail();
}
}
use of ddf.catalog.operation.impl.QueryRequestImpl in project ddf by codice.
the class MetacardsMigratable method export.
/**
* Exports all the metacards currently stored in the catalog framework.
* <p>
* {@inheritDoc}
*/
@Override
@NotNull
public MigrationMetadata export(@NotNull Path exportPath) throws MigrationException {
config.setExportPath(exportPath.resolve(this.getId()));
fileWriter.createExportDirectory(config.getExportPath());
Collection<MigrationWarning> warnings = new ArrayList<>();
Map<String, Serializable> props = createMapWithNativeQueryMode();
Filter dumpFilter = filterBuilder.attribute(Metacard.ANY_TEXT).is().like().text("*");
QueryImpl exportQuery = new QueryImpl(dumpFilter);
exportQuery.setPageSize(config.getExportQueryPageSize());
exportQuery.setRequestsTotalResultsCount(false);
QueryRequest exportQueryRequest = new QueryRequestImpl(exportQuery, props);
try {
executeQueryLoop(exportQuery, exportQueryRequest);
} catch (Exception e) {
LOGGER.info("Internal error occurred when exporting catalog: {}", e);
throw new ExportMigrationException(DEFAULT_FAILURE_MESSAGE);
} finally {
cleanup();
}
return new MigrationMetadata(warnings);
}
Aggregations