summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/SDDocumentTypeOrderer.java
blob: ba34045e7de8720de090306bb9bca81991594822 (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
// 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.documentmodel.NewDocumentType;
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 final Map<DataTypeName, SDDocumentType> createdSDTypes = new LinkedHashMap<>();
    private final Set<Integer> seenTypes = new LinkedHashSet<>();
    List<SDDocumentType> processingOrder = new LinkedList<>();
    private final 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, type);
        }
    }

    private void process(SDDocumentType docOrStruct, SDDocumentType owningDocument) {
        resolveAndProcessInheritedTemporaryTypes(docOrStruct, owningDocument);
        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(), owningDocument);
            }
        }
        processingOrder.add(docOrStruct);
    }

    private void resolveAndProcessInheritedTemporaryTypes(SDDocumentType type, SDDocumentType owningDocument) {
        List<DataTypeName> toReplace = new ArrayList<>();
        for (SDDocumentType sdoc : type.getInheritedTypes()) {
            if (sdoc instanceof TemporarySDDocumentType) {
                toReplace.add(sdoc.getDocumentName());
            }
        }
        for (DataTypeName name : toReplace) {
            SDDocumentType inherited;
            if (type.isStruct()) {
                inherited = owningDocument.allTypes().get(new NewDocumentType.Name(name.getName()));
                if (inherited == null) throw new IllegalArgumentException("Struct '" + name + "' not found in " + owningDocument);
                process(inherited, owningDocument);
            }
            else {
                inherited = createdSDTypes.get(name);
                if (inherited == null) throw new IllegalArgumentException("Document type '" + name + "' not found");
                process(inherited, inherited);
            }
            type.inherit(inherited);
        }
    }

    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, SDDocumentType owningDocument) {
        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() + "'");
            }
            process(sdDocType, owningDocument);
            return;
        }

        if (type instanceof MapDataType) {
            MapDataType mType = (MapDataType) type;
            visit(mType.getValueType(), owningDocument);
            visit(mType.getKeyType(), owningDocument);
        } else if (type instanceof WeightedSetDataType) {
            WeightedSetDataType wType = (WeightedSetDataType) type;
            visit(wType.getNestedType(), owningDocument);
        } else if (type instanceof CollectionDataType) {
            CollectionDataType cType = (CollectionDataType) type;
            visit(cType.getNestedType(), owningDocument);
        } 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);
        }
    }

}