Search in sources :

Example 1 with InternalIndex

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

the class MetadataResolver method resolveFromHd.

@Nullable
@SuppressWarnings("rawtypes")
private Metadata resolveFromHd(MapContainer container) {
    if (container.getIndexes() == null) {
        return null;
    }
    InternalIndex[] indexes = container.getIndexes().getIndexes();
    if (indexes == null || indexes.length == 0) {
        return null;
    }
    InternalIndex index = indexes[0];
    Iterator<QueryableEntry> entryIterator = index.getSqlRecordIterator(false);
    if (!entryIterator.hasNext()) {
        return null;
    }
    QueryableEntry entry = entryIterator.next();
    return resolveMetadata(entry.getKey(), entry.getValue());
}
Also used : InternalIndex(com.hazelcast.query.impl.InternalIndex) QueryableEntry(com.hazelcast.query.impl.QueryableEntry) Nullable(javax.annotation.Nullable)

Example 2 with InternalIndex

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

the class MapFetchIndexOperation method runInternal.

@Override
protected void runInternal() {
    Indexes indexes = mapContainer.getIndexes();
    if (indexes == null) {
        throw QueryException.error(SqlErrorCode.INDEX_INVALID, "Cannot use the index \"" + indexName + "\" of the IMap \"" + name + "\" because it is not global " + "(make sure the property \"" + ClusterProperty.GLOBAL_HD_INDEX_ENABLED + "\" is set to \"true\")");
    }
    InternalIndex index = indexes.getIndex(indexName);
    if (index == null) {
        throw QueryException.error(SqlErrorCode.INDEX_INVALID, "Index \"" + indexName + "\" does not exist");
    }
    PartitionStamp indexStamp = index.getPartitionStamp();
    if (indexStamp == null) {
        throw new RetryableHazelcastException("Index is being rebuilt");
    }
    if (indexStamp.partitions.equals(partitionIdSet)) {
        // We clear the requested partitionIdSet, which means that we won't filter out any partitions.
        // This is an optimization for the case when there was no concurrent migration.
        partitionIdSet = null;
    } else {
        if (!indexStamp.partitions.containsAll(partitionIdSet)) {
            throw new MissingPartitionException("some requested partitions are not indexed");
        }
    }
    switch(index.getConfig().getType()) {
        case HASH:
            response = runInternalHash(index);
            break;
        case SORTED:
            response = runInternalSorted(index);
            break;
        case BITMAP:
            throw new UnsupportedOperationException("BITMAP index scan is not implemented");
        default:
            throw new UnsupportedOperationException("Unknown index type: \"" + index.getConfig().getType().name() + "\"");
    }
    if (!index.validatePartitionStamp(indexStamp.stamp)) {
        throw new MissingPartitionException("partition timestamp has changed");
    }
}
Also used : InternalIndex(com.hazelcast.query.impl.InternalIndex) RetryableHazelcastException(com.hazelcast.spi.exception.RetryableHazelcastException) PartitionStamp(com.hazelcast.query.impl.GlobalIndexPartitionTracker.PartitionStamp) Indexes(com.hazelcast.query.impl.Indexes)

Example 3 with InternalIndex

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

the class MapTableUtils method getPartitionedMapIndexes.

