diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-10-18 16:20:40 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-10-18 16:20:40 +0200 |
commit | 42164bf6d23b411ad13dec8a4540a3450d072483 (patch) | |
tree | 290f4ab52c296244103a66f4b1be8f62f29389bb /config-model/src | |
parent | 6f6f21414a004a967ce54ab2a0a47e29b1f8609a (diff) |
Inherit constants
Diffstat (limited to 'config-model/src')
7 files changed, 54 insertions, 27 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/RankingConstants.java b/config-model/src/main/java/com/yahoo/searchdefinition/RankingConstants.java index 8df68b210ce..1e0eacaea16 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankingConstants.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankingConstants.java @@ -6,7 +6,9 @@ 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; @@ -18,11 +20,16 @@ import java.util.function.Function; */ public class RankingConstants { - private final Map<String, RankingConstant> constants = new ConcurrentHashMap<>(); private final FileRegistry fileRegistry; - public RankingConstants(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) { @@ -33,12 +40,14 @@ public class RankingConstants { 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); @@ -50,16 +59,26 @@ public class RankingConstants { /** Returns the ranking constant with the given name, or null if not present */ public RankingConstant get(String name) { - return constants.get(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() { - return Collections.unmodifiableMap(constants); + // 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) { - constants.values().forEach(constant -> constant.sendTo(services)); + asMap().values().forEach(constant -> constant.sendTo(services)); } } diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java b/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java index fcecac3fc2e..aeab718cda1 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/Schema.java @@ -140,7 +140,7 @@ public class Schema implements ImmutableSearch { this.properties = properties; this.documentsOnly = documentsOnly; largeRankExpressions = new LargeRankExpressions(fileRegistry); - rankingConstants = new RankingConstants(fileRegistry); + rankingConstants = new RankingConstants(fileRegistry, Optional.of(this)); onnxModels = new OnnxModels(fileRegistry); } diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java b/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java index 9f043770eab..d507fb917c0 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/derived/RankProfileList.java @@ -23,13 +23,14 @@ import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.logging.Logger; /** - * The derived rank profiles of a search definition + * The derived rank profiles of a schema * * @author bratseth */ @@ -45,7 +46,7 @@ public class RankProfileList extends Derived implements RankProfilesConfig.Produ public static RankProfileList empty = new RankProfileList(); private RankProfileList() { - rankingConstants = new RankingConstants(null); + rankingConstants = new RankingConstants(null, Optional.empty()); largeRankExpressions = new LargeRankExpressions(null); onnxModels = new OnnxModels(null); } @@ -53,7 +54,7 @@ public class RankProfileList extends Derived implements RankProfilesConfig.Produ /** * Creates a rank profile * - * @param schema the search definition this is a rank profile from + * @param schema the schema this is a rank profile from * @param attributeFields the attribute fields to create a ranking for */ public RankProfileList(Schema schema, diff --git a/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java b/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java index b5fc0e88540..3873596c835 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java @@ -178,7 +178,7 @@ public final class VespaModel extends AbstractConfigProducerRoot implements Seri version = deployState.getVespaVersion(); fileRegistry = deployState.getFileRegistry(); largeRankExpressions = new LargeRankExpressions(deployState.getFileRegistry()); - rankingConstants = new RankingConstants(deployState.getFileRegistry()); + rankingConstants = new RankingConstants(deployState.getFileRegistry(), Optional.empty()); validationOverrides = deployState.validationOverrides(); applicationPackage = deployState.getApplicationPackage(); provisioned = deployState.provisioned(); diff --git a/config-model/src/main/javacc/SDParser.jj b/config-model/src/main/javacc/SDParser.jj index bfab2138b92..0f60db40069 100644 --- a/config-model/src/main/javacc/SDParser.jj +++ b/config-model/src/main/javacc/SDParser.jj @@ -738,7 +738,7 @@ void annotationOutside(Schema schema) : } /** - * This rule consumes a annotation block from within either a document element. + * Consumes a annotation block from within either a document element. * * @param document the document object to add content to */ @@ -761,9 +761,9 @@ void annotation(Schema schema, SDDocumentType document) : /** - * This rule consumes a single element of an annotation body block. + * Consumes a single element of an annotation body block. * - * @param schema the seaschemarch object to add content to + * @param schema the schema object to add content to * @param type the type being built * @return a modified or new AnnotationType instance */ diff --git a/config-model/src/test/derived/annotationsreference/annotationsreference.sd b/config-model/src/test/derived/annotationsreference/annotationsreference.sd index 11972c13c39..a0e2af3c6f5 100755 --- a/config-model/src/test/derived/annotationsreference/annotationsreference.sd +++ b/config-model/src/test/derived/annotationsreference/annotationsreference.sd @@ -3,24 +3,25 @@ search annotationsreference { document annotationsreference { - annotation banana { - field brand type string { } - } + annotation banana { + field brand type string { } + } - annotation food { - field what type annotationreference<banana> { } - } + annotation food { + field what type annotationreference<banana> { } + } - annotation cyclic { - field blah type annotationreference<cyclic> { } - } + annotation cyclic { + field blah type annotationreference<cyclic> { } + } - annotation a { - field foo type annotationreference<b> { } - } + annotation a { + field foo type annotationreference<b> { } + } - annotation b { - } + annotation b { + } } + } diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java index 5799fdee238..e5bf879ac63 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java @@ -87,6 +87,10 @@ public class SchemaTestCase { " }" + " rank-profile parent_profile {" + " }" + + " constant parent_constant {" + + " file: constants/my_constant_tensor_file.json" + + " type: tensor<float>(x{},y{})" + + " }" + "}"); String childLines = joinLines( "schema child inherits parent {" + @@ -104,6 +108,8 @@ public class SchemaTestCase { assertNotNull(child.getField("parent_field")); assertNotNull(child.getExtraField("parent_field")); assertNotNull(application.rankProfileRegistry().get(child, "parent_profile")); + assertNotNull(child.rankingConstants().get("parent_constant")); + assertTrue(child.rankingConstants().asMap().containsKey("parent_constant")); } } |