Search in sources :

Example 1 with Result

use of org.opennms.newts.api.search.SearchResults.Result in project newts by OpenNMS.

the class CassandraResourceTreeWalker method depthFirstSearch.

/**
     * Visits all nodes in the resource tree bellow the given resource using
     * depth-first search.
     */
public void depthFirstSearch(Context context, SearchResultVisitor visitor, Resource root) {
    ArrayDeque<SearchResults.Result> stack = Queues.newArrayDeque();
    // Build an instance of a SearchResult for the root resource
    // but don't invoke the visitor with it
    boolean skipFirstVisit = true;
    SearchResults initialResults = new SearchResults();
    initialResults.addResult(root, new ArrayList<String>(0));
    stack.add(initialResults.iterator().next());
    while (!stack.isEmpty()) {
        SearchResults.Result r = stack.pop();
        if (skipFirstVisit) {
            skipFirstVisit = false;
        } else {
            if (!visitor.visit(r)) {
                return;
            }
        }
        // Reverse the order of the results so we walk the left-most
        // branches first
        ImmutableList<SearchResults.Result> results = ImmutableList.copyOf(m_searcher.search(context, matchKeyAndValue(Constants.PARENT_TERM_FIELD, r.getResource().getId())));
        for (SearchResults.Result result : results.reverse()) {
            stack.push(result);
        }
    }
}
Also used : Result(org.opennms.newts.api.search.SearchResults.Result) SearchResults(org.opennms.newts.api.search.SearchResults) Result(org.opennms.newts.api.search.SearchResults.Result)

Example 2 with Result

use of org.opennms.newts.api.search.SearchResults.Result in project opennms by OpenNMS.

the class NewtsResourceStorageDao method children.

@Override
public Set<ResourcePath> children(ResourcePath path, int depth) {
    Preconditions.checkArgument(depth >= 0, "depth must be non-negative");
    Set<ResourcePath> matches = Sets.newTreeSet();
    SearchResults results = searchFor(path, depth, false);
    for (Result result : results) {
        // Relativize the path
        ResourcePath child = toChildResourcePath(path, result.getResource().getId());
        if (child == null) {
            // This shouldn't happen
            LOG.warn("Encountered non-child resource {} when searching for {} with depth {}. Ignoring resource.", result.getResource(), path, depth);
            continue;
        }
        matches.add(child);
    }
    return matches;
}
Also used : NewtsUtils.toResourcePath(org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath) ResourcePath(org.opennms.netmgt.model.ResourcePath) SearchResults(org.opennms.newts.api.search.SearchResults) Result(org.opennms.newts.api.search.SearchResults.Result)

Example 3 with Result

use of org.opennms.newts.api.search.SearchResults.Result in project newts by OpenNMS.

the class CassandraIndexerITCase method test.

@Test
public void test() {
    Map<String, String> base = map("meat", "people", "bread", "beer");
    List<Sample> samples = Lists.newArrayList();
    samples.add(sampleFor(new Resource("aaa", Optional.of(base)), "m0"));
    samples.add(sampleFor(new Resource("aab", Optional.of(map(base, "music", "metal", "beverage", "beer"))), "m0"));
    samples.add(sampleFor(new Resource("aac:aaa", Optional.of(map(base, "music", "country"))), "m0"));
    CassandraSession session = newtsInstance.getCassandraSession();
    ResourceMetadataCache mockCache = mock(ResourceMetadataCache.class);
    when(mockCache.get(any(Context.class), any(Resource.class))).thenReturn(Optional.<ResourceMetadata>absent());
    MetricRegistry registry = new MetricRegistry();
    ContextConfigurations contextConfigurations = new ContextConfigurations();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(false).build();
    Indexer indexer = new CassandraIndexer(session, 86400, mockCache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    indexer.update(samples);
    CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
    // Match path components
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(2));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aac")).size(), equalTo(1));
    // Match attribute values
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("people")).size(), equalTo(3));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).size(), equalTo(1));
    // Match attribute key + value pairs
    BooleanQuery query = new BooleanQuery();
    query.add(new TermQuery(new Term("beverage", "beer")), Operator.OR);
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(1));
    // Or'd terms
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal", "country")).size(), equalTo(2));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("beer", "wine")).size(), equalTo(3));
    // And'd terms
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("metal", "country")).size(), equalTo(0));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAllValues("aaa", "aac")).size(), equalTo(1));
    // Groups queries
    // (beer AND metal) OR (aaa AND country)
    BooleanQuery subquery1 = new BooleanQuery();
    subquery1.add(new TermQuery(new Term("beer")), Operator.OR);
    subquery1.add(new TermQuery(new Term("metal")), Operator.AND);
    BooleanQuery subquery2 = new BooleanQuery();
    subquery2.add(new TermQuery(new Term("aaa")), Operator.OR);
    subquery2.add(new TermQuery(new Term("country")), Operator.AND);
    query = new BooleanQuery();
    query.add(subquery1, Operator.OR);
    query.add(subquery2, Operator.OR);
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, query).size(), equalTo(2));
    // Attributes are retrieved
    Result r = searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("metal")).iterator().next();
    assertThat(r.getResource().getId(), is(equalTo("aab")));
    assertThat(r.getResource().getAttributes().isPresent(), is(true));
    assertThat(r.getResource().getAttributes().get(), equalTo(map(base, "music", "metal", "beverage", "beer")));
    // Metrics too
    assertThat(r.getMetrics().size(), equalTo(1));
    assertThat(r.getMetrics().iterator().next(), equalTo("m0"));
}
Also used : Context(org.opennms.newts.api.Context) BooleanQuery(org.opennms.newts.api.search.BooleanQuery) TermQuery(org.opennms.newts.api.search.TermQuery) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) Term(org.opennms.newts.api.search.Term) Result(org.opennms.newts.api.search.SearchResults.Result) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 4 with Result

