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;
}
};
}
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;
}
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);
}
}
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);
}
}
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 + ']';
}
};
}
Aggregations