Search in sources :

Example 16 with EntryObject

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

the class EvictionTest method testMaxIdle_readThroughIndex.

private void testMaxIdle_readThroughIndex(IndexType type) {
    String mapName = randomMapName();
    Config config = getConfig();
    config.getMapConfig("default").setPerEntryStatsEnabled(true);
    // "disable" the cleaner task
    config.setProperty(PROP_TASK_PERIOD_SECONDS, Integer.toString(MAX_VALUE));
    HazelcastInstance node = createHazelcastInstance(config);
    IMap<Integer, Employee> map = node.getMap(mapName);
    map.addIndex(type, "city");
    int entryCount = 5;
    Map<Integer, Long> lastAccessTimes = new HashMap<>();
    for (int i = 0; i < entryCount; ++i) {
        String cityName = i % 2 == 0 ? "cityname" : null;
        Employee emp = new Employee(i, "name" + i, cityName, 0, true, i);
        map.put(i, emp, 0L, SECONDS, 60L, SECONDS);
        // we do get to set the last access time
        map.get(i);
        EntryView view = map.getEntryView(i);
        long lastAccessTime = view.getLastAccessTime();
        assertTrue(lastAccessTime > 0);
        lastAccessTimes.put(i, lastAccessTime);
    }
    sleepAtLeastSeconds(1);
    EntryObject entryObject = new PredicateBuilderImpl().getEntryObject();
    Predicate predicateCityNull = entryObject.get("city").isNull();
    Collection<Employee> valuesNullCity = map.values(predicateCityNull);
    Collection<Employee> valuesNotNullCity = map.values(Predicates.equal("city", "cityname"));
    assertEquals(entryCount, valuesNullCity.size() + valuesNotNullCity.size());
    // check that evaluating the predicate didn't update the last access time of the returned records
    for (int i = 0; i < entryCount; ++i) {
        EntryView view = map.getEntryView(i);
        assertNotNull(view);
        long lastAccessTime = view.getLastAccessTime();
        long prevLastAccessTime = lastAccessTimes.get(i);
        assertTrue("lastAccessTime=" + lastAccessTime + ", prevLastAccessTime=" + prevLastAccessTime, lastAccessTime == prevLastAccessTime);
    }
}
Also used : EntryObject(com.hazelcast.query.PredicateBuilder.EntryObject) HashMap(java.util.HashMap) MapConfig(com.hazelcast.config.MapConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) Config(com.hazelcast.config.Config) Predicate(com.hazelcast.query.Predicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleTestObjects.Employee) PredicateBuilderImpl(com.hazelcast.query.impl.PredicateBuilderImpl) EntryView(com.hazelcast.core.EntryView)

Example 17 with EntryObject

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

the class MapLiteMemberTest method testMapKeysQuery.

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

Example 18 with EntryObject

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

the class QueryBasicTest method doFunctionalQueryTest.

public static void doFunctionalQueryTest(IMap<String, Employee> map) {
    map.put("1", new Employee("joe", 33, false, 14.56));
    map.put("2", new Employee("ali", 23, true, 15.00));
    for (int i = 3; i < 103; i++) {
        map.put(String.valueOf(i), new Employee("name" + i, i % 60, ((i & 1) == 1), i));
    }
    Set<Map.Entry<String, Employee>> entries = map.entrySet();
    assertEquals(102, entries.size());
    int entryCount = 0;
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertNotNull(employee);
        entryCount++;
    }
    assertEquals(102, entryCount);
    EntryObject entryObject = Predicates.newPredicateBuilder().getEntryObject();
    Predicate predicate = entryObject.is("active").and(entryObject.get("age").equal(23));
    entries = map.entrySet(predicate);
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertEquals(employee.getAge(), 23);
        assertTrue(employee.isActive());
    }
    map.remove("2");
    entries = map.entrySet(predicate);
    assertEquals(2, entries.size());
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertEquals(employee.getAge(), 23);
        assertTrue(employee.isActive());
    }
    entries = map.entrySet(Predicates.sql(" (age >= " + 30 + ") AND (age <= " + 40 + ")"));
    assertEquals(23, entries.size());
    for (Map.Entry entry : entries) {
        Employee employee = (Employee) entry.getValue();
        assertTrue(employee.getAge() >= 30);
        assertTrue(employee.getAge() <= 40);
    }
}
Also used : Employee(com.hazelcast.query.SampleTestObjects.Employee) EntryObject(com.hazelcast.query.PredicateBuilder.EntryObject) Map(java.util.Map) IMap(com.hazelcast.map.IMap) Predicate(com.hazelcast.query.Predicate)

