Search in sources :

Example 31 with Indexes

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

the class MapReplicationStateHolder method applyState.

@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:methodlength", "checkstyle:cyclomaticcomplexity", "checkstyle:nestedifdepth" })
void applyState() {
    ThreadUtil.assertRunningOnPartitionThread();
    applyIndexesState();
    if (!isNullOrEmpty(data)) {
        for (Map.Entry<String, List> dataEntry : data.entrySet()) {
            String mapName = dataEntry.getKey();
            List keyRecordExpiry = dataEntry.getValue();
            RecordStore recordStore = operation.getRecordStore(mapName);
            recordStore.beforeOperation();
            try {
                initializeRecordStore(mapName, recordStore);
                recordStore.setPreMigrationLoadedStatus(loaded.get(mapName));
                MapContainer mapContainer = recordStore.getMapContainer();
                PartitionContainer partitionContainer = recordStore.getMapContainer().getMapServiceContext().getPartitionContainer(operation.getPartitionId());
                for (Map.Entry<String, IndexConfig> indexDefinition : mapContainer.getIndexDefinitions().entrySet()) {
                    Indexes indexes = mapContainer.getIndexes(partitionContainer.getPartitionId());
                    indexes.addOrGetIndex(indexDefinition.getValue());
                }
                final Indexes indexes = mapContainer.getIndexes(partitionContainer.getPartitionId());
                final boolean populateIndexes = indexesMustBePopulated(indexes, operation);
                InternalIndex[] indexesSnapshot = null;
                if (populateIndexes) {
                    // defensively clear possible stale leftovers in non-global indexes from
                    // the previous failed promotion attempt
                    indexesSnapshot = indexes.getIndexes();
                    Indexes.beginPartitionUpdate(indexesSnapshot);
                    indexes.clearAll();
                }
                long nowInMillis = Clock.currentTimeMillis();
                forEachReplicatedRecord(keyRecordExpiry, mapContainer, recordStore, populateIndexes, nowInMillis);
                if (populateIndexes) {
                    Indexes.markPartitionAsIndexed(partitionContainer.getPartitionId(), indexesSnapshot);
                }
            } finally {
                recordStore.afterOperation();
            }
        }
    }
    for (Map.Entry<String, LocalRecordStoreStats> statsEntry : recordStoreStatsPerMapName.entrySet()) {
        String mapName = statsEntry.getKey();
        LocalRecordStoreStats stats = statsEntry.getValue();
        RecordStore recordStore = operation.getRecordStore(mapName);
        recordStore.setStats(stats);
    }
}
Also used : PartitionContainer(com.hazelcast.map.impl.PartitionContainer) Indexes(com.hazelcast.query.impl.Indexes) MapContainer(com.hazelcast.map.impl.MapContainer) LocalRecordStoreStats(com.hazelcast.internal.monitor.LocalRecordStoreStats) InternalIndex(com.hazelcast.query.impl.InternalIndex) IndexConfig(com.hazelcast.config.IndexConfig) RecordStore(com.hazelcast.map.impl.recordstore.RecordStore) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 32 with Indexes

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

the class PartitionWideEntryOperation method runWithIndex.

/**
 * @return {@code true} if index has been used and the EP
 * has been executed on its keys, {@code false} otherwise
 */
private boolean runWithIndex() {
    // here we try to query the partitioned-index
    Predicate predicate = getPredicate();
    if (predicate == null) {
        return false;
    }
    // we use the partitioned-index to operate on the selected keys only
    Indexes indexes = mapContainer.getIndexes(getPartitionId());
    Iterable<QueryableEntry> entries = indexes.query(queryOptimizer.optimize(predicate, indexes), 1);
    if (entries == null) {
        return false;
    }
    responses = new MapEntries();
    // when NATIVE we can pass null as predicate since it's all
    // happening on partition thread so no data-changes may occur
    operator = operator(this, entryProcessor, null);
    keysFromIndex = new HashSet<>();
    for (QueryableEntry entry : entries) {
        keysFromIndex.add(entry.getKeyData());
        Data response = operator.operateOnKey(entry.getKeyData()).doPostOperateOps().getResult();
        if (response != null) {
            responses.add(entry.getKeyData(), response);
        }
    }
    return true;
}
Also used : MapEntries(com.hazelcast.map.impl.MapEntries) ToHeapDataConverter.toHeapData(com.hazelcast.internal.util.ToHeapDataConverter.toHeapData) Data(com.hazelcast.internal.serialization.Data) Indexes(com.hazelcast.query.impl.Indexes) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) Predicate(com.hazelcast.query.Predicate)

Example 33 with Indexes

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

the class MapChunk method applyIndexStateBefore.

