Search in sources :

Example 6 with Indexer

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

the class CassandraIndexerITCase method testCache.

@Test
public void testCache() {
    ResourceMetadataCache cache = mock(ResourceMetadataCache.class);
    when(cache.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(newtsInstance.getCassandraSession(), 86400, cache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    Sample s = sampleFor(new Resource("aaa", Optional.of(map("beverage", "beer"))), "m0");
    indexer.update(Collections.singletonList(s));
    ResourceMetadata expected = new ResourceMetadata().putMetric("m0").putAttribute("beverage", "beer");
    verify(cache, atLeast(1)).get(any(Context.class), any(Resource.class));
    verify(cache).merge(any(Context.class), any(Resource.class), eq(expected));
}
Also used : Context(org.opennms.newts.api.Context) Indexer(org.opennms.newts.api.search.Indexer) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 7 with Indexer

use of org.opennms.newts.api.search.Indexer 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)

Aggregations

Indexer (org.opennms.newts.api.search.Indexer)7 MetricRegistry (com.codahale.metrics.MetricRegistry)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Test (org.junit.Test)5 Resource (org.opennms.newts.api.Resource)5 QueryBuilder (org.opennms.newts.api.search.QueryBuilder)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 Context (org.opennms.newts.api.Context)4 Sample (org.opennms.newts.api.Sample)4 SampleRepository (org.opennms.newts.api.SampleRepository)2 Result (org.opennms.newts.api.search.SearchResults.Result)2 JmxReporter (com.codahale.metrics.JmxReporter)1 Injector (com.google.inject.Injector)1 Managed (io.dropwizard.lifecycle.Managed)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 SampleProcessor (org.opennms.newts.api.SampleProcessor)1 BooleanQuery (org.opennms.newts.api.search.BooleanQuery)1 SearchResults (org.opennms.newts.api.search.SearchResults)1 Searcher (org.opennms.newts.api.search.Searcher)1