summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/RankingConstants.java
blob: 1e0eacaea16af86fd6c1832832c5c37e53a09cfc (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
// 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.FileRegistry;
import com.yahoo.vespa.model.AbstractService;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * Constant values for ranking/model execution tied to a search definition, or globally to an application
 * package
 *
 * @author bratseth
 */
public class RankingConstants {

    private final FileRegistry fileRegistry;

    /** The schema this belongs to, or empty if it is global */
    private final Optional<Schema> owner;

    private final Map<String, RankingConstant> constants = new ConcurrentHashMap<>();

    public RankingConstants(FileRegistry fileRegistry, Optional<Schema> owner) {
        this.fileRegistry = fileRegistry;
        this.owner = owner;
    }

    public void add(RankingConstant constant) {
        constant.validate();
        constant.register(fileRegistry);
        String name = constant.getName();
        RankingConstant prev = constants.putIfAbsent(name, constant);
        if ( prev != null )
            throw new IllegalArgumentException("Ranking constant '" + name + "' defined twice");
    }

    public void putIfAbsent(RankingConstant constant) {
        constant.validate();
        constant.register(fileRegistry);
        String name = constant.getName();
        constants.putIfAbsent(name, constant);
    }

    public void computeIfAbsent(String name, Function<? super String, ? extends RankingConstant> createConstant) {
        constants.computeIfAbsent(name, key -> {
            RankingConstant constant = createConstant.apply(key);
            constant.validate();
            constant.register(fileRegistry);
            return constant;
        });
    }

    /** Returns the ranking constant with the given name, or null if not present */
    public RankingConstant get(String name) {
        var constant = constants.get(name);
        if (constant != null) return constant;
        if (owner.isPresent() && owner.get().inherited().isPresent())
            return owner.get().inherited().get().rankingConstants().get(name);
        return null;
    }

    /** Returns a read-only map of the ranking constants in this indexed by name */
    public Map<String, RankingConstant> asMap() {
        // Shortcuts
        if (owner.isEmpty() || owner.get().inherited().isEmpty()) return Collections.unmodifiableMap(constants);
        if (constants.isEmpty()) return owner.get().inherited().get().rankingConstants().asMap();

        var allConstants = new HashMap<>(owner.get().inherited().get().rankingConstants().asMap());
        allConstants.putAll(constants);
        return Collections.unmodifiableMap(allConstants);
    }

    /** Initiate sending of these constants to some services over file distribution */
    public void sendTo(Collection<? extends AbstractService> services) {
        asMap().values().forEach(constant -> constant.sendTo(services));
    }
}