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

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.document.*;
import com.yahoo.document.annotation.AnnotationReferenceDataType;
import com.yahoo.searchdefinition.document.SDDocumentType;
import com.yahoo.searchdefinition.document.TemporarySDDocumentType;

import java.util.*;
import java.util.logging.Level;

/**
 * @author Einar M R Rosenvinge
 */
public class SDDocumentTypeOrderer {

    private Map<DataTypeName, SDDocumentType> createdSDTypes = new LinkedHashMap<>();
    private Set<Integer> seenTypes = new LinkedHashSet<>();
    List<SDDocumentType> processingOrder = new LinkedList<>();
    private DeployLogger deployLogger;

    public SDDocumentTypeOrderer(List<SDDocumentType> sdTypes, DeployLogger deployLogger) {
        this.deployLogger = deployLogger;
        for (SDDocumentType type : sdTypes) {
            createdSDTypes.put(type.getDocumentName(), type);
        }
        DocumentTypeManager dtm = new DocumentTypeManager();
        for (DataType type : dtm.getDataTypes()) {
            seenTypes.add(type.getId());
        }

    }

    List<SDDocumentType> getOrdered() { return processingOrder; }

    public void process() {
        for (SDDocumentType type : createdSDTypes.values()) {
            process(type);
        }
    }
    private void process(SDDocumentType type) {
        List<DataTypeName> toReplace = new ArrayList<>();
        for (SDDocumentType sdoc : type.getInheritedTypes()) {
            if (sdoc instanceof TemporarySDDocumentType) {
                toReplace.add(sdoc.getDocumentName());
            }
        }
        for (DataTypeName name : toReplace) {
            SDDocumentType inherited = createdSDTypes.get(name);
            if (inherited == null) {
                throw new IllegalStateException("Document type '" + name + "' not found.");
            }
            process(inherited);
            type.inherit(inherited);
        }
        visit(type);
    }

    private void visit(SDDocumentType docOrStruct) {
        int id;
        if (docOrStruct.isStruct()) {
            id = new StructDataType(docOrStruct.getName()).getId();
        } else {
            id = new DocumentType(docOrStruct.getName()).getId();
        }

        if (seenTypes.contains(id)) {
            return;
        } else {
            seenTypes.add((new StructDataType(docOrStruct.getName()).getId()));
        }


        for (Field field : docOrStruct.fieldSet()) {
            if (!seenTypes.contains(field.getDataType().getId())) {
                //we haven't seen this before, do it
                visit(field.getDataType());
            }
        }
        processingOrder.add(docOrStruct);
    }

    private SDDocumentType find(String name) {
        SDDocumentType sdDocType = createdSDTypes.get(new DataTypeName(name));
        if (sdDocType != null) {
            return sdDocType;
        }
        for(SDDocumentType sdoc : createdSDTypes.values()) {
             for (SDDocumentType stype : sdoc.getTypes()) {
                 if (stype.getName().equals(name)) {
                    return stype;
                 }
             }
        }
        return null;
    }
    private void visit(DataType type) {
        if (type instanceof StructuredDataType) {
            StructuredDataType structType = (StructuredDataType) type;
            SDDocumentType sdDocType = find(structType.getName());
            if (sdDocType == null) {
                throw new IllegalArgumentException("Could not find struct '" + type.getName() + "'.");
            }
            visit(sdDocType);
            return;
        }

        if (type instanceof MapDataType) {
            MapDataType mType = (MapDataType) type;
            visit(mType.getValueType());
            visit(mType.getKeyType());
        } else if (type instanceof WeightedSetDataType) {
            WeightedSetDataType wType = (WeightedSetDataType) type;
            visit(wType.getNestedType());
        } else if (type instanceof CollectionDataType) {
            CollectionDataType cType = (CollectionDataType) type;
            visit(cType.getNestedType());
        } else if (type instanceof AnnotationReferenceDataType) {
            //do nothing
        } else if (type instanceof PrimitiveDataType) {
            //do nothing
        } else if (type instanceof TensorDataType) {
            //do nothing
        } else if (type instanceof ReferenceDataType) {
            //do nothing
        } else {
            deployLogger.logApplicationPackage(Level.WARNING, "Unknown type : " + type);
        }
    }
}