// 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 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 owner; private final Map constants = new ConcurrentHashMap<>(); public RankingConstants(FileRegistry fileRegistry, Optional 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 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 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); } }