aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/document/ComplexAttributeFieldUtils.java
blob: 993c9180f78a517c200a78bc4f0c636a239c6bdd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.document;

import com.yahoo.document.ArrayDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.MapDataType;
import com.yahoo.document.StructDataType;

/**
 * Utils used to check whether a complex field supports being represented as struct field attributes.
 *
 * Currently we support:
 *   - array of simple struct
 *   - 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) {
        return (isArrayOfSimpleStruct(field) ||
                isMapOfSimpleStruct(field) ||
                isMapOfPrimitiveType(field));
    }

    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) {
        if (field.getDataType() instanceof MapDataType) {
            MapDataType mapType = (MapDataType)field.getDataType();
            return isPrimitiveType(mapType.getKeyType()) &&
                    isStructWithPrimitiveStructFieldAttributes(mapType.getValueType(),
                            field.getStructField("value"));
        } else {
            return false;
        }
    }

    public static boolean isMapOfPrimitiveType(ImmutableSDField field) {
        if (field.getDataType() instanceof MapDataType) {
            MapDataType mapType = (MapDataType)field.getDataType();
            return isPrimitiveType(mapType.getKeyType()) &&
                    isPrimitiveType(mapType.getValueType());
        } else {
            return false;
        }
    }

    private static boolean isStructWithPrimitiveStructFieldAttributes(DataType type, ImmutableSDField field) {
        if (type instanceof StructDataType && ! GeoPos.isPos(type)) {
            for (ImmutableSDField structField : field.getStructFields()) {
                Attribute attribute = structField.getAttributes().get(structField.getName());
                if (attribute != null) {
                    if (!isPrimitiveType(attribute)) {
                        return false;
                    }
                } else if (structField.wasConfiguredToDoAttributing()) {
                    if (!isPrimitiveType(structField.getDataType())) {
                        return false;
                    }
                }
            }
            return true;
        } else {
            return false;
        }
    }

    public static boolean isPrimitiveType(Attribute attribute) {
        return attribute.getCollectionType().equals(Attribute.CollectionType.SINGLE) &&
                isPrimitiveType(attribute.getDataType());
    }

    public static boolean isPrimitiveType(DataType dataType) {
        return dataType.equals(DataType.BYTE) ||
                dataType.equals(DataType.INT) ||
                dataType.equals(DataType.LONG) ||
                dataType.equals(DataType.FLOAT) ||
                dataType.equals(DataType.DOUBLE) ||
                dataType.equals(DataType.STRING);
    }

    public static boolean isComplexFieldWithOnlyStructFieldAttributes(ImmutableSDField field) {
        if (isArrayOfSimpleStruct(field)) {
            return hasOnlyStructFieldAttributes(field);
        } else if (isMapOfSimpleStruct(field)) {
            return (field.getStructField("key").hasSingleAttribute()) &&
                    hasOnlyStructFieldAttributes(field.getStructField("value"));
        } else if (isMapOfPrimitiveType(field)) {
            return (field.getStructField("key").hasSingleAttribute() &&
                    field.getStructField("value").hasSingleAttribute());
        }
        return false;
    }

    private static boolean hasOnlyStructFieldAttributes(ImmutableSDField field) {
        for (ImmutableSDField structField : field.getStructFields()) {
            if (!structField.hasSingleAttribute()) {
                return false;
            }
        }
        return true;
    }

}