aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/types/QueryProfileType.java
blob: 3da2ad53f9a6282779ef3ff06fe595ee2aa52bb6 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.types;

import com.yahoo.component.ComponentId;
import com.yahoo.component.provider.FreezableSimpleComponent;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.query.profile.QueryProfile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.yahoo.text.Lowercase.toLowerCase;

/**
 * Defines a kind of query profiles
 *
 * @author bratseth
 */
public class QueryProfileType extends FreezableSimpleComponent {

    private final CompoundName componentIdAsCompoundName;

    /** The fields of this query profile type */
    private Map<String, FieldDescription> fields;

    /** The query profile types this inherits */
    private List<QueryProfileType> inherited;

    /** If this is true, keys which are not declared in this type cannot be set in instances */
    private boolean strict = false;

    /** True if the name of instances of this profile should be matched as path names, see QueryProfileRegistry */
    private boolean matchAsPath = false;

    private boolean builtin = false;

    /** Aliases *from* any strings *to* field names. Aliases are case insensitive */
    private Map<String, String> aliases = null;

    public QueryProfileType(String idString) {
        this(new ComponentId(idString));
    }

    public QueryProfileType(ComponentId id) {
        this(id, new LinkedHashMap<>(), new ArrayList<>());
    }

    private QueryProfileType(ComponentId id, Map<String, FieldDescription> fields, List<QueryProfileType> inherited) {
        super(id);
        QueryProfile.validateName(id.getName());
        componentIdAsCompoundName = new CompoundName(getId().getName());
        this.fields = fields;
        this.inherited = inherited;
    }

    private QueryProfileType(ComponentId id, Map<String, FieldDescription> fields, List<QueryProfileType> inherited,
                            boolean strict, boolean matchAsPath, boolean builtin, Map<String,String> aliases) {
        this(id, new LinkedHashMap<>(fields), new ArrayList<>(inherited));
        this.strict = strict;
        this.matchAsPath = matchAsPath;
        this.builtin = builtin;
        this.aliases = aliases == null ? null : new HashMap<>(aliases);
    }

    /** Return this is it is not frozen, returns a modifiable deeply unfrozen copy otherwise */
    public QueryProfileType unfrozen() {
        if ( ! isFrozen()) return this;

        // Unfreeze inherited query profile references
        List<QueryProfileType> unfrozenInherited = new ArrayList<>();
        for (QueryProfileType inheritedType : inherited) {
            unfrozenInherited.add(inheritedType.unfrozen());
        }

        // Unfreeze nested query profile references
        Map<String, FieldDescription> unfrozenFields = new LinkedHashMap<>();
        for (Map.Entry<String, FieldDescription> field : fields.entrySet()) {
            FieldDescription unfrozenFieldValue = field.getValue();
            if (field.getValue().getType() instanceof QueryProfileFieldType queryProfileFieldType) {
                if (queryProfileFieldType.getQueryProfileType() != null) {
                    QueryProfileFieldType unfrozenType =
                            new QueryProfileFieldType(queryProfileFieldType.getQueryProfileType().unfrozen());
                    unfrozenFieldValue = field.getValue().withType(unfrozenType);
                }
            }
            unfrozenFields.put(field.getKey(), unfrozenFieldValue);
        }

        return new QueryProfileType(getId(), unfrozenFields, unfrozenInherited, strict, matchAsPath, builtin, aliases);
    }

    public CompoundName getComponentIdAsCompoundName() { return componentIdAsCompoundName; }

    /** Mark this type as built into the system. Do not use */
    public void setBuiltin(boolean builtin) { this.builtin=builtin; }

    /** Returns whether this type is built into the system */
    public boolean isBuiltin() { return builtin; }

    /**
     * Returns the query profile types inherited from this (never null).
     * If this profile type is not frozen, this list can be modified to change the set of inherited types.
     * If it is frozen, the returned list is immutable.
     */
    public List<QueryProfileType> inherited() { return inherited; }

    /**
     * Returns the fields declared in this (i.e not including those inherited) as an immutable map.
     *
     * @throws IllegalStateException if this is frozen
     */
    public Map<String, FieldDescription> declaredFields() {
        ensureNotFrozen();
        return Collections.unmodifiableMap(fields);
    }

    /**
     * Returns true if <i>this</i> is declared strict.
     *
     * @throws IllegalStateException if this is frozen
     */
    public boolean isDeclaredStrict() {
        ensureNotFrozen();
        return strict;
    }

