IGNITE-5027: Fixed issue with duplicate names on quto-generated indexes. This closes #1831.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/1cd3cac5 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/1cd3cac5 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/1cd3cac5 Branch: refs/heads/ignite-2893 Commit: 1cd3cac534403172145481310b597b6175306222 Parents: 130b1fd Author: devozerov <voze...@gridgain.com> Authored: Wed Apr 19 19:44:30 2017 +0300 Committer: devozerov <voze...@gridgain.com> Committed: Wed Apr 19 19:44:30 2017 +0300 ---------------------------------------------------------------------- .../configuration/CacheConfiguration.java | 17 ++-- .../processors/query/GridQueryProcessor.java | 10 +-- .../query/h2/H2IndexingAbstractGeoSelfTest.java | 8 +- .../IgniteCacheAbstractFieldsQuerySelfTest.java | 10 ++- .../index/DuplicateKeyValueClassesSelfTest.java | 94 ++++++++++++++++++++ .../IgniteCacheQuerySelfTestSuite.java | 4 + 6 files changed, 126 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/1cd3cac5/modules/core/src/main/java/org/apache/ignite/configuration/CacheConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/configuration/CacheConfiguration.java b/modules/core/src/main/java/org/apache/ignite/configuration/CacheConfiguration.java index a2f7cc8..d378343 100644 --- a/modules/core/src/main/java/org/apache/ignite/configuration/CacheConfiguration.java +++ b/modules/core/src/main/java/org/apache/ignite/configuration/CacheConfiguration.java @@ -2145,7 +2145,7 @@ public class CacheConfiguration<K, V> extends MutableConfiguration<K, V> { // properties override will happen properly (first parent, then children). type.addProperty(prop, key, true); - processAnnotation(key, sqlAnn, txtAnn, field.getType(), prop, type); + processAnnotation(key, sqlAnn, txtAnn, cls, c, field.getType(), prop, type); } } @@ -2171,7 +2171,7 @@ public class CacheConfiguration<K, V> extends MutableConfiguration<K, V> { // properties override will happen properly (first parent, then children). type.addProperty(prop, key, true); - processAnnotation(key, sqlAnn, txtAnn, mtd.getReturnType(), prop, type); + processAnnotation(key, sqlAnn, txtAnn, cls, c, mtd.getReturnType(), prop, type); } } } @@ -2183,20 +2183,25 @@ public class CacheConfiguration<K, V> extends MutableConfiguration<K, V> { * @param key If given class relates to key. * @param sqlAnn SQL annotation, can be {@code null}. * @param txtAnn H2 text annotation, can be {@code null}. - * @param cls Class of field or return type for method. + * @param cls Entity class. + * @param curCls Current entity class. + * @param fldCls Class of field or return type for method. * @param prop Current property. * @param desc Class description. */ private static void processAnnotation(boolean key, QuerySqlField sqlAnn, QueryTextField txtAnn, - Class<?> cls, ClassProperty prop, TypeDescriptor desc) { + Class<?> cls, Class<?> curCls, Class<?> fldCls, ClassProperty prop, TypeDescriptor desc) { if (sqlAnn != null) { - processAnnotationsInClass(key, cls, desc, prop); + processAnnotationsInClass(key, fldCls, desc, prop); if (!sqlAnn.name().isEmpty()) prop.alias(sqlAnn.name()); if (sqlAnn.index()) { - String idxName = prop.alias() + "_idx"; + String idxName = curCls.getSimpleName() + "_" + prop.alias() + "_idx"; + + if (cls != curCls) + idxName = cls.getSimpleName() + "_" + idxName; desc.addIndex(idxName, QueryUtils.isGeometryClass(prop.type()) ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED); http://git-wip-us.apache.org/repos/asf/ignite/blob/1cd3cac5/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 ceb139a..8381882 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 @@ -685,15 +685,15 @@ public class GridQueryProcessor extends GridProcessorAdapter { QueryTypeDescriptorImpl oldDesc = tblTypMap.put(desc.tableName(), desc); if (oldDesc != null) - throw new IgniteException("Duplicate table name [tblName=" + desc.tableName() + - ", type1=" + desc.name() + ", type2=" + oldDesc.name() + ']'); + throw new IgniteException("Duplicate table name [cache=" + space + + ", tblName=" + desc.tableName() + ", type1=" + desc.name() + ", type2=" + oldDesc.name() + ']'); for (String idxName : desc.indexes().keySet()) { oldDesc = idxTypMap.put(idxName, desc); if (oldDesc != null) - throw new IgniteException("Duplicate index name [idxName=" + idxName + - ", type1=" + desc.name() + ", type2=" + oldDesc.name() + ']'); + throw new IgniteException("Duplicate index name [cache=" + space + + ", idxName=" + idxName + ", type1=" + desc.name() + ", type2=" + oldDesc.name() + ']'); } } @@ -1290,7 +1290,7 @@ public class GridQueryProcessor extends GridProcessorAdapter { QueryIndexDescriptorImpl oldIdx = idxs.putIfAbsent(idxKey, idx); if (oldIdx != null) { - throw new IgniteException("Duplicate index name [space=" + space + + throw new IgniteException("Duplicate index name [cache=" + space + ", idxName=" + idx.name() + ", existingTable=" + oldIdx.typeDescriptor().tableName() + ", table=" + desc.tableName() + ']'); } http://git-wip-us.apache.org/repos/asf/ignite/blob/1cd3cac5/modules/geospatial/src/test/java/org/apache/ignite/internal/processors/query/h2/H2IndexingAbstractGeoSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/geospatial/src/test/java/org/apache/ignite/internal/processors/query/h2/H2IndexingAbstractGeoSelfTest.java b/modules/geospatial/src/test/java/org/apache/ignite/internal/processors/query/h2/H2IndexingAbstractGeoSelfTest.java index 914bb62..80e9f93 100644 --- a/modules/geospatial/src/test/java/org/apache/ignite/internal/processors/query/h2/H2IndexingAbstractGeoSelfTest.java +++ b/modules/geospatial/src/test/java/org/apache/ignite/internal/processors/query/h2/H2IndexingAbstractGeoSelfTest.java @@ -179,7 +179,7 @@ public abstract class H2IndexingAbstractGeoSelfTest extends GridCacheAbstractSel GridStringBuilder sb = new SB("CREATE ") .a(spatial ? "SPATIAL " : "") .a("INDEX ") - .a(idx.getName()) + .a("\"" + idx.getName() + "\"") .a(" ON ") .a(QueryUtils.tableName(entity)) .a(" ("); @@ -333,7 +333,7 @@ public abstract class H2IndexingAbstractGeoSelfTest extends GridCacheAbstractSel assertTrue("__ explain: " + plan, plan.contains("coords_idx")); if (dynamic) - cache.query(new SqlFieldsQuery("DROP INDEX coords_idx")).getAll(); + cache.query(new SqlFieldsQuery("DROP INDEX \"EnemyCamp_coords_idx\"")).getAll(); } finally { cache.destroy(); @@ -582,8 +582,8 @@ public abstract class H2IndexingAbstractGeoSelfTest extends GridCacheAbstractSel } } - final SqlFieldsQuery query = new SqlFieldsQuery("select e._val, c._val from \"enemy\".Enemy e, \"camp\".EnemyCamp c " + - "where e.campId = c._key and c.coords && ?").setArgs(lethalArea); + final SqlFieldsQuery query = new SqlFieldsQuery("select e._val, c._val from \"enemy\".Enemy e, " + + "\"camp\".EnemyCamp c where e.campId = c._key and c.coords && ?").setArgs(lethalArea); List<List<?>> result = c1.query(query.setDistributedJoins(true)).getAll(); http://git-wip-us.apache.org/repos/asf/ignite/blob/1cd3cac5/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java index b40dd78..8c8828d 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java @@ -26,6 +26,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.concurrent.Callable; import javax.cache.CacheException; @@ -270,6 +271,8 @@ public abstract class IgniteCacheAbstractFieldsQuerySelfTest extends GridCommonA assertNotNull("Indexes should be defined", indexes); assertEquals(2, indexes.size()); + Set<String> idxFields = new HashSet<>(); + Iterator<GridCacheSqlIndexMetadata> it = indexes.iterator(); Collection<String> indFlds = it.next().fields(); @@ -279,7 +282,7 @@ public abstract class IgniteCacheAbstractFieldsQuerySelfTest extends GridCommonA Iterator<String> indFldIt = indFlds.iterator(); - assertEquals(indFldIt.next(), "AGE"); + idxFields.add(indFldIt.next()); indFlds = it.next().fields(); @@ -288,7 +291,10 @@ public abstract class IgniteCacheAbstractFieldsQuerySelfTest extends GridCommonA indFldIt = indFlds.iterator(); - assertEquals(indFldIt.next(), "ORGID"); + idxFields.add(indFldIt.next()); + + assertTrue(idxFields.contains("AGE")); + assertTrue(idxFields.contains("ORGID")); } else if (orgCache.getName().equals(meta.cacheName())) { assertEquals("Invalid types size", 1, types.size()); http://git-wip-us.apache.org/repos/asf/ignite/blob/1cd3cac5/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/DuplicateKeyValueClassesSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/DuplicateKeyValueClassesSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/DuplicateKeyValueClassesSelfTest.java new file mode 100644 index 0000000..4ee884f --- /dev/null +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/index/DuplicateKeyValueClassesSelfTest.java @@ -0,0 +1,94 @@ +/* + * 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.cache.index; + +import org.apache.ignite.cache.query.annotations.QuerySqlField; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +import java.util.UUID; + +/** + * Make sure that cache can start with multiple key-value classes of the same type. + */ +@SuppressWarnings("unchecked") +public class DuplicateKeyValueClassesSelfTest extends GridCommonAbstractTest { + /** Cache name. */ + private static final String CACHE_NAME = "cache"; + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + startGrid(0); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + grid(0).destroyCache(CACHE_NAME); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** + * Test duplicate key class. + * + * @throws Exception If failed. + */ + public void testDuplicateKeyClass() throws Exception { + CacheConfiguration ccfg = new CacheConfiguration() + .setName(CACHE_NAME) + .setIndexedTypes(UUID.class, Clazz1.class, UUID.class, Clazz2.class); + + grid(0).createCache(ccfg); + } + + /** + * Test duplicate value class. + * + * @throws Exception If failed. + */ + public void testDuplicateValueClass() throws Exception { + CacheConfiguration ccfg = new CacheConfiguration() + .setName(CACHE_NAME) + .setIndexedTypes(UUID.class, Clazz1.class, String.class, Clazz1.class); + + grid(0).createCache(ccfg); + } + + /** + * Class 1. + */ + private static class Clazz1 { + /** ID. */ + @QuerySqlField(index = true) + int id; + } + + /** + * Class 2. + */ + private static class Clazz2 { + /** ID. */ + @QuerySqlField(index = true) + int id; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/1cd3cac5/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java index b61affe..405e1f6 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java +++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite.java @@ -81,6 +81,7 @@ import org.apache.ignite.internal.processors.cache.distributed.replicated.Ignite import org.apache.ignite.internal.processors.cache.distributed.replicated.IgniteCacheReplicatedFieldsQuerySelfTest; import org.apache.ignite.internal.processors.cache.distributed.replicated.IgniteCacheReplicatedQueryP2PDisabledSelfTest; import org.apache.ignite.internal.processors.cache.distributed.replicated.IgniteCacheReplicatedQuerySelfTest; +import org.apache.ignite.internal.processors.cache.index.DuplicateKeyValueClassesSelfTest; import org.apache.ignite.internal.processors.cache.index.H2DynamicIndexAtomicPartitionedNearSelfTest; import org.apache.ignite.internal.processors.cache.index.H2DynamicIndexAtomicPartitionedSelfTest; import org.apache.ignite.internal.processors.cache.index.H2DynamicIndexAtomicReplicatedSelfTest; @@ -128,6 +129,9 @@ public class IgniteCacheQuerySelfTestSuite extends TestSuite { public static TestSuite suite() throws Exception { IgniteTestSuite suite = new IgniteTestSuite("Ignite Cache Queries Test Suite"); + // Misc tests. + suite.addTest(new TestSuite(DuplicateKeyValueClassesSelfTest.class)); + // Dynamic index create/drop tests. suite.addTest(new TestSuite(SchemaExchangeSelfTest.class));