Search in sources :

Example 1 with IndexingQueryFilter

use of org.apache.ignite.spi.indexing.IndexingQueryFilter in project ignite by apache.

the class GridCacheQueryManager method backupsFilter.

/**
     * @param <K> Key type.
     * @param <V> Value type.
     * @param includeBackups Include backups.
     * @return Predicate.
     */
@SuppressWarnings("unchecked")
@Nullable
public <K, V> IndexingQueryFilter backupsFilter(boolean includeBackups) {
    if (includeBackups)
        return null;
    return new IndexingQueryFilter() {

        @Nullable
        @Override
        public IgniteBiPredicate<K, V> forCache(final String cacheName) {
            final GridKernalContext ctx = cctx.kernalContext();
            final GridCacheAdapter<Object, Object> cache = ctx.cache().internalCache(cacheName);
            if (cache.context().isReplicated() || cache.configuration().getBackups() == 0)
                return null;
            return new IgniteBiPredicate<K, V>() {

                @Override
                public boolean apply(K k, V v) {
                    return cache.context().affinity().primaryByKey(ctx.discovery().localNode(), k, NONE);
                }
            };
        }

        @Override
        public boolean isValueRequired() {
            return false;
        }
    };
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with IndexingQueryFilter

use of org.apache.ignite.spi.indexing.IndexingQueryFilter in project ignite by apache.

the class GridH2TreeIndex method getRowCount.

/** {@inheritDoc} */
@Override
public long getRowCount(@Nullable Session ses) {
    IndexingQueryFilter f = threadLocalFilter();
    int seg = threadLocalSegment();
    // Fast path if we don't need to perform any filtering.
    if (f == null || f.forCache((getTable()).cacheName()) == null)
        try {
            return treeForRead(seg).size();
        } catch (IgniteCheckedException e) {
            throw DbException.convert(e);
        }
    GridCursor<GridH2Row> cursor = doFind(null, false, null);
    long size = 0;
    try {
        while (cursor.next()) size++;
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
    return size;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter)

Example 3 with IndexingQueryFilter

use of org.apache.ignite.spi.indexing.IndexingQueryFilter in project ignite by apache.

the class IgniteH2Indexing method doRunPrepared.

/**
 * Execute an all-ready {@link SqlFieldsQuery}.
 * @param schemaName Schema name.
 * @param prepared H2 command.
 * @param qry Fields query with flags.
 * @param twoStepQry Two-step query if this query must be executed in a distributed way.
 * @param meta Metadata for {@code twoStepQry}.
 * @param keepBinary Whether binary objects must not be deserialized automatically.
 * @param cancel Query cancel state holder.    @return Query result.
 */
private List<? extends FieldsQueryCursor<List<?>>> doRunPrepared(String schemaName, Prepared prepared, SqlFieldsQuery qry, GridCacheTwoStepQuery twoStepQry, List<GridQueryFieldMetadata> meta, boolean keepBinary, GridQueryCancel cancel) {
    String sqlQry = qry.getSql();
    boolean loc = qry.isLocal();
    IndexingQueryFilter filter = (loc ? backupFilter(null, qry.getPartitions()) : null);
    if (!prepared.isQuery()) {
        if (DmlStatementsProcessor.isDmlStatement(prepared)) {
            try {
                Connection conn = connectionForSchema(schemaName);
                if (!loc)
                    return dmlProc.updateSqlFieldsDistributed(schemaName, conn, prepared, qry, cancel);
                else {
                    final GridQueryFieldsResult updRes = dmlProc.updateSqlFieldsLocal(schemaName, conn, prepared, qry, filter, cancel);
                    return Collections.singletonList(new QueryCursorImpl<>(new Iterable<List<?>>() {

                        @Override
                        public Iterator<List<?>> iterator() {
                            try {
                                return new GridQueryCacheObjectsIterator(updRes.iterator(), objectContext(), true);
                            } catch (IgniteCheckedException e) {
                                throw new IgniteException(e);
                            }
                        }
                    }, cancel));
                }
            } catch (IgniteCheckedException e) {
                throw new IgniteSQLException("Failed to execute DML statement [stmt=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
            }
        }
        if (DdlStatementsProcessor.isDdlStatement(prepared)) {
            if (loc)
                throw new IgniteSQLException("DDL statements are not supported for LOCAL caches", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
            try {
                return Collections.singletonList(ddlProc.runDdlStatement(sqlQry, prepared));
            } catch (IgniteCheckedException e) {
                throw new IgniteSQLException("Failed to execute DDL statement [stmt=" + sqlQry + ']', e);
            }
        }
        if (prepared instanceof NoOperation) {
            QueryCursorImpl<List<?>> resCur = (QueryCursorImpl<List<?>>) new QueryCursorImpl(Collections.singletonList(Collections.singletonList(0L)), null, false);
            resCur.fieldsMeta(UPDATE_RESULT_META);
            return Collections.singletonList(resCur);
        }
        throw new IgniteSQLException("Unsupported DDL/DML operation: " + prepared.getClass().getName());
    }
    if (twoStepQry != null) {
        if (log.isDebugEnabled())
            log.debug("Parsed query: `" + sqlQry + "` into two step query: " + twoStepQry);
        checkQueryType(qry, true);
        return Collections.singletonList(doRunDistributedQuery(schemaName, qry, twoStepQry, meta, keepBinary, cancel));
    }
    // We've encountered a local query, let's just run it.
    try {
        return Collections.singletonList(queryLocalSqlFields(schemaName, qry, keepBinary, filter, cancel));
    } catch (IgniteCheckedException e) {
        throw new IgniteSQLException("Failed to execute local statement [stmt=" + sqlQry + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
    }
}
Also used : IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) Connection(java.sql.Connection) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) NoOperation(org.h2.command.dml.NoOperation) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with IndexingQueryFilter

use of org.apache.ignite.spi.indexing.IndexingQueryFilter in project ignite by apache.

the class H2PkHashIndex method find.

/**
 * {@inheritDoc}
 */
@Override
public Cursor find(Session ses, final SearchRow lower, final SearchRow upper) {
    IndexingQueryFilter f = threadLocalFilter();
    IndexingQueryCacheFilter p = null;
    if (f != null) {
        String cacheName = getTable().cacheName();
        p = f.forCache(cacheName);
    }
    KeyCacheObject lowerObj = null;
    KeyCacheObject upperObj = null;
    if (lower != null)
        lowerObj = cctx.toCacheKeyObject(lower.getValue(0).getObject());
    if (upper != null)
        upperObj = cctx.toCacheKeyObject(upper.getValue(0).getObject());
    try {
        List<GridCursor<? extends CacheDataRow>> cursors = new ArrayList<>();
        for (IgniteCacheOffheapManager.CacheDataStore store : cctx.offheap().cacheDataStores()) cursors.add(store.cursor(cctx.cacheId(), lowerObj, upperObj));
        return new H2Cursor(new CompositeGridCursor<>(cursors.iterator()), p);
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCursor(org.apache.ignite.internal.util.lang.GridCursor) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) ArrayList(java.util.ArrayList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) IndexingQueryCacheFilter(org.apache.ignite.spi.indexing.IndexingQueryCacheFilter) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 5 with IndexingQueryFilter

use of org.apache.ignite.spi.indexing.IndexingQueryFilter in project ignite by apache.

the class IgniteH2Indexing method backupFilter.

/** {@inheritDoc} */
@Override
public IndexingQueryFilter backupFilter(@Nullable final AffinityTopologyVersion topVer, @Nullable final int[] parts) {
    final AffinityTopologyVersion topVer0 = topVer != null ? topVer : AffinityTopologyVersion.NONE;
    return new IndexingQueryFilter() {

        @Nullable
        @Override
        public <K, V> IgniteBiPredicate<K, V> forCache(String cacheName) {
            final GridCacheAdapter<Object, Object> cache = ctx.cache().internalCache(cacheName);
            if (cache.context().isReplicated())
                return null;
            final GridCacheAffinityManager aff = cache.context().affinity();
            if (parts != null) {
                if (parts.length < 64) {
                    // Fast scan for small arrays.
                    return new IgniteBiPredicate<K, V>() {

                        @Override
                        public boolean apply(K k, V v) {
                            int p = aff.partition(k);
                            for (int p0 : parts) {
                                if (p0 == p)
                                    return true;
                                if (// Array is sorted.
                                p0 > p)
                                    return false;
                            }
                            return false;
                        }
                    };
                }
                return new IgniteBiPredicate<K, V>() {

                    @Override
                    public boolean apply(K k, V v) {
                        int p = aff.partition(k);
                        return Arrays.binarySearch(parts, p) >= 0;
                    }
                };
            }
            final ClusterNode locNode = ctx.discovery().localNode();
            return new IgniteBiPredicate<K, V>() {

                @Override
                public boolean apply(K k, V v) {
                    return aff.primaryByKey(locNode, k, topVer0);
                }
            };
        }

        @Override
        public boolean isValueRequired() {
            return false;
        }

        @Override
        public String toString() {
            return "IndexingQueryFilter [ver=" + topVer + ']';
        }
    };
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheAffinityManager(org.apache.ignite.internal.processors.cache.GridCacheAffinityManager) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString)

Aggregations

IndexingQueryFilter (org.apache.ignite.spi.indexing.IndexingQueryFilter)8 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 ArrayList (java.util.ArrayList)4 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)3 CacheException (javax.cache.CacheException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)2 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)2 IndexingQueryCacheFilter (org.apache.ignite.spi.indexing.IndexingQueryCacheFilter)2 Connection (java.sql.Connection)1 List (java.util.List)1 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)1 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)1 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 GridCacheAffinityManager (org.apache.ignite.internal.processors.cache.GridCacheAffinityManager)1 IgniteCacheOffheapManager (org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager)1