aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/application/validation/change/search/StructFieldAttributeChangeValidator.java
blob: 0909afb71e9ae133f300d47de1b249b99666d60d (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation.change.search;

import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.document.ArrayDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.document.MapDataType;
import com.yahoo.document.StructDataType;
import com.yahoo.documentmodel.NewDocumentType;
import com.yahoo.schema.derived.AttributeFields;
import com.yahoo.schema.document.Attribute;
import com.yahoo.schema.document.ComplexAttributeFieldUtils;
import com.yahoo.vespa.model.application.validation.change.VespaConfigChangeAction;
import com.yahoo.vespa.model.application.validation.change.VespaRestartAction;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

/**
 * Validates the changes between the current and next set of struct field attributes in a document database.
 *
 * Complex fields of the following types are considered (as they might have struct field attributes):
 *   - array of simple struct
 *   - map of simple struct
 *   - map of primitive types
 *
 * @author geirst
 */
public class StructFieldAttributeChangeValidator {

    private final ClusterSpec.Id id;
    private final NewDocumentType currentDocType;
    private final AttributeFields currentAttributes;
    private final NewDocumentType nextDocType;
    private final AttributeFields nextAttributes;

    public StructFieldAttributeChangeValidator(ClusterSpec.Id id,
                                               NewDocumentType currentDocType,
                                               AttributeFields currentAttributes,
                                               NewDocumentType nextDocType,
                                               AttributeFields nextAttributes) {
        this.id = id;
        this.currentDocType = currentDocType;
        this.currentAttributes = currentAttributes;
        this.nextDocType = nextDocType;
        this.nextAttributes = nextAttributes;
    }

    public List<VespaConfigChangeAction> validate() {
        List<VespaConfigChangeAction> result = new ArrayList<>();
        for (Field currentField : currentDocType.getAllFields()) {
            Field nextField = nextDocType.getField(currentField.getName());
            if (nextField != null) {
                result.addAll(validateAddAttributeAspect(new Context(currentField, currentAttributes),
                                                         new Context(nextField, nextAttributes)));
            }
        }
        return result;
    }

    private List<VespaConfigChangeAction> validateAddAttributeAspect(Context current, Context next) {
        return next.structFieldAttributes.stream()
                .filter(nextAttr -> current.hasFieldForStructFieldAttribute(nextAttr) &&
                                    !current.hasStructFieldAttribute(nextAttr))
                .map(nextAttr -> new VespaRestartAction(id, new ChangeMessageBuilder(nextAttr.getName()).addChange("add attribute aspect").build()))
                .collect(Collectors.toList());
    }

    private static class Context {
        public Field field;
        public Collection<Attribute> structFieldAttributes;

        public Context(Field field, AttributeFields attributes) {
            this.field = field;
            this.structFieldAttributes = attributes.structFieldAttributes(field.getName());
        }

        public DataType dataType() {
            return field.getDataType();
        }

        public boolean hasStructFieldAttribute(Attribute structFieldAttribute) {
            return structFieldAttributes.stream()
                    .anyMatch(attr -> attr.getName().equals(structFieldAttribute.getName()));
        }

        public boolean hasFieldForStructFieldAttribute(Attribute structFieldAttribute) {
            StringTokenizer fieldNames = new StringTokenizer(structFieldAttribute.getName(), ".");
            if (!fieldNames.nextToken().equals(field.getName())) {
                return false;
            }
            if (isArrayOfStructType(dataType())) {
                StructDataType nestedType = (StructDataType)((ArrayDataType)dataType()).getNestedType();
                if (structTypeContainsLastFieldNameComponent(nestedType, fieldNames)) {
                    return true;
                }
            } else if (isMapOfStructType(dataType())) {
                MapDataType mapType = (MapDataType)dataType();
                StructDataType valueType = (StructDataType)mapType.getValueType();
                String subFieldName = fieldNames.nextToken();
                if (subFieldName.equals("key") && !fieldNames.hasMoreTokens()) {
                    return true;
                } else if (subFieldName.equals("value") && structTypeContainsLastFieldNameComponent(valueType, fieldNames)) {
                    return true;
                }
            } else if (isMapOfPrimitiveType(dataType())) {
                String subFieldName = fieldNames.nextToken();
                if ((subFieldName.equals("key") || subFieldName.equals("value")) &&
                        !fieldNames.hasMoreTokens()) {
                    return true;
                }
            }
            return false;
        }

        private static boolean isArrayOfStructType(DataType type) {
            if (type instanceof ArrayDataType) {
                ArrayDataType arrayType = (ArrayDataType)type;
                return isStructType(arrayType.getNestedType());
            } else {
                return false;
            }
        }

        private static boolean isMapOfStructType(DataType type) {
            if (type instanceof MapDataType) {
                MapDataType mapType = (MapDataType)type;
                return ComplexAttributeFieldUtils.isPrimitiveType(mapType.getKeyType()) &&
                        isStructType(mapType.getValueType());
            } else {
                return false;
            }
        }

        public static boolean isMapOfPrimitiveType(DataType type) {
            if (type instanceof MapDataType) {
                MapDataType mapType = (MapDataType)type;
                return ComplexAttributeFieldUtils.isPrimitiveType(mapType.getKeyType()) &&
                        ComplexAttributeFieldUtils.isPrimitiveType(mapType.getValueType());
            } else {
                return false;
            }
        }

        private static boolean isStructType(DataType type) {
            return (type instanceof StructDataType);
        }

        private static boolean structTypeContainsLastFieldNameComponent(StructDataType structType, StringTokenizer fieldNames) {
            return structType.getField(fieldNames.nextToken()) != null && !fieldNames.hasMoreTokens();
        }
    }

}