Search in sources :

Example 6 with PredicateBuilder

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));
        }
    }
}
Also used : EntryObject(com.hazelcast.query.EntryObject) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Predicate(com.hazelcast.query.Predicate) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) PredicateBuilder(com.hazelcast.query.PredicateBuilder) AssertTask(com.hazelcast.test.AssertTask)

Example 7 with PredicateBuilder

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();
    }
}
Also used : EntryObject(com.hazelcast.query.EntryObject) MapConfig(com.hazelcast.config.MapConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) Predicate(com.hazelcast.query.Predicate) IndexAwarePredicate(com.hazelcast.query.IndexAwarePredicate) SqlPredicate(com.hazelcast.query.SqlPredicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) PredicateBuilder(com.hazelcast.query.PredicateBuilder) EntryObject(com.hazelcast.query.EntryObject) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with PredicateBuilder

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ValueType(com.hazelcast.query.SampleObjects.ValueType) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Value(com.hazelcast.query.SampleObjects.Value) ArrayList(java.util.ArrayList) SqlPredicate(com.hazelcast.query.SqlPredicate) Predicate(com.hazelcast.query.Predicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with PredicateBuilder

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) SqlPredicate(com.hazelcast.query.SqlPredicate) Predicate(com.hazelcast.query.Predicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with PredicateBuilder

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);
}
Also used : EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Config(com.hazelcast.config.Config) Before(org.junit.Before)

Aggregations

PredicateBuilder (com.hazelcast.query.PredicateBuilder)29 Test (org.junit.Test)20 Predicate (com.hazelcast.query.Predicate)19 SqlPredicate (com.hazelcast.query.SqlPredicate)19 QuickTest (com.hazelcast.test.annotation.QuickTest)19 EntryObject (com.hazelcast.query.EntryObject)18 HazelcastInstance (com.hazelcast.core.HazelcastInstance)14 ParallelTest (com.hazelcast.test.annotation.ParallelTest)12 Config (com.hazelcast.config.Config)6 Employee (com.hazelcast.query.SampleObjects.Employee)6 Value (com.hazelcast.query.SampleObjects.Value)6 MapConfig (com.hazelcast.config.MapConfig)4 MapIndexConfig (com.hazelcast.config.MapIndexConfig)3 IMap (com.hazelcast.core.IMap)3 ValueType (com.hazelcast.query.SampleObjects.ValueType)3 MapStoreConfig (com.hazelcast.config.MapStoreConfig)2 IndexAwarePredicate (com.hazelcast.query.IndexAwarePredicate)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2