Search in sources :

Example 6 with QueryableEntry

use of com.hazelcast.query.impl.QueryableEntry in project hazelcast by hazelcast.

the class TransactionalMapProxy method values.

@Override
@SuppressWarnings("unchecked")
public Collection values(Predicate predicate) {
    checkTransactionState();
    checkNotNull(predicate, "Predicate can not be null!");
    checkNotInstanceOf(PagingPredicate.class, predicate, "Paging is not supported for Transactional queries");
    MapQueryEngine queryEngine = mapServiceContext.getMapQueryEngine(name);
    SerializationService serializationService = getNodeEngine().getSerializationService();
    Query query = Query.of().mapName(name).predicate(predicate).iterationType(IterationType.ENTRY).build();
    QueryResult queryResylt = queryEngine.execute(query, Target.ALL_NODES);
    Set result = QueryResultUtils.transformToSet(serializationService, queryResylt, predicate, IterationType.ENTRY, true);
    // TODO: Can't we just use the original set?
    List<Object> valueSet = new ArrayList<Object>();
    Set<Object> keyWontBeIncluded = new HashSet<Object>();
    Extractors extractors = mapServiceContext.getExtractors(name);
    // iterate over the txMap and see if the values are updated or removed
    for (Map.Entry<Data, TxnValueWrapper> entry : txMap.entrySet()) {
        boolean isRemoved = Type.REMOVED.equals(entry.getValue().type);
        boolean isUpdated = Type.UPDATED.equals(entry.getValue().type);
        Object keyObject = serializationService.toObject(entry.getKey());
        if (isRemoved) {
            keyWontBeIncluded.add(keyObject);
        } else {
            if (isUpdated) {
                keyWontBeIncluded.add(keyObject);
            }
            Object entryValue = entry.getValue().value;
            QueryableEntry queryEntry = new CachedQueryEntry((InternalSerializationService) serializationService, entry.getKey(), entryValue, extractors);
            if (predicate.apply(queryEntry)) {
                valueSet.add(queryEntry.getValue());
            }
        }
    }
    removeFromResultSet(result, valueSet, keyWontBeIncluded);
    return valueSet;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Query(com.hazelcast.map.impl.query.Query) ArrayList(java.util.ArrayList) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) QueryResult(com.hazelcast.map.impl.query.QueryResult) Extractors(com.hazelcast.query.impl.getters.Extractors) MapQueryEngine(com.hazelcast.map.impl.query.MapQueryEngine) CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) HashMap(java.util.HashMap) Map(java.util.Map) TransactionalMap(com.hazelcast.core.TransactionalMap) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) HashSet(java.util.HashSet)

Example 7 with QueryableEntry

use of com.hazelcast.query.impl.QueryableEntry in project hazelcast by hazelcast.

the class AndPredicate method filter.

@Override
public Set<QueryableEntry> filter(QueryContext queryContext) {
    Set<QueryableEntry> smallestIndexedResult = null;
    List<Set<QueryableEntry>> otherIndexedResults = new LinkedList<Set<QueryableEntry>>();
    List<Predicate> lsNoIndexPredicates = null;
    for (Predicate predicate : predicates) {
        boolean indexed = false;
        if (predicate instanceof IndexAwarePredicate) {
            IndexAwarePredicate iap = (IndexAwarePredicate) predicate;
            if (iap.isIndexed(queryContext)) {
                indexed = true;
                Set<QueryableEntry> s = iap.filter(queryContext);
                if (smallestIndexedResult == null) {
                    smallestIndexedResult = s;
                } else if (size(s) < size(smallestIndexedResult)) {
                    otherIndexedResults.add(smallestIndexedResult);
                    smallestIndexedResult = s;
                } else {
                    otherIndexedResults.add(s);
                }
            }
        }
        if (!indexed) {
            if (lsNoIndexPredicates == null) {
                lsNoIndexPredicates = new LinkedList<Predicate>();
            }
            lsNoIndexPredicates.add(predicate);
        }
    }
    if (smallestIndexedResult == null) {
        return null;
    }
    return new AndResultSet(smallestIndexedResult, otherIndexedResults, lsNoIndexPredicates);
}
Also used : OrResultSet(com.hazelcast.query.impl.OrResultSet) Set(java.util.Set) AndResultSet(com.hazelcast.query.impl.AndResultSet) AndResultSet(com.hazelcast.query.impl.AndResultSet) IndexAwarePredicate(com.hazelcast.query.IndexAwarePredicate) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) LinkedList(java.util.LinkedList) IndexAwarePredicate(com.hazelcast.query.IndexAwarePredicate) Predicate(com.hazelcast.query.Predicate) VisitablePredicate(com.hazelcast.query.VisitablePredicate)

