summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/AllValuesQueryProfileVisitor.java
blob: 6886b53e1d4716820bce45c92cf1e8167130d91f (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
// Copyright 2017 Yahoo Holdings. 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 Map<String, ValueWithSource> values = new HashMap<>();

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

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

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

    private void putValue(String key, Object value, QueryProfile owner, DimensionValues variant) {
        if (value == null) return;
        CompoundName fullName = currentPrefix.append(key);
        if (fullName.isEmpty()) return; // Avoid putting a non-leaf (subtree) root in the list
        if (values.containsKey(fullName.toString())) return; // The first value encountered has priority

        values.put(fullName.toString(), new ValueWithSource(value, owner.getSource(), 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; }

}