Search in sources :

Example 1 with QueryEngine

use of com.hazelcast.map.impl.query.QueryEngine 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");
    QueryEngine queryEngine = mapServiceContext.getQueryEngine(name);
    Query query = Query.of().mapName(name).predicate(predicate).iterationType(IterationType.ENTRY).build();
    QueryResult queryResult = queryEngine.execute(query, Target.ALL_NODES);
    Set result = QueryResultUtils.transformToSet(ss, queryResult, predicate, IterationType.ENTRY, true, true);
    // TODO: can't we just use the original set?
    List<Object> valueSet = new ArrayList<>();
    Set<Data> keyWontBeIncluded = new HashSet<>();
    Extractors extractors = mapServiceContext.getExtractors(name);
    CachedQueryEntry cachedQueryEntry = new CachedQueryEntry();
    // 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);
        if (isRemoved) {
            keyWontBeIncluded.add(entry.getKey());
        } else {
            if (isUpdated) {
                keyWontBeIncluded.add(entry.getKey());
            }
            Object entryValue = entry.getValue().value;
            cachedQueryEntry.init(ss, entry.getKey(), entryValue, extractors);
            if (predicate.apply(cachedQueryEntry)) {
                valueSet.add(toObjectIfNeeded(cachedQueryEntry.getValueData()));
            }
        }
    }
    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) Data(com.hazelcast.internal.serialization.Data) QueryEngine(com.hazelcast.map.impl.query.QueryEngine) QueryResult(com.hazelcast.map.impl.query.QueryResult) Extractors(com.hazelcast.query.impl.getters.Extractors) CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) HashMap(java.util.HashMap) Map(java.util.Map) TransactionalMap(com.hazelcast.transaction.TransactionalMap) HashSet(java.util.HashSet)

Example 2 with QueryEngine

use of com.hazelcast.map.impl.query.QueryEngine in project hazelcast by hazelcast.

the class PublisherCreateOperation method runInitialQuery.

private QueryResult runInitialQuery() {
    QueryEngine queryEngine = getMapServiceContext().getQueryEngine(name);
    IterationType iterationType = info.isIncludeValue() ? IterationType.ENTRY : IterationType.KEY;
    Query query = Query.of().mapName(name).predicate(info.getPredicate()).iterationType(iterationType).build();
    return queryEngine.execute(query, Target.LOCAL_NODE);
}
Also used : Query(com.hazelcast.map.impl.query.Query) IterationType(com.hazelcast.internal.util.IterationType) QueryEngine(com.hazelcast.map.impl.query.QueryEngine)

Example 3 with QueryEngine

use of com.hazelcast.map.impl.query.QueryEngine in project hazelcast by hazelcast.

the class TransactionalMapProxy method keySet.

@Override
@SuppressWarnings("unchecked")
public Set keySet(Predicate predicate) {
    checkTransactionState();
    checkNotNull(predicate, "Predicate should not be null!");
    checkNotInstanceOf(PagingPredicate.class, predicate, "Paging is not supported for Transactional queries!");
    QueryEngine queryEngine = mapServiceContext.getQueryEngine(name);
    Query query = Query.of().mapName(name).predicate(predicate).iterationType(IterationType.KEY).build();
    QueryResult queryResult = queryEngine.execute(query, Target.ALL_NODES);
    Set queryResultSet = QueryResultUtils.transformToSet(ss, queryResult, predicate, IterationType.KEY, true, tx.isOriginatedFromClient());
    Extractors extractors = mapServiceContext.getExtractors(name);
    Set<Object> returningKeySet = new HashSet<Object>(queryResultSet);
    CachedQueryEntry cachedQueryEntry = new CachedQueryEntry();
    for (Map.Entry<Data, TxnValueWrapper> entry : txMap.entrySet()) {
        if (entry.getValue().type == Type.REMOVED) {
            // meanwhile remove keys which are not in txMap
            returningKeySet.remove(toObjectIfNeeded(entry.getKey()));
        } else {
            Data keyData = entry.getKey();
            if (predicate == Predicates.alwaysTrue()) {
                returningKeySet.add(toObjectIfNeeded(keyData));
            } else {
                cachedQueryEntry.init(ss, keyData, entry.getValue().value, extractors);
                // apply predicate on txMap
                if (predicate.apply(cachedQueryEntry)) {
                    returningKeySet.add(toObjectIfNeeded(keyData));
                }
            }
        }
    }
    return returningKeySet;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Query(com.hazelcast.map.impl.query.Query) Data(com.hazelcast.internal.serialization.Data) QueryEngine(com.hazelcast.map.impl.query.QueryEngine) QueryResult(com.hazelcast.map.impl.query.QueryResult) Extractors(com.hazelcast.query.impl.getters.Extractors) CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) HashMap(java.util.HashMap) Map(java.util.Map) TransactionalMap(com.hazelcast.transaction.TransactionalMap) HashSet(java.util.HashSet)

