Search in sources :

Example 66 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientQueryCacheIndexTest method test_entrySet_onIndexedKeys_whenValuesNotCached.

@Test
public void test_entrySet_onIndexedKeys_whenValuesNotCached() {
    String mapName = randomString();
    String cacheName = randomString();
    HazelcastInstance client = factory.newHazelcastClient();
    IMap<Integer, Employee> map = client.getMap(mapName);
    int putCount = 111;
    for (int i = 0; i < putCount; i++) {
        map.put(i, new Employee(i));
    }
    final QueryCache<Integer, Employee> cache = map.getQueryCache(cacheName, TRUE_PREDICATE, false);
    // here add index to key. (key --> integer; value --> Employee)
    cache.addIndex("__key", true);
    for (int i = 17; i < putCount; i++) {
        map.remove(i);
    }
    // just choose arbitrary numbers to prove whether #entrySet with predicate is working
    int smallerThan = 17;
    int expectedSize = 17;
    assertEntrySetSizeEventually(expectedSize, new SqlPredicate("__key < " + smallerThan), cache);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.mapreduce.helpers.Employee) SqlPredicate(com.hazelcast.query.SqlPredicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 67 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientQueryCacheIndexTest method test_values_withoutIndex_whenValuesNotCached.

@Test
public void test_values_withoutIndex_whenValuesNotCached() {
    String mapName = randomString();
    String cacheName = randomString();
    HazelcastInstance client = factory.newHazelcastClient();
    IMap<Integer, Employee> map = client.getMap(mapName);
    int putCount = 111;
    for (int i = 0; i < putCount; i++) {
        map.put(i, new Employee(i));
    }
    QueryCache<Integer, Employee> cache = map.getQueryCache(cacheName, TRUE_PREDICATE, false);
    for (int i = 17; i < putCount; i++) {
        map.remove(i);
    }
    // just choose arbitrary numbers to prove whether #entrySet with predicate is working
    int smallerThan = 17;
    int expectedSize = 0;
    assertValuesSizeEventually(expectedSize, new SqlPredicate("__key < " + smallerThan), cache);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.mapreduce.helpers.Employee) SqlPredicate(com.hazelcast.query.SqlPredicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 68 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientQueryCacheIndexTest method test_keySet_withPredicate_whenValuesAreNotCached.

@Test
public void test_keySet_withPredicate_whenValuesAreNotCached() {
    String mapName = randomString();
    String cacheName = randomString();
    QueryCacheConfig queryCacheConfig = new QueryCacheConfig(cacheName);
    queryCacheConfig.setDelaySeconds(1);
    queryCacheConfig.setBatchSize(3);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addQueryCacheConfig(mapName, queryCacheConfig);
    HazelcastInstance client = factory.newHazelcastClient();
    IMap<Integer, Employee> map = client.getMap(mapName);
    // populate map before construction of query cache
    int putCount = 111;
    for (int i = 0; i < putCount; i++) {
        map.put(i, new Employee(i));
    }
    QueryCache<Integer, Employee> cache = map.getQueryCache(cacheName, TRUE_PREDICATE, false);
    cache.addIndex("__key", true);
    // populate map after construction of query cache
    for (int i = putCount; i < 2 * putCount; i++) {
        map.put(i, new Employee(i));
    }
    // just choose arbitrary numbers for querying in order to prove whether #keySet with predicate is correctly working
    int equalsOrBiggerThan = 27;
    int expectedSize = 2 * putCount - equalsOrBiggerThan;
    assertKeySetSizeEventually(expectedSize, new SqlPredicate("__key >= " + equalsOrBiggerThan), cache);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.mapreduce.helpers.Employee) QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) SqlPredicate(com.hazelcast.query.SqlPredicate) ClientConfig(com.hazelcast.client.config.ClientConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 69 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientQueryCacheListenerTest method shouldReceiveEvent_whenListeningKey_withPredicate.

@Test
public void shouldReceiveEvent_whenListeningKey_withPredicate() {
    String mapName = randomString();
    String cacheName = randomString();
    HazelcastInstance instance = factory.newHazelcastClient();
    IMap<Integer, Employee> map = instance.getMap(mapName);
    CountDownLatch numberOfCaughtEvents = new CountDownLatch(1);
    QueryCache<Integer, Employee> cache = map.getQueryCache(cacheName, TRUE_PREDICATE, true);
    int keyToListen = 109;
    cache.addEntryListener(new QueryCacheAdditionListener(numberOfCaughtEvents), new SqlPredicate("id > 100"), keyToListen, true);
    int putCount = 111;
    for (int i = 0; i < putCount; i++) {
        map.put(i, new Employee(i));
    }
    assertOpenEventually(numberOfCaughtEvents, 10);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.mapreduce.helpers.Employee) SqlPredicate(com.hazelcast.query.SqlPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 70 with SqlPredicate

use of com.hazelcast.query.SqlPredicate in project hazelcast by hazelcast.

the class ClientQueryCacheListenerTest method shouldReceiveEvent_whenListeningKey_withMultipleListener.

@Test
public void shouldReceiveEvent_whenListeningKey_withMultipleListener() {
    String mapName = randomString();
    String cacheName = randomString();
    HazelcastInstance instance = factory.newHazelcastClient();
    IMap<Integer, Employee> map = instance.getMap(mapName);
    CountDownLatch additionCount = new CountDownLatch(2);
    CountDownLatch removalCount = new CountDownLatch(2);
    final QueryCache<Integer, Employee> cache = map.getQueryCache(cacheName, TRUE_PREDICATE, true);
    int keyToListen = 109;
    cache.addEntryListener(new QueryCacheAdditionListener(additionCount), new SqlPredicate("id > 100"), keyToListen, true);
    cache.addEntryListener(new QueryCacheRemovalListener(removalCount), new SqlPredicate("id > 100"), keyToListen, true);
    // populate map before construction of query cache
    int count = 111;
    for (int i = 0; i < count; i++) {
        map.put(i, new Employee(i));
    }
    for (int i = 0; i < count; i++) {
        map.remove(i);
    }
    for (int i = 0; i < count; i++) {
        map.put(i, new Employee(i));
    }
    for (int i = 0; i < count; i++) {
        map.remove(i);
    }
    AssertTask task = new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(0, cache.size());
        }
    };
    assertTrueEventually(task);
    assertOpenEventually(cache.size() + "", additionCount, 10);
    assertOpenEventually(cache.size() + "", removalCount, 10);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.mapreduce.helpers.Employee) SqlPredicate(com.hazelcast.query.SqlPredicate) AssertTask(com.hazelcast.test.AssertTask) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

SqlPredicate (com.hazelcast.query.SqlPredicate)111 Test (org.junit.Test)93 QuickTest (com.hazelcast.test.annotation.QuickTest)87 ParallelTest (com.hazelcast.test.annotation.ParallelTest)83 HazelcastInstance (com.hazelcast.core.HazelcastInstance)73 Config (com.hazelcast.config.Config)36 Employee (com.hazelcast.mapreduce.helpers.Employee)28 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)24 MapIndexConfig (com.hazelcast.config.MapIndexConfig)22 IMap (com.hazelcast.core.IMap)21 Employee (com.hazelcast.query.SampleObjects.Employee)21 MapStoreConfig (com.hazelcast.config.MapStoreConfig)18 Collection (java.util.Collection)14 MapConfig (com.hazelcast.config.MapConfig)13 Predicate (com.hazelcast.query.Predicate)12 Map (java.util.Map)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 PortableEmployee (com.hazelcast.query.SampleObjects.PortableEmployee)10 AssertTask (com.hazelcast.test.AssertTask)10 Value (com.hazelcast.query.SampleObjects.Value)9