From 41c341a302ccf641acc91c2b3afb49844a43d216 Mon Sep 17 00:00:00 2001 From: Harald Musum Date: Tue, 1 Feb 2022 23:30:50 +0100 Subject: Revert "Bratseth/avoid schema dir" --- .../yahoo/searchdefinition/ApplicationBuilder.java | 55 ++++++++++++---------- .../com/yahoo/searchdefinition/RankProfile.java | 19 ++++---- config-model/src/main/javacc/SDParser.jj | 11 +++-- 3 files changed, 46 insertions(+), 39 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 533546b4d39..98c6897dbb7 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)); + return addSchema(IOUtils.readFile(file), file.getAbsoluteFile().getParent()); } /** @@ -148,9 +148,9 @@ public class ApplicationBuilder { * * @param reader the reader whose content to import */ - public void addSchema(NamedReader reader) { + private void addSchema(NamedReader reader) { try { - String schemaName = addSchema(IOUtils.readAll(reader)).getName(); + String schemaName = addSchema(IOUtils.readAll(reader), reader.getName()).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,12 +172,18 @@ public class ApplicationBuilder { } /** - * Adds a schema to this + * Adds a schema to this application. * - * @param schemaString the content of the schema + * @param string the string to parse + * @return the schema + * @throws ParseException thrown if the file does not contain a valid search definition */ - public Schema addSchema(String schemaString) throws ParseException { - return add(createSchema(schemaString)); + 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)); } /** @@ -195,16 +201,18 @@ public class ApplicationBuilder { return schema; } - private Schema createSchema(String schemaString) throws ParseException { - Schema schema = parseSchema(schemaString); - addRankProfileFiles(schema); + private Schema createSchema(String schemaString, String schemaPath) throws ParseException { + Schema schema = parseSchema(schemaString, schemaPath); + addRankProfileFiles(schema, schemaPath); return schema; } - private Schema parseSchema(String schemaString) throws ParseException { + private Schema parseSchema(String schemaString, String schemaPath) throws ParseException { SimpleCharStream stream = new SimpleCharStream(schemaString); try { - return parserOf(stream).schema(documentTypeManager); + return new SDParser(stream, applicationPackage, fileRegistry, deployLogger, properties, + rankProfileRegistry, documentsOnly) + .schema(documentTypeManager, schemaPath); } catch (TokenMgrException e) { throw new ParseException("Unknown symbol: " + e.getMessage()); } catch (ParseException pe) { @@ -212,16 +220,12 @@ public class ApplicationBuilder { } } - 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")) + 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")) { parseRankProfile(reader, schema); + } } /** Parses the rank profile of the given reader and adds it to the rank profile registry for this schema. */ @@ -229,7 +233,9 @@ public class ApplicationBuilder { try { SimpleCharStream stream = new SimpleCharStream(IOUtils.readAll(reader.getReader())); try { - parserOf(stream).rankProfile(schema); + new SDParser(stream, applicationPackage, fileRegistry, deployLogger, properties, + rankProfileRegistry, documentsOnly) + .rankProfile(schema); } catch (TokenMgrException e) { throw new ParseException("Unknown symbol: " + e.getMessage()); } catch (ParseException pe) { @@ -244,10 +250,7 @@ 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 5b842b002bd..53e9cfd5601 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java @@ -205,22 +205,25 @@ 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 inheritedNames() { return Collections.unmodifiableList(inheritedNames); } + public List inheritedNames() { return inheritedNames; } /** Returns the rank profiles inherited by this. */ private List inherited() { if (inheritedNames.isEmpty()) return List.of(); if (inherited != null) return inherited; - inherited = resolveInheritedProfiles(schema); - List children = new ArrayList<>(); - children.add(createFullyQualifiedName()); - inherited.forEach(profile -> verifyNoInheritanceCycle(children, profile)); + for (String inheritedName : inheritedNames) { + inherited = (schema() != null) ? resolveInheritedProfiles(schema) + : List.of(rankProfileRegistry.getGlobal(inheritedName)); + + List children = new ArrayList<>(); + children.add(createFullyQualifiedName()); + inherited.forEach(profile -> verifyNoInheritanceCycle(children, profile)); + } return inherited; } @@ -242,9 +245,7 @@ public class RankProfile implements Cloneable { private List resolveInheritedProfiles(ImmutableSchema schema) { List inherited = new ArrayList<>(); for (String inheritedName : inheritedNames) { - RankProfile inheritedProfile = schema == null - ? rankProfileRegistry.getGlobal(inheritedName) - : resolveInheritedProfile(schema, inheritedName); + RankProfile inheritedProfile = 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 b6a9ab789bf..5b7721c0fe9 100644 --- a/config-model/src/main/javacc/SDParser.jj +++ b/config-model/src/main/javacc/SDParser.jj @@ -395,15 +395,16 @@ 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) : +Schema schema(DocumentTypeManager docMan, String dir) : { this.docMan = docMan; Schema schema; } { - ()* (schema = rootSchema() | schema = rootDocument()) + ()* (schema = rootSchema(dir) | schema = rootDocument(dir)) { return schema; } } @@ -411,9 +412,10 @@ Schema schema(DocumentTypeManager docMan) : * 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() : +Schema rootSchema(String dir) : { String name; String inherited = null; @@ -457,9 +459,10 @@ 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() : +Schema rootDocument(String dir) : { Schema schema = new DocumentOnlySchema(applicationPackage, fileRegistry, deployLogger, properties); } -- cgit v1.2.3