Search in sources :

Example 16 with PredicateBuilder

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

the class QueryBasicTest method testPredicateWithEntryKeyObject.

/**
     * Test for issue 711
     */
@Test(timeout = 1000 * 60)
public void testPredicateWithEntryKeyObject() {
    HazelcastInstance instance = createHazelcastInstance(getConfig());
    IMap<String, Integer> map = instance.getMap("testPredicateWithEntryKeyObject");
    map.put("1", 11);
    map.put("2", 22);
    map.put("3", 33);
    map.put("4", 44);
    map.put("5", 55);
    map.put("6", 66);
    Predicate predicate = new PredicateBuilder().getEntryObject().key().equal("1");
    assertEquals(1, map.values(predicate).size());
    predicate = new PredicateBuilder().getEntryObject().key().in("2", "3");
    assertEquals(2, map.keySet(predicate).size());
    predicate = new PredicateBuilder().getEntryObject().key().in("2", "3", "5", "6", "7");
    assertEquals(4, map.keySet(predicate).size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) PredicateBuilder(com.hazelcast.query.PredicateBuilder) Predicate(com.hazelcast.query.Predicate) SqlPredicate(com.hazelcast.query.SqlPredicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 17 with PredicateBuilder

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

the class QueryIndexTest method testOneIndexedFieldsWithTwoCriteriaField.

@Test(timeout = 1000 * 60)
public void testOneIndexedFieldsWithTwoCriteriaField() {
    HazelcastInstance h1 = createHazelcastInstance();
    IMap<String, Employee> map = h1.getMap("employees");
    map.addIndex("name", false);
    map.put("1", new Employee(1L, "joe", 30, true, 100D));
    EntryObject e = new PredicateBuilder().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.SampleObjects.Employee) 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 18 with PredicateBuilder

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

the class QueryIndexTest method testPredicateNotEqualWithIndex.

private void testPredicateNotEqualWithIndex(IMap<Integer, Value> map, boolean ordered) {
    map.addIndex("name", ordered);
    map.put(1, new Value("abc", 1));
    map.put(2, new Value("xyz", 2));
    map.put(3, new Value("aaa", 3));
    assertEquals(3, map.values(new SqlPredicate("name != 'aac'")).size());
    assertEquals(2, map.values(new SqlPredicate("index != 2")).size());
    assertEquals(3, map.values(new SqlPredicate("name <> 'aac'")).size());
    assertEquals(2, map.values(new SqlPredicate("index <> 2")).size());
    assertEquals(3, map.values(new PredicateBuilder().getEntryObject().get("name").notEqual("aac")).size());
    assertEquals(2, map.values(new PredicateBuilder().getEntryObject().get("index").notEqual(2)).size());
}
Also used : PredicateBuilder(com.hazelcast.query.PredicateBuilder) Value(com.hazelcast.query.SampleObjects.Value) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 19 with PredicateBuilder

use of com.hazelcast.query.PredicateBuilder 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 = new PredicateBuilder().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(new SqlPredicate(" (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.SampleObjects.Employee) EntryObject(com.hazelcast.query.EntryObject) PredicateBuilder(com.hazelcast.query.PredicateBuilder) SqlPredicate(com.hazelcast.query.SqlPredicate) Map(java.util.Map) IMap(com.hazelcast.core.IMap) Predicate(com.hazelcast.query.Predicate) SqlPredicate(com.hazelcast.query.SqlPredicate)

Example 20 with PredicateBuilder

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

the class QueryBasicTest method testPredicateStringAttribute.

private void testPredicateStringAttribute(IMap<Integer, Value> map) {
    map.put(1, new Value("abc"));
    map.put(2, new Value("xyz"));
    map.put(3, new Value("aaa"));
    map.put(4, new Value("zzz"));
    map.put(5, new Value("klm"));
    map.put(6, new Value("prs"));
    map.put(7, new Value("prs"));
    map.put(8, new Value("def"));
    map.put(9, new Value("qwx"));
    assertEquals(8, map.values(new SqlPredicate("name > 'aac'")).size());
    assertEquals(9, map.values(new SqlPredicate("name between 'aaa' and 'zzz'")).size());
    assertEquals(7, map.values(new SqlPredicate("name < 't'")).size());
    assertEquals(6, map.values(new SqlPredicate("name >= 'gh'")).size());
    assertEquals(8, map.values(new PredicateBuilder().getEntryObject().get("name").greaterThan("aac")).size());
    assertEquals(9, map.values(new PredicateBuilder().getEntryObject().get("name").between("aaa", "zzz")).size());
    assertEquals(7, map.values(new PredicateBuilder().getEntryObject().get("name").lessThan("t")).size());
    assertEquals(6, map.values(new PredicateBuilder().getEntryObject().get("name").greaterEqual("gh")).size());
}
Also used : PredicateBuilder(com.hazelcast.query.PredicateBuilder) Value(com.hazelcast.query.SampleObjects.Value) SqlPredicate(com.hazelcast.query.SqlPredicate)

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