use of org.opennms.newts.api.search.SearchResults.Result in project newts by OpenNMS.

the class CassandraIndexerITCase method canWalkTheResourceTree.

@Test
public void canWalkTheResourceTree() {
    Map<String, String> base = map("meat", "people", "bread", "beer");
    List<Sample> samples = Lists.newArrayList();
    samples.add(sampleFor(new Resource("a:b:c", Optional.of(base)), "m0"));
    samples.add(sampleFor(new Resource("a:b", Optional.of(base)), "m1"));
    samples.add(sampleFor(new Resource("x:b:z", Optional.of(base)), "m2"));
    CassandraSession session = newtsInstance.getCassandraSession();
    ResourceMetadataCache mockCache = mock(ResourceMetadataCache.class);
    when(mockCache.get(any(Context.class), any(Resource.class))).thenReturn(Optional.<ResourceMetadata>absent());
    MetricRegistry registry = new MetricRegistry();
    ContextConfigurations contextConfigurations = new ContextConfigurations();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).build();
    Indexer indexer = new CassandraIndexer(session, 86400, mockCache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    indexer.update(samples);
    CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
    // Verify specific search results
    SearchResults results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "_root"));
    Iterator<Result> it = results.iterator();
    Result result = it.next();
    assertThat(result.getResource().getId(), equalTo("a"));
    // a is a resource with no metrics
    assertThat(result.getMetrics().size(), equalTo(0));
    result = it.next();
    assertThat(result.getResource().getId(), equalTo("x"));
    // x is a resource with no metrics
    assertThat(result.getMetrics().size(), equalTo(0));
    results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "a"));
    result = results.iterator().next();
    assertThat(result.getResource().getId(), equalTo("a:b"));
    assertThat(result.getMetrics().size(), equalTo(1));
    results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "a:b"));
    result = results.iterator().next();
    assertThat(result.getResource().getId(), equalTo("a:b:c"));
    assertThat(result.getMetrics().size(), equalTo(1));
    results = searcher.search(Context.DEFAULT_CONTEXT, matchKeyAndValue("_parent", "a:b:c"));
    assertThat(results.iterator().hasNext(), equalTo(false));
    // Walk the tree via BFS
    LoggingResourceVisitor visitor = new LoggingResourceVisitor();
    CassandraResourceTreeWalker resourceTreeWalker = new CassandraResourceTreeWalker(searcher);
    resourceTreeWalker.breadthFirstSearch(Context.DEFAULT_CONTEXT, visitor);
    assertThat(visitor.getResourceIds(), equalTo(Lists.newArrayList("a", "x", "a:b", "x:b", "a:b:c", "x:b:z")));
    // Walk the tree via DFS
    visitor = new LoggingResourceVisitor();
    resourceTreeWalker.depthFirstSearch(Context.DEFAULT_CONTEXT, visitor);
    assertThat(visitor.getResourceIds(), equalTo(Lists.newArrayList("a", "a:b", "a:b:c", "x", "x:b", "x:b:z")));
}
Also used : Context(org.opennms.newts.api.Context) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) SearchResults(org.opennms.newts.api.search.SearchResults) Result(org.opennms.newts.api.search.SearchResults.Result) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 5 with Result

use of org.opennms.newts.api.search.SearchResults.Result in project newts by OpenNMS.

the class CassandraResourceTreeWalker method breadthFirstSearch.

/**
     * Visits all nodes in the resource tree bellow the given resource using
     * breadth-first search.
     */
public void breadthFirstSearch(Context context, SearchResultVisitor visitor, Resource root) {
    Queue<Resource> queue = Lists.newLinkedList();
    queue.add(root);
    while (!queue.isEmpty()) {
        Resource r = queue.remove();
        for (SearchResults.Result result : m_searcher.search(context, matchKeyAndValue(Constants.PARENT_TERM_FIELD, r.getId()))) {
            if (!visitor.visit(result)) {
                return;
            }
            queue.add(result.getResource());
        }
    }
}
Also used : Result(org.opennms.newts.api.search.SearchResults.Result) Resource(org.opennms.newts.api.Resource) SearchResults(org.opennms.newts.api.search.SearchResults)

Aggregations

Result (org.opennms.newts.api.search.SearchResults.Result)7 SearchResults (org.opennms.newts.api.search.SearchResults)6 Resource (org.opennms.newts.api.Resource)4 Context (org.opennms.newts.api.Context)3 Sample (org.opennms.newts.api.Sample)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Test (org.junit.Test)2 ResourcePath (org.opennms.netmgt.model.ResourcePath)2 NewtsUtils.toResourcePath (org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath)2 Indexer (org.opennms.newts.api.search.Indexer)2 QueryBuilder (org.opennms.newts.api.search.QueryBuilder)2 CassandraSession (org.opennms.newts.cassandra.CassandraSession)2 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Preconditions (com.google.common.base.Preconditions)1 Throwables (com.google.common.base.Throwables)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1