    /**
     * Returns true if <i>this</i> is declared as match as path.
     *
     * @throws IllegalStateException if this is frozen
     */
    public boolean getDeclaredMatchAsPath() {
        ensureNotFrozen();
        return matchAsPath;
    }

    /** Set whether nondeclared fields are permissible. Throws an exception if this is frozen. */
    public void setStrict(boolean strict) {
        ensureNotFrozen();
        this.strict=strict;
    }

    /** Returns whether field not declared in this type is permissible in instances. Default is false: Additional values are allowed */
    public boolean isStrict() {
        if (isFrozen()) return strict;

        // Check if any of this or an inherited is true
        if (strict) return true;
        for (QueryProfileType inheritedType : inherited)
            if (inheritedType.isStrict()) return true;
        return false;
    }

    /** Returns whether instances of this should be matched as path names. Throws if this is frozen. */
    public void setMatchAsPath(boolean matchAsPath) {
        ensureNotFrozen();
        this.matchAsPath=matchAsPath;
    }

    /** Returns whether instances of this should be matched as path names. Default is false: Use exact name matching. */
    public boolean getMatchAsPath() {
        if (isFrozen()) return matchAsPath;

        // Check if any of this or an inherited is true
        if (matchAsPath) return true;
        for (QueryProfileType inheritedType : inherited)
            if (inheritedType.getMatchAsPath()) return true;
        return false;
    }

    public void freeze() {
        if (isFrozen()) return;
        // Flatten for faster lookup
        for (QueryProfileType inheritedType : inherited) {
            for (FieldDescription field : inheritedType.fields().values())
                if ( ! fields.containsKey(field.getName())) {
                    fields.put(field.getName(), field);
                }
        }
        fields = Collections.unmodifiableMap(fields);
        inherited = List.copyOf(inherited);
        strict = isStrict();
        matchAsPath = getMatchAsPath();
        super.freeze();
    }

    /**
     * Returns whether the given field name is overridable in this type.
     * Default: true (so all non-declared fields returns true)
     */
    public boolean isOverridable(String fieldName) {
        FieldDescription field = getField(fieldName);
        if (field == null) return true;
        return field.isOverridable();
    }

    /**
     * Returns the permissible class for the value of the given name in this type
     *
     * @return the permissible class for a value, <code>Object</code> if all types are legal,
     *         null if no types are legal (i.e if the name is not legal)
     */
    public Class<?> getValueClass(String name) {
        FieldDescription fieldDescription = getField(name);
        if (fieldDescription == null) {
            if (strict)
                return null; // Undefined -> Not legal
            else
                return Object.class; // Undefined -> Anything is legal
        }
        return fieldDescription.getType().getValueClass();
    }

    /** Returns the type of the given query profile type declared as a field in this */
    public QueryProfileType getType(String localName) {
        FieldDescription fieldDescription = getField(localName);
        if (fieldDescription == null) return null;
        if ( ! (fieldDescription.getType() instanceof QueryProfileFieldType)) return null;
        return ((QueryProfileFieldType) fieldDescription.getType()).getQueryProfileType();
    }

    /** Returns the type of the given name under this, of null if none */
    public FieldType getFieldType(CompoundName name) {
        FieldDescription field = getField(name.first());
        if (field == null) return null;

        FieldType fieldType = field.getType();
        if (name.size() == 1) return fieldType;

        if ( ! (fieldType instanceof QueryProfileFieldType)) return null;

        return ((QueryProfileFieldType)fieldType).getQueryProfileType().getFieldType(name.rest());
    }

    /** Returns the description of the given name under this, of null if none */
    public FieldDescription getField(CompoundName globalName) {
        FieldDescription field = getField(globalName.first());
        if (field == null) return null;

        if (globalName.size() == 1) return field;

        FieldType fieldType = field.getType();
        if ( ! (fieldType instanceof QueryProfileFieldType)) return null;

        return ((QueryProfileFieldType)fieldType).getQueryProfileType().getField(globalName.rest());
    }

    /**
     * Returns the description of the field with the given name in this type or an inherited type
     * (depth first left to right search). Returns null if the field is not defined in this or an inherited profile.
     */
    public FieldDescription getField(String localName) {
        FieldDescription field = fields.get(localName);
        if ( field != null ) return field;

        if ( isFrozen() ) return null; // Inherited are collapsed into this

        for (QueryProfileType inheritedType : this.inherited() ) {
            field = inheritedType.getField(localName);
            if (field != null) return field;
        }

        return null;
    }

