Search in sources :

Example 11 with ModifiedExpiryPolicy

use of javax.cache.expiry.ModifiedExpiryPolicy in project ignite by apache.

the class CacheOperationsWithExpirationTest method concurrentPutGetRemoveExpireAndQuery.

/**
 * @param ccfg Cache configuration.
 * @throws Exception If failed.
 */
private void concurrentPutGetRemoveExpireAndQuery(CacheConfiguration<String, TestIndexedType> ccfg) throws Exception {
    Ignite ignite = ignite(0);
    final IgniteCache<String, TestIndexedType> cache = ignite.createCache(ccfg);
    final boolean qryEnabled = !F.isEmpty(ccfg.getQueryEntities());
    try {
        final long stopTime = U.currentTimeMillis() + 30_000;
        GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {

            @Override
            public void apply(Integer idx) {
                while (U.currentTimeMillis() < stopTime) {
                    if (!qryEnabled || idx % 2 == 0)
                        putGet(cache);
                    else
                        query(cache);
                }
            }

            void putGet(IgniteCache<String, TestIndexedType> cache) {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                cache = cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(MILLISECONDS, rnd.nextLong(100) + 1)));
                for (int i = 0; i < KEYS; i++) {
                    String key = String.valueOf(rnd.nextInt(KEYS));
                    cache.put(key, testValue(rnd));
                }
                Set<String> s = new TreeSet<>();
                for (int i = 0; i < 1000; i++) {
                    String key = String.valueOf(rnd.nextInt(KEYS));
                    s.add(key);
                }
                cache.getAll(s);
                cache.removeAll(s);
            }

            void query(IgniteCache<String, TestIndexedType> cache) {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                String k = String.valueOf(rnd.nextInt(KEYS));
                SqlFieldsQuery qry1;
                if (rnd.nextBoolean()) {
                    qry1 = new SqlFieldsQuery("select _key, _val from TestIndexedType where key1=? and intVal=?");
                    qry1.setArgs(k, k);
                } else
                    qry1 = new SqlFieldsQuery("select _key, _val from TestIndexedType");
                List res = cache.query(qry1).getAll();
                assertNotNull(res);
            }
        }, 10, "test-thread");
    } finally {
        ignite.destroyCache(cache.getName());
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) Duration(javax.cache.expiry.Duration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) List(java.util.List) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Aggregations

Duration (javax.cache.expiry.Duration)11 ModifiedExpiryPolicy (javax.cache.expiry.ModifiedExpiryPolicy)11 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)3 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)3 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)3 Test (org.junit.Test)3 AccessedExpiryPolicy (javax.cache.expiry.AccessedExpiryPolicy)2 Ignite (org.apache.ignite.Ignite)2 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)2 CaffeineConfiguration (com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1