Search in sources :

Example 36 with Predicate

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

the class AbstractQueryCacheEndToEndConstructor method initQueryCacheConfig.

private QueryCacheConfig initQueryCacheConfig(QueryCacheRequest request, String cacheId) {
    Predicate predicate = request.getPredicate();
    QueryCacheConfig queryCacheConfig;
    if (predicate == null) {
        // user called IMap#getQueryCache method only providing
        // a name (but without a predicate), here we are trying
        // to find a matching configuration for this query cache.
        queryCacheConfig = getOrNullQueryCacheConfig(mapName, request.getCacheName(), cacheId);
    } else {
        queryCacheConfig = getOrCreateQueryCacheConfig(mapName, request.getCacheName(), cacheId);
        queryCacheConfig.setIncludeValue(request.isIncludeValue());
        queryCacheConfig.getPredicateConfig().setImplementation(predicate);
    }
    if (queryCacheConfig == null) {
        return null;
    }
    this.predicate = queryCacheConfig.getPredicateConfig().getImplementation();
    return queryCacheConfig;
}
Also used : QueryCacheConfig(com.hazelcast.config.QueryCacheConfig) Predicate(com.hazelcast.query.Predicate)

Example 37 with Predicate

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

the class QueryRunner method runPartitionScanQueryOnPartitionChunk.

/**
 * Runs a query on a chunk of a single partition. The chunk is defined by
 * the {@code pointers} and the soft limit is defined by the {@code fetchSize}.
 *
 * @param query       the query
 * @param partitionId the partition which is queried
 * @param pointers    the pointers defining the state of iteration
 * @param fetchSize   the soft limit for the number of items to be queried
 * @return the queried entries along with the next {@code tableIndex} to resume querying
 */
public ResultSegment runPartitionScanQueryOnPartitionChunk(Query query, int partitionId, IterationPointer[] pointers, int fetchSize) {
    MapContainer mapContainer = mapServiceContext.getMapContainer(query.getMapName());
    Predicate predicate = queryOptimizer.optimize(query.getPredicate(), mapContainer.getIndexes(partitionId));
    QueryableEntriesSegment entries = partitionScanExecutor.execute(query.getMapName(), predicate, partitionId, pointers, fetchSize);
    ResultProcessor processor = resultProcessorRegistry.get(query.getResultType());
    Result result = processor.populateResult(query, Long.MAX_VALUE, entries.getEntries(), singletonPartitionIdSet(partitionCount, partitionId));
    return new ResultSegment(result, entries.getPointers());
}
Also used : QueryableEntriesSegment(com.hazelcast.query.impl.QueryableEntriesSegment) MapContainer(com.hazelcast.map.impl.MapContainer) Predicate(com.hazelcast.query.Predicate)

Example 38 with Predicate

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

the class QueryRunner method runIndexQueryOnOwnedPartitions.

/**
 * Performs the given query using indexes.
 * <p>
 * The method may return a special failure result, which has {@code null}
 * {@link Result#getPartitionIds() partition IDs}, in the following
 * situations:
 * <ul>
 * <li>If a partition migration is detected during the query execution.
 * <li>If it's impossible to perform the given query using indexes.
 * </ul>
 * <p>
 * The method may be invoked on any thread.
 *
 * @param query the query to perform.
 * @return the result of the query; if the result has {@code null} {@link
 * Result#getPartitionIds() partition IDs} this indicates a failure.
 */
