use of com.hazelcast.transaction.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 SampleTestObjects.PortableEmployee();
}
});
final HazelcastInstance instance = createHazelcastInstance(config);
IMap map = instance.getMap(mapName);
final SampleTestObjects.PortableEmployee emp1 = new SampleTestObjects.PortableEmployee(34, "abc-123-xvz");
final SampleTestObjects.PortableEmployee emp2 = new SampleTestObjects.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(Predicates.sql("a = 10")).size());
assertEquals(0, txMap.values(Predicates.sql("a = 10")).size());
assertEquals(2, txMap.keySet(Predicates.sql("a >= 10")).size());
assertEquals(2, txMap.values(Predicates.sql("a >= 10")).size());
context.commitTransaction();
assertEquals(2, map.size());
assertEquals(2, map.values().size());
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method tesPutIfAbsent_whenNullValue.
@Test(expected = NullPointerException.class)
public void tesPutIfAbsent_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.putIfAbsent("key", null);
return true;
}
});
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testValues_WithPredicates_notContains_oldValues.
@Test
public void testValues_WithPredicates_notContains_oldValues() throws TransactionException {
Config config = getConfig();
final String mapName = "testValuesWithPredicate_notContains_oldValues";
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap<Integer, Employee> map = h1.getMap(mapName);
final Employee employeeAtAge22 = new Employee("emin", 22, true, 10D);
final Employee employeeAtAge23 = new Employee("emin", 23, true, 10D);
map.put(1, employeeAtAge22);
h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> txMap = context.getMap(mapName);
assertEquals(1, txMap.values(Predicates.sql("age > 21")).size());
txMap.put(1, employeeAtAge23);
Collection coll = txMap.values(Predicates.sql("age > 21"));
assertEquals(1, coll.size());
return true;
}
});
h1.shutdown();
h2.shutdown();
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_whenNullKey.
// =================== getForUpdate ===============================
@Test(expected = NullPointerException.class)
public void testGetForUpdate_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.getForUpdate(null);
return true;
}
});
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionLockingTest method testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlockedDuringTransaction.
@Test
public void testTxnPutIfAbsent_whenPutFails_keyShouldRemainUnlockedDuringTransaction() throws InterruptedException {
final HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
final IMap<String, Object> map = hazelcastInstance.getMap(mapName);
map.put(key, value);
hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
transactionalMap.putIfAbsent(key, "t");
assertFalse("Key remains locked!", map.isLocked(key));
return null;
}
});
}
Aggregations