Search in sources :

Example 1 with QueryCache

use of org.infinispan.query.core.impl.QueryCache in project infinispan by infinispan.

the class NamedParamsPerfTest method testNamedParamPerfComparison.

public void testNamedParamPerfComparison() {
    QueryFactory factory = getQueryFactory();
    String[] fnames = { "Matej", "Roman", "Jakub", "Jiri", "Anna", "Martin", "Vojta", "Alan" };
    String[] lnames = { "Cimbora", "Macor", "Markos", "Holusa", "Manukyan", "Gencur", "Vrabel", "Juranek", "Field" };
    Random random = new Random(3);
    for (int i = 0; i < 999; i++) {
        cache(0).put(i, new Person(i, fnames[random.nextInt(fnames.length)], lnames[random.nextInt(lnames.length)]));
    }
    cache(0).put(1000, new Person(999, "Unnamed", "Unnamed"));
    QueryCache queryCache = manager(0).getGlobalComponentRegistry().getComponent(QueryCache.class);
    assertNotNull(queryCache);
    Query<Person> query = factory.create("FROM " + Person.class.getName() + " WHERE firstName = :nameParam1 OR lastName = :nameParam2 OR id >= :idParam1 OR id < :idParam2");
    long t1 = 0;
    long t2 = 0;
    long t3 = 0;
    for (int i = 0; i < ITERATIONS; i++) {
        queryCache.clear();
        long start = System.nanoTime();
        query.setParameter("nameParam1", "Unnamed").setParameter("nameParam2", "ww").setParameter("idParam1", 1000).setParameter("idParam2", 0);
        List<Person> list = query.execute().list();
        // first run is expected to take much longer than subsequent runs
        t1 += (System.nanoTime() - start);
        assertEquals(1, list.size());
        start = System.nanoTime();
        query.setParameter("nameParam1", "Unnamed").setParameter("nameParam2", "zz").setParameter("idParam1", 2000).setParameter("idParam2", -1000);
        list = query.execute().list();
        t2 += (System.nanoTime() - start);
        assertEquals(1, list.size());
        start = System.nanoTime();
        query.setParameter("nameParam1", "Unnamed").setParameter("nameParam2", "bb").setParameter("idParam1", 5000).setParameter("idParam2", -3000);
        list = query.execute().list();
        t3 += (System.nanoTime() - start);
        assertEquals(1, list.size());
    }
    System.out.println("NamedParamsPerfTest.testNamedParamPerfComparison t1 (avg, us) = " + (t1 / 1000.0 / ITERATIONS));
    System.out.println("NamedParamsPerfTest.testNamedParamPerfComparison t2 (avg, us) = " + (t2 / 1000.0 / ITERATIONS));
    System.out.println("NamedParamsPerfTest.testNamedParamPerfComparison t3 (avg, us) = " + (t3 / 1000.0 / ITERATIONS));
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) QueryCache(org.infinispan.query.core.impl.QueryCache) Random(java.util.Random)

Example 2 with QueryCache

use of org.infinispan.query.core.impl.QueryCache in project infinispan by infinispan.

the class QueryCacheEmbeddedTest method testQueryCache.

public void testQueryCache() {
    // populate our data cache
    UserHS user = new UserHS();
    user.setId(1);
    user.setName("John");
    cache.put("user_" + user.getId(), user);
    // obtain the query cache component
    QueryCache queryCache = ComponentRegistryUtils.getQueryCache(cache);
    // force creation of the lazy internal cache and ensure it is empty
    queryCache.get("someCacheName", "someQueryString", null, "typeDiscriminator", (queryString, acc) -> queryString);
    // ensure internal cache is empty
    queryCache.clear();
    // obtain a reference to the internal query cache via reflection
    Cache<?, ?> internalCache = TestingUtil.extractField(QueryCache.class, queryCache, "lazyCache");
    String queryString = "from org.infinispan.query.dsl.embedded.testdomain.hsearch.UserHS u where u.name = 'John'";
    // everything is ready to go
    // ensure the QueryCreator gets invoked
    int[] invoked = { 0 };
    IckleParsingResult<?> created = queryCache.get(cache.getName(), queryString, null, IckleParsingResult.class, (qs, acc) -> {
        invoked[0]++;
        return null;
    });
    assertEquals(1, invoked[0]);
    assertNull(created);
    // test that the query cache does not have it already
    assertEquals(0, internalCache.size());
    // create and execute a query
    Query<?> query = Search.getQueryFactory(cache).create(queryString);
    query.execute().list();
    // ensure the query cache has it now: one FilterParsingResult and one SearchQueryParsingResult
    assertEquals(2, internalCache.size());
    Set<Class<?>> cacheValueClasses = internalCache.values().stream().map(Object::getClass).collect(Collectors.toSet());
    Set<Class<?>> expectedCachedValueClasses = Sets.newLinkedHashSet(IckleParsingResult.class, SearchQueryParsingResult.class);
    assertEquals(expectedCachedValueClasses, cacheValueClasses);
    // ensure the QueryCreator does not get invoked now
    IckleParsingResult<?> cached = queryCache.get(cache.getName(), queryString, null, IckleParsingResult.class, (qs, acc) -> {
        throw new AssertionError("QueryCreator should not be invoked now");
    });
    assertNotNull(cached);
}
Also used : QueryCache(org.infinispan.query.core.impl.QueryCache) UserHS(org.infinispan.query.dsl.embedded.testdomain.hsearch.UserHS)

Example 3 with QueryCache

use of org.infinispan.query.core.impl.QueryCache in project infinispan by infinispan.

the class IckleContinuousQueryCacheEventFilterConverter method injectDependencies.

/**
 * Acquires a Matcher instance from the ComponentRegistry of the given Cache object.
 */
@Inject
protected void injectDependencies(Cache<?, ?> cache) {
    cacheName = cache.getName();
    ComponentRegistry componentRegistry = cache.getAdvancedCache().getComponentRegistry();
    queryCache = componentRegistry.getComponent(QueryCache.class);
    matcher = componentRegistry.getComponent(matcherImplClass);
    if (matcher == null) {
        throw new CacheException("Expected component not found in registry: " + matcherImplClass.getName());
    }
}
Also used : QueryCache(org.infinispan.query.core.impl.QueryCache) ComponentRegistry(org.infinispan.factories.ComponentRegistry) CacheException(org.infinispan.commons.CacheException) Inject(org.infinispan.factories.annotations.Inject)

Aggregations

QueryCache (org.infinispan.query.core.impl.QueryCache)3 Random (java.util.Random)1 CacheException (org.infinispan.commons.CacheException)1 ComponentRegistry (org.infinispan.factories.ComponentRegistry)1 Inject (org.infinispan.factories.annotations.Inject)1 QueryFactory (org.infinispan.query.dsl.QueryFactory)1 UserHS (org.infinispan.query.dsl.embedded.testdomain.hsearch.UserHS)1