Search in sources :

Example 1 with EntryObject

use of com.hazelcast.query.EntryObject in project hazelcast by hazelcast.

the class MapLiteMemberTest method testMapValuesQuery.

public static void testMapValuesQuery(final IMap<Integer, Object> map) {
    map.put(1, 2);
    EntryObject entryObject = new PredicateBuilder().getEntryObject();
    PredicateBuilder predicateBuilder = entryObject.key().equal(1);
    Collection values = map.values(predicateBuilder);
    assertEquals(1, values.size());
    assertEquals(2, values.iterator().next());
}
Also used : EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Collection(java.util.Collection)

Example 2 with EntryObject

use of com.hazelcast.query.EntryObject in project hazelcast by hazelcast.

the class NearCacheTest method testAfterExecuteOnEntriesNearCacheIsInvalidated.

@Test
public void testAfterExecuteOnEntriesNearCacheIsInvalidated() {
    int mapSize = 10;
    String mapName = randomMapName();
    Config config = createNearCachedMapConfig(mapName);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<Integer, Employee> map = instance.getMap(mapName);
    for (int i = 0; i < mapSize; i++) {
        map.put(i, new Employee(i, "", 0, true, 0D));
    }
    populateNearCache(map, mapSize);
    EntryObject e = new PredicateBuilder().getEntryObject();
    Predicate predicate = e.get("salary").equal(0);
    map.executeOnEntries(new AbstractEntryProcessor<Integer, Employee>() {

        @Override
        public Object process(Map.Entry<Integer, Employee> entry) {
            Employee employee = entry.getValue();
            double currentSalary = employee.getSalary();
            double newSalary = currentSalary + 10;
            employee.setSalary(newSalary);
            return newSalary;
        }
    }, predicate);
    assertEquals(0, getNearCacheStats(map).getOwnedEntryCount());
}
Also used : EntryObject(com.hazelcast.query.EntryObject) MapConfig(com.hazelcast.config.MapConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) Predicate(com.hazelcast.query.Predicate) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) PredicateBuilder(com.hazelcast.query.PredicateBuilder) EntryObject(com.hazelcast.query.EntryObject) HashMap(java.util.HashMap) Map(java.util.Map) IMap(com.hazelcast.core.IMap) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with EntryObject

use of com.hazelcast.query.EntryObject in project hazelcast by hazelcast.

the class MapTransactionRegressionTest method test_Issue1076.

@Test
public void test_Issue1076() {
    Config config = getConfig();
    final HazelcastInstance inst = createHazelcastInstance(config);
    IMap map = inst.getMap("default");
    EntryListener<String, Integer> l = new EntryAdapter<String, Integer>() {
    };
    EntryObject e = new PredicateBuilder().getEntryObject();
    Predicate<String, Integer> p = e.equal(1);
    map.addEntryListener(l, p, null, false);
    for (Integer i = 0; i < 100; i++) {
        TransactionContext context = inst.newTransactionContext();
        context.beginTransaction();
        TransactionalMap<String, Integer> txnMap = context.getMap("default");
        txnMap.remove(i.toString());
        context.commitTransaction();
    }
    assertEquals(0, map.size());
    inst.shutdown();
}
Also used : IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Config(com.hazelcast.config.Config) TransactionContext(com.hazelcast.transaction.TransactionContext) EntryAdapter(com.hazelcast.core.EntryAdapter) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 4 with EntryObject

use of com.hazelcast.query.EntryObject 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 5 with EntryObject

use of com.hazelcast.query.EntryObject 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)

Aggregations

EntryObject (com.hazelcast.query.EntryObject)18 PredicateBuilder (com.hazelcast.query.PredicateBuilder)18 Predicate (com.hazelcast.query.Predicate)13 Test (org.junit.Test)13 QuickTest (com.hazelcast.test.annotation.QuickTest)12 SqlPredicate (com.hazelcast.query.SqlPredicate)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 Employee (com.hazelcast.query.SampleObjects.Employee)6 Config (com.hazelcast.config.Config)5 ParallelTest (com.hazelcast.test.annotation.ParallelTest)5 MapConfig (com.hazelcast.config.MapConfig)3 IMap (com.hazelcast.core.IMap)3 MapIndexConfig (com.hazelcast.config.MapIndexConfig)2 MapStoreConfig (com.hazelcast.config.MapStoreConfig)2 IndexAwarePredicate (com.hazelcast.query.IndexAwarePredicate)2 Collection (java.util.Collection)2 Map (java.util.Map)2 EvictionConfig (com.hazelcast.config.EvictionConfig)1 MaxSizeConfig (com.hazelcast.config.MaxSizeConfig)1 NearCacheConfig (com.hazelcast.config.NearCacheConfig)1