summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java
diff options
context:
space:
mode:
authorGeir Storli <geirst@oath.com>2018-08-29 14:05:36 +0200
committerGeir Storli <geirst@oath.com>2018-08-29 14:21:08 +0200
commitae38db15dee838d8e6dcf9eaeabf08690a53af0e (patch)
treecf6ed06379933ed6bd085708b305f1be30f89dea /config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java
parent4b4ee6503ad05926218b9a0d01a6ac0846bdf71c (diff)
Relax validation of complex attribute fields by only considering the defined struct field attributes.
The struct type itself can contain fields of any type, but only primitive struct field attributes are supported in the complex field (array or map) using the struct type.
Diffstat (limited to 'config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java')
-rw-r--r--config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java100
1 files changed, 31 insertions, 69 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java b/config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java
index 72eb1c96e0f..c3ac8c77173 100644
--- a/config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java
+++ b/config-model/src/main/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtils.java
@@ -3,15 +3,9 @@ package com.yahoo.searchdefinition.document;
import com.yahoo.document.ArrayDataType;
import com.yahoo.document.DataType;
-import com.yahoo.document.Field;
import com.yahoo.document.MapDataType;
import com.yahoo.document.PositionDataType;
import com.yahoo.document.StructDataType;
-import com.yahoo.document.TemporaryStructuredDataType;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Optional;
/**
* Utils used to check whether a complex field supports being represented as struct field attributes.
@@ -21,64 +15,42 @@ import java.util.Optional;
* - map of primitive type to simple struct
* - map of primitive type to primitive type
*
+ * A simple struct can contain fields of any type, but only fields of primitive type can be defined as
+ * struct field attributes in the complex field using the simple struct.
+ *
* @author geirst
*/
public class ComplexAttributeFieldUtils {
- public static boolean isSupportedComplexField(ImmutableSDField field, SDDocumentType docType) {
- return (isArrayOfSimpleStruct(field, docType) ||
- isMapOfSimpleStruct(field, docType) ||
+ public static boolean isSupportedComplexField(ImmutableSDField field) {
+ return (isArrayOfSimpleStruct(field) ||
+ isMapOfSimpleStruct(field) ||
isMapOfPrimitiveType(field));
}
- public static boolean isSupportedComplexField(DataType fieldType) {
- return (isArrayOfSimpleStruct(fieldType) ||
- isMapOfSimpleStruct(fieldType) ||
- isMapOfPrimitiveType(fieldType));
- }
-
- public static boolean isArrayOfSimpleStruct(ImmutableSDField field, SDDocumentType docType) {
- return isArrayOfSimpleStruct(field.getDataType(), Optional.of(docType));
- }
-
- public static boolean isArrayOfSimpleStruct(DataType fieldType) {
- return isArrayOfSimpleStruct(fieldType, Optional.empty());
- }
-
- private static boolean isArrayOfSimpleStruct(DataType fieldType, Optional<SDDocumentType> docType) {
- if (fieldType instanceof ArrayDataType) {
- ArrayDataType arrayType = (ArrayDataType)fieldType;
- return isSimpleStruct(arrayType.getNestedType(), docType);
+ public static boolean isArrayOfSimpleStruct(ImmutableSDField field) {
+ if (field.getDataType() instanceof ArrayDataType) {
+ ArrayDataType arrayType = (ArrayDataType)field.getDataType();
+ return isStructWithPrimitiveStructFieldAttributes(arrayType.getNestedType(), field);
} else {
return false;
}
}
- public static boolean isMapOfSimpleStruct(ImmutableSDField field, SDDocumentType docType) {
- return isMapOfSimpleStruct(field.getDataType(), Optional.of(docType));
- }
-
- public static boolean isMapOfSimpleStruct(DataType fieldType) {
- return isMapOfSimpleStruct(fieldType, Optional.empty());
- }
-
- private static boolean isMapOfSimpleStruct(DataType fieldType, Optional<SDDocumentType> docType) {
- if (fieldType instanceof MapDataType) {
- MapDataType mapType = (MapDataType)fieldType;
+ public static boolean isMapOfSimpleStruct(ImmutableSDField field) {
+ if (field.getDataType() instanceof MapDataType) {
+ MapDataType mapType = (MapDataType)field.getDataType();
return isPrimitiveType(mapType.getKeyType()) &&
- isSimpleStruct(mapType.getValueType(), docType);
+ isStructWithPrimitiveStructFieldAttributes(mapType.getValueType(),
+ field.getStructField("value"));
} else {
return false;
}
}
public static boolean isMapOfPrimitiveType(ImmutableSDField field) {
- return isMapOfPrimitiveType(field.getDataType());
- }
-
- public static boolean isMapOfPrimitiveType(DataType fieldType) {
- if (fieldType instanceof MapDataType) {
- MapDataType mapType = (MapDataType)fieldType;
+ if (field.getDataType() instanceof MapDataType) {
+ MapDataType mapType = (MapDataType)field.getDataType();
return isPrimitiveType(mapType.getKeyType()) &&
isPrimitiveType(mapType.getValueType());
} else {
@@ -86,17 +58,15 @@ public class ComplexAttributeFieldUtils {
}
}
- private static boolean isSimpleStruct(DataType type, Optional<SDDocumentType> docType) {
+ private static boolean isStructWithPrimitiveStructFieldAttributes(DataType type, ImmutableSDField field) {
if (type instanceof StructDataType &&
!(type.equals(PositionDataType.INSTANCE))) {
- StructDataType structType = (StructDataType) type;
- Collection<Field> structFields = getStructFields(structType, docType);
- if (structFields.isEmpty()) {
- return false;
- }
- for (Field innerField : structFields) {
- if (!isPrimitiveType(innerField.getDataType())) {
- return false;
+ for (ImmutableSDField structField : field.getStructFields()) {
+ Attribute attribute = structField.getAttributes().get(structField.getName());
+ if (attribute != null) {
+ if (!isPrimitiveType(attribute)) {
+ return false;
+ }
}
}
return true;
@@ -105,20 +75,12 @@ public class ComplexAttributeFieldUtils {
}
}
- private static Collection<Field> getStructFields(StructDataType structType, Optional<SDDocumentType> docType) {
- // The struct data type might be unresolved at this point. If so we use the document type to resolve it.
- if (docType.isPresent() && (structType instanceof TemporaryStructuredDataType)) {
- SDDocumentType realStructType = docType.get().getOwnedType(structType.getName());
- if (structType != null) {
- return realStructType.getDocumentType().getFields();
- }
- return Collections.emptyList();
- } else {
- return structType.getFields();
- }
+ private static boolean isPrimitiveType(Attribute attribute) {
+ return attribute.getCollectionType().equals(Attribute.CollectionType.SINGLE) &&
+ isPrimitiveType(attribute.getDataType());
}
- private static boolean isPrimitiveType(DataType dataType) {
+ public static boolean isPrimitiveType(DataType dataType) {
return dataType.equals(DataType.BYTE) ||
dataType.equals(DataType.INT) ||
dataType.equals(DataType.LONG) ||
@@ -127,10 +89,10 @@ public class ComplexAttributeFieldUtils {
dataType.equals(DataType.STRING);
}
- public static boolean isComplexFieldWithOnlyStructFieldAttributes(ImmutableSDField field, SDDocumentType docType) {
- if (isArrayOfSimpleStruct(field, docType)) {
+ public static boolean isComplexFieldWithOnlyStructFieldAttributes(ImmutableSDField field) {
+ if (isArrayOfSimpleStruct(field)) {
return hasOnlyStructFieldAttributes(field);
- } else if (isMapOfSimpleStruct(field, docType)) {
+ } else if (isMapOfSimpleStruct(field)) {
return hasSingleAttribute(field.getStructField("key")) &&
hasOnlyStructFieldAttributes(field.getStructField("value"));
} else if (isMapOfPrimitiveType(field)) {