From 8de5efa93f5190be2adfd9a97d0f0a6f549b61d9 Mon Sep 17 00:00:00 2001 From: Luigi Dell'Aquila Date: Fri, 8 Mar 2019 10:36:19 +0100 Subject: [PATCH] Fix index creation on complex field names Resolves: #8761 --- .../core/index/OIndexDefinitionFactory.java | 44 ++++++++++++------- .../test/database/auto/ClassIndexTest.java | 8 +--- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/com/orientechnologies/orient/core/index/OIndexDefinitionFactory.java b/core/src/main/java/com/orientechnologies/orient/core/index/OIndexDefinitionFactory.java index b9e8cd51ab9..240dd57c264 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/index/OIndexDefinitionFactory.java +++ b/core/src/main/java/com/orientechnologies/orient/core/index/OIndexDefinitionFactory.java @@ -47,16 +47,14 @@ public class OIndexDefinitionFactory { /** * Creates an instance of {@link OIndexDefinition} for automatic index. * - * @param oClass - * class which will be indexed - * @param fieldNames - * list of properties which will be indexed. Format should be ' [by key|value]', use 'by key' or 'by value' to - * describe how to index maps. By default maps indexed by key - * @param types - * types of indexed properties + * @param oClass class which will be indexed + * @param fieldNames list of properties which will be indexed. Format should be ' [by key|value]', use 'by key' or 'by + * value' to describe how to index maps. By default maps indexed by key + * @param types types of indexed properties * @param collates * @param indexKind * @param algorithm + * * @return index definition instance */ public static OIndexDefinition createIndexDefinition(final OClass oClass, final List fieldNames, final List types, @@ -73,19 +71,30 @@ public static OIndexDefinition createIndexDefinition(final OClass oClass, final /** * Extract field name from ' [by key|value]' field format. * - * @param fieldDefinition - * definition of field + * @param fieldDefinition definition of field + * * @return extracted property name */ public static String extractFieldName(final String fieldDefinition) { String[] fieldNameParts = FILED_NAME_PATTERN.split(fieldDefinition); - if (fieldNameParts.length == 1) - return fieldDefinition; + if (fieldNameParts.length == 0) { + throw new IllegalArgumentException( + "Illegal field name format, should be ' [by key|value]' but was '" + fieldDefinition + '\''); + } if (fieldNameParts.length == 3 && "by".equalsIgnoreCase(fieldNameParts[1])) return fieldNameParts[0]; - throw new IllegalArgumentException( - "Illegal field name format, should be ' [by key|value]' but was '" + fieldDefinition + '\''); + if (fieldNameParts.length == 1) + return fieldDefinition; + + StringBuilder result = new StringBuilder(); + result.append(fieldNameParts[0]); + for (int i = 1; i < fieldNameParts.length; i++) { + result.append(" "); + result.append(fieldNameParts[i]); + } + return result.toString(); + } private static OIndexDefinition createMultipleFieldIndexDefinition(final OClass oClass, final List fieldsToIndex, @@ -108,8 +117,9 @@ private static OIndexDefinition createMultipleFieldIndexDefinition(final OClass private static void checkTypes(OClass oClass, List fieldNames, List types) { if (fieldNames.size() != types.size()) - throw new IllegalArgumentException("Count of field names doesn't match count of field types. It was " + fieldNames.size() - + " fields, but " + types.size() + " types."); + throw new IllegalArgumentException( + "Count of field names doesn't match count of field types. It was " + fieldNames.size() + " fields, but " + types.size() + + " types."); for (int i = 0, fieldNamesSize = fieldNames.size(); i < fieldNamesSize; i++) { String fieldName = fieldNames.get(i); @@ -148,8 +158,8 @@ private static OIndexDefinition createSingleFieldIndexDefinition(OClass oClass, } indexDefinition = new OPropertyMapIndexDefinition(oClass.getName(), fieldName, indexType, indexBy); - } else if (type.equals(OType.EMBEDDEDLIST) || type.equals(OType.EMBEDDEDSET) || type.equals(OType.LINKLIST) - || type.equals(OType.LINKSET)) { + } else if (type.equals(OType.EMBEDDEDLIST) || type.equals(OType.EMBEDDEDSET) || type.equals(OType.LINKLIST) || type + .equals(OType.LINKSET)) { if (type.equals(OType.LINKSET)) indexType = OType.LINK; else if (type.equals(OType.LINKLIST)) { diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ClassIndexTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ClassIndexTest.java index 99653e21019..503be5ae260 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ClassIndexTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ClassIndexTest.java @@ -451,10 +451,8 @@ public void testCreateOnePropertyWrongSpecifierEmbeddedMapIndexTwo() { boolean exceptionIsThrown = false; try { oClass.createIndex("ClassIndexTestPropertyWrongSpecifierEmbeddedMap", OClass.INDEX_TYPE.UNIQUE.toString(), null, new ODocument().fields("ignoreNullValues", true), new String[]{"fEmbeddedMap b value"}); - } catch (IllegalArgumentException e) { + } catch (OIndexException e) { exceptionIsThrown = true; - assertEquals(e.getMessage(), - "Illegal field name format, should be ' [by key|value]' but was 'fEmbeddedMap b value'"); } assertTrue(exceptionIsThrown); @@ -466,10 +464,8 @@ public void testCreateOnePropertyWrongSpecifierEmbeddedMapIndexThree() { boolean exceptionIsThrown = false; try { oClass.createIndex("ClassIndexTestPropertyWrongSpecifierEmbeddedMap", OClass.INDEX_TYPE.UNIQUE.toString(), null, new ODocument().fields("ignoreNullValues", true), new String[]{"fEmbeddedMap by value t"}); - } catch (IllegalArgumentException e) { + } catch (OIndexException e) { exceptionIsThrown = true; - assertEquals(e.getMessage(), - "Illegal field name format, should be ' [by key|value]' but was 'fEmbeddedMap by value t'"); } assertTrue(exceptionIsThrown);