public Result runIndexQueryOnOwnedPartitions(Query query) {
    int migrationStamp = getMigrationStamp();
    PartitionIdSet initialPartitions = mapServiceContext.getOrInitCachedMemberPartitions();
    MapContainer mapContainer = mapServiceContext.getMapContainer(query.getMapName());
    // to optimize the query we need to get any index instance
    Indexes indexes = mapContainer.getIndexes();
    if (indexes == null) {
        indexes = mapContainer.getIndexes(initialPartitions.iterator().next());
    }
    // first we optimize the query
    Predicate predicate = queryOptimizer.optimize(query.getPredicate(), indexes);
    // then we try to run using an index
    Iterable<QueryableEntry> entries = runUsingGlobalIndexSafely(predicate, mapContainer, migrationStamp, initialPartitions.size());
    Result result;
    if (entries == null) {
        // failed with index query because of ongoing migrations
        result = populateEmptyResult(query, initialPartitions);
    } else {
        // success
        result = populateNonEmptyResult(query, entries, initialPartitions);
    }
    return result;
}
Also used : PartitionIdSet(com.hazelcast.internal.util.collection.PartitionIdSet) SetUtil.singletonPartitionIdSet(com.hazelcast.internal.util.SetUtil.singletonPartitionIdSet) Indexes(com.hazelcast.query.impl.Indexes) MapContainer(com.hazelcast.map.impl.MapContainer) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) Predicate(com.hazelcast.query.Predicate)

Example 39 with Predicate

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

the class PredicateBuilderImpl method accept.

@Override
public Predicate accept(Visitor visitor, Indexes indexes) {
    Predicate predicate = lsPredicates.get(0);
    if (predicate instanceof VisitablePredicate) {
        Predicate newPredicate = ((VisitablePredicate) predicate).accept(visitor, indexes);
        if (newPredicate != predicate) {
            PredicateBuilderImpl newPredicateBuilder = new PredicateBuilderImpl();
            newPredicateBuilder.attribute = attribute;
            newPredicateBuilder.lsPredicates.addAll(lsPredicates);
            newPredicateBuilder.lsPredicates.set(0, newPredicate);
            return newPredicateBuilder;
        }
    }
    return this;
}
Also used : VisitablePredicate(com.hazelcast.query.impl.predicates.VisitablePredicate) IndexAwarePredicate(com.hazelcast.query.impl.predicates.IndexAwarePredicate) VisitablePredicate(com.hazelcast.query.impl.predicates.VisitablePredicate) Predicate(com.hazelcast.query.Predicate)

Example 40 with Predicate

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

the class PredicateBuilderImpl method or.

@Override
public PredicateBuilder or(Predicate predicate) {
    if (predicate != PredicateBuilderImpl.this) {
        throw new RuntimeException("Illegal or statement expected: " + PredicateBuilderImpl.class.getSimpleName() + ", found: " + ((predicate == null) ? "null" : predicate.getClass().getSimpleName()));
    }
    int index = lsPredicates.size() - 2;
    Predicate first = lsPredicates.remove(index);
    Predicate second = lsPredicates.remove(index);
    return addPredicate(Predicates.or(first, second));
}
Also used : IndexAwarePredicate(com.hazelcast.query.impl.predicates.IndexAwarePredicate) VisitablePredicate(com.hazelcast.query.impl.predicates.VisitablePredicate) Predicate(com.hazelcast.query.Predicate)

Aggregations

Predicate (com.hazelcast.query.Predicate)248 Test (org.junit.Test)165 QuickTest (com.hazelcast.test.annotation.QuickTest)159 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)105 HazelcastInstance (com.hazelcast.core.HazelcastInstance)38 MapListener (com.hazelcast.map.listener.MapListener)17 ParallelTest (com.hazelcast.test.annotation.ParallelTest)17 EntryListener (com.hazelcast.core.EntryListener)16 FalsePredicate (com.hazelcast.query.impl.FalsePredicate)15 ArrayList (java.util.ArrayList)15 EntryObject (com.hazelcast.query.PredicateBuilder.EntryObject)14 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)14 Value (com.hazelcast.query.SampleTestObjects.Value)10 SqlPredicate (com.hazelcast.query.SqlPredicate)10 Config (com.hazelcast.config.Config)9 PredicateTestUtils.createMockVisitablePredicate (com.hazelcast.query.impl.predicates.PredicateTestUtils.createMockVisitablePredicate)9 EntryEvent (com.hazelcast.core.EntryEvent)8 Data (com.hazelcast.internal.serialization.Data)8 MapListenerAdapter (com.hazelcast.map.impl.MapListenerAdapter)8 Indexes (com.hazelcast.query.impl.Indexes)8