Search in sources :

Example 16 with IMap

use of com.hazelcast.map.IMap in project hazelcast by hazelcast.

the class ClientTxnMapTest method testTnxMapRemove.

@Test
public void testTnxMapRemove() throws Exception {
    final String mapName = randomString();
    final String key = "key1";
    final String value = "old1";
    IMap map = client.getMap(mapName);
    map.put(key, value);
    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);
    txMap.remove(key);
    context.commitTransaction();
    assertNull(map.get(key));
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) IMap(com.hazelcast.map.IMap) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 17 with IMap

use of com.hazelcast.map.IMap in project hazelcast by hazelcast.

the class ClientTxnMapTest method testDuplicateValuesWithPredicates.

@Test
public void testDuplicateValuesWithPredicates() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);
    final SampleTestObjects.Employee emp1 = new SampleTestObjects.Employee("employee1", 10, true, 10D);
    map.put("employee1", emp1);
    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);
    assertNull(txMap.put("employee1_repeated", emp1));
    assertEquals(2, txMap.size());
    assertEquals(2, txMap.keySet(Predicates.sql("age = 10")).size());
    assertEquals(2, txMap.values(Predicates.sql("age = 10")).size());
    context.commitTransaction();
    assertEquals(2, map.keySet(Predicates.sql("age = 10")).size());
    assertEquals(2, map.values(Predicates.sql("age = 10")).size());
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) IMap(com.hazelcast.map.IMap) TransactionContext(com.hazelcast.transaction.TransactionContext) SampleTestObjects(com.hazelcast.query.SampleTestObjects) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 18 with IMap

use of com.hazelcast.map.IMap in project hazelcast by hazelcast.

the class ClientTxnMapTest method prepareServerAndGetServerMap.

private IMap prepareServerAndGetServerMap(String mapName, int keyInServerNearCache) {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setCacheLocalEntries(true);
    Config serverConfig = new Config();
    serverConfig.getMapConfig(mapName).setNearCacheConfig(nearCacheConfig);
    HazelcastInstance server = hazelcastFactory.newHazelcastInstance(serverConfig);
    // populate server near cache.
    IMap map = server.getMap(mapName);
    map.put(keyInServerNearCache, 1);
    map.get(keyInServerNearCache);
    return map;
}
Also used : IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig)

Example 19 with IMap

use of com.hazelcast.map.IMap in project hazelcast by hazelcast.

the class MapListenerAdapterTest method testMapListenerAdapter_whenEntryExpired.

