aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/application/validation/change/search/DocumentTypeChangeValidator.java
blob: 585651ca49520c8387b4f1ef5f7d3623970546b3 (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
159
160
161
162
163
164
// Copyright Vespa.ai. 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.application.api.ValidationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.document.StructDataType;
import com.yahoo.documentmodel.NewDocumentType;
import com.yahoo.document.Field;
import com.yahoo.vespa.model.application.validation.change.VespaConfigChangeAction;
import com.yahoo.vespa.model.application.validation.change.VespaRefeedAction;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Validates the changes between a current and next document type used in a document database.
 *
 * @author toregge
 */
public class DocumentTypeChangeValidator {

    private final ClusterSpec.Id id;
    private final NewDocumentType currentDocType;
    private final NewDocumentType nextDocType;

    private static abstract class FieldChange {

        protected final Field currentField;
        protected final Field nextField;

        public FieldChange(Field currentField, Field nextField) {
            this.currentField = currentField;
            this.nextField = nextField;
        }

        public String fieldName() {
            return currentField.getName();
        }

        public boolean valid() {
            return nextField != null;
        }

        public abstract boolean changedType();
        public abstract String currentTypeName();
        public abstract String nextTypeName();
    }

    private static class SimpleFieldChange extends FieldChange {

        public SimpleFieldChange(Field currentField, Field nextField) {
            super(currentField, nextField);
        }

        public boolean changedType() {
            return !currentField.getDataType().equals(nextField.getDataType());
        }

        public String currentTypeName() {
            return currentField.getDataType().getName();
        }

        public String nextTypeName() {
            return nextField.getDataType().getName();
        }
    }

    private static class StructFieldChange extends FieldChange {

        private final StructDataType currentType;
        private final StructDataType nextType;

        public StructFieldChange(Field currentField, Field nextField) {
            super(currentField, nextField);
            this.currentType = (StructDataType)currentField.getDataType();
            this.nextType = (StructDataType)nextField.getDataType();
        }

        public boolean changedType() {
            return changedType(currentType, nextType);
        }

        public String currentTypeName() {
            return toString(currentType);
        }

        public String nextTypeName() {
            return toString(nextType);
        }

        private static boolean changedType(StructDataType currentType, StructDataType nextType) {
            for (Field currentField : currentType.getFields()) {
                Field nextField = nextType.getField(currentField.getName());
                if (nextField != null) {
                    if (areStructFields(currentField, nextField)) {
                        if (changedType((StructDataType) currentField.getDataType(),
                                (StructDataType) nextField.getDataType())) {
                            return true;
                        }
                    } else {
                        if (!currentField.getDataType().equals(nextField.getDataType())) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        private static String toString(StructDataType dataType) {
            StringBuilder builder = new StringBuilder();
            builder.append(dataType.getName()).append(":{");
            boolean first = true;
            for (Field field : dataType.getFields()) {
                if (!first) {
                    builder.append(",");
                }
                if (field.getDataType() instanceof StructDataType) {
                    builder.append(toString((StructDataType) field.getDataType()));
                } else {
                    builder.append(field.getName() + ":" + field.getDataType().getName());
                }
                first = false;
            }
            builder.append("}");
            return builder.toString();
        }
    }

    public DocumentTypeChangeValidator(ClusterSpec.Id id,
                                       NewDocumentType currentDocType,
                                       NewDocumentType nextDocType) {
        this.id = id;
        this.currentDocType = currentDocType;
        this.nextDocType = nextDocType;
    }

    public List<VespaConfigChangeAction> validate() {
        return currentDocType.getAllFields().stream().
                map(field -> createFieldChange(field, nextDocType)).
                filter(fieldChange -> fieldChange.valid() && fieldChange.changedType()).
                map(fieldChange -> VespaRefeedAction.of(id,
                                                        ValidationId.fieldTypeChange,
                                                        new ChangeMessageBuilder(fieldChange.fieldName()).
                                                                                 addChange("data type", fieldChange.currentTypeName(),
                                                                                 fieldChange.nextTypeName()).build()
                )).
                collect(Collectors.toList());
    }

    private static FieldChange createFieldChange(Field currentField, NewDocumentType nextDocType) {
        Field nextField = nextDocType.getField(currentField.getName());
        if (nextField != null && areStructFields(currentField, nextField)) {
            return new StructFieldChange(currentField, nextField);
        }
        return new SimpleFieldChange(currentField, nextField);
    }

    private static boolean areStructFields(Field currentField, Field nextField) {
        return (currentField.getDataType() instanceof StructDataType) &&
                (nextField.getDataType() instanceof StructDataType);
    }

}