use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class TransactionalMapReplaceMessageTask method innerCall.
@Override
protected Object innerCall() throws Exception {
final TransactionContext context = getEndpoint().getTransactionContext(parameters.txnId);
final TransactionalMap map = context.getMap(parameters.name);
Object oldValue = map.replace(parameters.key, parameters.value);
return serializationService.toData(oldValue);
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testSet.
// =================== set ===============================
@Test
public void testSet() 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");
txMap.set("1", "value");
txMap.set("1", "value2");
assertEquals("value2", txMap.get("1"));
assertNull(map1.get("1"));
assertNull(map1.get("2"));
assertEquals(1, txMap.size());
return true;
}
});
assertTrue(b);
assertEquals("value2", map1.get("1"));
assertEquals("value2", map2.get("1"));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testCommitOrder.
@Test
public void testCommitOrder() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(4);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final HazelcastInstance h3 = factory.newHazelcastInstance(config);
final HazelcastInstance h4 = factory.newHazelcastInstance(config);
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.put("1", "value1");
assertEquals("value1", txMap.put("1", "value2"));
assertEquals("value2", txMap.put("1", "value3"));
assertEquals("value3", txMap.put("1", "value4"));
assertEquals("value4", txMap.put("1", "value5"));
assertEquals("value5", txMap.put("1", "value6"));
assertEquals(1, txMap.size());
return true;
}
});
assertEquals("value6", h4.getMap("default").get("1"));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testReplace_whenSame.
@Test
public void testReplace_whenSame() 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");
map2.put("1", "1");
map2.put("2", "2");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
assertEquals(true, txMap.replace("1", "1", "11"));
assertEquals(false, txMap.replace("5", "5", "55"));
assertEquals(false, txMap.replace("2", "1", "22"));
assertEquals("1", map1.get("1"));
assertEquals("11", txMap.get("1"));
assertEquals("2", map1.get("2"));
assertEquals("2", txMap.get("2"));
return true;
}
});
assertTrue(b);
assertEquals("11", map1.get("1"));
assertEquals("11", map2.get("1"));
assertEquals("2", map1.get("2"));
assertEquals("2", map2.get("2"));
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testDelete.
@Test
public void testDelete() 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");
map2.put("1", "1");
map2.put("2", "2");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.put("3", "3");
map2.put("4", "4");
txMap.delete("1");
map2.delete("2");
assertEquals("1", map1.get("1"));
assertEquals(null, txMap.get("1"));
txMap.delete("2");
assertEquals(2, txMap.size());
return true;
}
});
assertTrue(b);
assertEquals(null, map1.get("1"));
assertEquals(null, map2.get("1"));
assertEquals(null, map1.get("2"));
assertEquals(null, map2.get("2"));
assertEquals("3", map1.get("3"));
assertEquals("3", map2.get("3"));
assertEquals("4", map1.get("4"));
assertEquals("4", map2.get("4"));
}
Aggregations