Merge pull request #15245 from jpountz/fix/check_type_name

Make MappedFieldType.checkTypeName part of MappedFieldType.checkCompatibility.
This commit is contained in:
Adrien Grand 2015-12-07 18:03:07 +01:00
commit 3a58af04c0
5 changed files with 23 additions and 33 deletions

View File

@ -370,15 +370,6 @@ public abstract class FieldMapper extends Mapper {
return; return;
} }
FieldMapper fieldMergeWith = (FieldMapper) mergeWith; FieldMapper fieldMergeWith = (FieldMapper) mergeWith;
List<String> subConflicts = new ArrayList<>(); // TODO: just expose list from MergeResult?
fieldType().checkTypeName(fieldMergeWith.fieldType(), subConflicts);
if (subConflicts.isEmpty() == false) {
// return early if field types don't match
assert subConflicts.size() == 1;
mergeResult.addConflict(subConflicts.get(0));
return;
}
multiFields.merge(mergeWith, mergeResult); multiFields.merge(mergeWith, mergeResult);
if (mergeResult.simulate() == false && mergeResult.hasConflicts() == false) { if (mergeResult.simulate() == false && mergeResult.hasConflicts() == false) {

View File

@ -154,12 +154,9 @@ class FieldTypeLookup implements Iterable<MappedFieldType> {
MappedFieldTypeReference ref = fullNameToFieldType.get(fieldMapper.fieldType().names().fullName()); MappedFieldTypeReference ref = fullNameToFieldType.get(fieldMapper.fieldType().names().fullName());
if (ref != null) { if (ref != null) {
List<String> conflicts = new ArrayList<>(); List<String> conflicts = new ArrayList<>();
ref.get().checkTypeName(fieldMapper.fieldType(), conflicts); final Set<String> types = fullNameToTypes.get(fieldMapper.fieldType().names().fullName());
if (conflicts.isEmpty()) { // only check compat if they are the same type boolean strict = beStrict(type, types, updateAllTypes);
final Set<String> types = fullNameToTypes.get(fieldMapper.fieldType().names().fullName()); ref.get().checkCompatibility(fieldMapper.fieldType(), conflicts, strict);
boolean strict = beStrict(type, types, updateAllTypes);
ref.get().checkCompatibility(fieldMapper.fieldType(), conflicts, strict);
}
if (conflicts.isEmpty() == false) { if (conflicts.isEmpty() == false) {
throw new IllegalArgumentException("Mapper for [" + fieldMapper.fieldType().names().fullName() + "] conflicts with existing mapping in other types:\n" + conflicts.toString()); throw new IllegalArgumentException("Mapper for [" + fieldMapper.fieldType().names().fullName() + "] conflicts with existing mapping in other types:\n" + conflicts.toString());
} }
@ -169,12 +166,9 @@ class FieldTypeLookup implements Iterable<MappedFieldType> {
MappedFieldTypeReference indexNameRef = indexNameToFieldType.get(fieldMapper.fieldType().names().indexName()); MappedFieldTypeReference indexNameRef = indexNameToFieldType.get(fieldMapper.fieldType().names().indexName());
if (indexNameRef != null) { if (indexNameRef != null) {
List<String> conflicts = new ArrayList<>(); List<String> conflicts = new ArrayList<>();
indexNameRef.get().checkTypeName(fieldMapper.fieldType(), conflicts); final Set<String> types = indexNameToTypes.get(fieldMapper.fieldType().names().indexName());
if (conflicts.isEmpty()) { // only check compat if they are the same type boolean strict = beStrict(type, types, updateAllTypes);
final Set<String> types = indexNameToTypes.get(fieldMapper.fieldType().names().indexName()); indexNameRef.get().checkCompatibility(fieldMapper.fieldType(), conflicts, strict);
boolean strict = beStrict(type, types, updateAllTypes);
indexNameRef.get().checkCompatibility(fieldMapper.fieldType(), conflicts, strict);
}
if (conflicts.isEmpty() == false) { if (conflicts.isEmpty() == false) {
throw new IllegalArgumentException("Mapper for [" + fieldMapper.fieldType().names().fullName() + "] conflicts with mapping with the same index name in other types" + conflicts.toString()); throw new IllegalArgumentException("Mapper for [" + fieldMapper.fieldType().names().fullName() + "] conflicts with mapping with the same index name in other types" + conflicts.toString());
} }

View File

@ -229,9 +229,9 @@ public abstract class MappedFieldType extends FieldType {
public abstract String typeName(); public abstract String typeName();
/** Checks this type is the same type as other. Adds a conflict if they are different. */ /** Checks this type is the same type as other. Adds a conflict if they are different. */
public final void checkTypeName(MappedFieldType other, List<String> conflicts) { private final void checkTypeName(MappedFieldType other) {
if (typeName().equals(other.typeName()) == false) { if (typeName().equals(other.typeName()) == false) {
conflicts.add("mapper [" + names().fullName() + "] cannot be changed from type [" + typeName() + "] to [" + other.typeName() + "]"); throw new IllegalArgumentException("mapper [" + names().fullName() + "] cannot be changed from type [" + typeName() + "] to [" + other.typeName() + "]");
} else if (getClass() != other.getClass()) { } else if (getClass() != other.getClass()) {
throw new IllegalStateException("Type names equal for class " + getClass().getSimpleName() + " and " + other.getClass().getSimpleName()); throw new IllegalStateException("Type names equal for class " + getClass().getSimpleName() + " and " + other.getClass().getSimpleName());
} }
@ -243,6 +243,8 @@ public abstract class MappedFieldType extends FieldType {
* Otherwise, only properties which must never change in an index are checked. * Otherwise, only properties which must never change in an index are checked.
*/ */
public void checkCompatibility(MappedFieldType other, List<String> conflicts, boolean strict) { public void checkCompatibility(MappedFieldType other, List<String> conflicts, boolean strict) {
checkTypeName(other);
boolean indexed = indexOptions() != IndexOptions.NONE; boolean indexed = indexOptions() != IndexOptions.NONE;
boolean mergeWithIndexed = other.indexOptions() != IndexOptions.NONE; boolean mergeWithIndexed = other.indexOptions() != IndexOptions.NONE;
// TODO: should be validating if index options go "up" (but "down" is ok) // TODO: should be validating if index options go "up" (but "down" is ok)

View File

@ -281,7 +281,7 @@ public abstract class FieldTypeTestCase extends ESTestCase {
public void testCheckTypeName() { public void testCheckTypeName() {
final MappedFieldType fieldType = createNamedDefaultFieldType(); final MappedFieldType fieldType = createNamedDefaultFieldType();
List<String> conflicts = new ArrayList<>(); List<String> conflicts = new ArrayList<>();
fieldType.checkTypeName(fieldType, conflicts); fieldType.checkCompatibility(fieldType, conflicts, random().nextBoolean()); // no exception
assertTrue(conflicts.toString(), conflicts.isEmpty()); assertTrue(conflicts.toString(), conflicts.isEmpty());
MappedFieldType bogus = new MappedFieldType() { MappedFieldType bogus = new MappedFieldType() {
@ -291,7 +291,7 @@ public abstract class FieldTypeTestCase extends ESTestCase {
public String typeName() { return fieldType.typeName();} public String typeName() { return fieldType.typeName();}
}; };
try { try {
fieldType.checkTypeName(bogus, conflicts); fieldType.checkCompatibility(bogus, conflicts, random().nextBoolean());
fail("expected bad types exception"); fail("expected bad types exception");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("Type names equal")); assertTrue(e.getMessage().contains("Type names equal"));
@ -304,10 +304,13 @@ public abstract class FieldTypeTestCase extends ESTestCase {
@Override @Override
public String typeName() { return "othertype";} public String typeName() { return "othertype";}
}; };
fieldType.checkTypeName(other, conflicts); try {
assertFalse(conflicts.isEmpty()); fieldType.checkCompatibility(other, conflicts, random().nextBoolean());
assertTrue(conflicts.get(0).contains("cannot be changed from type")); fail();
assertEquals(1, conflicts.size()); } catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("cannot be changed from type"));
}
assertTrue(conflicts.toString(), conflicts.isEmpty());
} }
public void testCheckCompatibility() { public void testCheckCompatibility() {

View File

@ -151,7 +151,7 @@ public class UpdateMappingTests extends ESSingleNodeTestCase {
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
assertTrue(e.getMessage().contains("conflicts with existing mapping in other types")); assertTrue(e.getMessage(), e.getMessage().contains("mapper [foo] cannot be changed from type [long] to [double]"));
} }
try { try {
@ -159,7 +159,7 @@ public class UpdateMappingTests extends ESSingleNodeTestCase {
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
assertTrue(e.getMessage().contains("conflicts with existing mapping in other types")); assertTrue(e.getMessage(), e.getMessage().contains("mapper [foo] cannot be changed from type [long] to [double]"));
} }
assertTrue(mapperService.documentMapper("type1").mapping().root().getMapper("foo") instanceof LongFieldMapper); assertTrue(mapperService.documentMapper("type1").mapping().root().getMapper("foo") instanceof LongFieldMapper);
@ -186,7 +186,7 @@ public class UpdateMappingTests extends ESSingleNodeTestCase {
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
assertTrue(e.getMessage().contains("conflicts with existing mapping in other types")); assertTrue(e.getMessage(), e.getMessage().contains("mapper [foo] cannot be changed from type [long] to [double]"));
} }
try { try {
@ -194,7 +194,7 @@ public class UpdateMappingTests extends ESSingleNodeTestCase {
fail(); fail();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
assertTrue(e.getMessage().contains("conflicts with existing mapping in other types")); assertTrue(e.getMessage(), e.getMessage().contains("mapper [foo] cannot be changed from type [long] to [double]"));
} }
assertTrue(mapperService.documentMapper("type1").mapping().root().getMapper("foo") instanceof LongFieldMapper); assertTrue(mapperService.documentMapper("type1").mapping().root().getMapper("foo") instanceof LongFieldMapper);