diff options
author | Arne H Juul <arnej27959@users.noreply.github.com> | 2023-06-13 08:27:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-13 08:27:55 +0200 |
commit | 982817bd81908a320338ae1c2b1945392ca69398 (patch) | |
tree | 34f6c7b5ce6dcd0ab91cd9e0290afab27b44dd12 /config-model/src/main/java/com/yahoo/schema | |
parent | bb324300a8884035423dafaab1ebb7c72da2ae4c (diff) | |
parent | d876e1b4855c55ab6a8544001009418efec9abac (diff) |
Merge pull request #27382 from vespa-engine/bratseth/validate-prefix-matching-take-2-alternative-ending
Bratseth/validate prefix matching take 2 alternative ending
Diffstat (limited to 'config-model/src/main/java/com/yahoo/schema')
5 files changed, 118 insertions, 9 deletions
diff --git a/config-model/src/main/java/com/yahoo/schema/Index.java b/config-model/src/main/java/com/yahoo/schema/Index.java index 190081cf80f..aa1d226d12e 100644 --- a/config-model/src/main/java/com/yahoo/schema/Index.java +++ b/config-model/src/main/java/com/yahoo/schema/Index.java @@ -15,7 +15,7 @@ import java.util.Optional; import java.util.Set; /** - * An index definition in a search definition. + * An index definition in a schema. * Two indices are equal if they have the same name and the same settings, except * alias settings (which are excluded). * diff --git a/config-model/src/main/java/com/yahoo/schema/derived/Index.java b/config-model/src/main/java/com/yahoo/schema/derived/Index.java index 3b5e617d3dc..2f5b674abee 100644 --- a/config-model/src/main/java/com/yahoo/schema/derived/Index.java +++ b/config-model/src/main/java/com/yahoo/schema/derived/Index.java @@ -16,11 +16,11 @@ public class Index { /** The index type enumeration */ public static class Type { - public static final Type TEXT=new Type("text"); - public static final Type INT64=new Type("long"); - public static final Type BOOLEANTREE=new Type("booleantree"); + public static final Type TEXT = new Type("text"); + public static final Type INT64 = new Type("long"); + public static final Type BOOLEANTREE = new Type("booleantree"); - private String name; + private final String name; private Type(String name) { this.name=name; diff --git a/config-model/src/main/java/com/yahoo/schema/derived/SchemaInfo.java b/config-model/src/main/java/com/yahoo/schema/derived/SchemaInfo.java index 517a43fe129..291c67cae02 100644 --- a/config-model/src/main/java/com/yahoo/schema/derived/SchemaInfo.java +++ b/config-model/src/main/java/com/yahoo/schema/derived/SchemaInfo.java @@ -1,7 +1,21 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.schema.derived; +import com.yahoo.document.ArrayDataType; +import com.yahoo.document.DataType; +import com.yahoo.document.MapDataType; +import com.yahoo.document.PrimitiveDataType; +import com.yahoo.document.ReferenceDataType; +import com.yahoo.document.StructuredDataType; +import com.yahoo.document.TensorDataType; +import com.yahoo.document.WeightedSetDataType; +import com.yahoo.document.annotation.AnnotationReferenceDataType; +import com.yahoo.documentmodel.NewDocumentReferenceDataType; +import com.yahoo.schema.document.Attribute; +import com.yahoo.schema.document.FieldSet; +import com.yahoo.schema.document.ImmutableSDField; import com.yahoo.search.config.SchemaInfoConfig; +import com.yahoo.schema.Index; import com.yahoo.schema.RankProfile; import com.yahoo.schema.RankProfileRegistry; import com.yahoo.schema.Schema; @@ -9,6 +23,7 @@ import com.yahoo.searchlib.rankingexpression.Reference; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; @@ -52,11 +67,79 @@ public final class SchemaInfo extends Derived implements SchemaInfoConfig.Produc public void getConfig(SchemaInfoConfig.Builder builder) { var schemaBuilder = new SchemaInfoConfig.Schema.Builder(); schemaBuilder.name(schema.getName()); + addFieldsConfig(schemaBuilder); + addFieldSetConfig(schemaBuilder); addSummaryConfig(schemaBuilder); addRankProfilesConfig(schemaBuilder); builder.schema(schemaBuilder); } + private void addFieldsConfig(SchemaInfoConfig.Schema.Builder schemaBuilder) { + for (var field : schema.allFieldsList()) { + addFieldConfig(field, schemaBuilder); + for (var index : field.getIndices().values()) { + if ( ! index.getName().equals(field.getName())) // additional index + addFieldConfig(index, field.getDataType(), schemaBuilder); + } + for (var attribute : field.getAttributes().values()) { + if ( ! attribute.getName().equals(field.getName())) // additional attribute + addFieldConfig(attribute, field.getDataType(), schemaBuilder); + } + } + } + + private void addFieldConfig(ImmutableSDField field, SchemaInfoConfig.Schema.Builder schemaBuilder) { + var fieldBuilder = new SchemaInfoConfig.Schema.Field.Builder(); + fieldBuilder.name(field.getName()); + fieldBuilder.type(toTypeSpec(field.getDataType())); + for (var alias : field.getAliasToName().entrySet()) { + if (alias.getValue().equals(field.getName())) + fieldBuilder.alias(alias.getKey()); + } + fieldBuilder.attribute(field.doesAttributing()); + fieldBuilder.index(field.doesIndexing()); + schemaBuilder.field(fieldBuilder); + } + + // TODO: Make fields and indexes 1-1 so that this can be removed + private void addFieldConfig(Index index, DataType type, SchemaInfoConfig.Schema.Builder schemaBuilder) { + var fieldBuilder = new SchemaInfoConfig.Schema.Field.Builder(); + fieldBuilder.name(index.getName()); + fieldBuilder.type(toTypeSpec(type)); + for (Iterator<String> i = index.aliasIterator(); i.hasNext(); ) + fieldBuilder.alias(i.next()); + fieldBuilder.attribute(false); + fieldBuilder.index(true); + schemaBuilder.field(fieldBuilder); + } + + // TODO: Make fields and attributes 1-1 so that this can be removed + private void addFieldConfig(Attribute attribute, DataType type, SchemaInfoConfig.Schema.Builder schemaBuilder) { + var fieldBuilder = new SchemaInfoConfig.Schema.Field.Builder(); + fieldBuilder.name(attribute.getName()); + fieldBuilder.type(toTypeSpec(type)); + for (var alias : attribute.getAliases()) + fieldBuilder.alias(alias); + fieldBuilder.attribute(true); + fieldBuilder.index(false); + schemaBuilder.field(fieldBuilder); + } + + private void addFieldSetConfig(SchemaInfoConfig.Schema.Builder schemaBuilder) { + for (var fieldSet : schema.fieldSets().builtInFieldSets().values()) + addFieldSetConfig(fieldSet, schemaBuilder); + for (var fieldSet : schema.fieldSets().userFieldSets().values()) + addFieldSetConfig(fieldSet, schemaBuilder); + } + + private void addFieldSetConfig(FieldSet fieldSet, SchemaInfoConfig.Schema.Builder schemaBuilder) { + var fieldSetBuilder = new SchemaInfoConfig.Schema.Fieldset.Builder(); + fieldSetBuilder.name(fieldSet.getName()); + for (String fieldName : fieldSet.getFieldNames()) + fieldSetBuilder.field(fieldName); + schemaBuilder.fieldset(fieldSetBuilder); + } + private void addSummaryConfig(SchemaInfoConfig.Schema.Builder schemaBuilder) { for (var summary : summaries.asList()) { var summaryBuilder = new SchemaInfoConfig.Schema.Summaryclass.Builder(); @@ -88,6 +171,29 @@ public final class SchemaInfo extends Derived implements SchemaInfoConfig.Produc } } + /** Returns this type as a spec on the form following "field [name] type " in schemas. */ + private String toTypeSpec(DataType dataType) { + if (dataType instanceof PrimitiveDataType) + return dataType.getName(); + if (dataType instanceof AnnotationReferenceDataType annotationType) + return "annotationreference<" + annotationType.getAnnotationType().getName() + ">"; + if (dataType instanceof ArrayDataType arrayType) + return "array<" + toTypeSpec(arrayType.getNestedType()) + ">"; + if (dataType instanceof MapDataType mapType) + return "map<" + toTypeSpec(mapType.getKeyType()) + "," + toTypeSpec(mapType.getValueType()) + ">"; + if (dataType instanceof ReferenceDataType referenceType) + return "reference<" + toTypeSpec(referenceType.getTargetType()) + ">"; + if (dataType instanceof NewDocumentReferenceDataType referenceType) + return "reference<" + toTypeSpec(referenceType.getTargetType()) + ">"; + if (dataType instanceof StructuredDataType structType) + return structType.getName(); + if (dataType instanceof TensorDataType tensorType) + return tensorType.getTensorType().toString(); + if (dataType instanceof WeightedSetDataType weightedSetDataType) + return "weightedset<" + toTypeSpec(weightedSetDataType.getNestedType()) + ">"; + throw new IllegalArgumentException("Unknown data type " + dataType + " class " + dataType.getClass()); + } + /** A store of a *small* (in memory) amount of rank profile info. */ public static final class RankProfileInfo { diff --git a/config-model/src/main/java/com/yahoo/schema/document/SDDocumentType.java b/config-model/src/main/java/com/yahoo/schema/document/SDDocumentType.java index 919a6023151..5ffe4ec533c 100644 --- a/config-model/src/main/java/com/yahoo/schema/document/SDDocumentType.java +++ b/config-model/src/main/java/com/yahoo/schema/document/SDDocumentType.java @@ -216,7 +216,7 @@ public class SDDocumentType implements Cloneable { return f; } - public void addField(Field field) { + public Field addField(Field field) { verifyInheritance(field); for (Iterator<Field> i = docType.fieldIteratorThisTypeOnly(); i.hasNext(); ) { if (field.getName().equalsIgnoreCase((i.next()).getName())) { @@ -224,6 +224,7 @@ public class SDDocumentType implements Cloneable { } } docType.addField(field); + return field; } /** Parse-time inheritance check. */ diff --git a/config-model/src/main/java/com/yahoo/schema/document/SDField.java b/config-model/src/main/java/com/yahoo/schema/document/SDField.java index 4eaa3e05047..7821c101880 100644 --- a/config-model/src/main/java/com/yahoo/schema/document/SDField.java +++ b/config-model/src/main/java/com/yahoo/schema/document/SDField.java @@ -556,8 +556,9 @@ public class SDField extends Field implements TypedKey, ImmutableSDField { } /** Adds an explicit index defined in this field */ - public void addIndex(Index index) { - indices.put(index.getName(),index); + public Index addIndex(Index index) { + indices.put(index.getName(), index); + return index; } /** @@ -624,13 +625,14 @@ public class SDField extends Field implements TypedKey, ImmutableSDField { return attributes.get(getName()); } - public void addAttribute(Attribute attribute) { + public Attribute addAttribute(Attribute attribute) { String name = attribute.getName(); if (name == null || "".equals(name)) { name = getName(); attribute.setName(name); } attributes.put(attribute.getName(),attribute); + return attribute; } /** |