Example 19 with EntryObject

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

the class QueryIndexTest method testOneIndexedFieldsWithTwoCriteriaField.

@Test(timeout = 1000 * 60)
public void testOneIndexedFieldsWithTwoCriteriaField() {
    HazelcastInstance h1 = createTestHazelcastInstance();
    IMap<String, Employee> map = h1.getMap("employees");
    map.addIndex(IndexType.HASH, "name");
    map.put("1", new Employee(1L, "joe", 30, true, 100D));
    EntryObject e = Predicates.newPredicateBuilder().getEntryObject();
    PredicateBuilder a = e.get("name").equal("joe");
    Predicate b = e.get("age").equal("30");
    Collection<Employee> actual = map.values(a.and(b));
    assertEquals(1, actual.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleTestObjects.Employee) EntryObject(com.hazelcast.query.PredicateBuilder.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Predicate(com.hazelcast.query.Predicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 20 with EntryObject

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

the class IndexIntegrationTest method putRemove_withIndex_whereAttributeIsNull.

@Test
public void putRemove_withIndex_whereAttributeIsNull() {
    // GIVEN
    IndexConfig indexConfig = new IndexConfig();
    indexConfig.addAttribute("amount");
    indexConfig.setType(IndexType.HASH);
    MapConfig mapConfig = new MapConfig().setName("map");
    mapConfig.addIndexConfig(indexConfig);
    Config config = new Config();
    config.addMapConfig(mapConfig);
    Trade trade = new Trade();
    trade.setCurrency("EUR");
    trade.setAmount(null);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<Integer, Trade> map = instance.getMap("map");
    // WHEN
    map.put(1, trade);
    map.remove(1);
    EntryObject e = Predicates.newPredicateBuilder().getEntryObject();
    Predicate predicate = e.get("amount").isNull();
    Collection<Trade> values = map.values(predicate);
    // THEN
    assertEquals(0, values.size());
    assertNull(map.get(1));
}
Also used : IndexConfig(com.hazelcast.config.IndexConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) EntryObject(com.hazelcast.query.PredicateBuilder.EntryObject) MapConfig(com.hazelcast.config.MapConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) Config(com.hazelcast.config.Config) IndexConfig(com.hazelcast.config.IndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapConfig(com.hazelcast.config.MapConfig) Predicate(com.hazelcast.query.Predicate) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

EntryObject (com.hazelcast.query.PredicateBuilder.EntryObject)24 Test (org.junit.Test)18 QuickTest (com.hazelcast.test.annotation.QuickTest)16 Predicate (com.hazelcast.query.Predicate)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)12 Employee (com.hazelcast.query.SampleTestObjects.Employee)7 Config (com.hazelcast.config.Config)6 PredicateBuilder (com.hazelcast.query.PredicateBuilder)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)5 MapConfig (com.hazelcast.config.MapConfig)4 EvictionConfig (com.hazelcast.config.EvictionConfig)2 IndexConfig (com.hazelcast.config.IndexConfig)2 MapStoreConfig (com.hazelcast.config.MapStoreConfig)2 NearCacheConfig (com.hazelcast.config.NearCacheConfig)2 IMap (com.hazelcast.map.IMap)2 Collection (java.util.Collection)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 EntryListenerConfig (com.hazelcast.config.EntryListenerConfig)1 EntryAdapter (com.hazelcast.core.EntryAdapter)1 EntryView (com.hazelcast.core.EntryView)1