Search in sources :

Example 6 with SqlPredicate

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

the class CompositeIndexQueriesTest method check.

private void check(String sql, int expectedSize, int... queryCounts) {
    if (sql != null) {
        SqlPredicate sqlPredicate = (SqlPredicate) Predicates.sql(sql);
        Set<Map.Entry<Integer, Person>> result = map.entrySet(sqlPredicate);
        assertEquals(expectedSize, result.size());
        // here we are checking the original unoptimized predicate
        for (Map.Entry<Integer, Person> entry : result) {
            assertTrue(sqlPredicate.apply(new ExtractableAdapter(entry)));
        }
        // The goal here is to check fallback predicates generated by
        // CompositeEqualPredicate and CompositeRangePredicate.
        Set<Map.Entry<Integer, Person>> noIndexResult = map.entrySet(new NoIndexPredicate(sqlPredicate.getPredicate()));
        assertEquals(result, noIndexResult);
    }
    assert indexes.size() == queryCounts.length;
    for (int i = 0; i < queryCounts.length; ++i) {
        assertEquals(queryCounts[i], map.getLocalMapStats().getIndexStats().get(indexes.get(i)).getQueryCount());
    }
}
Also used : QueryableEntry(com.hazelcast.query.impl.QueryableEntry) SqlPredicate(com.hazelcast.query.impl.predicates.SqlPredicate) Map(java.util.Map) IMap(com.hazelcast.map.IMap)

Example 7 with SqlPredicate

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

the class IndexJsonTest method testJsonIndex.

@Test
public void testJsonIndex() {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    Indexes indexes = Indexes.newBuilder(ss, copyBehavior, DEFAULT_IN_MEMORY_FORMAT).extractors(Extractors.newBuilder(ss).build()).indexProvider(new DefaultIndexProvider()).usesCachedQueryableEntries(true).statsEnabled(true).global(true).build();
    Index numberIndex = indexes.addOrGetIndex(IndexUtils.createTestIndexConfig(IndexType.HASH, "age"));
    Index boolIndex = indexes.addOrGetIndex(IndexUtils.createTestIndexConfig(IndexType.HASH, "active"));
    Index stringIndex = indexes.addOrGetIndex(IndexUtils.createTestIndexConfig(IndexType.HASH, "name"));
    for (int i = 0; i < 1001; i++) {
        Data key = ss.toData(i);
        String jsonString = "{\"age\" : " + i + "  , \"name\" : \"sancar\" , \"active\" :  " + (i % 2 == 0) + " } ";
        indexes.putEntry(new QueryEntry(ss, key, new HazelcastJsonValue(jsonString), Extractors.newBuilder(ss).build()), null, Index.OperationSource.USER);
    }
    assertEquals(1, numberIndex.getRecords(10).size());
    assertEquals(0, numberIndex.getRecords(-1).size());
    assertEquals(1001, stringIndex.getRecords("sancar").size());
    assertEquals(501, boolIndex.getRecords(true).size());
    assertEquals(501, size(indexes.query(new AndPredicate(new EqualPredicate("name", "sancar"), new EqualPredicate("active", "true")), SKIP_PARTITIONS_COUNT_CHECK)));
    assertEquals(300, size(indexes.query(Predicates.and(Predicates.greaterThan("age", 400), Predicates.equal("active", true)), SKIP_PARTITIONS_COUNT_CHECK)));
    assertEquals(1001, size(indexes.query(new SqlPredicate("name == sancar"), SKIP_PARTITIONS_COUNT_CHECK)));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) AndPredicate(com.hazelcast.query.impl.predicates.AndPredicate) EqualPredicate(com.hazelcast.query.impl.predicates.EqualPredicate) SqlPredicate(com.hazelcast.query.impl.predicates.SqlPredicate) Data(com.hazelcast.internal.serialization.Data) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SqlPredicate (com.hazelcast.query.impl.predicates.SqlPredicate)7 Test (org.junit.Test)6 QuickTest (com.hazelcast.test.annotation.QuickTest)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 Config (com.hazelcast.config.Config)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 IndexMatchHint (com.hazelcast.query.impl.QueryContext.IndexMatchHint)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)1 Data (com.hazelcast.internal.serialization.Data)1 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)1 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)1 IMap (com.hazelcast.map.IMap)1 Employee (com.hazelcast.query.SampleTestObjects.Employee)1 Value (com.hazelcast.query.SampleTestObjects.Value)1 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)1 AndPredicate (com.hazelcast.query.impl.predicates.AndPredicate)1 EqualPredicate (com.hazelcast.query.impl.predicates.EqualPredicate)1 Collection (java.util.Collection)1 Map (java.util.Map)1