From cf14979ed2766f05bd2f131b93d33996aa6662e3 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Thu, 29 Sep 2022 11:19:52 +0200 Subject: Use consistent terminology --- .../com/yahoo/schema/LargeRankingExpressions.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 config-model/src/main/java/com/yahoo/schema/LargeRankingExpressions.java (limited to 'config-model/src/main/java/com/yahoo/schema/LargeRankingExpressions.java') diff --git a/config-model/src/main/java/com/yahoo/schema/LargeRankingExpressions.java b/config-model/src/main/java/com/yahoo/schema/LargeRankingExpressions.java new file mode 100644 index 00000000000..2ae9041b1a2 --- /dev/null +++ b/config-model/src/main/java/com/yahoo/schema/LargeRankingExpressions.java @@ -0,0 +1,52 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.schema; + +import com.yahoo.config.application.api.FileRegistry; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +public class LargeRankingExpressions { + + private final Map expressions = new ConcurrentHashMap<>(); + private final FileRegistry fileRegistry; + private final int limit; + + public LargeRankingExpressions(FileRegistry fileRegistry) { + this(fileRegistry, 8192); + } + public LargeRankingExpressions(FileRegistry fileRegistry, int limit) { + this.fileRegistry = fileRegistry; + this.limit = limit; + } + + public void add(RankingExpressionBody expression) { + String name = expression.getName(); + RankingExpressionBody prev = expressions.putIfAbsent(name, expression); + if (prev == null) { + expression.validate(); + expression.register(fileRegistry); + } else { + if ( ! prev.getBlob().equals(expression.getBlob())) { + throw new IllegalArgumentException("Ranking expression '" + name + + "' defined twice. Previous blob with " + prev.getBlob().remaining() + + " bytes, while current has " + expression.getBlob().remaining() + " bytes"); + } + } + } + public int limit() { return limit; } + + /** Returns a read-only list of ranking constants ordered by name */ + public Collection expressions() { + return expressions.values().stream().sorted().collect(Collectors.toUnmodifiableList()); + } + + // Note: Use by integration tests in internal repo + /** Returns a read-only map of the ranking constants in this indexed by name */ + public Map asMap() { + return Collections.unmodifiableMap(expressions); + } + +} -- cgit v1.2.3