Example 4 with QueryEngine

use of com.hazelcast.map.impl.query.QueryEngine in project hazelcast by hazelcast.

the class MapProxySupport method executeQueryInternal.

protected <T extends Result> T executeQueryInternal(Predicate predicate, Aggregator aggregator, Projection projection, IterationType iterationType, Target target) {
    QueryEngine queryEngine = getMapQueryEngine();
    final Predicate userPredicate;
    if (predicate instanceof PartitionPredicate) {
        PartitionPredicate partitionPredicate = (PartitionPredicate) predicate;
        Data key = toData(partitionPredicate.getPartitionKey());
        int partitionId = partitionService.getPartitionId(key);
        if (target.mode() == TargetMode.LOCAL_NODE && !partitionService.isPartitionOwner(partitionId) || target.mode() == TargetMode.PARTITION_OWNER && !target.partitions().contains(partitionId)) {
            userPredicate = alwaysFalse();
        } else {
            target = createPartitionTarget(new PartitionIdSet(partitionService.getPartitionCount(), partitionId));
            userPredicate = partitionPredicate.getTarget();
        }
    } else {
        userPredicate = predicate;
    }
    handleHazelcastInstanceAwareParams(userPredicate);
    Query query = Query.of().mapName(getName()).predicate(userPredicate).iterationType(iterationType).aggregator(aggregator).projection(projection).build();
    return queryEngine.execute(query, target);
}
Also used : PartitionPredicate(com.hazelcast.query.PartitionPredicate) Query(com.hazelcast.map.impl.query.Query) PartitionIdSet(com.hazelcast.internal.util.collection.PartitionIdSet) Data(com.hazelcast.internal.serialization.Data) QueryEngine(com.hazelcast.map.impl.query.QueryEngine) PartitionPredicate(com.hazelcast.query.PartitionPredicate) TruePredicate(com.hazelcast.query.impl.predicates.TruePredicate) Predicate(com.hazelcast.query.Predicate)

Aggregations

Query (com.hazelcast.map.impl.query.Query)4 QueryEngine (com.hazelcast.map.impl.query.QueryEngine)4 Data (com.hazelcast.internal.serialization.Data)3 QueryResult (com.hazelcast.map.impl.query.QueryResult)2 CachedQueryEntry (com.hazelcast.query.impl.CachedQueryEntry)2 Extractors (com.hazelcast.query.impl.getters.Extractors)2 TransactionalMap (com.hazelcast.transaction.TransactionalMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 IterationType (com.hazelcast.internal.util.IterationType)1 PartitionIdSet (com.hazelcast.internal.util.collection.PartitionIdSet)1 PartitionPredicate (com.hazelcast.query.PartitionPredicate)1 Predicate (com.hazelcast.query.Predicate)1 TruePredicate (com.hazelcast.query.impl.predicates.TruePredicate)1 ArrayList (java.util.ArrayList)1