use of com.hazelcast.query.PredicateBuilder in project hazelcast by hazelcast.
the class EntryProcessorBouncingNodesTest method testEntryProcessorWhileTwoNodesAreBouncing.
private void testEntryProcessorWhileTwoNodesAreBouncing(boolean withPredicate, boolean withIndex) {
CountDownLatch startLatch = new CountDownLatch(1);
AtomicBoolean isRunning = new AtomicBoolean(true);
// start up three instances
HazelcastInstance instance = newInstance(withIndex);
HazelcastInstance instance2 = newInstance(withIndex);
HazelcastInstance instance3 = newInstance(withIndex);
assertClusterSizeEventually(3, instance);
final IMap<Integer, ListHolder> map = instance.getMap(MAP_NAME);
final ListHolder expected = new ListHolder();
// initialize the list synchronously to ensure the map is correctly initialized
InitMapProcessor initProcessor = new InitMapProcessor();
for (int i = 0; i < ENTRIES; ++i) {
map.executeOnKey(i, initProcessor);
}
assertEquals(ENTRIES, map.size());
// spin up the thread that stops/starts the instance2 and instance3, always keeping one instance running
Runnable runnable = new TwoNodesRestartingRunnable(startLatch, isRunning, withPredicate, instance2, instance3);
Thread bounceThread = new Thread(runnable);
bounceThread.start();
// now, with nodes joining and leaving the cluster concurrently, start adding numbers to the lists
int iteration = 0;
while (iteration < ITERATIONS) {
if (iteration == 30) {
// let the bounce threads start bouncing
startLatch.countDown();
}
IncrementProcessor processor = new IncrementProcessor(iteration);
expected.add(iteration);
for (int i = 0; i < ENTRIES; ++i) {
if (withPredicate) {
EntryObject eo = new PredicateBuilder().getEntryObject();
Predicate keyPredicate = eo.key().equal(i);
map.executeOnEntries(processor, keyPredicate);
} else {
map.executeOnKey(i, processor);
}
}
// give processing time to catch up
++iteration;
}
// signal the bounce threads that we're done
isRunning.set(false);
// wait for the instance bounces to complete
assertJoinable(bounceThread);
final CountDownLatch latch = new CountDownLatch(ENTRIES);
for (int i = 0; i < ENTRIES; ++i) {
final int id = i;
new Thread(new Runnable() {
@Override
public void run() {
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertTrue(expected.size() <= map.get(id).size());
}
});
latch.countDown();
}
}).start();
}
assertOpenEventually(latch);
for (int index = 0; index < ENTRIES; ++index) {
ListHolder holder = map.get(index);
assertEquals("The ListHolder should contain ITERATIONS entries", ITERATIONS, holder.size());
for (int i = 0; i < ITERATIONS; i++) {
assertEquals(i, holder.get(i));
}
}
}
use of com.hazelcast.query.PredicateBuilder in project hazelcast by hazelcast.
the class EntryProcessorTest method testMapEntryProcessorWithPredicate.
@Test
public void testMapEntryProcessorWithPredicate() {
TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
Config cfg = getConfig();
HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(cfg);
HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(cfg);
try {
IMap<Integer, Employee> map = instance1.getMap(MAP_NAME);
int size = 10;
for (int i = 0; i < size; i++) {
map.put(i, new Employee(i, "", 0, false, 0D, SampleObjects.State.STATE1));
}
EntryProcessor entryProcessor = new ChangeStateEntryProcessor();
EntryObject entryObject = new PredicateBuilder().getEntryObject();
Predicate predicate = entryObject.get("id").lessThan(5);
Map<Integer, Object> res = map.executeOnEntries(entryProcessor, predicate);
for (int i = 0; i < 5; i++) {
assertEquals(SampleObjects.State.STATE2, map.get(i).getState());
}
for (int i = 5; i < size; i++) {
assertEquals(SampleObjects.State.STATE1, map.get(i).getState());
}
for (int i = 0; i < 5; i++) {
assertEquals(((Employee) res.get(i)).getState(), SampleObjects.State.STATE2);
}
} finally {
instance1.shutdown();
instance2.shutdown();
}
}
use of com.hazelcast.query.PredicateBuilder in project hazelcast by hazelcast.
the class QueryIndexTest method testInnerIndex.
@Test(timeout = 1000 * 60)
public void testInnerIndex() {
HazelcastInstance instance = createHazelcastInstance();
IMap<String, SampleObjects.Value> map = instance.getMap("default");
map.addIndex("name", false);
map.addIndex("type.typeName", false);
for (int i = 0; i < 10; i++) {
Value v = new Value("name" + i, i < 5 ? null : new ValueType("type" + i), i);
map.put("" + i, v);
}
Predicate predicate = new PredicateBuilder().getEntryObject().get("type.typeName").in("type8", "type6");
Collection<SampleObjects.Value> values = map.values(predicate);
assertEquals(2, values.size());
List<String> typeNames = new ArrayList<String>();
for (Value configObject : values) {
typeNames.add(configObject.getType().getTypeName());
}
String[] array = typeNames.toArray(new String[typeNames.size()]);
Arrays.sort(array);
assertArrayEquals(typeNames.toString(), new String[] { "type6", "type8" }, array);
}
use of com.hazelcast.query.PredicateBuilder in project hazelcast by hazelcast.
the class QueryIndexTest method testResultsReturned_whenCustomAttributeIndexed.
@Test
public void testResultsReturned_whenCustomAttributeIndexed() {
HazelcastInstance h1 = createHazelcastInstance();
IMap<String, CustomObject> imap = h1.getMap("objects");
imap.addIndex("attribute", true);
for (int i = 0; i < 10; i++) {
CustomAttribute attr = new CustomAttribute(i, 200);
CustomObject object = new CustomObject("o" + i, randomUUID(), attr);
imap.put(object.getName(), object);
}
EntryObject entry = new PredicateBuilder().getEntryObject();
Predicate predicate = entry.get("attribute").greaterEqual(new CustomAttribute(5, 200));
Collection<CustomObject> values = imap.values(predicate);
assertEquals(5, values.size());
}
use of com.hazelcast.query.PredicateBuilder in project hazelcast by hazelcast.
the class QueryIndexingTest method setUp.
@Before
public void setUp() {
employees = newEmployees(count);
nodeFactory = createHazelcastInstanceFactory(2);
Config config = newConfig();
h1 = nodeFactory.newHazelcastInstance(config);
h2 = nodeFactory.newHazelcastInstance(config);
EntryObject entryObject = new PredicateBuilder().getEntryObject();
predicate = entryObject.get("name").equal(null).and(entryObject.get("city").isNull());
assertClusterSizeEventually(2, h1);
}
Aggregations