IGNITE-10253: SQL: Merged SqlQuery execution logic with SqlFieldsQuery. Now the first one is simply an additional converter on top of the second. This closes #5395.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/c4ec543a Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/c4ec543a Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/c4ec543a Branch: refs/heads/ignite-10043 Commit: c4ec543a8b0da9c5d9a2e25739ac31fd68d13a5d Parents: a802074 Author: devozerov <ppoze...@gmail.com> Authored: Thu Nov 15 22:40:22 2018 +0300 Committer: devozerov <ppoze...@gmail.com> Committed: Thu Nov 15 22:40:22 2018 +0300 ---------------------------------------------------------------------- .../processors/query/GridQueryIndexing.java | 23 +- .../processors/query/GridQueryProcessor.java | 275 ++++---- .../processors/query/QueryKeyValueIterable.java | 53 ++ .../processors/query/QueryKeyValueIterator.java | 68 ++ ...IgniteClientCacheInitializationFailTest.java | 19 +- .../processors/query/h2/IgniteH2Indexing.java | 285 ++------- ...CacheAbstractQueryDetailMetricsSelfTest.java | 15 +- .../CacheAbstractQueryMetricsSelfTest.java | 13 - ...niteCacheLockPartitionOnAffinityRunTest.java | 2 + .../h2/GridIndexingSpiAbstractSelfTest.java | 639 +------------------ ...H2ResultSetIteratorNullifyOnEndSelfTest.java | 186 ------ 11 files changed, 337 insertions(+), 1241 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java index 60b0221..3eb732c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryIndexing.java @@ -21,11 +21,9 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Collection; import java.util.List; -import javax.cache.Cache; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteDataStreamer; import org.apache.ignite.cache.query.FieldsQueryCursor; -import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.internal.GridKernalContext; @@ -70,16 +68,13 @@ public interface GridQueryIndexing { public void onClientDisconnect() throws IgniteCheckedException; /** - * Parses SQL query into two step query and executes it. + * Generate SqlFieldsQuery from SqlQuery. * - * @param schemaName Schema name. * @param cacheName Cache name. * @param qry Query. - * @param keepBinary Keep binary flag. - * @throws IgniteCheckedException If failed. + * @return Fields query. */ - public <K, V> QueryCursor<Cache.Entry<K, V>> queryDistributedSql(String schemaName, String cacheName, SqlQuery qry, - boolean keepBinary) throws IgniteCheckedException; + public SqlFieldsQuery generateFieldsQuery(String cacheName, SqlQuery qry); /** * Detect whether SQL query should be executed in distributed or local manner and execute it. @@ -121,18 +116,6 @@ public interface GridQueryIndexing { SqlClientContext cliCtx) throws IgniteCheckedException; /** - * Executes regular query. - * - * @param schemaName Schema name. - * @param cacheName Cache name. - * @param qry Query. - * @param filter Cache name and key filter. - * @param keepBinary Keep binary flag. @return Cursor. - */ - public <K, V> QueryCursor<Cache.Entry<K,V>> queryLocalSql(String schemaName, String cacheName, SqlQuery qry, - IndexingQueryFilter filter, boolean keepBinary) throws IgniteCheckedException; - - /** * Queries individual fields (generally used by JDBC drivers). * * @param schemaName Schema name. http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java index 957d3ae..30c28b1 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java @@ -66,6 +66,7 @@ import org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor; import org.apache.ignite.internal.processors.cache.GridCacheAdapter; import org.apache.ignite.internal.processors.cache.GridCacheContext; import org.apache.ignite.internal.processors.cache.KeyCacheObject; +import org.apache.ignite.internal.processors.cache.QueryCursorImpl; import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot; import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow; import org.apache.ignite.internal.processors.cache.query.CacheQueryFuture; @@ -2078,7 +2079,13 @@ public class GridQueryProcessor extends GridProcessorAdapter { */ public List<FieldsQueryCursor<List<?>>> querySqlFields(final SqlFieldsQuery qry, final boolean keepBinary, final boolean failOnMultipleStmts) { - return querySqlFields(null, qry, null, keepBinary, failOnMultipleStmts); + return querySqlFields( + null, + qry, + null, + keepBinary, + failOnMultipleStmts + ); } /** @@ -2089,7 +2096,13 @@ public class GridQueryProcessor extends GridProcessorAdapter { * @return Cursor. */ public FieldsQueryCursor<List<?>> querySqlFields(final SqlFieldsQuery qry, final boolean keepBinary) { - return querySqlFields(null, qry, null, keepBinary, true).get(0); + return querySqlFields( + null, + qry, + null, + keepBinary, + true + ).get(0); } /** @@ -2103,40 +2116,92 @@ public class GridQueryProcessor extends GridProcessorAdapter { * more then one SQL statement. * @return Cursor. */ - public List<FieldsQueryCursor<List<?>>> querySqlFields(@Nullable final GridCacheContext<?, ?> cctx, - final SqlFieldsQuery qry, final SqlClientContext cliCtx, final boolean keepBinary, - final boolean failOnMultipleStmts) { - checkxEnabled(); - - validateSqlFieldsQuery(qry, ctx, cctx); + public List<FieldsQueryCursor<List<?>>> querySqlFields( + @Nullable final GridCacheContext<?, ?> cctx, + final SqlFieldsQuery qry, + final SqlClientContext cliCtx, + final boolean keepBinary, + final boolean failOnMultipleStmts + ) { + return querySqlFields( + cctx, + qry, + cliCtx, + keepBinary, + failOnMultipleStmts, + GridCacheQueryType.SQL_FIELDS + ); + } - if (!busyLock.enterBusy()) - throw new IllegalStateException("Failed to execute query (grid is stopping)."); + /** + * Query SQL fields. + * + * @param cctx Cache context. + * @param qry Query. + * @param cliCtx Client context. + * @param keepBinary Keep binary flag. + * @param failOnMultipleStmts If {@code true} the method must throws exception when query contains + * more then one SQL statement. + * @param qryType Real query type. + * @return Cursor. + */ + public List<FieldsQueryCursor<List<?>>> querySqlFields( + @Nullable final GridCacheContext<?, ?> cctx, + final SqlFieldsQuery qry, + final SqlClientContext cliCtx, + final boolean keepBinary, + final boolean failOnMultipleStmts, + GridCacheQueryType qryType + ) { + // Validate. + checkxEnabled(); - GridCacheContext oldCctx = curCache.get(); + if (qry.isDistributedJoins() && qry.getPartitions() != null) + throw new CacheException("Using both partitions and distributed JOINs is not supported for the same query"); - curCache.set(cctx); + if (qry.isLocal() && ctx.clientNode() && (cctx == null || cctx.config().getCacheMode() != CacheMode.LOCAL)) + throw new CacheException("Execution of local SqlFieldsQuery on client node disallowed."); - final String schemaName = qry.getSchema() != null ? qry.getSchema() - : (cctx != null ? idx.schema(cctx.name()) : QueryUtils.DFLT_SCHEMA); + return executeQuerySafe(cctx, () -> { + final String schemaName = qry.getSchema() != null ? qry.getSchema() + : (cctx != null ? idx.schema(cctx.name()) : QueryUtils.DFLT_SCHEMA); - try { IgniteOutClosureX<List<FieldsQueryCursor<List<?>>>> clo = new IgniteOutClosureX<List<FieldsQueryCursor<List<?>>>>() { - @Override public List<FieldsQueryCursor<List<?>>> applyx() throws IgniteCheckedException { - GridQueryCancel cancel = new GridQueryCancel(); + @Override public List<FieldsQueryCursor<List<?>>> applyx() { + GridQueryCancel cancel = new GridQueryCancel(); - List<FieldsQueryCursor<List<?>>> res = - idx.querySqlFields(schemaName, qry, cliCtx, keepBinary, failOnMultipleStmts, null, cancel); + List<FieldsQueryCursor<List<?>>> res = + idx.querySqlFields(schemaName, qry, cliCtx, keepBinary, failOnMultipleStmts, null, cancel); - if (cctx != null) - sendQueryExecutedEvent(qry.getSql(), qry.getArgs(), cctx); + if (cctx != null) + sendQueryExecutedEvent(qry.getSql(), qry.getArgs(), cctx); - return res; - } - }; + return res; + } + }; + + return executeQuery(qryType, qry.getSql(), cctx, clo, true); + }); + } + + /** + * Execute query setting busy lock, preserving current cache context and properly handling checked exceptions. + * + * @param cctx Cache context. + * @param supplier Code to be executed. + * @return Result. + */ + private <T> T executeQuerySafe(@Nullable final GridCacheContext<?, ?> cctx, SupplierX<T> supplier) { + GridCacheContext oldCctx = curCache.get(); + + curCache.set(cctx); + + if (!busyLock.enterBusy()) + throw new IllegalStateException("Failed to execute query (grid is stopping)."); - return executeQuery(GridCacheQueryType.SQL_FIELDS, qry.getSql(), cctx, clo, true); + try { + return supplier.get(); } catch (IgniteCheckedException e) { throw new CacheException(e); @@ -2149,34 +2214,6 @@ public class GridQueryProcessor extends GridProcessorAdapter { } /** - * Validate SQL fields query. - * - * @param qry Query. - * @param ctx Kernal context. - * @param cctx Cache context. - */ - private static void validateSqlFieldsQuery(SqlFieldsQuery qry, GridKernalContext ctx, - @Nullable GridCacheContext<?, ?> cctx) { - if (qry.isDistributedJoins() && qry.getPartitions() != null) - throw new CacheException("Using both partitions and distributed JOINs is not supported for the same query"); - - if (qry.isLocal() && ctx.clientNode() && (cctx == null || cctx.config().getCacheMode() != CacheMode.LOCAL)) - throw new CacheException("Execution of local SqlFieldsQuery on client node disallowed."); - } - - /** - * Validate SQL query. - * - * @param qry Query. - * @param ctx Kernal context. - * @param cctx Cache context. - */ - private static void validateSqlQuery(SqlQuery qry, GridKernalContext ctx, GridCacheContext<?, ?> cctx) { - if (qry.isLocal() && ctx.clientNode() && cctx.config().getCacheMode() != CacheMode.LOCAL) - throw new CacheException("Execution of local SqlQuery on client node disallowed."); - } - - /** * @param cacheName Cache name. * @param schemaName Schema name. * @param streamer Data streamer. @@ -2242,99 +2279,38 @@ public class GridQueryProcessor extends GridProcessorAdapter { * @param keepBinary Keep binary flag. * @return Cursor. */ - public <K, V> QueryCursor<Cache.Entry<K,V>> querySql(final GridCacheContext<?,?> cctx, final SqlQuery qry, - boolean keepBinary) { - validateSqlQuery(qry, ctx, cctx); + public <K, V> QueryCursor<Cache.Entry<K,V>> querySql( + final GridCacheContext<?,?> cctx, + final SqlQuery qry, + boolean keepBinary + ) { + // Generate. + String type = qry.getType(); - if (qry.isReplicatedOnly() && qry.getPartitions() != null) - throw new CacheException("Partitions are not supported in replicated only mode."); + String typeName = typeName(cctx.name(), type); - if (qry.isDistributedJoins() && qry.getPartitions() != null) - throw new CacheException( - "Using both partitions and distributed JOINs is not supported for the same query"); - - if ((qry.isReplicatedOnly() && cctx.isReplicatedAffinityNode()) || cctx.isLocal() || qry.isLocal()) - return queryLocalSql(cctx, qry, keepBinary); - - return queryDistributedSql(cctx, qry, keepBinary); - } + qry.setType(typeName); - /** - * @param cctx Cache context. - * @param qry Query. - * @param keepBinary Keep binary flag. - * @return Cursor. - */ - private <K,V> QueryCursor<Cache.Entry<K,V>> queryDistributedSql(final GridCacheContext<?,?> cctx, - final SqlQuery qry, final boolean keepBinary) { - checkxEnabled(); + SqlFieldsQuery fieldsQry = idx.generateFieldsQuery(cctx.name(), qry); - if (!busyLock.enterBusy()) - throw new IllegalStateException("Failed to execute query (grid is stopping)."); + // Execute. + FieldsQueryCursor<List<?>> res = querySqlFields( + cctx, + fieldsQry, + null, + keepBinary, + true, + GridCacheQueryType.SQL + ).get(0); - try { - final String schemaName = idx.schema(cctx.name()); + // Convert. + QueryKeyValueIterable<K, V>converted = new QueryKeyValueIterable<>(res); - return executeQuery(GridCacheQueryType.SQL, qry.getSql(), cctx, - new IgniteOutClosureX<QueryCursor<Cache.Entry<K, V>>>() { - @Override public QueryCursor<Cache.Entry<K, V>> applyx() throws IgniteCheckedException { - return idx.queryDistributedSql(schemaName, cctx.name(), qry, keepBinary); - } - }, true); - } - catch (IgniteCheckedException e) { - throw new IgniteException(e); - } - finally { - busyLock.leaveBusy(); - } - } - - /** - * @param cctx Cache context. - * @param qry Query. - * @param keepBinary Keep binary flag. - * @return Cursor. - */ - private <K, V> QueryCursor<Cache.Entry<K, V>> queryLocalSql(final GridCacheContext<?, ?> cctx, final SqlQuery qry, - final boolean keepBinary) { - if (!busyLock.enterBusy()) - throw new IllegalStateException("Failed to execute query (grid is stopping)."); - - final String schemaName = idx.schema(cctx.name()); - - try { - return executeQuery(GridCacheQueryType.SQL, qry.getSql(), cctx, - new IgniteOutClosureX<QueryCursor<Cache.Entry<K, V>>>() { - @Override public QueryCursor<Cache.Entry<K, V>> applyx() throws IgniteCheckedException { - String type = qry.getType(); - - String typeName = typeName(cctx.name(), type); - - qry.setType(typeName); - - sendQueryExecutedEvent( - qry.getSql(), - qry.getArgs(), - cctx); - - if (cctx.config().getQueryParallelism() > 1) { - qry.setDistributedJoins(true); - - return idx.queryDistributedSql(schemaName, cctx.name(), qry, keepBinary); - } - else - return idx.queryLocalSql(schemaName, cctx.name(), qry, idx.backupFilter(requestTopVer.get(), - qry.getPartitions()), keepBinary); - } - }, true); - } - catch (IgniteCheckedException e) { - throw new CacheException(e); - } - finally { - busyLock.leaveBusy(); - } + return new QueryCursorImpl<Cache.Entry<K, V>>(converted) { + @Override public void close() { + converted.cursor().close(); + } + }; } /** @@ -2667,13 +2643,12 @@ public class GridQueryProcessor extends GridProcessorAdapter { * @param cacheName Cache name. * @param typeName Type name. * @return Type descriptor. - * @throws IgniteCheckedException If failed. */ - private String typeName(@Nullable String cacheName, String typeName) throws IgniteCheckedException { + private String typeName(@Nullable String cacheName, String typeName) throws IgniteException { QueryTypeDescriptorImpl type = typesByName.get(new QueryTypeNameKey(cacheName, typeName)); if (type == null) - throw new IgniteCheckedException("Failed to find SQL table for type: " + typeName); + throw new IgniteException("Failed to find SQL table for type: " + typeName); return type.name(); } @@ -3144,4 +3119,18 @@ public class GridQueryProcessor extends GridProcessorAdapter { return S.toString(TableCacheFilter.class, this); } } + + /** + * Function which can throw exception. + */ + @FunctionalInterface + private interface SupplierX<T> { + /** + * Get value. + * + * @return Value. + * @throws IgniteCheckedException If failed. + */ + T get() throws IgniteCheckedException; + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterable.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterable.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterable.java new file mode 100644 index 0000000..41d5145 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterable.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.query; + +import org.apache.ignite.cache.query.QueryCursor; + +import javax.cache.Cache; +import java.util.Iterator; +import java.util.List; + +/** + * SqlQuery key-value iterable. + */ +public class QueryKeyValueIterable<K, V> implements Iterable<Cache.Entry<K, V>> { + /** Underlying fields query cursor. */ + private final QueryCursor<List<?>> cur; + + /** + * Constructor. + * + * @param cur Underlying fields query cursor. + */ + public QueryKeyValueIterable(QueryCursor<List<?>> cur) { + this.cur = cur; + } + + /** {@inheritDoc} */ + @Override public Iterator<Cache.Entry<K, V>> iterator() { + return new QueryKeyValueIterator<>(cur.iterator()); + } + + /** + * @return Underlying fields query cursor. + */ + QueryCursor<List<?>> cursor() { + return cur; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterator.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterator.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterator.java new file mode 100644 index 0000000..02dde9d --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryKeyValueIterator.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.query; + +import org.apache.ignite.internal.processors.cache.CacheEntryImpl; + +import javax.cache.Cache; +import javax.cache.CacheException; +import java.util.Iterator; +import java.util.List; + +/** + * SqlQuery key-value iterator. + */ +public class QueryKeyValueIterator<K, V> implements Iterator<Cache.Entry<K, V>> { + /** Target iterator. */ + private final Iterator<List<?>> iter; + + /** + * Constructor. + * + * @param iter Target iterator. + */ + public QueryKeyValueIterator(Iterator<List<?>> iter) { + this.iter = iter; + } + + /** {@inheritDoc} */ + @Override public boolean hasNext() { + return iter.hasNext(); + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override public Cache.Entry<K, V> next() { + try { + List<?> row = iter.next(); + + return new CacheEntryImpl<>((K)row.get(0), (V)row.get(1)); + } + catch (CacheException e) { + throw e; + } + catch (Exception e) { + throw new CacheException(e); + } + } + + /** {@inheritDoc} */ + @Override public void remove() { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteClientCacheInitializationFailTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteClientCacheInitializationFailTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteClientCacheInitializationFailTest.java index 69b1655..ba2fec6 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteClientCacheInitializationFailTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteClientCacheInitializationFailTest.java @@ -26,7 +26,6 @@ import java.util.List; import java.util.Random; import java.util.Set; import java.util.concurrent.Callable; -import javax.cache.Cache; import javax.cache.CacheException; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; @@ -34,7 +33,6 @@ import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteDataStreamer; import org.apache.ignite.cache.CacheAtomicityMode; import org.apache.ignite.cache.query.FieldsQueryCursor; -import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.configuration.CacheConfiguration; @@ -231,19 +229,18 @@ public class IgniteClientCacheInitializationFailTest extends GridCommonAbstractT } /** {@inheritDoc} */ - @Override public void start(GridKernalContext ctx, GridSpinBusyLock busyLock) throws IgniteCheckedException { - // No-op + @Override public SqlFieldsQuery generateFieldsQuery(String cacheName, SqlQuery qry) { + return null; } /** {@inheritDoc} */ - @Override public void stop() throws IgniteCheckedException { + @Override public void start(GridKernalContext ctx, GridSpinBusyLock busyLock) throws IgniteCheckedException { // No-op } /** {@inheritDoc} */ - @Override public <K, V> QueryCursor<Cache.Entry<K, V>> queryDistributedSql(String schemaName, String cacheName, - SqlQuery qry, boolean keepBinary) throws IgniteCheckedException { - return null; + @Override public void stop() throws IgniteCheckedException { + // No-op } /** {@inheritDoc} */ @@ -265,12 +262,6 @@ public class IgniteClientCacheInitializationFailTest extends GridCommonAbstractT } /** {@inheritDoc} */ - @Override public <K, V> QueryCursor<Cache.Entry<K, V>> queryLocalSql(String schemaName, String cacheName, - SqlQuery qry, IndexingQueryFilter filter, boolean keepBinary) throws IgniteCheckedException { - return null; - } - - /** {@inheritDoc} */ @Override public FieldsQueryCursor<List<?>> queryLocalSqlFields(String schemaName, SqlFieldsQuery qry, boolean keepBinary, IndexingQueryFilter filter, GridQueryCancel cancel) throws IgniteCheckedException { return null; http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java index e2dba50..58e09cb 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java @@ -44,7 +44,6 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.regex.Pattern; -import javax.cache.Cache; import javax.cache.CacheException; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteDataStreamer; @@ -53,7 +52,6 @@ import org.apache.ignite.IgniteLogger; import org.apache.ignite.IgniteSystemProperties; import org.apache.ignite.cache.query.FieldsQueryCursor; import org.apache.ignite.cache.query.QueryCancelledException; -import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.cache.query.annotations.QuerySqlFunction; @@ -62,8 +60,6 @@ import org.apache.ignite.internal.GridKernalContext; import org.apache.ignite.internal.GridTopic; import org.apache.ignite.internal.IgniteInternalFuture; import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion; -import org.apache.ignite.internal.processors.cache.CacheEntryImpl; -import org.apache.ignite.internal.processors.cache.CacheObjectUtils; import org.apache.ignite.internal.processors.cache.CacheObjectValueContext; import org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor; import org.apache.ignite.internal.processors.cache.GridCacheContext; @@ -209,7 +205,6 @@ import static org.apache.ignite.internal.processors.cache.mvcc.MvccUtils.checkAc import static org.apache.ignite.internal.processors.cache.mvcc.MvccUtils.mvccEnabled; import static org.apache.ignite.internal.processors.cache.mvcc.MvccUtils.tx; import static org.apache.ignite.internal.processors.cache.mvcc.MvccUtils.txStart; -import static org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SQL; import static org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SQL_FIELDS; import static org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.TEXT; import static org.apache.ignite.internal.processors.query.QueryUtils.KEY_FIELD_NAME; @@ -1568,111 +1563,6 @@ public class IgniteH2Indexing implements GridQueryIndexing { return cursor; } - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public <K, V> QueryCursor<Cache.Entry<K,V>> queryLocalSql(String schemaName, String cacheName, - final SqlQuery qry, final IndexingQueryFilter filter, final boolean keepBinary) throws IgniteCheckedException { - String type = qry.getType(); - String sqlQry = qry.getSql(); - String alias = qry.getAlias(); - Object[] params = qry.getArgs(); - - GridQueryCancel cancel = new GridQueryCancel(); - - final GridCloseableIterator<IgniteBiTuple<K, V>> i = queryLocalSql(schemaName, cacheName, sqlQry, alias, - F.asList(params), type, filter, cancel); - - return new QueryCursorImpl<>(new Iterable<Cache.Entry<K, V>>() { - @SuppressWarnings("NullableProblems") - @Override public Iterator<Cache.Entry<K, V>> iterator() { - return new ClIter<Cache.Entry<K, V>>() { - @Override public void close() throws Exception { - i.close(); - } - - @Override public boolean hasNext() { - return i.hasNext(); - } - - @Override public Cache.Entry<K, V> next() { - IgniteBiTuple<K, V> t = i.next(); - - K key = (K)CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get1(), keepBinary, false); - V val = (V)CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get2(), keepBinary, false); - - return new CacheEntryImpl<>(key, val); - } - - @Override public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - }, cancel); - } - - /** - * Executes regular query. - * - * @param schemaName Schema name. - * @param cacheName Cache name. - * @param qry Query. - * @param alias Table alias. - * @param params Query parameters. - * @param type Query return type. - * @param filter Cache name and key filter. - * @param cancel Cancel object. - * @return Queried rows. - * @throws IgniteCheckedException If failed. - */ - @SuppressWarnings("unchecked") - <K, V> GridCloseableIterator<IgniteBiTuple<K, V>> queryLocalSql(String schemaName, String cacheName, - final String qry, String alias, @Nullable final Collection<Object> params, String type, - final IndexingQueryFilter filter, GridQueryCancel cancel) throws IgniteCheckedException { - final H2TableDescriptor tbl = tableDescriptor(schemaName, cacheName, type); - - if (tbl == null) - throw new IgniteSQLException("Failed to find SQL table for type: " + type, - IgniteQueryErrorCode.TABLE_NOT_FOUND); - - String sql = generateQuery(qry, alias, tbl); - - Connection conn = connectionForThread(tbl.schemaName()); - - H2Utils.setupConnection(conn, false, false); - - GridH2QueryContext qctx = new GridH2QueryContext(nodeId, nodeId, 0, LOCAL).filter(filter) - .distributedJoinMode(OFF); - - PreparedStatement stmt = preparedStatementWithParams(conn, sql, params, true); - - MvccQueryTracker mvccTracker = mvccTracker(stmt, false); - - if (mvccTracker != null) - qctx.mvccSnapshot(mvccTracker.snapshot()); - - GridH2QueryContext.set(qctx); - - GridRunningQueryInfo run = new GridRunningQueryInfo(qryIdGen.incrementAndGet(), qry, SQL, schemaName, - U.currentTimeMillis(), null, true); - - runs.put(run.id(), run); - - try { - ResultSet rs = executeSqlQueryWithTimer(stmt, conn, sql, params, 0, cancel); - - return new H2KeyValueIterator(rs); - } - finally { - GridH2QueryContext.clearThreadLocal(); - - if (mvccTracker != null) - mvccTracker.onDone(); - - runs.remove(run.id()); - } - } - /** * Initialises MVCC filter and returns MVCC query tracker if needed. * @param stmt Prepared statement. @@ -1754,8 +1644,6 @@ public class IgniteH2Indexing implements GridQueryIndexing { PreparedStatementEx stmtEx = stmt.unwrap(PreparedStatementEx.class); if (mvccEnabled) { - assert mvccCacheId != null; - stmtEx.putMeta(MVCC_CACHE_ID, mvccCacheId); stmtEx.putMeta(MVCC_STATE, Boolean.TRUE); } @@ -1840,9 +1728,9 @@ public class IgniteH2Indexing implements GridQueryIndexing { } /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public <K, V> QueryCursor<Cache.Entry<K, V>> queryDistributedSql(String schemaName, String cacheName, - SqlQuery qry, boolean keepBinary) { + @Override public SqlFieldsQuery generateFieldsQuery(String cacheName, SqlQuery qry) { + String schemaName = schema(cacheName); + String type = qry.getType(); H2TableDescriptor tblDesc = tableDescriptor(schemaName, cacheName, type); @@ -1860,58 +1748,80 @@ public class IgniteH2Indexing implements GridQueryIndexing { throw new IgniteException(e); } - SqlFieldsQuery fqry = new SqlFieldsQuery(sql); + SqlFieldsQuery res = new SqlFieldsQuery(sql); - fqry.setArgs(qry.getArgs()); - fqry.setPageSize(qry.getPageSize()); - fqry.setDistributedJoins(qry.isDistributedJoins()); - fqry.setPartitions(qry.getPartitions()); - fqry.setLocal(qry.isLocal()); + res.setArgs(qry.getArgs()); + res.setDistributedJoins(qry.isDistributedJoins()); + res.setLocal(qry.isLocal()); + res.setPageSize(qry.getPageSize()); + res.setPartitions(qry.getPartitions()); + res.setReplicatedOnly(qry.isReplicatedOnly()); + res.setSchema(schemaName); + res.setSql(sql); if (qry.getTimeout() > 0) - fqry.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS); + res.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS); + + return res; + } - final QueryCursor<List<?>> res = - querySqlFields(schemaName, fqry, null, keepBinary, true, null, null).get(0); + /** + * Prepares statement for query. + * + * @param qry Query string. + * @param tableAlias table alias. + * @param tbl Table to use. + * @return Prepared statement. + * @throws IgniteCheckedException In case of error. + */ + private static String generateQuery(String qry, String tableAlias, H2TableDescriptor tbl) + throws IgniteCheckedException { + assert tbl != null; - final Iterable<Cache.Entry<K, V>> converted = new Iterable<Cache.Entry<K, V>>() { - @Override public Iterator<Cache.Entry<K, V>> iterator() { - final Iterator<List<?>> iter0 = res.iterator(); + final String qry0 = qry; - return new Iterator<Cache.Entry<K, V>>() { - @Override public boolean hasNext() { - return iter0.hasNext(); - } + String t = tbl.fullTableName(); - @Override public Cache.Entry<K, V> next() { - List<?> l; + String from = " "; - try { - l = iter0.next(); - } - catch (CacheException e) { - throw e; - } - catch (Exception e) { - throw new CacheException(e); - } + qry = qry.trim(); - return new CacheEntryImpl<>((K)l.get(0), (V)l.get(1)); - } + String upper = qry.toUpperCase(); - @Override public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - }; + if (upper.startsWith("SELECT")) { + qry = qry.substring(6).trim(); - // No metadata for SQL queries. - return new QueryCursorImpl<Cache.Entry<K, V>>(converted) { - @Override public void close() { - res.close(); + final int star = qry.indexOf('*'); + + if (star == 0) + qry = qry.substring(1).trim(); + else if (star > 0) { + if (F.eq('.', qry.charAt(star - 1))) { + t = qry.substring(0, star - 1); + + qry = qry.substring(star + 1).trim(); + } + else + throw new IgniteCheckedException("Invalid query (missing alias before asterisk): " + qry0); } - }; + else + throw new IgniteCheckedException("Only queries starting with 'SELECT *' and 'SELECT alias.*' " + + "are supported (rewrite your query or use SqlFieldsQuery instead): " + qry0); + + upper = qry.toUpperCase(); + } + + if (!upper.startsWith("FROM")) + from = " FROM " + t + (tableAlias != null ? " as " + tableAlias : "") + + (upper.startsWith("WHERE") || upper.startsWith("ORDER") || upper.startsWith("LIMIT") ? + " " : " WHERE "); + + if(tableAlias != null) + t = tableAlias; + + qry = "SELECT " + t + "." + KEY_FIELD_NAME + ", " + t + "." + VAL_FIELD_NAME + from + qry; + + return qry; } /** @@ -2774,64 +2684,6 @@ public class IgniteH2Indexing implements GridQueryIndexing { } /** - * Prepares statement for query. - * - * @param qry Query string. - * @param tableAlias table alias. - * @param tbl Table to use. - * @return Prepared statement. - * @throws IgniteCheckedException In case of error. - */ - private String generateQuery(String qry, String tableAlias, H2TableDescriptor tbl) throws IgniteCheckedException { - assert tbl != null; - - final String qry0 = qry; - - String t = tbl.fullTableName(); - - String from = " "; - - qry = qry.trim(); - - String upper = qry.toUpperCase(); - - if (upper.startsWith("SELECT")) { - qry = qry.substring(6).trim(); - - final int star = qry.indexOf('*'); - - if (star == 0) - qry = qry.substring(1).trim(); - else if (star > 0) { - if (F.eq('.', qry.charAt(star - 1))) { - t = qry.substring(0, star - 1); - - qry = qry.substring(star + 1).trim(); - } - else - throw new IgniteCheckedException("Invalid query (missing alias before asterisk): " + qry0); - } - else - throw new IgniteCheckedException("Only queries starting with 'SELECT *' and 'SELECT alias.*' " + - "are supported (rewrite your query or use SqlFieldsQuery instead): " + qry0); - - upper = qry.toUpperCase(); - } - - if (!upper.startsWith("FROM")) - from = " FROM " + t + (tableAlias != null ? " as " + tableAlias : "") + - (upper.startsWith("WHERE") || upper.startsWith("ORDER") || upper.startsWith("LIMIT") ? - " " : " WHERE "); - - if(tableAlias != null) - t = tableAlias; - - qry = "SELECT " + t + "." + KEY_FIELD_NAME + ", " + t + "." + VAL_FIELD_NAME + from + qry; - - return qry; - } - - /** * Registers new class description. * * This implementation doesn't support type reregistration. @@ -3842,11 +3694,4 @@ public class IgniteH2Indexing implements GridQueryIndexing { return false; } - - /** - * Closeable iterator. - */ - private interface ClIter<X> extends AutoCloseable, Iterator<X> { - // No-op. - } } http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryDetailMetricsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryDetailMetricsSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryDetailMetricsSelfTest.java index 837de65..e1d7154 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryDetailMetricsSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryDetailMetricsSelfTest.java @@ -186,7 +186,7 @@ public abstract class CacheAbstractQueryDetailMetricsSelfTest extends GridCommon assertTrue(lastMetrics.contains("SQL_FIELDS select * from String limit 2;")); assertTrue(lastMetrics.contains("SCAN A;")); - assertTrue(lastMetrics.contains("SQL from String;")); + assertTrue(lastMetrics.contains("SELECT \"A\".\"STRING\"._KEY, \"A\".\"STRING\"._VAL from String;")); cache = grid(0).context().cache().jcache("B"); @@ -349,19 +349,6 @@ public abstract class CacheAbstractQueryDetailMetricsSelfTest extends GridCommon } /** - * Test metrics for failed Scan queries. - * - * @throws Exception In case of error. - */ - public void testSqlQueryFailedMetrics() throws Exception { - IgniteCache<Integer, String> cache = grid(0).context().cache().jcache("A"); - - SqlQuery<Integer, String> qry = new SqlQuery<>("Long", "from Long"); - - checkQueryFailedMetrics(cache, qry); - } - - /** * Test metrics for Sql queries. * * @throws Exception In case of error. http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryMetricsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryMetricsSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryMetricsSelfTest.java index eb3c8d6..e1c796b 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryMetricsSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/CacheAbstractQueryMetricsSelfTest.java @@ -209,19 +209,6 @@ public abstract class CacheAbstractQueryMetricsSelfTest extends GridCommonAbstra } /** - * Test metrics for failed Scan queries. - * - * @throws Exception In case of error. - */ - public void testSqlQueryFailedMetrics() throws Exception { - IgniteCache<Integer, String> cache = grid(0).context().cache().jcache("A"); - - SqlQuery qry = new SqlQuery<>("Long", "from Long"); - - checkQueryFailedMetrics(cache, qry); - } - - /** * Test metrics for Sql queries. * * @throws Exception In case of error. http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheLockPartitionOnAffinityRunTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheLockPartitionOnAffinityRunTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheLockPartitionOnAffinityRunTest.java index 89ef607..d0cfbd9 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheLockPartitionOnAffinityRunTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheLockPartitionOnAffinityRunTest.java @@ -291,6 +291,8 @@ public class IgniteCacheLockPartitionOnAffinityRunTest extends IgniteCacheLockPa * @throws Exception If failed. */ public void testSingleCache() throws Exception { + fail("https://issues.apache.org/jira/browse/IGNITE-7692"); + final PersonsCountGetter personsCntGetter = new PersonsCountGetter() { @Override public int getPersonsCount(IgniteEx ignite, IgniteLogger log, int orgId) throws Exception { return getPersonsCountSingleCache(ignite, log, orgId); http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java index 6b76230..7e198f8 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java @@ -17,20 +17,7 @@ package org.apache.ignite.internal.processors.query.h2; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteLogger; -import org.apache.ignite.binary.BinaryObject; -import org.apache.ignite.binary.BinaryObjectBuilder; import org.apache.ignite.cache.QueryEntity; import org.apache.ignite.cache.QueryIndex; import org.apache.ignite.cache.QueryIndexType; @@ -38,48 +25,33 @@ import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.internal.IgniteEx; import org.apache.ignite.internal.binary.BinaryMarshaller; -import org.apache.ignite.internal.binary.BinaryObjectImpl; -import org.apache.ignite.internal.processors.cache.CacheObject; -import org.apache.ignite.internal.processors.cache.CacheObjectContext; -import org.apache.ignite.internal.processors.cache.CacheObjectValueContext; -import org.apache.ignite.internal.processors.cache.KeyCacheObject; import org.apache.ignite.internal.processors.query.GridQueryFieldsResult; -import org.apache.ignite.internal.processors.query.GridQueryIndexDescriptor; -import org.apache.ignite.internal.processors.query.GridQueryProperty; -import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor; -import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.internal.U; -import org.apache.ignite.lang.IgniteBiTuple; -import org.apache.ignite.plugin.extensions.communication.MessageReader; -import org.apache.ignite.plugin.extensions.communication.MessageWriter; -import org.apache.ignite.spi.IgniteSpiCloseableIterator; -import org.apache.ignite.spi.IgniteSpiException; import org.apache.ignite.testframework.GridStringLogger; import org.apache.ignite.testframework.GridTestUtils; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.h2.util.JdbcUtils; -import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; /** * Tests for all SQL based indexing SPI implementations. */ public abstract class GridIndexingSpiAbstractSelfTest extends GridCommonAbstractTest { /** */ - private static final TextIndex textIdx = new TextIndex(F.asList("txt")); - - /** */ private static final LinkedHashMap<String, String> fieldsAA = new LinkedHashMap<>(); /** */ private static final LinkedHashMap<String, String> fieldsAB = new LinkedHashMap<>(); /** */ - private static final LinkedHashMap<String, String> fieldsBA = new LinkedHashMap<>(); - - /** */ private IgniteEx ignite0; /** {@inheritDoc} */ + @SuppressWarnings("deprecation") @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(gridName); @@ -98,20 +70,8 @@ public abstract class GridIndexingSpiAbstractSelfTest extends GridCommonAbstract fieldsAB.putAll(fieldsAA); fieldsAB.put("txt", String.class.getName()); - - fieldsBA.putAll(fieldsAA); - fieldsBA.put("sex", Boolean.class.getName()); } - /** */ - private static TypeDesc typeAA = new TypeDesc("A", "A", "A", Collections.<String, Class<?>>emptyMap(), null); - - /** */ - private static TypeDesc typeAB = new TypeDesc("A", "A", "B", Collections.<String, Class<?>>emptyMap(), textIdx); - - /** */ - private static TypeDesc typeBA = new TypeDesc("B", "B", "A", Collections.<String, Class<?>>emptyMap(), null); - /** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { ignite0 = startGrid(0); @@ -144,82 +104,12 @@ public abstract class GridIndexingSpiAbstractSelfTest extends GridCommonAbstract return cfg; } - /** - * - */ - private CacheConfiguration cacheBCfg() { - CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME); - - cfg.setName("B"); - - QueryEntity eA = new QueryEntity(Integer.class.getName(), "A"); - eA.setFields(fieldsBA); - - cfg.setQueryEntities(Collections.singleton(eA)); - - return cfg; - } - /** {@inheritDoc} */ @Override protected void afterTest() throws Exception { stopAllGrids(); } /** - * @param id Id. - * @param name Name. - * @param age Age. - * @return AA. - */ - private BinaryObjectBuilder aa(String typeName, long id, String name, int age) { - BinaryObjectBuilder aBuilder = ignite0.binary().builder(typeName) - .setField("id", id) - .setField("name", name) - .setField("age", age); - - return aBuilder; - } - - /** - * @param id Id. - * @param name Name. - * @param age Age. - * @param txt Text. - * @return AB. - */ - private BinaryObjectBuilder ab(long id, String name, int age, String txt) { - BinaryObjectBuilder aBuilder = aa("B", id, name, age); - - aBuilder.setField("txt", txt); - - return aBuilder; - } - - /** - * @param id Id. - * @param name Name. - * @param age Age. - * @param sex Sex. - * @return BA. - */ - private BinaryObjectBuilder ba(long id, String name, int age, boolean sex) { - BinaryObjectBuilder builder = aa("A", id, name, age); - - builder.setField("sex", sex); - - return builder; - } - - /** - * @param row Row - * @return Value. - * @throws IgniteSpiException If failed. - */ - private BinaryObjectImpl value(IgniteBiTuple<Integer, BinaryObjectImpl> row) throws IgniteSpiException { - return row.get2(); - } - - /** * @return Indexing. */ private IgniteH2Indexing getIndexing() { @@ -234,147 +124,11 @@ public abstract class GridIndexingSpiAbstractSelfTest extends GridCommonAbstract } /** - * @param key Key. - * @return Cache object. - */ - private KeyCacheObject key(int key) { - return new TestCacheObject(key); - } - - /** - * @throws Exception If failed. - */ - public void testSpi() throws Exception { - IgniteH2Indexing spi = getIndexing(); - - IgniteCache<Integer, BinaryObject> cacheA = ignite0.createCache(cacheACfg()); - - IgniteCache<Integer, BinaryObject> cacheB = ignite0.createCache(cacheBCfg()); - - assertFalse(spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), "select * from A.A", null, - Collections.emptySet(), typeAA.name(), null, null).hasNext()); - - assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select * from A.B", null, - Collections.emptySet(), typeAB.name(), null, null).hasNext()); - - assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select * from B.A", null, - Collections.emptySet(), typeBA.name(), null, null).hasNext()); - - assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), - "select * from B.A, A.B, A.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext()); - - try { - spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), - "select aa.*, ab.*, ba.* from A.A aa, A.B ab, B.A ba", - null, Collections.emptySet(), typeBA.name(), null, null).hasNext(); - - fail("Enumerations of aliases in select block must be prohibited"); - } - catch (IgniteCheckedException ignored) { - // all fine - } - - assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select ab.* from A.B ab", - null, Collections.emptySet(), typeAB.name(), null, null).hasNext()); - - assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), - "select ba.* from B.A as ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext()); - - cacheA.put(1, aa("A", 1, "Vasya", 10).build()); - cacheA.put(1, ab(1, "Vasya", 20, "Some text about Vasya goes here.").build()); - cacheB.put(1, ba(2, "Petya", 25, true).build()); - cacheB.put(1, ba(2, "Kolya", 25, true).build()); - cacheA.put(2, aa("A", 2, "Valera", 19).build()); - cacheA.put(3, aa("A", 3, "Borya", 18).build()); - cacheA.put(4, ab(4, "Vitalya", 20, "Very Good guy").build()); - - // Query data. - Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), - typeAA.cacheName(), "from a order by age", null, Collections.emptySet(), typeAA.name(), null, null); - - assertTrue(res.hasNext()); - assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next())); - assertTrue(res.hasNext()); - assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next())); - assertFalse(res.hasNext()); - - res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), - "select aa.* from a aa order by aa.age", null, Collections.emptySet(), typeAA.name(), null, null); - - assertTrue(res.hasNext()); - assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next())); - assertTrue(res.hasNext()); - assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next())); - assertFalse(res.hasNext()); - - res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "from b order by name", null, - Collections.emptySet(), typeAB.name(), null, null); - - assertTrue(res.hasNext()); - assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next())); - assertTrue(res.hasNext()); - assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next())); - assertFalse(res.hasNext()); - - res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), - "select bb.* from b as bb order by bb.name", null, Collections.emptySet(), typeAB.name(), null, null); - - assertTrue(res.hasNext()); - assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next())); - assertTrue(res.hasNext()); - assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next())); - assertFalse(res.hasNext()); - - res = spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "from a", null, - Collections.emptySet(), typeBA.name(), null, null); - - assertTrue(res.hasNext()); - assertEquals(ba(2, "Kolya", 25, true).build(), value(res.next())); - assertFalse(res.hasNext()); - - // Text queries - Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> txtRes = spi.queryLocalText(spi.schema(typeAB.cacheName()), - typeAB.cacheName(), "good", typeAB.name(), null); - - assertTrue(txtRes.hasNext()); - assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(txtRes.next())); - assertFalse(txtRes.hasNext()); - - // Fields query - GridQueryFieldsResult fieldsRes = - spi.queryLocalSqlFields(spi.schema("A"), "select a.a.name n1, a.a.age a1, b.a.name n2, " + - "b.a.age a2 from a.a, b.a where a.a.id = b.a.id ", Collections.emptySet(), null, false, false, 0, null); - - String[] aliases = {"N1", "A1", "N2", "A2"}; - Object[] vals = { "Valera", 19, "Kolya", 25}; - - IgniteSpiCloseableIterator<List<?>> it = fieldsRes.iterator(); - - assertTrue(it.hasNext()); - - List<?> fields = it.next(); - - assertEquals(4, fields.size()); - - int i = 0; - - for (Object f : fields) { - assertEquals(aliases[i], fieldsRes.metaData().get(i).fieldName()); - assertEquals(vals[i++], f); - } - - assertFalse(it.hasNext()); - - // Remove - cacheA.remove(2); - cacheB.remove(1); - } - - /** * Test long queries write explain warnings into log. * * @throws Exception If failed. */ + @SuppressWarnings({"unchecked", "deprecation"}) public void testLongQueries() throws Exception { IgniteH2Indexing spi = getIndexing(); @@ -416,381 +170,4 @@ public abstract class GridIndexingSpiAbstractSelfTest extends GridCommonAbstract GridTestUtils.setFieldValue(spi, "log", oldLog); } } - - /** - * Index descriptor. - */ - private static class TextIndex implements GridQueryIndexDescriptor { - /** */ - private final Collection<String> fields; - - /** - * @param fields Fields. - */ - private TextIndex(Collection<String> fields) { - this.fields = Collections.unmodifiableCollection(fields); - } - - /** {@inheritDoc} */ - @Override public String name() { - return null; - } - - /** {@inheritDoc} */ - @Override public Collection<String> fields() { - return fields; - } - - /** {@inheritDoc} */ - @Override public boolean descending(String field) { - return false; - } - - /** {@inheritDoc} */ - @Override public QueryIndexType type() { - return QueryIndexType.FULLTEXT; - } - - /** {@inheritDoc} */ - @Override public int inlineSize() { - return 0; - } - } - - /** - * Type descriptor. - */ - private static class TypeDesc implements GridQueryTypeDescriptor { - /** */ - private final String name; - - /** */ - private final String cacheName; - - /** */ - private final String schemaName; - - /** */ - private final Map<String, Class<?>> valFields; - - /** */ - private final GridQueryIndexDescriptor textIdx; - - /** - * @param cacheName Cache name. - * @param schemaName Schema name. - * @param name Type name. - * @param valFields Fields. - * @param textIdx Fulltext index. - */ - private TypeDesc(String cacheName, String schemaName, String name, Map<String, Class<?>> valFields, GridQueryIndexDescriptor textIdx) { - this.name = name; - this.cacheName = cacheName; - this.schemaName = schemaName; - this.valFields = Collections.unmodifiableMap(valFields); - this.textIdx = textIdx; - } - - /** {@inheritDoc} */ - @Override public String affinityKey() { - return null; - } - - /** {@inheritDoc} */ - @Override public String name() { - return name; - } - - /** {@inheritDoc} */ - @Override public String schemaName() { - return schemaName; - } - - /** {@inheritDoc} */ - @Override public String tableName() { - return null; - } - - /** - * @return Cache name. - */ - String cacheName() { - return cacheName; - } - - /** {@inheritDoc} */ - @Override public Map<String, Class<?>> fields() { - return valFields; - } - - /** {@inheritDoc} */ - @Override public GridQueryProperty property(final String name) { - return new GridQueryProperty() { - /** */ - @Override public Object value(Object key, Object val) throws IgniteCheckedException { - return TypeDesc.this.value(name, key, val); - } - - /** */ - @Override public void setValue(Object key, Object val, Object propVal) throws IgniteCheckedException { - throw new UnsupportedOperationException(); - } - - /** */ - @Override public String name() { - return name; - } - - /** */ - @Override public Class<?> type() { - return Object.class; - } - - /** */ - @Override public boolean key() { - return false; - } - - /** */ - @Override public GridQueryProperty parent() { - return null; - } - - /** */ - @Override public boolean notNull() { - return false; - } - - /** */ - @Override public Object defaultValue() { - return null; - } - - /** */ - @Override public int precision() { - return -1; - } - - /** */ - @Override public int scale() { - return -1; - } - }; - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public <T> T value(String field, Object key, Object val) throws IgniteSpiException { - assert !F.isEmpty(field); - - assert key instanceof Integer; - - Map<String, T> m = (Map<String, T>)val; - - if (m.containsKey(field)) - return m.get(field); - - return null; - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public void setValue(String field, Object key, Object val, Object propVal) throws IgniteCheckedException { - assert !F.isEmpty(field); - - assert key instanceof Integer; - - Map<String, Object> m = (Map<String, Object>)val; - - m.put(field, propVal); - } - - /** */ - @Override public Map<String, GridQueryIndexDescriptor> indexes() { - return Collections.emptyMap(); - } - - /** */ - @Override public GridQueryIndexDescriptor textIndex() { - return textIdx; - } - - /** */ - @Override public Class<?> valueClass() { - return Object.class; - } - - /** */ - @Override public Class<?> keyClass() { - return Integer.class; - } - - /** */ - @Override public String keyTypeName() { - return null; - } - - /** */ - @Override public String valueTypeName() { - return null; - } - - /** */ - @Override public boolean valueTextIndex() { - return textIdx == null; - } - - /** */ - @Override public int typeId() { - return 0; - } - - /** {@inheritDoc} */ - @Override public String keyFieldName() { - return null; - } - - /** {@inheritDoc} */ - @Override public String valueFieldName() { - return null; - } - - /** {@inheritDoc} */ - @Nullable @Override public String keyFieldAlias() { - return null; - } - - /** {@inheritDoc} */ - @Nullable @Override public String valueFieldAlias() { - return null; - } - - /** {@inheritDoc} */ - @Override public void validateKeyAndValue(Object key, Object value) throws IgniteCheckedException { - // No-op. - } - - /** {@inheritDoc} */ - @Override public void setDefaults(Object key, Object val) throws IgniteCheckedException { - // No-op. - } - } - - /** - */ - private static class TestCacheObject implements KeyCacheObject { - /** */ - private Object val; - - /** */ - private int part; - - /** - * @param val Value. - */ - private TestCacheObject(Object val) { - this.val = val; - } - - /** {@inheritDoc} */ - @Override public void onAckReceived() { - // No-op. - } - - /** {@inheritDoc} */ - @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) { - return (T)val; - } - - /** {@inheritDoc} */ - @Override public int partition() { - return part; - } - - /** {@inheritDoc} */ - @Override public void partition(int part) { - this.part = part; - } - - /** {@inheritDoc} */ - @Override public byte[] valueBytes(CacheObjectValueContext ctx) throws IgniteCheckedException { - return JdbcUtils.serialize(val, null); - } - - /** {@inheritDoc} */ - @Override public boolean putValue(ByteBuffer buf) throws IgniteCheckedException { - return false; - } - - /** {@inheritDoc} */ - @Override public int putValue(long addr) throws IgniteCheckedException { - return 0; - } - - /** {@inheritDoc} */ - @Override public boolean putValue(final ByteBuffer buf, final int off, final int len) - throws IgniteCheckedException { - return false; - } - - /** {@inheritDoc} */ - @Override public int valueBytesLength(CacheObjectContext ctx) throws IgniteCheckedException { - return 0; - } - - /** {@inheritDoc} */ - @Override public byte cacheObjectType() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public boolean isPlatformType() { - return true; - } - - /** {@inheritDoc} */ - @Override public KeyCacheObject copy(int part) { - return this; - } - - /** {@inheritDoc} */ - @Override public CacheObject prepareForCache(CacheObjectContext ctx) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public void finishUnmarshal(CacheObjectValueContext ctx, ClassLoader ldr) throws IgniteCheckedException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public void prepareMarshal(CacheObjectValueContext ctx) throws IgniteCheckedException { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public short directType() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public byte fieldsCount() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public boolean internal() { - return false; - } - } } http://git-wip-us.apache.org/repos/asf/ignite/blob/c4ec543a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/H2ResultSetIteratorNullifyOnEndSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/H2ResultSetIteratorNullifyOnEndSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/H2ResultSetIteratorNullifyOnEndSelfTest.java index 31b0b97..66f9dd2 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/H2ResultSetIteratorNullifyOnEndSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/H2ResultSetIteratorNullifyOnEndSelfTest.java @@ -17,22 +17,16 @@ package org.apache.ignite.internal.processors.query.h2; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Objects; -import javax.cache.Cache; import org.apache.ignite.IgniteCache; -import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.cache.query.annotations.QuerySqlField; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.internal.processors.cache.QueryCursorImpl; import org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator; -import org.apache.ignite.internal.processors.query.GridQueryProcessor; -import org.apache.ignite.internal.util.lang.GridCloseableIterator; import org.apache.ignite.testframework.GridTestUtils; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; @@ -47,80 +41,9 @@ public class H2ResultSetIteratorNullifyOnEndSelfTest extends GridCommonAbstractT private static final int PERSON_COUNT = 20; /** */ - private static final String SELECT_ALL_SQL = "SELECT p.* FROM Person p ORDER BY p.salary"; - - /** */ private static final String SELECT_MAX_SAL_SQLF = "select max(salary) from Person"; /** - * Non local SQL check nullification after close - */ - public void testSqlQueryClose() { - SqlQuery<String, Person> qry = new SqlQuery<>(Person.class, SELECT_ALL_SQL); - - QueryCursor<Cache.Entry<String, Person>> qryCurs = cache().query(qry); - - qryCurs.iterator(); - - qryCurs.close(); - - H2ResultSetIterator h2It = extractIteratorInnerGridIteratorInnerH2ResultSetIterator(qryCurs); - - checkIterator(h2It); - } - - /** - * Non local SQL check nullification after complete - */ - public void testSqlQueryComplete() { - SqlQuery<String, Person> qry = new SqlQuery<>(Person.class, SELECT_ALL_SQL); - - QueryCursor<Cache.Entry<String, Person>> qryCurs = cache().query(qry); - - qryCurs.getAll(); - - H2ResultSetIterator h2It = extractIteratorInnerGridIteratorInnerH2ResultSetIterator(qryCurs); - - checkIterator(h2It); - } - - /** - * Local SQL check nullification after close - */ - public void testSqlQueryLocalClose() { - SqlQuery<String, Person> qry = new SqlQuery<>(Person.class, SELECT_ALL_SQL); - - qry.setLocal(true); - - QueryCursor<Cache.Entry<String, Person>> qryCurs = cache().query(qry); - - qryCurs.iterator(); - - qryCurs.close(); - - H2ResultSetIterator h2It = extractIterableInnerH2ResultSetIterator(qryCurs); - - checkIterator(h2It); - } - - /** - * Local SQL check nullification after complete - */ - public void testSqlQueryLocalComplete() { - SqlQuery<String, Person> qry = new SqlQuery<>(Person.class, SELECT_ALL_SQL); - - qry.setLocal(true); - - QueryCursor<Cache.Entry<String, Person>> qryCurs = cache().query(qry); - - qryCurs.getAll(); - - H2ResultSetIterator h2It = extractIterableInnerH2ResultSetIterator(qryCurs); - - checkIterator(h2It); - } - - /** * Non local SQL Fields check nullification after close */ public void testSqlFieldsQueryClose() { @@ -200,45 +123,6 @@ public class H2ResultSetIteratorNullifyOnEndSelfTest extends GridCommonAbstractT } /** - * Extract H2ResultSetIterator by reflection for non local SQL cases - * @param qryCurs source cursor - * @return target iterator or null of not extracted - */ - private H2ResultSetIterator extractIteratorInnerGridIteratorInnerH2ResultSetIterator( - QueryCursor<Cache.Entry<String, Person>> qryCurs) { - if (QueryCursorImpl.class.isAssignableFrom(qryCurs.getClass())) { - Iterator inner = GridTestUtils.getFieldValue(qryCurs, QueryCursorImpl.class, "iter"); - - GridQueryCacheObjectsIterator it = GridTestUtils.getFieldValue(inner, inner.getClass(), "val$iter0"); - - Iterator<List<?>> h2RsIt = GridTestUtils.getFieldValue(it, GridQueryCacheObjectsIterator.class, "iter"); - - if (H2ResultSetIterator.class.isAssignableFrom(h2RsIt.getClass())) - return (H2ResultSetIterator)h2RsIt; - } - return null; - } - - /** - * Extract H2ResultSetIterator by reflection for local SQL cases. - * - * @param qryCurs source cursor - * @return target iterator or null of not extracted - */ - private H2ResultSetIterator extractIterableInnerH2ResultSetIterator( - QueryCursor<Cache.Entry<String, Person>> qryCurs) { - if (QueryCursorImpl.class.isAssignableFrom(qryCurs.getClass())) { - Iterable iterable = GridTestUtils.getFieldValue(qryCurs, QueryCursorImpl.class, "iterExec"); - - Iterator h2RsIt = GridTestUtils.getFieldValue(iterable, iterable.getClass(), "val$i"); - - if (H2ResultSetIterator.class.isAssignableFrom(h2RsIt.getClass())) - return (H2ResultSetIterator)h2RsIt; - } - return null; - } - - /** * Extract H2ResultSetIterator by reflection for SQL Fields cases. * * @param qryCurs source cursor @@ -256,67 +140,6 @@ public class H2ResultSetIteratorNullifyOnEndSelfTest extends GridCommonAbstractT return null; } - /** - * "onClose" should remove links to data. - */ - public void testOnClose() { - try { - GridCloseableIterator it = indexing().queryLocalSql( - indexing().schema(cache().getName()), - cache().getName(), - SELECT_ALL_SQL, - null, - Collections.emptySet(), - "Person", - null, - null); - - if (H2ResultSetIterator.class.isAssignableFrom(it.getClass())) { - H2ResultSetIterator h2it = (H2ResultSetIterator)it; - - h2it.onClose(); - - assertNull(GridTestUtils.getFieldValue(h2it, H2ResultSetIterator.class, "data")); - } - else - fail(); - } - catch (IgniteCheckedException e) { - fail(e.getMessage()); - } - } - - /** - * Complete iterate should remove links to data. - */ - public void testOnComplete() { - try { - GridCloseableIterator it = indexing().queryLocalSql( - indexing().schema(cache().getName()), - cache().getName(), - SELECT_ALL_SQL, - null, - Collections.emptySet(), - "Person", - null, - null); - - if (H2ResultSetIterator.class.isAssignableFrom(it.getClass())) { - H2ResultSetIterator h2it = (H2ResultSetIterator)it; - - while (h2it.onHasNext()) - h2it.onNext(); - - assertNull(GridTestUtils.getFieldValue(h2it, H2ResultSetIterator.class, "data")); - } - else - fail(); - } - catch (IgniteCheckedException e) { - fail(e.getMessage()); - } - } - /** {@inheritDoc} */ @Override protected void beforeTestsStarted() throws Exception { startGrids(NODES_COUNT); @@ -336,15 +159,6 @@ public class H2ResultSetIteratorNullifyOnEndSelfTest extends GridCommonAbstractT } /** - * @return H2 indexing instance. - */ - private IgniteH2Indexing indexing() { - GridQueryProcessor qryProcessor = grid(0).context().query(); - - return GridTestUtils.getFieldValue(qryProcessor, GridQueryProcessor.class, "idx"); - } - - /** * @return Cache. */ private IgniteCache<String, Person> cache() {