diff options
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.java | 59 |
1 files changed, 48 insertions, 11 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 a56364a6975..857959d0678 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 @@ -7,6 +7,11 @@ 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. @@ -14,33 +19,48 @@ import com.yahoo.document.StructDataType; * Currently we support: * - array of simple struct * - map of primitive type to simple struct + * - map of primitive type to primitive type * * @author geirst */ public class ComplexAttributeFieldUtils { - public static boolean isArrayOfSimpleStruct(ImmutableSDField field) { - return isArrayOfSimpleStruct(field.getDataType()); + public static boolean isSupportedComplexField(ImmutableSDField field, SDDocumentType docType) { + return (isArrayOfSimpleStruct(field, docType) || + isMapOfSimpleStruct(field, docType) || + isMapOfPrimitiveType(field)); + } + + 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()); + return isSimpleStruct(arrayType.getNestedType(), docType); } else { return false; } } - public static boolean isMapOfSimpleStruct(ImmutableSDField field) { - return isMapOfSimpleStruct(field.getDataType()); + 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; return isPrimitiveType(mapType.getKeyType()) && - isSimpleStruct(mapType.getValueType()); + isSimpleStruct(mapType.getValueType(), docType); } else { return false; } @@ -60,11 +80,15 @@ public class ComplexAttributeFieldUtils { } } - private static boolean isSimpleStruct(DataType type) { + private static boolean isSimpleStruct(DataType type, Optional<SDDocumentType> docType) { if (type instanceof StructDataType && !(type.equals(PositionDataType.INSTANCE))) { StructDataType structType = (StructDataType) type; - for (Field innerField : structType.getFields()) { + Collection<Field> structFields = getStructFields(structType, docType); + if (structFields.isEmpty()) { + return false; + } + for (Field innerField : structFields) { if (!isPrimitiveType(innerField.getDataType())) { return false; } @@ -75,6 +99,19 @@ 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(DataType dataType) { return dataType.equals(DataType.BYTE) || dataType.equals(DataType.INT) || @@ -84,10 +121,10 @@ public class ComplexAttributeFieldUtils { dataType.equals(DataType.STRING); } - public static boolean isComplexFieldWithOnlyStructFieldAttributes(ImmutableSDField field) { - if (isArrayOfSimpleStruct(field)) { + public static boolean isComplexFieldWithOnlyStructFieldAttributes(ImmutableSDField field, SDDocumentType docType) { + if (isArrayOfSimpleStruct(field, docType)) { return hasOnlyStructFieldAttributes(field); - } else if (isMapOfSimpleStruct(field)) { + } else if (isMapOfSimpleStruct(field, docType)) { return hasSingleAttribute(field.getStructField("key")) && hasOnlyStructFieldAttributes(field.getStructField("value")); } else if (isMapOfPrimitiveType(field)) { |