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));
}
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());
}
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;
}
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);
}
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;
}
Aggregations