use of com.hazelcast.map.IMap 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"));
assertNull(txMap.get("1"));
txMap.delete("2");
assertEquals(2, txMap.size());
return true;
}
});
assertTrue(b);
assertNull(map1.get("1"));
assertNull(map2.get("1"));
assertNull(map1.get("2"));
assertNull(map2.get("2"));
assertEquals("3", map1.get("3"));
assertEquals("3", map2.get("3"));
assertEquals("4", map1.get("4"));
assertEquals("4", map2.get("4"));
}
use of com.hazelcast.map.IMap 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.map.IMap 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.map.IMap in project hazelcast by hazelcast.
the class SourcesTest method remoteMapWithUnknownValueClass_whenQueryingIsNotNecessary.
@Test
public void remoteMapWithUnknownValueClass_whenQueryingIsNotNecessary() throws Exception {
// Given
URL jarResource = Thread.currentThread().getContextClassLoader().getResource("deployment/sample-pojo-1.0-car.jar");
assertNotNull("jar not found", jarResource);
ClassLoader cl = new URLClassLoader(new URL[] { jarResource });
Class<?> personClz = cl.loadClass("com.sample.pojo.car.Car");
Object person = personClz.getConstructor(String.class, String.class).newInstance("make", "model");
IMap<String, Object> map = remoteHz.getMap(srcName);
// the class of the value is unknown to the remote IMDG member, it will be only known to Jet
map.put("key", person);
// When
BatchSource<Entry<String, Object>> source = Sources.remoteMap(srcName, clientConfig);
// Then
p.readFrom(source).map(en -> en.getValue().toString()).writeTo(sink);
JobConfig jobConfig = new JobConfig();
jobConfig.addJar(jarResource);
hz().getJet().newJob(p, jobConfig).join();
List<Object> expected = singletonList(person.toString());
List<Object> actual = new ArrayList<>(sinkList);
assertEquals(expected, actual);
}
use of com.hazelcast.map.IMap in project hazelcast by hazelcast.
the class MapPredicateJsonTest method testJsonValueIsJustABoolean.
@Test
public void testJsonValueIsJustABoolean() {
IMap<Integer, HazelcastJsonValue> map = instance.getMap(randomMapName());
for (int i = 0; i < 10; i++) {
map.put(i, new HazelcastJsonValue(Json.value(i < 7).toString()));
}
Collection<Map.Entry<Integer, HazelcastJsonValue>> vals = map.entrySet(Predicates.equal("this", true));
assertEquals(7, vals.size());
for (Map.Entry<Integer, HazelcastJsonValue> entry : vals) {
assertTrue(entry.getKey() < 7);
assertEquals("true", entry.getValue().toString());
}
}
Aggregations