aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/processing/DisallowComplexMapAndWsetKeyTypes.java
blob: a5b4ca9a71f94582743bec6f397c8bd6e61c1f17 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.document.ArrayDataType;
import com.yahoo.document.Field;
import com.yahoo.schema.RankProfileRegistry;
import com.yahoo.document.DataType;
import com.yahoo.document.MapDataType;
import com.yahoo.document.PrimitiveDataType;
import com.yahoo.schema.Schema;
import com.yahoo.schema.document.SDField;
import com.yahoo.document.WeightedSetDataType;
import com.yahoo.vespa.model.container.search.QueryProfiles;

/**
 * Non-primitive key types for map and weighted set forbidden (though OK in document model)
 *
 * @author Vegard Havdal
 */
public class DisallowComplexMapAndWsetKeyTypes extends Processor {

    public DisallowComplexMapAndWsetKeyTypes(Schema schema, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(schema, deployLogger, rankProfileRegistry, queryProfiles);
    }

    @Override
    public void process(boolean validate, boolean documentsOnly) {
        if ( ! validate) return;

    	// TODO also traverse struct types to search for bad map or wset types.
        // Do this after document manager is fixed, do not start using the static stuff on SDDocumentTypes any more.
        for (SDField field : schema.allConcreteFields()) {
            checkFieldType(field, field.getDataType());
        }
    }

    private void checkFieldType(Field field, DataType dataType) {
        if (dataType instanceof ArrayDataType) {
            DataType nestedType = ((ArrayDataType) dataType).getNestedType();
            checkFieldType(field, nestedType);
        } else if (dataType instanceof WeightedSetDataType) {
            DataType nestedType = ((WeightedSetDataType) dataType).getNestedType();
            if ( ! (nestedType instanceof PrimitiveDataType)) {
                fail(schema, field, "Weighted set must have a primitive key type.");
            }
        } else if (dataType instanceof MapDataType) {
            DataType keyType = ((MapDataType) dataType).getKeyType();
            if ( ! (keyType instanceof PrimitiveDataType)) {
                fail(schema, field, "Map key type must be a primitive type.");
            }
            checkFieldType(field, ((MapDataType) dataType).getValueType());
        }

    }

}