private void applyIndexStateBefore(RecordStore recordStore) {
    MapContainer mapContainer = recordStore.getMapContainer();
    PartitionContainer partitionContainer = mapContainer.getMapServiceContext().getPartitionContainer(getPartitionId());
    for (Map.Entry<String, IndexConfig> indexDefinition : mapContainer.getIndexDefinitions().entrySet()) {
        Indexes indexes = mapContainer.getIndexes(partitionContainer.getPartitionId());
        indexes.addOrGetIndex(indexDefinition.getValue());
    }
    Indexes indexes = mapContainer.getIndexes(partitionContainer.getPartitionId());
    boolean populateIndexes = indexesMustBePopulated(indexes);
    if (populateIndexes) {
        // defensively clear possible stale
        // leftovers in non-global indexes from
        // the previous failed promotion attempt
        Indexes.beginPartitionUpdate(indexes.getIndexes());
        indexes.clearAll();
    }
}
Also used : IndexConfig(com.hazelcast.config.IndexConfig) PartitionContainer(com.hazelcast.map.impl.PartitionContainer) Indexes(com.hazelcast.query.impl.Indexes) Map(java.util.Map) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) MapContainer(com.hazelcast.map.impl.MapContainer)

Example 34 with Indexes

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

the class MapChunk method putOrUpdateReplicatedDataWithPerNodeEviction.

// owned or backup
private void putOrUpdateReplicatedDataWithPerNodeEviction(RecordStore recordStore) {
    MapContainer mapContainer = recordStore.getMapContainer();
    EvictionConfig evictionConfig = mapContainer.getMapConfig().getEvictionConfig();
    long ownedEntryCountOnThisNode = entryCountOnThisNode(mapContainer);
    int count = 0;
    long nowInMillis = Clock.currentTimeMillis();
    do {
        Data dataKey = (Data) keyRecordExpiry.poll();
        Record record = (Record) keyRecordExpiry.poll();
        ExpiryMetadata expiryMetadata = (ExpiryMetadata) keyRecordExpiry.poll();
        if (ownedEntryCountOnThisNode >= evictionConfig.getSize()) {
            if (getReplicaIndex() == 0) {
                recordStore.doPostEvictionOperations(dataKey, record.getValue(), ExpiryReason.NOT_EXPIRED);
            }
        } else {
            Indexes indexes = mapContainer.getIndexes(recordStore.getPartitionId());
            recordStore.putOrUpdateReplicatedRecord(dataKey, record, expiryMetadata, indexesMustBePopulated(indexes), nowInMillis);
            ownedEntryCountOnThisNode++;
        }
        if (++count % DISPOSE_AT_COUNT == 0) {
            recordStore.disposeDeferredBlocks();
        }
    } while (!keyRecordExpiry.isEmpty());
    recordStore.disposeDeferredBlocks();
}
Also used : EvictionConfig(com.hazelcast.config.EvictionConfig) Data(com.hazelcast.internal.serialization.Data) Record(com.hazelcast.map.impl.record.Record) Indexes(com.hazelcast.query.impl.Indexes) MapContainer(com.hazelcast.map.impl.MapContainer) ExpiryMetadata(com.hazelcast.map.impl.recordstore.expiry.ExpiryMetadata)

Example 35 with Indexes

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

the class QueryRunner method runPartitionIndexOrPartitionScanQueryOnGivenOwnedPartition.

// MIGRATION UNSAFE QUERYING - MIGRATION STAMPS ARE NOT VALIDATED, so assumes a run on partition-thread
// for a single partition. If the index is global it won't be asked
public Result runPartitionIndexOrPartitionScanQueryOnGivenOwnedPartition(Query query, int partitionId) {
    MapContainer mapContainer = mapServiceContext.getMapContainer(query.getMapName());
    PartitionIdSet partitions = singletonPartitionIdSet(partitionCount, partitionId);
    // first we optimize the query
    Predicate predicate = queryOptimizer.optimize(query.getPredicate(), mapContainer.getIndexes(partitionId));
    Iterable<QueryableEntry> entries = null;
    Indexes indexes = mapContainer.getIndexes(partitionId);
    if (indexes != null && !indexes.isGlobal()) {
        entries = indexes.query(predicate, partitions.size());
    }
    Result result;
    if (entries == null) {
        result = createResult(query, partitions);
        partitionScanExecutor.execute(query.getMapName(), predicate, partitions, result);
        result.completeConstruction(partitions);
    } else {
        result = populateNonEmptyResult(query, entries, partitions);
    }
    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)

Aggregations

Indexes (com.hazelcast.query.impl.Indexes)56 MapContainer (com.hazelcast.map.impl.MapContainer)15 QuickTest (com.hazelcast.test.annotation.QuickTest)14 Test (org.junit.Test)14 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)13 Predicate (com.hazelcast.query.Predicate)11 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)11 InternalIndex (com.hazelcast.query.impl.InternalIndex)10 RecordStore (com.hazelcast.map.impl.recordstore.RecordStore)9 IndexConfig (com.hazelcast.config.IndexConfig)8 Record (com.hazelcast.map.impl.record.Record)8 MapServiceContext (com.hazelcast.map.impl.MapServiceContext)7 MapService (com.hazelcast.map.impl.MapService)6 Data (com.hazelcast.nio.serialization.Data)6 ArrayList (java.util.ArrayList)6 PartitionContainer (com.hazelcast.map.impl.PartitionContainer)5 PredicateTestUtils.createPassthroughVisitor (com.hazelcast.query.impl.predicates.PredicateTestUtils.createPassthroughVisitor)5 Index (com.hazelcast.query.impl.Index)4 Map (java.util.Map)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3