    /**
     * Removes a field from this (not from any inherited profile)
     *
     * @return the removed field or null if none
     * @throws IllegalStateException if this is frozen
     */
    public FieldDescription removeField(String fieldName) {
        ensureNotFrozen();
        return fields.remove(fieldName);
    }

    /**
     * Adds a field to this, without associating with a type registry; field descriptions with compound
     * is not be supported.
     *
     * @throws IllegalStateException if this is frozen
     */
    public void addField(FieldDescription fieldDescription) {
        // Compound names translates to new types, which must be added to a supplied registry
        if (fieldDescription.getCompoundName().isCompound())
            throw new IllegalArgumentException("Adding compound names is only legal when supplying a registry");
        addField(fieldDescription, null);
    }

    /**
     * Adds a field to this
     *
     * @throws IllegalStateException if this is frozen
     */
    public void addField(FieldDescription fieldDescription, QueryProfileTypeRegistry registry) {
        CompoundName name = fieldDescription.getCompoundName();
        if (name.isCompound()) {
            // Add (/to) a query profile type containing the rest of the name.
            // (we do not need the field description settings for intermediate query profile types
            // as the leaf entry will enforce them)
            QueryProfileType type = extendOrCreateQueryProfileType(name.first(), registry);
            type.addField(fieldDescription.withName(name.rest()), registry);
        }
        else {
            ensureNotFrozen();
            fields.put(fieldDescription.getName(), fieldDescription);
        }

        for (String alias : fieldDescription.getAliases())
            addAlias(alias, fieldDescription.getName());
    }

    private QueryProfileType extendOrCreateQueryProfileType(String name, QueryProfileTypeRegistry registry) {
        QueryProfileType type = null;
        FieldDescription fieldDescription = getField(name);
        if (fieldDescription != null) {
            if ( ! (fieldDescription.getType() instanceof QueryProfileFieldType))
                throw new IllegalArgumentException("Cannot use name '" + name + "' as a prefix because it is " +
                                                   "already a " + fieldDescription.getType());
            QueryProfileFieldType fieldType = (QueryProfileFieldType) fieldDescription.getType();
            type = fieldType.getQueryProfileType();
        }

        if (type == null) {
            type = registry.getComponent(name);
        }

        // found in registry but not already added in *this* type (getField also checks parents): extend it
        if (type != null && ! fields.containsKey(name)) {
            type = new QueryProfileType(registry.createAnonymousId(type.getIdString()),
                                        new LinkedHashMap<>(),
                                        List.of(type));
        }

        if (type == null) { // create it
            type = new QueryProfileType(registry.createAnonymousId(name));
        }

        if (fieldDescription == null) {
            fieldDescription = new FieldDescription(name, new QueryProfileFieldType(type));
        }
        else {
            fieldDescription = fieldDescription.withType(new QueryProfileFieldType(type));
        }

        registry.register(type);
        fields.put(name, fieldDescription);
        return type;
    }

    private void addAlias(String alias, String field) {
        ensureNotFrozen();
        if (aliases == null)
            aliases = new HashMap<>();
        aliases.put(toLowerCase(alias), field);
    }

    /** Returns all the fields of this profile type and all types it inherits as a read-only map */
    public Map<String, FieldDescription> fields() {
        if (isFrozen()) return fields;
        if (inherited().size() == 0) return Collections.unmodifiableMap(fields);

        // Collapse inherited
        Map<String, FieldDescription> allFields = new LinkedHashMap<>();
        for (QueryProfileType inheritedType : inherited)
            allFields.putAll(inheritedType.fields());
        allFields.putAll(fields);
        return Collections.unmodifiableMap(allFields);
    }

    /**
     * Returns the alias to field mapping of this type as a read-only map. This is never null.
     * Note that all keys are lower-cased because aliases are case-insensitive
     */
    public Map<String, String> aliases() {
        if (isFrozen()) return aliases;
        if (aliases == null) return Map.of();
        return Collections.unmodifiableMap(aliases);
    }

    /** Returns the field name of an alias or field name */
    public String unalias(String aliasOrField) {
        if (aliases == null || aliases.isEmpty()) return aliasOrField;
        String field = aliases.get(toLowerCase(aliasOrField));
        if (field != null) return field;
        return aliasOrField;
    }

    @Override
    public int hashCode() {
        return getId().hashCode();
    }

    /** Two types are equal if they have the same id */
    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof QueryProfileType)) return false;
        QueryProfileType other = (QueryProfileType)o;
        return other.getId().equals(this.getId());
    }

    @Override
    public String toString() {
        return "query profile type '" + getId() + "'";
    }

}