Search in sources :

Example 11 with CachedQueryEntry

use of com.hazelcast.query.impl.CachedQueryEntry 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 12 with CachedQueryEntry

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

the class AbstractInternalQueryCache method scanWithPredicate.

/**
 * Scan all key-value pairs and add matching ones with predicate to result.
 */
private void scanWithPredicate(Predicate predicate, BiConsumer consumer) {
    // needed for optimization where key and value are not an instance of Data type
    final boolean areKeyValueObjectType = !queryCacheConfig.isSerializeKeys() && InMemoryFormat.OBJECT == queryCacheConfig.getInMemoryFormat();
    CachedQueryEntry queryEntry = new CachedQueryEntry(ss, extractors);
    Set<Map.Entry<Object, QueryCacheRecord>> entries = recordStore.entrySet();
    for (Map.Entry<Object, QueryCacheRecord> entry : entries) {
        Object queryCacheKey = entry.getKey();
        Object rawValue = entry.getValue().getRawValue();
        if (areKeyValueObjectType) {
            queryEntry.initWithObjectKeyValue(queryCacheKey, rawValue);
        } else {
            queryEntry.init(queryCacheKey, rawValue);
        }
        if (!predicate.apply(queryEntry)) {
            continue;
        }
        consumer.accept(queryCacheKey, queryEntry.getByPrioritizingObjectValue());
    }
}
Also used : CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) QueryCacheRecord(com.hazelcast.map.impl.querycache.subscriber.record.QueryCacheRecord) CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) Map(java.util.Map) IMap(com.hazelcast.map.IMap)

Example 13 with CachedQueryEntry

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

the class DefaultQueryCacheRecordStore method saveIndex.

/**
 * Same as {@link #saveIndex}
 * with explicit {@link CachedQueryEntry} arguments for reuse, to avoid
 * excessive litter when adding several entries in batch.
 */
private void saveIndex(Data keyData, QueryCacheRecord currentRecord, QueryCacheRecord oldRecord, CachedQueryEntry newEntry, CachedQueryEntry oldEntry) {
    if (indexes.haveAtLeastOneIndex()) {
        Object currentValue = currentRecord.getValue();
        QueryEntry queryEntry = new QueryEntry(ss, keyData, currentValue, extractors);
        Object oldValue = oldRecord == null ? null : oldRecord.getValue();
        newEntry.init(keyData, currentValue);
        oldEntry.init(keyData, oldValue);
        indexes.putEntry(newEntry, oldEntry, queryEntry, Index.OperationSource.USER);
    }
}
Also used : CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) QueryEntry(com.hazelcast.query.impl.QueryEntry)

Example 14 with CachedQueryEntry

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

the class DefaultQueryCacheRecordStore method saveIndex.

private void saveIndex(Object queryCacheKey, QueryCacheRecord currentRecord, QueryCacheRecord oldRecord) {
    if (indexes.haveAtLeastOneIndex()) {
        Data keyData = ss.toData(queryCacheKey);
        Object currentValue = currentRecord.getValue();
        QueryEntry queryEntry = new QueryEntry(ss, keyData, currentValue, extractors);
        Object oldValue = oldRecord == null ? null : oldRecord.getValue();
        CachedQueryEntry newEntry = new CachedQueryEntry(ss, keyData, currentValue, extractors);
        CachedQueryEntry oldEntry = new CachedQueryEntry(ss, keyData, oldValue, extractors);
        indexes.putEntry(newEntry, oldEntry, queryEntry, Index.OperationSource.USER);
    }
}
Also used : CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) QueryEntry(com.hazelcast.query.impl.QueryEntry) Data(com.hazelcast.internal.serialization.Data) CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry)

Example 15 with CachedQueryEntry

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

the class SortingUtil method getSortedSubListData.

/**
 * @param list The entry list to be sorted
 * @param pagingPredicate The predicate to be used for query. The anchor list in the predicate is also updated.
 * @return The list of Data for the requested page. If iteration type is KEY only key data list is returned,
 * else if iterationType is VALUE the value data list is returned. If iterationType is ENTRY, then list of entry of
 * (key data, value data) is returned.
 */
public static List getSortedSubListData(List<QueryableEntry> list, PagingPredicateImpl pagingPredicate) {
    IterationType iterationType = pagingPredicate.getIterationType();
    Map.Entry<Integer, Integer> pageIndex = getPageIndexesAndUpdateAnchor(list, pagingPredicate, iterationType);
    int begin = pageIndex.getKey();
    int end = pageIndex.getValue();
    if (begin == -1) {
        return Collections.EMPTY_LIST;
    }
    List result = new ArrayList(end - begin);
    for (int i = begin; i < end; ++i) {
        CachedQueryEntry entry = (CachedQueryEntry) list.get(i);
        switch(iterationType) {
            case KEY:
                result.add(entry.getKeyData());
                break;
            case VALUE:
                result.add(entry.getValueData());
                break;
            default:
                // iteration type is ENTRY
                result.add(new AbstractMap.SimpleImmutableEntry<>(entry.getKeyData(), entry.getValueData()));
        }
    }
    return result;
}
Also used : AbstractMap(java.util.AbstractMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CachedQueryEntry(com.hazelcast.query.impl.CachedQueryEntry) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Aggregations

CachedQueryEntry (com.hazelcast.query.impl.CachedQueryEntry)15 Map (java.util.Map)10 QueryCacheRecord (com.hazelcast.map.impl.querycache.subscriber.record.QueryCacheRecord)6 Data (com.hazelcast.internal.serialization.Data)5 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)4 Data (com.hazelcast.nio.serialization.Data)4 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)4 Extractors (com.hazelcast.query.impl.getters.Extractors)4 AbstractMap (java.util.AbstractMap)4 IMap (com.hazelcast.core.IMap)3 QueryEntry (com.hazelcast.query.impl.QueryEntry)3 ArrayList (java.util.ArrayList)3 IMap (com.hazelcast.map.IMap)2 Query (com.hazelcast.map.impl.query.Query)2 QueryEngine (com.hazelcast.map.impl.query.QueryEngine)2 QueryResult (com.hazelcast.map.impl.query.QueryResult)2 Record (com.hazelcast.map.impl.record.Record)2 TransactionalMap (com.hazelcast.transaction.TransactionalMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2