diff options
author | Harald Musum <musum@verizonmedia.com> | 2022-05-15 22:34:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-15 22:34:34 +0200 |
commit | 02f96c03fd5b434788e1c5e77436c99bb7562f77 (patch) | |
tree | 257d5bf34bae451bb11700896824ce73c73be1f5 /config-model/src/main/java/com/yahoo/searchdefinition | |
parent | 2a382c9221a704c6bd33b5aabe7c9a0d756eba63 (diff) |
Revert "Bratseth/constants cleanup 2"
Diffstat (limited to 'config-model/src/main/java/com/yahoo/searchdefinition')
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/derived/FileDistributedConstants.java | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/derived/FileDistributedConstants.java b/config-model/src/main/java/com/yahoo/searchdefinition/derived/FileDistributedConstants.java index 8de86beacdb..c9b0ed2f628 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/derived/FileDistributedConstants.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/derived/FileDistributedConstants.java @@ -20,30 +20,54 @@ import java.util.function.Function; */ public class FileDistributedConstants { - private final Map<String, DistributableConstant> constants; + private final FileRegistry fileRegistry; + + private final Map<String, DistributableConstant> constants = new LinkedHashMap<>(); public FileDistributedConstants(FileRegistry fileRegistry, Collection<RankProfile.Constant> constants) { - Map<String, DistributableConstant> distributableConstants = new LinkedHashMap<>(); + this.fileRegistry = fileRegistry; for (var constant : constants) { - if ( ! constant.valuePath().isPresent()) continue; - - var distributableConstant = new DistributableConstant(constant.name().simpleArgument().get(), - constant.type(), - constant.valuePath().get(), - constant.pathType().get()); - distributableConstant.validate(); - distributableConstant.register(fileRegistry); - distributableConstants.put(distributableConstant.getName(), distributableConstant); + if (constant.valuePath().isPresent()) + add(new DistributableConstant(constant.name().simpleArgument().get(), + constant.type(), + constant.valuePath().get(), + constant.pathType().get())); } - this.constants = Collections.unmodifiableMap(distributableConstants); + } + + public void add(DistributableConstant constant) { + constant.validate(); + constant.register(fileRegistry); + String name = constant.getName(); + DistributableConstant prev = constants.putIfAbsent(name, constant); + if ( prev != null ) + throw new IllegalArgumentException("Constant '" + name + "' defined twice"); + } + + public void putIfAbsent(DistributableConstant constant) { + constant.validate(); + constant.register(fileRegistry); + String name = constant.getName(); + constants.putIfAbsent(name, constant); + } + + public void computeIfAbsent(String name, Function<? super String, ? extends DistributableConstant> createConstant) { + constants.computeIfAbsent(name, key -> { + DistributableConstant constant = createConstant.apply(key); + constant.validate(); + constant.register(fileRegistry); + return constant; + }); } /** Returns a read-only map of the constants in this indexed by name. */ - public Map<String, DistributableConstant> asMap() { return constants; } + public Map<String, DistributableConstant> asMap() { + return Collections.unmodifiableMap(constants); + } public static class DistributableConstant extends DistributableResource { - private final TensorType tensorType; + private TensorType tensorType; public DistributableConstant(String name, TensorType type, String fileName) { this(name, type, fileName, PathType.FILE); @@ -55,6 +79,10 @@ public class FileDistributedConstants { validate(); } + public void setType(TensorType type) { + this.tensorType = type; + } + public TensorType getTensorType() { return tensorType; } public String getType() { return tensorType.toString(); } |