summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/config/QueryProfileConfigurer.java
blob: f840885f2e43ef62fd6944b5cc89ba8d72f27746 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.config;

import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.config.subscription.ConfigSubscriber;
import com.yahoo.search.query.profile.DimensionValues;
import com.yahoo.search.query.profile.QueryProfile;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.search.query.profile.types.FieldDescription;
import com.yahoo.search.query.profile.types.FieldType;
import com.yahoo.search.query.profile.types.QueryProfileType;
import com.yahoo.search.query.profile.types.QueryProfileTypeRegistry;
import com.yahoo.text.BooleanParser;

import java.util.HashSet;
import java.util.Set;

/**
 * @author bratseth
 */
public class QueryProfileConfigurer implements ConfigSubscriber.SingleSubscriber<QueryProfilesConfig> {

    private final ConfigSubscriber subscriber = new ConfigSubscriber();

    private volatile QueryProfileRegistry currentRegistry;

    public QueryProfileConfigurer(String configId) {
        subscriber.subscribe(this, QueryProfilesConfig.class, configId);
    }

    /** Returns the registry created by the last occurring call to configure */
    public QueryProfileRegistry getCurrentRegistry() { return currentRegistry; }

    private void setCurrentRegistry(QueryProfileRegistry registry) {
        this.currentRegistry=registry;
    }

    public void configure(QueryProfilesConfig config) {
        QueryProfileRegistry registry = createFromConfig(config);
        setCurrentRegistry(registry);
    }

    public static QueryProfileRegistry createFromConfig(QueryProfilesConfig config) {
        QueryProfileRegistry registry = new QueryProfileRegistry();

        // Pass 1: Create all profiles and profile types
        for (QueryProfilesConfig.Queryprofiletype profileTypeConfig : config.queryprofiletype()) {
            createProfileType(profileTypeConfig, registry.getTypeRegistry());
        }
        for (QueryProfilesConfig.Queryprofile profileConfig : config.queryprofile()) {
            createProfile(profileConfig, registry);
        }

        // Pass 2: Resolve references and add content
        for (QueryProfilesConfig.Queryprofiletype profileTypeConfig : config.queryprofiletype()) {
            fillProfileType(profileTypeConfig, registry.getTypeRegistry());
        }

        // To ensure topological sorting, using DPS. This will _NOT_ detect cycles
        // (but it will not fail if they exist)
        Set<ComponentId> filled = new HashSet<>();
        for (QueryProfilesConfig.Queryprofile profileConfig : config.queryprofile()) {
            fillProfile(profileConfig, config, registry, filled);
        }

        registry.freeze();
        return registry;
    }

    /** Stop subscribing from this configurer */
    public void shutdown() {
        subscriber.close();
    }