@Test
public void testMapListenerAdapter_whenEntryExpired() {
    String mapName = randomMapName();
    Config cfg = new Config();
    TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(1);
    HazelcastInstance instance = instanceFactory.newHazelcastInstance(cfg);
    IMap map = instance.getMap(mapName);
    final CountDownLatch expirationLatch = new CountDownLatch(1);
    map.addEntryListener(new MapListenerAdapter() {

        public void onEntryEvent(EntryEvent event) {
            expirationLatch.countDown();
        }
    }, false);
    map.put(1, 1, 100, TimeUnit.MILLISECONDS);
    sleepSeconds(1);
    // trigger immediate expiration.
    map.get(1);
    assertOpenEventually(expirationLatch);
}
Also used : IMap(com.hazelcast.map.IMap) Config(com.hazelcast.config.Config) MapListenerAdapter(com.hazelcast.map.impl.MapListenerAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 20 with IMap

use of com.hazelcast.map.IMap in project hazelcast by hazelcast.

the class AllTest method loadMapOperations.

private List<Runnable> loadMapOperations() {
    ArrayList<Runnable> operations = new ArrayList<Runnable>();
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.evict(random.nextInt(SIZE));
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            try {
                map.getAsync(random.nextInt(SIZE)).toCompletableFuture().get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.containsKey(random.nextInt(SIZE));
        }
    }, 2);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.containsValue(new Customer(random.nextInt(100), String.valueOf(random.nextInt(100000))));
        }
    }, 2);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            int key = random.nextInt(SIZE);
            map.lock(key);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                map.unlock(key);
            }
        }
    }, 1);
    // addOperation(operations, new Runnable() {
    // public void run() {
    // IMap map = hazelcast.getMap("myMap");
    // int key = random.nextInt(SIZE);
    // map.lockMap(10, TimeUnit.MILLISECONDS);
    // try {
    // Thread.sleep(1);
    // } catch (InterruptedException e) {
    // } finally {
    // map.unlockMap();
    // }
    // }
    // }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            int key = random.nextInt(SIZE);
            boolean locked = map.tryLock(key);
            if (locked) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    map.unlock(key);
                }
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            int key = random.nextInt(SIZE);
            boolean locked = false;
            try {
                locked = map.tryLock(key, 10, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (locked) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    map.unlock(key);
                }
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.entrySet().iterator();
            for (int i = 0; i < 10 && it.hasNext(); i++) {
                it.next();
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.getEntryView(random.nextInt(SIZE));
        }
    }, 2);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.isEmpty();
        }
    }, 3);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.put(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 50);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.tryPut(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            try {
                map.putAsync(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000)))).toCompletableFuture().get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.put(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.putIfAbsent(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.putIfAbsent(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 5);
    addOperation(operations, () -> {
        MapProxyImpl<Object, Object> map = (MapProxyImpl<Object, Object>) hazelcast.getMap("myMap");
        map.putIfAbsentAsync(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
    }, 5);
    addOperation(operations, () -> {
        MapProxyImpl<Object, Object> map = (MapProxyImpl<Object, Object>) hazelcast.getMap("myMap");
        map.putIfAbsentAsync(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Map localMap = new HashMap();
            for (int i = 0; i < 10; i++) {
                localMap.put(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
            }
            map.putAll(localMap);
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.get(random.nextInt(SIZE));
        }
    }, 100);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.remove(random.nextInt(SIZE));
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.tryRemove(random.nextInt(SIZE), 10, TimeUnit.MILLISECONDS);
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.removeAsync(random.nextInt(SIZE));
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.remove(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.replace(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 4);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.replace(random.nextInt(SIZE), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
        }
    }, 5);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.size();
        }
    }, 4);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.entrySet(Predicates.sql("year=" + random.nextInt(100))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.entrySet(Predicates.sql("name=" + random.nextInt(10000))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.keySet(Predicates.sql("name=" + random.nextInt(10000))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.localKeySet().iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            Iterator it = map.localKeySet(Predicates.sql("name=" + random.nextInt(10000))).iterator();
            while (it.hasNext()) {
                it.next();
            }
        }
    }, 10);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            final CountDownLatch latch = new CountDownLatch(1);
            EntryListener listener = new EntryAdapter() {

                @Override
                public void onEntryEvent(EntryEvent event) {
                    latch.countDown();
                }
            };
            UUID id = map.addEntryListener(listener, true);
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            map.removeEntryListener(id);
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            map.addIndex(IndexType.SORTED, "year");
        }
    }, 1);
    addOperation(operations, new Runnable() {

        public void run() {
            IMap map = hazelcast.getMap("myMap");
            final CountDownLatch latch = new CountDownLatch(1);
            EntryListener listener = new EntryAdapter() {

                @Override
                public void onEntryEvent(EntryEvent event) {
                    latch.countDown();
                }
            };
            UUID id = map.addLocalEntryListener(listener);
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            map.removeEntryListener(id);
        }
    }, 1);
    return operations;
}
Also used : HashMap(java.util.HashMap) EntryAdapter(com.hazelcast.core.EntryAdapter) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) EntryListener(com.hazelcast.core.EntryListener) IMap(com.hazelcast.map.IMap) MapProxyImpl(com.hazelcast.map.impl.proxy.MapProxyImpl) EntryEvent(com.hazelcast.core.EntryEvent) Iterator(java.util.Iterator) ExecutionException(java.util.concurrent.ExecutionException) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) IMap(com.hazelcast.map.IMap)

Aggregations

IMap (com.hazelcast.map.IMap)294 Test (org.junit.Test)259 QuickTest (com.hazelcast.test.annotation.QuickTest)237 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)228 HazelcastInstance (com.hazelcast.core.HazelcastInstance)139 Config (com.hazelcast.config.Config)103 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)82 Map (java.util.Map)75 CountDownLatch (java.util.concurrent.CountDownLatch)65 MapStoreConfig (com.hazelcast.config.MapStoreConfig)54 Category (org.junit.experimental.categories.Category)51 Assert.assertEquals (org.junit.Assert.assertEquals)50 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)48 HashMap (java.util.HashMap)48 Collection (java.util.Collection)41 RunWith (org.junit.runner.RunWith)41 MapConfig (com.hazelcast.config.MapConfig)36 Set (java.util.Set)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 AssertTask (com.hazelcast.test.AssertTask)32