diff options
author | Arne H Juul <arnej@yahooinc.com> | 2021-12-13 10:00:31 +0000 |
---|---|---|
committer | Arne H Juul <arnej@yahooinc.com> | 2021-12-13 13:12:21 +0000 |
commit | e10e86fd2dbfb9a38c71e82a84ac10fed31bac3d (patch) | |
tree | f04d31e0840e96b5c6f145b9e76db96862a862dd /config-model/src/main/java/com/yahoo | |
parent | 7187dafb2b9fadc11f49844b637723df36aa3e3e (diff) |
try harder to get multi-level inheritance working
Diffstat (limited to 'config-model/src/main/java/com/yahoo')
3 files changed, 92 insertions, 3 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/SchemaBuilder.java b/config-model/src/main/java/com/yahoo/searchdefinition/SchemaBuilder.java index 2ff4d2d44d0..098426865fb 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/SchemaBuilder.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/SchemaBuilder.java @@ -218,6 +218,8 @@ public class SchemaBuilder { public void build(boolean validate) { if (isBuilt) throw new IllegalStateException("Application already built"); + new TemporarySDTypeResolver(application.schemas().values(), deployLogger).process(); + if (validate) application.validate(deployLogger); diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/TemporarySDTypeResolver.java b/config-model/src/main/java/com/yahoo/searchdefinition/TemporarySDTypeResolver.java new file mode 100644 index 00000000000..71a1859e195 --- /dev/null +++ b/config-model/src/main/java/com/yahoo/searchdefinition/TemporarySDTypeResolver.java @@ -0,0 +1,77 @@ +// 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.DeployLogger; +import com.yahoo.searchdefinition.document.SDDocumentType; +import com.yahoo.searchdefinition.document.TemporarySDDocumentType; + +import java.util.*; +import java.util.logging.Level; + +/** + * @author arnej + */ +public class TemporarySDTypeResolver { + + private final DeployLogger deployLogger; + private final Collection<Schema> toProcess; + private final List<SDDocumentType> docTypes = new LinkedList<>(); + + public TemporarySDTypeResolver(Collection<Schema> schemas, DeployLogger deployLogger) { + this.deployLogger = deployLogger; + this.toProcess = schemas; + } + + private SDDocumentType findDocType(String name) { + assert(name != null); + for (var doc : docTypes) { + if (doc.getName().equals(name)) { + return doc; + } + } + deployLogger.logApplicationPackage(Level.WARNING, "No document type in application matching name: "+name); + return null; + } + + public void process() { + docTypes.add(SDDocumentType.VESPA_DOCUMENT); + for (Schema schema : toProcess) { + if (schema.hasDocument()) { + docTypes.add(schema.getDocument()); + } + } + // first, fix inheritance + for (SDDocumentType doc : docTypes) { + for (SDDocumentType inherited : doc.getInheritedTypes()) { + if (inherited instanceof TemporarySDDocumentType) { + var actual = findDocType(inherited.getName()); + if (actual != null) { + doc.inherit(actual); + } else { + deployLogger.logApplicationPackage(Level.WARNING, "Unresolved inherit '"+inherited.getName() +"' for document "+doc.getName()); + } + } + } + } + // next, check owned types (structs only?) + for (SDDocumentType doc : docTypes) { + for (SDDocumentType owned : doc.getTypes()) { + if (owned instanceof TemporarySDDocumentType) { + deployLogger.logApplicationPackage(Level.WARNING, "Schema '"+doc.getName()+"' owned type '"+owned.getName()+"' is temporary, should not happen"); + continue; + } + for (SDDocumentType inherited : owned.getInheritedTypes()) { + if (inherited instanceof TemporarySDDocumentType) { + var actual = doc.getType(inherited.getName()); + if (actual != null) { + owned.inherit(actual); + } else { + deployLogger.logApplicationPackage(Level.WARNING, "Unresolved inherit '"+inherited.getName() +"' for type '"+owned.getName()+"' in document "+doc.getName()); + } + } + } + } + } + } + +} diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/processing/ValidateStructTypeInheritance.java b/config-model/src/main/java/com/yahoo/searchdefinition/processing/ValidateStructTypeInheritance.java index 6b4ffa33136..d99832e3df6 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/processing/ValidateStructTypeInheritance.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/processing/ValidateStructTypeInheritance.java @@ -11,6 +11,7 @@ import com.yahoo.document.Field; import com.yahoo.document.StructDataType; import com.yahoo.searchdefinition.document.SDDocumentType; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.HashSet; @@ -33,11 +34,21 @@ public class ValidateStructTypeInheritance extends Processor { void fail(Field field, String message) { throw newProcessException(schema, field, message); - } + } void verifyNoRedeclarations(SDDocumentType docType) { for (SDDocumentType type : docType.allTypes().values()) { if (type.isStruct()) { + var inheritedTypes = new ArrayList<SDDocumentType>(type.getInheritedTypes()); + for (int i = 0; i < inheritedTypes.size(); i++) { + SDDocumentType inherit = inheritedTypes.get(i); + for (var extra : inherit.getInheritedTypes()) { + if (! inheritedTypes.contains(extra)) { + inheritedTypes.add(extra); + } + } + } + if (inheritedTypes.isEmpty()) continue; var seenFieldNames = new HashSet<>(); for (var field : type.getDocumentType().contentStruct().getFieldsThisTypeOnly()) { if (seenFieldNames.contains(field.getName())) { @@ -46,14 +57,13 @@ public class ValidateStructTypeInheritance extends Processor { } seenFieldNames.add(field.getName()); } - for (SDDocumentType inherit : type.getInheritedTypes()) { + for (SDDocumentType inherit : inheritedTypes) { if (inherit.isStruct()) { for (var field : inherit.getDocumentType().contentStruct().getFieldsThisTypeOnly()) { if (seenFieldNames.contains(field.getName())) { fail(field, "struct "+type.getName()+" cannot inherit from "+inherit.getName()+" and redeclare field "+field.getName()); } seenFieldNames.add(field.getName()); - } } else { fail(new Field("no field"), "struct cannot inherit from non-struct "+inherit.getName()+" class "+inherit.getClass()); |