Skip to content

Commit

Permalink
Fix index creation on complex field names
Browse files Browse the repository at this point in the history
Resolves: #8761
  • Loading branch information
luigidellaquila committed Mar 8, 2019
1 parent 5ef9f1f commit 5ad7089
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<property> [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 '<property> [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<String> fieldNames, final List<OType> types,
Expand All @@ -73,19 +71,30 @@ public static OIndexDefinition createIndexDefinition(final OClass oClass, final
/**
* Extract field name from '<property> [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 '<property> [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 '<property> [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<String> fieldsToIndex,
Expand All @@ -108,8 +117,9 @@ private static OIndexDefinition createMultipleFieldIndexDefinition(final OClass

private static void checkTypes(OClass oClass, List<String> fieldNames, List<OType> 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);
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,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 '<property> [by key|value]' but was 'fEmbeddedMap b value'");
}

assertTrue(exceptionIsThrown);
Expand All @@ -467,10 +465,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 '<property> [by key|value]' but was 'fEmbeddedMap by value t'");
}

assertTrue(exceptionIsThrown);
Expand Down

0 comments on commit 5ad7089

Please sign in to comment.