diff options
author | Jon Bratseth <bratseth@oath.com> | 2022-02-01 23:04:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-01 23:04:53 +0100 |
commit | 58dbe08ac1cb5783787d4abb826b6ffe7d9f6714 (patch) | |
tree | abf83f8aba13bb612a73e15e483910edea649f88 | |
parent | b754deecd3cd7856f7c407dbab479e950316a9ab (diff) | |
parent | d91569194fe13bed6c2fef28fc9b5633d6299a5f (diff) |
Merge pull request #21011 from vespa-engine/bratseth/avoid-schema-dir
Bratseth/avoid schema dir
3 files changed, 39 insertions, 46 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/ApplicationBuilder.java b/config-model/src/main/java/com/yahoo/searchdefinition/ApplicationBuilder.java index 98c6897dbb7..533546b4d39 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/ApplicationBuilder.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/ApplicationBuilder.java @@ -139,7 +139,7 @@ public class ApplicationBuilder { */ public Schema addSchemaFile(String fileName) throws IOException, ParseException { File file = new File(fileName); - return addSchema(IOUtils.readFile(file), file.getAbsoluteFile().getParent()); + return addSchema(IOUtils.readFile(file)); } /** @@ -148,9 +148,9 @@ public class ApplicationBuilder { * * @param reader the reader whose content to import */ - private void addSchema(NamedReader reader) { + public void addSchema(NamedReader reader) { try { - String schemaName = addSchema(IOUtils.readAll(reader), reader.getName()).getName(); + String schemaName = addSchema(IOUtils.readAll(reader)).getName(); String schemaFileName = stripSuffix(reader.getName(), ApplicationPackage.SD_NAME_SUFFIX); if ( ! schemaFileName.equals(schemaName)) { throw new IllegalArgumentException("The file containing schema '" + schemaName + "' must be named '" + @@ -172,18 +172,12 @@ public class ApplicationBuilder { } /** - * Adds a schema to this application. + * Adds a schema to this * - * @param string the string to parse - * @return the schema - * @throws ParseException thrown if the file does not contain a valid search definition + * @param schemaString the content of the schema */ - public Schema addSchema(String string) throws ParseException { - return addSchema(string, null); - } - - private Schema addSchema(String schemaString, String schemaPath) throws ParseException { - return add(createSchema(schemaString, schemaPath)); + public Schema addSchema(String schemaString) throws ParseException { + return add(createSchema(schemaString)); } /** @@ -201,18 +195,16 @@ public class ApplicationBuilder { return schema; } - private Schema createSchema(String schemaString, String schemaPath) throws ParseException { - Schema schema = parseSchema(schemaString, schemaPath); - addRankProfileFiles(schema, schemaPath); + private Schema createSchema(String schemaString) throws ParseException { + Schema schema = parseSchema(schemaString); + addRankProfileFiles(schema); return schema; } - private Schema parseSchema(String schemaString, String schemaPath) throws ParseException { + private Schema parseSchema(String schemaString) throws ParseException { SimpleCharStream stream = new SimpleCharStream(schemaString); try { - return new SDParser(stream, applicationPackage, fileRegistry, deployLogger, properties, - rankProfileRegistry, documentsOnly) - .schema(documentTypeManager, schemaPath); + return parserOf(stream).schema(documentTypeManager); } catch (TokenMgrException e) { throw new ParseException("Unknown symbol: " + e.getMessage()); } catch (ParseException pe) { @@ -220,12 +212,16 @@ public class ApplicationBuilder { } } - private void addRankProfileFiles(Schema schema, String schemaPath) { - if (applicationPackage == null || schemaPath == null) return; - Path rankProfilePath = Path.fromString(schemaPath).append(schema.getName()); - for (NamedReader reader : applicationPackage.getFiles(rankProfilePath, ".profile")) { + private void addRankProfileFiles(Schema schema) { + if (applicationPackage == null) return; + + Path legacyRankProfilePath = ApplicationPackage.SEARCH_DEFINITIONS_DIR.append(schema.getName()); + for (NamedReader reader : applicationPackage.getFiles(legacyRankProfilePath, ".profile")) + parseRankProfile(reader, schema); + + Path rankProfilePath = ApplicationPackage.SCHEMAS_DIR.append(schema.getName()); + for (NamedReader reader : applicationPackage.getFiles(rankProfilePath, ".profile")) parseRankProfile(reader, schema); - } } /** Parses the rank profile of the given reader and adds it to the rank profile registry for this schema. */ @@ -233,9 +229,7 @@ public class ApplicationBuilder { try { SimpleCharStream stream = new SimpleCharStream(IOUtils.readAll(reader.getReader())); try { - new SDParser(stream, applicationPackage, fileRegistry, deployLogger, properties, - rankProfileRegistry, documentsOnly) - .rankProfile(schema); + parserOf(stream).rankProfile(schema); } catch (TokenMgrException e) { throw new ParseException("Unknown symbol: " + e.getMessage()); } catch (ParseException pe) { @@ -250,7 +244,10 @@ public class ApplicationBuilder { } } - + private SDParser parserOf(SimpleCharStream stream) { + return new SDParser(stream, applicationPackage, fileRegistry, deployLogger, properties, + rankProfileRegistry, documentsOnly); + } /** * Processes and finalizes the schemas of this. diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java index 53e9cfd5601..5b842b002bd 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java @@ -205,25 +205,22 @@ public class RankProfile implements Cloneable { * The profile must belong to this schema (directly or by inheritance). */ public void inherit(String inheritedName) { + inherited = null; inheritedNames.add(inheritedName); } /** Returns the names of the profiles this inherits, if any. */ - public List<String> inheritedNames() { return inheritedNames; } + public List<String> inheritedNames() { return Collections.unmodifiableList(inheritedNames); } /** Returns the rank profiles inherited by this. */ private List<RankProfile> inherited() { if (inheritedNames.isEmpty()) return List.of(); if (inherited != null) return inherited; - for (String inheritedName : inheritedNames) { - inherited = (schema() != null) ? resolveInheritedProfiles(schema) - : List.of(rankProfileRegistry.getGlobal(inheritedName)); - - List<String> children = new ArrayList<>(); - children.add(createFullyQualifiedName()); - inherited.forEach(profile -> verifyNoInheritanceCycle(children, profile)); - } + inherited = resolveInheritedProfiles(schema); + List<String> children = new ArrayList<>(); + children.add(createFullyQualifiedName()); + inherited.forEach(profile -> verifyNoInheritanceCycle(children, profile)); return inherited; } @@ -245,7 +242,9 @@ public class RankProfile implements Cloneable { private List<RankProfile> resolveInheritedProfiles(ImmutableSchema schema) { List<RankProfile> inherited = new ArrayList<>(); for (String inheritedName : inheritedNames) { - RankProfile inheritedProfile = resolveInheritedProfile(schema, inheritedName); + RankProfile inheritedProfile = schema == null + ? rankProfileRegistry.getGlobal(inheritedName) + : resolveInheritedProfile(schema, inheritedName); if (inheritedProfile == null) throw new IllegalArgumentException("rank-profile '" + name() + "' inherits '" + inheritedName + "', but this is not found in " + diff --git a/config-model/src/main/javacc/SDParser.jj b/config-model/src/main/javacc/SDParser.jj index 5b7721c0fe9..b6a9ab789bf 100644 --- a/config-model/src/main/javacc/SDParser.jj +++ b/config-model/src/main/javacc/SDParser.jj @@ -395,16 +395,15 @@ SPECIAL_TOKEN : * The rule consumes any schema and returns the corresponding object. This is the only production that should * ever consume leading newlines. * - * @param dir the directory containing the file being parsed * @return the schema object */ -Schema schema(DocumentTypeManager docMan, String dir) : +Schema schema(DocumentTypeManager docMan) : { this.docMan = docMan; Schema schema; } { - (<NL>)* (schema = rootSchema(dir) | schema = rootDocument(dir)) + (<NL>)* (schema = rootSchema() | schema = rootDocument()) { return schema; } } @@ -412,10 +411,9 @@ Schema schema(DocumentTypeManager docMan, String dir) : * This rule consumes a proper schema block. This and rootDocument() are the only rules that should ever consume * trailing newline tokens. * - * @param dir the directory containing the file being parsed. * @return the schema definition object. */ -Schema rootSchema(String dir) : +Schema rootSchema() : { String name; String inherited = null; @@ -459,10 +457,9 @@ Object rootSchemaItem(Schema schema) : { } /** * Consumes a schema definition that contains only documents to be used for inheritance, etc. * - * @param dir the directory containing the file being parsed. * @return the schema definition object. */ -Schema rootDocument(String dir) : +Schema rootDocument() : { Schema schema = new DocumentOnlySchema(applicationPackage, fileRegistry, deployLogger, properties); } |