use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testTxnReplace2.
@Test
public void testTxnReplace2() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map2 = h2.getMap("default");
map2.put("1", "value2");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
assertEquals("value2", txMap.replace("1", "value3"));
assertEquals("value3", txMap.get("1"));
assertNull(map2.get("2"));
return true;
}
});
assertTrue(b);
IMap map1 = h1.getMap("default");
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 testKeySet_whenPortableKeysetAndValuesWithPredicates.
@Test
public void testKeySet_whenPortableKeysetAndValuesWithPredicates() throws Exception {
final String mapName = randomString();
final Config config = getConfig();
config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {
public Portable create(int classId) {
return new SampleObjects.PortableEmployee();
}
});
final HazelcastInstance instance = createHazelcastInstance(config);
IMap map = instance.getMap(mapName);
final SampleObjects.PortableEmployee emp1 = new SampleObjects.PortableEmployee(34, "abc-123-xvz");
final SampleObjects.PortableEmployee emp2 = new SampleObjects.PortableEmployee(20, "abc-123-xvz");
map.put(emp1, emp1);
final TransactionContext context = instance.newTransactionContext();
context.beginTransaction();
final TransactionalMap txMap = context.getMap(mapName);
assertNull(txMap.put(emp2, emp2));
assertEquals(2, txMap.size());
assertEquals(2, txMap.keySet().size());
assertEquals(0, txMap.keySet(new SqlPredicate("a = 10")).size());
assertEquals(0, txMap.values(new SqlPredicate("a = 10")).size());
assertEquals(2, txMap.keySet(new SqlPredicate("a >= 10")).size());
assertEquals(2, txMap.values(new SqlPredicate("a >= 10")).size());
context.commitTransaction();
assertEquals(2, map.size());
assertEquals(2, map.values().size());
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testGet_LoadsKeyFromMapLoader_whenKeyExistsInDb.
@Test
public void testGet_LoadsKeyFromMapLoader_whenKeyExistsInDb() {
final String mapName = randomMapName();
final String valueFromDB = randomString();
final MapStoreAdapter mock = mock(MapStoreAdapter.class);
when(mock.load(anyObject())).thenReturn(valueFromDB);
Config config = getConfig();
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.get(1);
assertEquals(valueFromDB, value);
verify(mock, times(1)).load(anyObject());
return null;
}
});
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testReplace_whenNullKey.
// ========================= replace =====================
@Test(expected = NullPointerException.class)
public void testReplace_whenNullKey() throws TransactionException {
final HazelcastInstance hz = createHazelcastInstance();
hz.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.replace(null, "oldvalue", "newvalue");
return true;
}
});
}
use of com.hazelcast.core.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testSet_whenNullValue.
@Test(expected = NullPointerException.class)
public void testSet_whenNullValue() throws TransactionException {
final HazelcastInstance hz = createHazelcastInstance();
hz.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.set("key", null);
return true;
}
});
}
Aggregations