    private static void createProfile(QueryProfilesConfig.Queryprofile config, QueryProfileRegistry registry) {
        QueryProfile profile = new QueryProfile(new ComponentId(config.id()), config.id());
        try {
            String typeId = config.type();
            if (typeId != null && ! typeId.isEmpty())
                profile.setType(registry.getType(typeId));

            if (config.dimensions().size()>0) {
                String[] dimensions = new String[config.dimensions().size()];
                for (int i = 0; i < config.dimensions().size(); i++)
                    dimensions[i] = config.dimensions().get(i);
                profile.setDimensions(dimensions);
            }

            registry.register(profile);
        }
        catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid " + profile, e);
        }
    }

    private static void createProfileType(QueryProfilesConfig.Queryprofiletype config, QueryProfileTypeRegistry registry) {
        QueryProfileType type = new QueryProfileType(config.id());
        type.setStrict(config.strict());
        type.setMatchAsPath(config.matchaspath());
        registry.register(type);
    }

    private static void fillProfile(QueryProfilesConfig.Queryprofile config,
                                    QueryProfilesConfig queryProfilesConfig,
                                    QueryProfileRegistry registry,
                                    Set<ComponentId> filled) {
        QueryProfile profile = registry.getComponent(new ComponentSpecification(config.id()).toId());
        if (filled.contains(profile.getId())) return;
        filled.add(profile.getId());
        try {
            for (String inheritedId : config.inherit()) {
                QueryProfile inherited = registry.getComponent(inheritedId);
                if (inherited == null)
                    throw new IllegalArgumentException("Inherited query profile '" + inheritedId + "' in " + profile + " was not found");
                fillProfile(inherited, queryProfilesConfig, registry, filled);
                profile.addInherited(inherited);
            }

            for (QueryProfilesConfig.Queryprofile.Reference referenceConfig : config.reference()) {
                QueryProfile referenced = registry.getComponent(referenceConfig.value());
                if (referenced == null)
                    throw new IllegalArgumentException("Query profile '" + referenceConfig.value() + "' referenced as '" +
                                                       referenceConfig.name() + "' in " + profile + " was not found");
                profile.set(referenceConfig.name(), referenced, registry);
                if (referenceConfig.overridable() != null && !referenceConfig.overridable().isEmpty())
                    profile.setOverridable(referenceConfig.name(),
                                           BooleanParser.parseBoolean(referenceConfig.overridable()),
                                           DimensionValues.empty);
            }

            for (QueryProfilesConfig.Queryprofile.Property propertyConfig : config.property()) {
                profile.set(propertyConfig.name(), propertyConfig.value(), registry);
                if (propertyConfig.overridable() != null && ! propertyConfig.overridable().isEmpty())
                    profile.setOverridable(propertyConfig.name(),
                                           BooleanParser.parseBoolean(propertyConfig.overridable()),
                                           DimensionValues.empty);
            }

            for (QueryProfilesConfig.Queryprofile.Queryprofilevariant variantConfig : config.queryprofilevariant()) {
                String[] forDimensionValueArray = new String[variantConfig.fordimensionvalues().size()];
                for (int i = 0; i<variantConfig.fordimensionvalues().size(); i++) {
                    forDimensionValueArray[i] = variantConfig.fordimensionvalues().get(i).trim();
                    if ("*".equals(forDimensionValueArray[i]))
                        forDimensionValueArray[i] = null;
                }
                DimensionValues forDimensionValues = DimensionValues.createFrom(forDimensionValueArray);

                for (String inheritedId : variantConfig.inherit()) {
                    QueryProfile inherited = registry.getComponent(inheritedId);
                    if (inherited == null)
                        throw new IllegalArgumentException("Inherited query profile '" + inheritedId + "' in " + profile +
                                                           " for '" + forDimensionValues + "' was not found");
                    fillProfile(inherited, queryProfilesConfig, registry, filled);
                    profile.addInherited(inherited, forDimensionValues);
                }

                for (QueryProfilesConfig.Queryprofile.Queryprofilevariant.Reference referenceConfig : variantConfig.reference()) {
                    QueryProfile referenced = registry.getComponent(referenceConfig.value());
                    if (referenced == null)
                        throw new IllegalArgumentException("Query profile '" + referenceConfig.value() + "' referenced as '" +
                                                           referenceConfig.name() + "' in " + profile +
                                                           " for '" + forDimensionValues + "' was not found");
                    profile.set(referenceConfig.name(), referenced, forDimensionValues, registry);
                    if ( ! referenceConfig.overridable().isEmpty())
                        profile.setOverridable(referenceConfig.name(),
                                               Boolean.parseBoolean(referenceConfig.overridable()),
                                               forDimensionValues);
                }

                for (QueryProfilesConfig.Queryprofile.Queryprofilevariant.Property propertyConfig : variantConfig.property()) {
                    profile.set(propertyConfig.name(), propertyConfig.value(), forDimensionValues, registry);
                    if ( ! propertyConfig.overridable().isEmpty())
                        profile.setOverridable(propertyConfig.name(),
                                               Boolean.parseBoolean(propertyConfig.overridable()),
                                               forDimensionValues);
                }

            }

        }
        catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid " + profile, e);
        }
    }

    /** Fill a given profile by locating its config */
    private static void fillProfile(QueryProfile inherited,
                                    QueryProfilesConfig queryProfilesConfig,
                                    QueryProfileRegistry registry,
                                    Set<ComponentId> visited) {
        for (QueryProfilesConfig.Queryprofile inheritedConfig : queryProfilesConfig.queryprofile()) {
            if (inherited.getId().stringValue().equals(inheritedConfig.id())) {
                fillProfile(inheritedConfig, queryProfilesConfig, registry, visited);
            }
        }
    }

    private static void fillProfileType(QueryProfilesConfig.Queryprofiletype config, QueryProfileTypeRegistry registry) {
        QueryProfileType type = registry.getComponent(new ComponentSpecification(config.id()).toId());
        try {

            for (String inheritedId : config.inherit()) {
                QueryProfileType inherited = registry.getComponent(inheritedId);
                if (inherited == null)
                    throw new IllegalArgumentException("Inherited query profile type '" + inheritedId + "' in " + type + " was not found");
                else
                    type.inherited().add(inherited);

            }

            for (QueryProfilesConfig.Queryprofiletype.Field fieldConfig : config.field())
                instantiateFieldDescription(fieldConfig,type,registry);
        }
        catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid " + type,e);
        }
    }

    private static void instantiateFieldDescription(QueryProfilesConfig.Queryprofiletype.Field fieldConfig,
                                                    QueryProfileType type,
                                                    QueryProfileTypeRegistry registry) {
        try {
            FieldType fieldType = FieldType.fromString(fieldConfig.type(), registry);
            FieldDescription field = new FieldDescription(fieldConfig.name(),
                                                          fieldType,
                                                          fieldConfig.alias(),
                                                          fieldConfig.mandatory(),
                                                          fieldConfig.overridable()
            );
            type.addField(field, registry);
        }
        catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid field '" + fieldConfig.name() + "' in " + type, e);
        }
    }


}