Example 8 with QueryableEntry

use of com.hazelcast.query.impl.QueryableEntry in project hazelcast by hazelcast.

the class ReplicatedQueryEventFilter method eval.

public boolean eval(Object arg) {
    final QueryableEntry entry = (QueryableEntry) arg;
    final Data keyData = entry.getKeyData();
    return (key == null || key.equals(keyData)) && predicate.apply((Map.Entry) arg);
}
Also used : QueryableEntry(com.hazelcast.query.impl.QueryableEntry) Data(com.hazelcast.nio.serialization.Data) QueryableEntry(com.hazelcast.query.impl.QueryableEntry)

Example 9 with QueryableEntry

use of com.hazelcast.query.impl.QueryableEntry in project hazelcast by hazelcast.

the class QueryEventFilterTest method testEval_givenFilterDoesNotContainKey_whenPredicateIsNotMatching_thenReturnFalse.

@Test
public void testEval_givenFilterDoesNotContainKey_whenPredicateIsNotMatching_thenReturnFalse() {
    //given
    Predicate predicate = FalsePredicate.INSTANCE;
    QueryEventFilter filter = new QueryEventFilter(true, null, predicate);
    //when
    Data key2 = serializationService.toData("key");
    QueryableEntry entry = mockEntryWithKeyData(key2);
    //then
    boolean result = filter.eval(entry);
    assertFalse(result);
}
Also used : Data(com.hazelcast.nio.serialization.Data) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) TruePredicate(com.hazelcast.query.TruePredicate) FalsePredicate(com.hazelcast.query.impl.FalsePredicate) Predicate(com.hazelcast.query.Predicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with QueryableEntry

use of com.hazelcast.query.impl.QueryableEntry in project hazelcast by hazelcast.

the class QueryEventFilterTest method testEval_givenFilterDoesNotContainKey_whenPredicateIsMatching_thenReturnTrue.

@Test
public void testEval_givenFilterDoesNotContainKey_whenPredicateIsMatching_thenReturnTrue() {
    //given
    Predicate predicate = TruePredicate.INSTANCE;
    QueryEventFilter filter = new QueryEventFilter(true, null, predicate);
    //when
    Data key2 = serializationService.toData("key");
    QueryableEntry entry = mockEntryWithKeyData(key2);
    //then
    boolean result = filter.eval(entry);
    assertTrue(result);
}
Also used : Data(com.hazelcast.nio.serialization.Data) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) TruePredicate(com.hazelcast.query.TruePredicate) FalsePredicate(com.hazelcast.query.impl.FalsePredicate) Predicate(com.hazelcast.query.Predicate) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

QueryableEntry (com.hazelcast.query.impl.QueryableEntry)34 Data (com.hazelcast.nio.serialization.Data)14 Predicate (com.hazelcast.query.Predicate)10 ParallelTest (com.hazelcast.test.annotation.ParallelTest)10 QuickTest (com.hazelcast.test.annotation.QuickTest)10 Test (org.junit.Test)10 TruePredicate (com.hazelcast.query.TruePredicate)5 FalsePredicate (com.hazelcast.query.impl.FalsePredicate)5 HashSet (java.util.HashSet)5 CachedQueryEntry (com.hazelcast.query.impl.CachedQueryEntry)4 Indexes (com.hazelcast.query.impl.Indexes)4 Extractors (com.hazelcast.query.impl.getters.Extractors)4 SerializationService (com.hazelcast.spi.serialization.SerializationService)4 Map (java.util.Map)4 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)3 MapContainer (com.hazelcast.map.impl.MapContainer)3 Record (com.hazelcast.map.impl.record.Record)3 TransactionalMap (com.hazelcast.core.TransactionalMap)2 MapQueryEngine (com.hazelcast.map.impl.query.MapQueryEngine)2 Query (com.hazelcast.map.impl.query.Query)2