use of org.opennms.newts.api.search.SearchResults in project newts by OpenNMS.
the class CassandraSearcher method search.
@Override
public SearchResults search(Context context, Query query, boolean populateMetricsAndAttributes) {
checkNotNull(context, "context argument");
checkNotNull(query, "query argument");
Timer.Context ctx = m_searchTimer.time();
ConsistencyLevel readConsistency = m_contextConfigurations.getReadConsistency(context);
SearchResults searchResults = new SearchResults();
try {
Set<String> ids;
Query q = query.rewrite();
if (q instanceof BooleanQuery) {
ids = searchForIds(context, (BooleanQuery) q, readConsistency);
} else if (q instanceof TermQuery) {
ids = searchForIds(context, (TermQuery) q, readConsistency);
} else {
throw new IllegalStateException("Unsupported query: " + q);
}
for (final String id : ids) {
if (!populateMetricsAndAttributes) {
Resource resource = new Resource(id);
List<String> emptyList = Collections.emptyList();
searchResults.addResult(resource, emptyList);
} else {
// Fetch the metric names and attributes concurrently
ResultSetFuture attrsFuture = fetchResourceAttributes(context, id, readConsistency);
ResultSetFuture metricsFuture = fetchMetricNames(context, id, readConsistency);
try {
Map<String, String> attrs = getResourceAttributesFromResults(attrsFuture);
Collection<String> metrics = getMetricNamesFromResults(metricsFuture);
Resource resource = attrs.size() > 0 ? new Resource(id, Optional.of(attrs)) : new Resource(id);
searchResults.addResult(resource, metrics);
} catch (ExecutionException | InterruptedException e) {
throw Throwables.propagate(e);
}
}
}
return searchResults;
} finally {
ctx.stop();
}
}
use of org.opennms.newts.api.search.SearchResults in project opennms by OpenNMS.
the class NewtsResourceStorageDao method getAttributes.
@Override
public Set<OnmsAttribute> getAttributes(ResourcePath path) {
Set<OnmsAttribute> attributes = Sets.newHashSet();
// Fetch the resource-level attributes in parallel
Future<Map<String, String>> stringAttributes = ForkJoinPool.commonPool().submit(getResourceAttributesCallable(path));
// Gather the list of metrics available under the resource path
SearchResults results = searchFor(path, 0, true);
for (Result result : results) {
final String resourceId = result.getResource().getId();
final ResourcePath resultPath = toResourcePath(resourceId);
if (!path.equals(resultPath)) {
// This shouldn't happen
LOG.warn("Encountered non-child resource {} when searching for {} with depth {}. Ignoring resource.", result.getResource(), path, 0);
continue;
}
for (String metric : result.getMetrics()) {
// Use the metric name as the dsName
// Store the resource id in the rrdFile field
attributes.add(new RrdGraphAttribute(metric, "", resourceId));
}
}
// Add the resource level attributes to the result set
try {
stringAttributes.get().entrySet().stream().map(e -> new StringPropertyAttribute(e.getKey(), e.getValue())).forEach(attr -> attributes.add(attr));
} catch (InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
return attributes;
}
use of org.opennms.newts.api.search.SearchResults in project opennms by OpenNMS.
the class NewtsResourceStorageDao method searchFor.
private SearchResults searchFor(ResourcePath path, int depth, boolean fetchMetrics) {
final Query q = findResourcesWithMetricsAtDepth(path, depth);
LOG.trace("Searching for '{}'.", q);
final SearchResults results = m_searcher.search(m_context, q, fetchMetrics);
LOG.trace("Found {} results.", results.size());
return results;
}
use of org.opennms.newts.api.search.SearchResults in project opennms by OpenNMS.
the class NewtsResourceStorageDao method delete.
@Override
public boolean delete(ResourcePath path) {
final SearchResults results = searchFor(path, 0, true);
if (results.isEmpty()) {
return false;
}
for (final Result result : results) {
m_sampleRepository.delete(m_context, result.getResource());
m_indexer.delete(m_context, result.getResource());
}
return true;
}
Aggregations