public static List<MapTableIndex> getPartitionedMapIndexes(MapContainer mapContainer, List<TableField> fields) {
    Map<QueryPath, Integer> pathToOrdinalMap = mapPathsToOrdinals(fields);
    if (mapContainer.getIndexes() == null) {
        return Collections.emptyList();
    }
    InternalIndex[] indexes = mapContainer.getIndexes().getIndexes();
    if (indexes == null || indexes.length == 0) {
        return Collections.emptyList();
    }
    List<MapTableIndex> res = new ArrayList<>(indexes.length);
    for (Index index : indexes) {
        IndexConfig indexConfig = index.getConfig();
        List<QueryDataType> resolvedFieldConverterTypes = indexConverterToSqlTypes(index.getConverter());
        List<String> indexAttributes = indexConfig.getAttributes();
        List<Integer> indexFieldOrdinals = new ArrayList<>(indexAttributes.size());
        List<QueryDataType> indexFieldConverterTypes = new ArrayList<>(indexAttributes.size());
        String[] components = index.getComponents();
        for (int i = 0; i < indexAttributes.size(); i++) {
            String attribute = indexAttributes.get(i);
            QueryPath attributePath = QueryPath.create(attribute);
            Integer ordinal = pathToOrdinalMap.get(attributePath);
            if (ordinal == null) {
                // No mapping for the field. Stop.
                break;
            }
            if (i >= resolvedFieldConverterTypes.size()) {
                // No more resolved converters. Stop.
                break;
            }
            QueryDataType fieldType = fields.get(ordinal).getType();
            QueryDataType converterType = resolvedFieldConverterTypes.get(i);
            if (!isCompatibleForIndexRequest(fieldType, converterType)) {
                // Field and converter types are not compatible (e.g. INT vs VARCHAR).
                break;
            }
            indexFieldOrdinals.add(ordinal);
            indexFieldConverterTypes.add(converterType);
        }
        MapTableIndex index0 = new MapTableIndex(indexConfig.getName(), indexConfig.getType(), components.length, indexFieldOrdinals, indexFieldConverterTypes);
        res.add(index0);
    }
    return res;
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) InternalIndex(com.hazelcast.query.impl.InternalIndex) Index(com.hazelcast.query.impl.Index) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) InternalIndex(com.hazelcast.query.impl.InternalIndex) IndexConfig(com.hazelcast.config.IndexConfig)

Example 4 with InternalIndex

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

the class LocalMapStatsProvider method aggregateFreshIndexStats.

private static Map<String, OnDemandIndexStats> aggregateFreshIndexStats(InternalIndex[] freshIndexes, Map<String, OnDemandIndexStats> freshStats) {
    if (freshIndexes.length > 0 && freshStats == null) {
        freshStats = new HashMap<>();
    }
    for (InternalIndex index : freshIndexes) {
        String indexName = index.getName();
        OnDemandIndexStats freshIndexStats = freshStats.get(indexName);
        if (freshIndexStats == null) {
            freshIndexStats = new OnDemandIndexStats();
            freshIndexStats.setCreationTime(Long.MAX_VALUE);
            freshStats.put(indexName, freshIndexStats);
        }
        PerIndexStats indexStats = index.getPerIndexStats();
        freshIndexStats.setCreationTime(Math.min(freshIndexStats.getCreationTime(), indexStats.getCreationTime()));
        long hitCount = indexStats.getHitCount();
        freshIndexStats.setHitCount(Math.max(freshIndexStats.getHitCount(), hitCount));
        freshIndexStats.setQueryCount(Math.max(freshIndexStats.getQueryCount(), indexStats.getQueryCount()));
        freshIndexStats.setMemoryCost(freshIndexStats.getMemoryCost() + indexStats.getMemoryCost());
        freshIndexStats.setAverageHitSelectivity(freshIndexStats.getAverageHitSelectivity() + indexStats.getTotalNormalizedHitCardinality());
        freshIndexStats.setAverageHitLatency(freshIndexStats.getAverageHitLatency() + indexStats.getTotalHitLatency());
        freshIndexStats.setTotalHitCount(freshIndexStats.getTotalHitCount() + hitCount);
        freshIndexStats.setInsertCount(freshIndexStats.getInsertCount() + indexStats.getInsertCount());
        freshIndexStats.setTotalInsertLatency(freshIndexStats.getTotalInsertLatency() + indexStats.getTotalInsertLatency());
        freshIndexStats.setUpdateCount(freshIndexStats.getUpdateCount() + indexStats.getUpdateCount());
        freshIndexStats.setTotalUpdateLatency(freshIndexStats.getTotalUpdateLatency() + indexStats.getTotalUpdateLatency());
        freshIndexStats.setRemoveCount(freshIndexStats.getRemoveCount() + indexStats.getRemoveCount());
        freshIndexStats.setTotalRemoveLatency(freshIndexStats.getTotalRemoveLatency() + indexStats.getTotalRemoveLatency());
    }
    return freshStats;
}
Also used : InternalIndex(com.hazelcast.query.impl.InternalIndex) OnDemandIndexStats(com.hazelcast.internal.monitor.impl.OnDemandIndexStats) PerIndexStats(com.hazelcast.internal.monitor.impl.PerIndexStats)

