aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/AllValuesQueryProfileVisitor.java
blob: 86bd50efb77e7c561d0d520b5a412703539e6ea7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile;

import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.query.profile.compiled.ValueWithSource;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bratseth
 */
final class AllValuesQueryProfileVisitor extends PrefixQueryProfileVisitor {

    private final Map<String, ValueWithSource> values = new HashMap<>();

    /* Lists all values starting at prefix */
    public AllValuesQueryProfileVisitor(CompoundName prefix, CompoundNameChildCache pathCache) {
        super(prefix, pathCache);
    }

    @Override
    public void onValue(String localName,
                        Object value,
                        DimensionBinding binding,
                        QueryProfile owner,
                        DimensionValues variant) {
        putValue(localName, value, null, owner, variant, binding);
    }

    @Override
    public void onQueryProfileInsidePrefix(QueryProfile profile,
                                           DimensionBinding binding,
                                           QueryProfile owner,
                                           DimensionValues variant) {
        putValue("", profile.getValue(), profile, owner, variant, binding);
    }

    private void putValue(String key,
                          Object value,
                          QueryProfile profile,
                          QueryProfile owner,
                          DimensionValues variant,
                          DimensionBinding binding) {
        CompoundName fullName = cache.append(currentPrefix, key);

        ValueWithSource existing = values.get(fullName.toString());

        // The first value encountered has priority and values have priority over profiles
        if (existing != null && (existing.value() != null || value == null)) return;

        Boolean isOverridable = owner != null ? owner.isLocalOverridable(key, binding) : null;

        values.put(fullName.toString(), new ValueWithSource(value,
                                                            owner == null ? "anonymous" : owner.getSource(),
                                                            isOverridable != null &&  ! isOverridable,
                                                            profile != null,
                                                            profile == null ? null : profile.getType(),
                                                            variant));
    }

    /** Returns the values resulting from this visiting */
    public Map<String, Object> values() {
        Map<String, Object> values = new HashMap<>();
        for (var entry : this.values.entrySet())
            values.put(entry.getKey(), entry.getValue().value());
        return values;
    }

    /** Returns the values with source resulting from this visiting */
    public Map<String, ValueWithSource> valuesWithSource() { return Collections.unmodifiableMap(values); }

    /** Returns false - we are not done until we have seen all */
    public boolean isDone() { return false; }

}