Search in sources :

Example 81 with SqlPredicate

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

the class ClientMapTest method testBasicPredicate.

@Test
public void testBasicPredicate() {
    IMap<String, String> map = createMap();
    fillMap(map);
    Collection collection = map.values(new SqlPredicate("this == value1"));
    assertEquals("value1", collection.iterator().next());
    Set<String> set = map.keySet(new SqlPredicate("this == value1"));
    assertEquals("key1", set.iterator().next());
    Set<Map.Entry<String, String>> set1 = map.entrySet(new SqlPredicate("this == value1"));
    assertEquals("key1", set1.iterator().next().getKey());
    assertEquals("value1", set1.iterator().next().getValue());
}
Also used : Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 82 with SqlPredicate

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

the class MapMBean method values.

@ManagedAnnotation(value = "values", operation = true)
public String values(String query) {
    Collection coll;
    if (query != null && !query.isEmpty()) {
        Predicate predicate = new SqlPredicate(query);
        coll = managedObject.values(predicate);
    } else {
        coll = managedObject.values();
    }
    StringBuilder buf = new StringBuilder();
    if (coll.size() == 0) {
        buf.append("Empty");
    } else {
        buf.append("[");
        for (Object obj : coll) {
            buf.append(obj);
            buf.append(", ");
        }
        buf.replace(buf.length() - 1, buf.length(), "]");
    }
    return buf.toString();
}
Also used : Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) Predicate(com.hazelcast.query.Predicate) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 83 with SqlPredicate

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

the class QueryAdvancedTest method testQueryWithTTL.

@Test
@SuppressWarnings("deprecation")
public void testQueryWithTTL() throws Exception {
    Config config = getConfig();
    String mapName = "default";
    config.getMapConfig(mapName).setTimeToLiveSeconds(5);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<String, Employee> map = instance.getMap(mapName);
    map.addIndex("name", false);
    map.addIndex("age", false);
    map.addIndex("active", true);
    int passiveEmployees = 5;
    int activeEmployees = 5;
    int allEmployees = passiveEmployees + activeEmployees;
    final CountDownLatch latch = new CountDownLatch(allEmployees);
    map.addEntryListener(new EntryAdapter() {

        @Override
        public void entryEvicted(EntryEvent event) {
            latch.countDown();
        }
    }, false);
    for (int i = 0; i < activeEmployees; i++) {
        Employee employee = new Employee("activeEmployee" + i, 60, true, i);
        map.put("activeEmployee" + i, employee);
    }
    for (int i = 0; i < passiveEmployees; i++) {
        Employee employee = new Employee("passiveEmployee" + i, 60, false, i);
        map.put("passiveEmployee" + i, employee);
    }
    // check the query result before eviction
    Collection values = map.values(new SqlPredicate("active"));
    assertEquals(activeEmployees, values.size());
    // wait until eviction is completed
    assertOpenEventually(latch);
    // check the query result after eviction
    values = map.values(new SqlPredicate("active"));
    assertEquals(0, values.size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) PortableEmployee(com.hazelcast.query.SampleObjects.PortableEmployee) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) EntryEvent(com.hazelcast.core.EntryEvent) Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 84 with SqlPredicate

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

the class QueryAdvancedTest method testTwoMembersWithIndexesAndShutdown2.

@Test
public void testTwoMembersWithIndexesAndShutdown2() {
    Config config = getConfig();
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    IMap<String, Employee> map = instance1.getMap("employees");
    map.addIndex("name", false);
    map.addIndex("age", true);
    map.addIndex("active", false);
    QueryBasicTest.doFunctionalQueryTest(map);
    assertEquals(101, map.size());
    instance1.getLifecycleService().shutdown();
    map = instance2.getMap("employees");
    assertEquals(101, map.size());
    Set<Map.Entry<String, Employee>> entries = map.entrySet(new SqlPredicate("active and age=23"));
    assertEquals(2, entries.size());
    for (Map.Entry<String, Employee> entry : entries) {
        Employee employee = entry.getValue();
        assertEquals(employee.getAge(), 23);
        assertTrue(employee.isActive());
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Employee(com.hazelcast.query.SampleObjects.Employee) PortableEmployee(com.hazelcast.query.SampleObjects.PortableEmployee) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) SqlPredicate(com.hazelcast.query.SqlPredicate) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) 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 85 with SqlPredicate

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

the class QueryAdvancedTest method testUnknownPortableField_notCausesQueryException_withIndex.

@Test
public // see: https://github.com/hazelcast/hazelcast/issues/3927
void testUnknownPortableField_notCausesQueryException_withIndex() {
    String mapName = "default";
    Config config = getConfig();
    config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {

        public Portable create(int classId) {
            return new PortableEmployee();
        }
    });
    config.getMapConfig(mapName).addMapIndexConfig(new MapIndexConfig("notExist", false)).addMapIndexConfig(new MapIndexConfig("n", false));
    HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
    IMap<Integer, PortableEmployee> map = hazelcastInstance.getMap(mapName);
    for (int i = 0; i < 5; i++) {
        map.put(i, new PortableEmployee(i, "name_" + i));
    }
    Collection values = map.values(new SqlPredicate("n = name_2 OR notExist = name_0"));
    assertEquals(1, values.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Portable(com.hazelcast.nio.serialization.Portable) MapIndexConfig(com.hazelcast.config.MapIndexConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) PortableEmployee(com.hazelcast.query.SampleObjects.PortableEmployee) Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

SqlPredicate (com.hazelcast.query.SqlPredicate)111 Test (org.junit.Test)93 QuickTest (com.hazelcast.test.annotation.QuickTest)87 ParallelTest (com.hazelcast.test.annotation.ParallelTest)83 HazelcastInstance (com.hazelcast.core.HazelcastInstance)73 Config (com.hazelcast.config.Config)36 Employee (com.hazelcast.mapreduce.helpers.Employee)28 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)24 MapIndexConfig (com.hazelcast.config.MapIndexConfig)22 IMap (com.hazelcast.core.IMap)21 Employee (com.hazelcast.query.SampleObjects.Employee)21 MapStoreConfig (com.hazelcast.config.MapStoreConfig)18 Collection (java.util.Collection)14 MapConfig (com.hazelcast.config.MapConfig)13 Predicate (com.hazelcast.query.Predicate)12 Map (java.util.Map)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 PortableEmployee (com.hazelcast.query.SampleObjects.PortableEmployee)10 AssertTask (com.hazelcast.test.AssertTask)10 Value (com.hazelcast.query.SampleObjects.Value)9