use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class ClientTxnMapTest method testTnxMapReplace.
@Test
public void testTnxMapReplace() throws Exception {
final String mapName = randomString();
IMap map = client.getMap(mapName);
final String key1 = "key1";
final String key2 = "key2";
final String replaceValue = "replaceValue";
map.put(key1, "OLD_VALUE");
final TransactionContext context = client.newTransactionContext();
context.beginTransaction();
final TransactionalMap txMap = context.getMap(mapName);
txMap.replace(key1, replaceValue);
txMap.replace(key2, "NOT_POSSIBLE");
context.commitTransaction();
assertEquals(replaceValue, map.get(key1));
assertNull(map.get(key2));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testValues_withPagingPredicate.
@Test(expected = IllegalArgumentException.class)
public void testValues_withPagingPredicate() throws TransactionException {
final int nodeCount = 1;
final String mapName = randomMapName("testValuesWithPagingPredicate");
final Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
final HazelcastInstance node = factory.newHazelcastInstance(config);
final IMap<Integer, Employee> map = node.getMap(mapName);
final Employee emp = new Employee("name", 77, true, 10D);
map.put(1, emp);
node.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Integer, Employee> txMap = context.getMap(mapName);
PagingPredicate<Integer, Employee> predicate = new PagingPredicate<Integer, Employee>(5);
txMap.values(predicate);
return true;
}
});
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testReplace.
@Test
public void testReplace() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map1 = h1.getMap("default");
final IMap map2 = h2.getMap("default");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
assertNull(txMap.replace("1", "value"));
txMap.put("1", "value2");
assertEquals("value2", txMap.replace("1", "value3"));
assertEquals("value3", txMap.get("1"));
assertNull(map1.get("1"));
assertNull(map2.get("2"));
return true;
}
});
assertTrue(b);
assertEquals("value3", map1.get("1"));
assertEquals("value3", map2.get("1"));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb.
@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyDoesNotExistsInDb() {
final String mapName = randomMapName();
final MapStoreAdapter mock = mock(MapStoreAdapter.class);
when(mock.load(anyObject())).thenReturn(null);
Config config = new Config();
MapStoreConfig storeConfig = new MapStoreConfig();
storeConfig.setEnabled(true).setImplementation(mock);
config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
HazelcastInstance instance = createHazelcastInstance(config);
instance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> map = context.getMap(mapName);
Object value = map.getForUpdate(1);
assertNull("value should be null", value);
verify(mock, times(1)).load(anyObject());
return null;
}
});
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testValues_shouldNotDeduplicateEntriesWhenGettingByPredicate.
@Test
public void testValues_shouldNotDeduplicateEntriesWhenGettingByPredicate() throws TransactionException {
final int nodeCount = 1;
final String mapName = randomMapName();
final Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
final HazelcastInstance node = factory.newHazelcastInstance(config);
final IMap map = node.getMap(mapName);
final Employee emp = new Employee("name", 77, true, 10D);
map.put(1, emp);
node.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Integer, Employee> txMap = context.getMap(mapName);
txMap.put(2, emp);
Collection<Employee> coll = txMap.values(new SqlPredicate("age = 77"));
assertEquals(2, coll.size());
return true;
}
});
node.shutdown();
}
Aggregations