Example 5 with InternalIndex

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

the class MapMigrationAwareService method populateIndexes.

@SuppressWarnings("checkstyle:NPathComplexity")
private void populateIndexes(PartitionMigrationEvent event, TargetIndexes targetIndexes, String stepName) {
    assert event.getMigrationEndpoint() == DESTINATION;
    assert targetIndexes != null;
    if (event.getNewReplicaIndex() != 0) {
        // backup partitions have no indexes to populate
        return;
    }
    PartitionContainer container = mapServiceContext.getPartitionContainer(event.getPartitionId());
    for (RecordStore<Record> recordStore : container.getMaps().values()) {
        MapContainer mapContainer = mapServiceContext.getMapContainer(recordStore.getName());
        Indexes indexes = mapContainer.getIndexes(event.getPartitionId());
        indexes.createIndexesFromRecordedDefinitions();
        if (!indexes.haveAtLeastOneIndex()) {
            // no indexes to work with
            continue;
        }
        if (indexes.isGlobal() && targetIndexes == TargetIndexes.NON_GLOBAL) {
            continue;
        }
        if (!indexes.isGlobal() && targetIndexes == TargetIndexes.GLOBAL) {
            continue;
        }
        InternalIndex[] indexesSnapshot = indexes.getIndexes();
        Indexes.beginPartitionUpdate(indexesSnapshot);
        CacheDeserializedValues cacheDeserializedValues = mapContainer.getMapConfig().getCacheDeserializedValues();
        CachedQueryEntry<?, ?> cachedEntry = cacheDeserializedValues == NEVER ? new CachedQueryEntry<>(serializationService, mapContainer.getExtractors()) : null;
        recordStore.forEach((key, record) -> {
            Object value = Records.getValueOrCachedValue(record, serializationService);
            if (value != null) {
                QueryableEntry queryEntry = mapContainer.newQueryEntry(key, value);
                queryEntry.setRecord(record);
                CachedQueryEntry<?, ?> newEntry = cachedEntry == null ? (CachedQueryEntry<?, ?>) queryEntry : cachedEntry.init(key, value);
                indexes.putEntry(newEntry, null, queryEntry, Index.OperationSource.SYSTEM);
            }
        }, false);
        Indexes.markPartitionAsIndexed(event.getPartitionId(), indexesSnapshot);
    }
    if (logger.isFinestEnabled()) {
        logger.finest(String.format("Populated indexes at step `%s`:[%s]", stepName, event));
    }
}
Also used : InternalIndex(com.hazelcast.query.impl.InternalIndex) CacheDeserializedValues(com.hazelcast.config.CacheDeserializedValues) Record(com.hazelcast.map.impl.record.Record) Indexes(com.hazelcast.query.impl.Indexes) QueryableEntry(com.hazelcast.query.impl.QueryableEntry)

Aggregations

InternalIndex (com.hazelcast.query.impl.InternalIndex)22 IndexConfig (com.hazelcast.config.IndexConfig)11 HazelcastInstance (com.hazelcast.core.HazelcastInstance)9 Indexes (com.hazelcast.query.impl.Indexes)9 ExpressionEvalContext (com.hazelcast.sql.impl.expression.ExpressionEvalContext)7 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 Test (org.junit.Test)5 IndexRangeFilter (com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter)4 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 CacheDeserializedValues (com.hazelcast.config.CacheDeserializedValues)2 MapContainer (com.hazelcast.map.impl.MapContainer)2 Record (com.hazelcast.map.impl.record.Record)2 Predicate (com.hazelcast.query.Predicate)2 QueryableEntry (com.hazelcast.query.impl.QueryableEntry)2 IndexEqualsFilter (com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter)2 HazelcastInstanceProxy (com.hazelcast.instance.impl.HazelcastInstanceProxy)1 LocalRecordStoreStats (com.hazelcast.internal.monitor.LocalRecordStoreStats)1 OnDemandIndexStats (com.hazelcast.internal.monitor.impl.OnDemandIndexStats)1