diff options
6 files changed, 134 insertions, 4 deletions
diff --git a/document/abi-spec.json b/document/abi-spec.json index 717b0c9f188..af1dbdea56d 100644 --- a/document/abi-spec.json +++ b/document/abi-spec.json @@ -427,6 +427,8 @@ "methods": [ "public void <init>(java.lang.String)", "public void <init>(java.lang.String, com.yahoo.document.StructDataType, com.yahoo.document.StructDataType)", + "public void <init>(java.lang.String, com.yahoo.document.StructDataType, com.yahoo.document.StructDataType, java.util.Set)", + "public void <init>(java.lang.String, java.util.Set)", "public com.yahoo.document.DocumentType clone()", "public com.yahoo.document.Document createFieldValue()", "public java.lang.Class getValueClass()", @@ -448,6 +450,8 @@ "public com.yahoo.document.Field getField(int)", "public boolean hasField(java.lang.String)", "public int getFieldCount()", + "public java.util.Set getImportedFieldNames()", + "public boolean hasImportedField(java.lang.String)", "public com.yahoo.document.Field removeField(java.lang.String)", "public java.util.Collection getFields()", "public java.util.Set fieldSet()", diff --git a/document/src/main/java/com/yahoo/document/DocumentType.java b/document/src/main/java/com/yahoo/document/DocumentType.java index f3ff6e4ed30..17c525f7cae 100755 --- a/document/src/main/java/com/yahoo/document/DocumentType.java +++ b/document/src/main/java/com/yahoo/document/DocumentType.java @@ -41,6 +41,7 @@ public class DocumentType extends StructuredDataType { private StructDataType bodyType; private List<DocumentType> inherits = new ArrayList<>(1); private Map<String, Set<Field>> fieldSets = new HashMap<>(); + private final Set<String> importedFieldNames; /** * Creates a new document type and registers it with the document type manager. @@ -51,8 +52,7 @@ public class DocumentType extends StructuredDataType { * @param name The name of the new document type */ public DocumentType(String name) { - this(name, new StructDataType(name + ".header"), - new StructDataType(name + ".body")); + this(name, createHeaderStructType(name), createBodyStructType(name)); } /** @@ -65,9 +65,27 @@ public class DocumentType extends StructuredDataType { * @param bodyType The type of the body struct */ public DocumentType(String name, StructDataType headerType, StructDataType bodyType) { + this(name, headerType, bodyType, Collections.emptySet()); + } + + public DocumentType(String name, StructDataType headerType, + StructDataType bodyType, Set<String> importedFieldNames) { super(name); this.headerType = headerType; this.bodyType = bodyType; + this.importedFieldNames = Collections.unmodifiableSet(importedFieldNames); + } + + public DocumentType(String name, Set<String> importedFieldNames) { + this(name, createHeaderStructType(name), createBodyStructType(name), importedFieldNames); + } + + private static StructDataType createHeaderStructType(String name) { + return new StructDataType(name + ".header"); + } + + private static StructDataType createBodyStructType(String name) { + return new StructDataType(name + ".body"); } @Override @@ -374,6 +392,14 @@ public class DocumentType extends StructuredDataType { return headerType.getFieldCount() + bodyType.getFieldCount(); } + public Set<String> getImportedFieldNames() { + return importedFieldNames; + } + + public boolean hasImportedField(String fieldName) { + return importedFieldNames.contains(fieldName); + } + /** * Removes an field from the DocumentType. * diff --git a/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java b/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java index 154c25880a9..9582f7d0e7f 100644 --- a/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java +++ b/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java @@ -10,8 +10,10 @@ import com.yahoo.log.LogLevel; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * Configures the Vespa document manager from a config id. @@ -144,7 +146,10 @@ public class DocumentTypeManagerConfigurer implements ConfigSubscriber.SingleSub for (Field field : body.getFields()) { field.setHeader(false); } - DocumentType type = new DocumentType(doc.name(), header, body); + var importedFields = doc.importedfield().stream() + .map(f -> f.name()) + .collect(Collectors.toUnmodifiableSet()); + DocumentType type = new DocumentType(doc.name(), header, body, importedFields); for (Object j : doc.inherits()) { DocumentmanagerConfig.Datatype.Documenttype.Inherits parent = (DocumentmanagerConfig.Datatype.Documenttype.Inherits) j; diff --git a/document/src/main/java/com/yahoo/document/FieldPath.java b/document/src/main/java/com/yahoo/document/FieldPath.java index 295b1c9dd5c..134681a4028 100755 --- a/document/src/main/java/com/yahoo/document/FieldPath.java +++ b/document/src/main/java/com/yahoo/document/FieldPath.java @@ -10,7 +10,7 @@ import java.util.List; * This class represents a path into a document, that can be used to iterate through the document and extract the field * values you're interested in. * - * @author <a href="mailto:thomasg@yahoo-inc.com">Thomas Gundersen</a> + * @author Thomas Gundersen */ public class FieldPath implements Iterable<FieldPathEntry> { diff --git a/document/src/test/document/documentmanager.importedfields.cfg b/document/src/test/document/documentmanager.importedfields.cfg new file mode 100644 index 00000000000..765e290ae82 --- /dev/null +++ b/document/src/test/document/documentmanager.importedfields.cfg @@ -0,0 +1,65 @@ +datatype[7] +datatype[0].id -1365874599 +datatype[0].arraytype[0] +datatype[0].weightedsettype[0] +datatype[0].structtype[1] +datatype[0].structtype[0].name referenced_type.header +datatype[0].structtype[0].version 9 +datatype[0].structtype[0].field[0] +datatype[0].documenttype[0] +datatype[1].id 278604398 +datatype[1].arraytype[0] +datatype[1].weightedsettype[0] +datatype[1].structtype[1] +datatype[1].structtype[0].name referenced_type.body +datatype[1].structtype[0].version 9 +datatype[1].structtype[0].field[0] +datatype[1].documenttype[0] +datatype[2].id 124647170 +datatype[2].arraytype[0] +datatype[2].weightedsettype[0] +datatype[2].structtype[0] +datatype[2].documenttype[1] +datatype[2].documenttype[0].name referenced_type +datatype[2].documenttype[0].version 9 +datatype[2].documenttype[0].inherits[0] +datatype[2].documenttype[0].headerstruct -1365874599 +datatype[2].documenttype[0].bodystruct 278604398 +datatype[3].id 12345678 +datatype[3].arraytype[0] +datatype[3].weightedsettype[0] +datatype[3].structtype[0] +datatype[3].documenttype[0] +datatype[3].referencetype[1] +datatype[3].referencetype[0].target_type_id 124647170 +datatype[4].id 673066331 +datatype[4].arraytype[0] +datatype[4].weightedsettype[0] +datatype[4].structtype[1] +datatype[4].structtype[0].name type_with_ref.header +datatype[4].structtype[0].version 234 +datatype[4].structtype[0].field[1] +datatype[4].structtype[0].field[0].name my_ref_field +datatype[4].structtype[0].field[0].id[0] +datatype[4].structtype[0].field[0].datatype 12345678 +datatype[4].documenttype[0] +datatype[5].id -176986064 +datatype[5].arraytype[0] +datatype[5].weightedsettype[0] +datatype[5].structtype[1] +datatype[5].structtype[0].name type_with_ref.body +datatype[5].structtype[0].version 234 +datatype[5].structtype[0].field[0] +datatype[5].documenttype[0] +datatype[6].id -1293964543 +datatype[6].arraytype[0] +datatype[6].weightedsettype[0] +datatype[6].structtype[0] +datatype[6].documenttype[1] +datatype[6].documenttype[0].name type_with_ref +datatype[6].documenttype[0].version 234 +datatype[6].documenttype[0].inherits[0] +datatype[6].documenttype[0].headerstruct 673066331 +datatype[6].documenttype[0].bodystruct -176986064 +datatype[6].documenttype[0].importedfield[0].name "my_cool_imported_field" +datatype[6].documenttype[0].importedfield[1].name "my_awesome_imported_field"
\ No newline at end of file diff --git a/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java b/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java index 65c217e09e1..3dff90e888e 100644 --- a/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java +++ b/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java @@ -8,6 +8,7 @@ import com.yahoo.document.datatypes.StringFieldValue; import com.yahoo.document.datatypes.StructuredFieldValue; import org.junit.Test; +import java.util.HashSet; import java.util.Iterator; import static org.junit.Assert.assertEquals; @@ -559,6 +560,35 @@ search annotationsimplicitstruct { assertTrue(fieldRefType.getTargetType() == targetDocType); } + @Test + public void imported_fields_are_empty_if_no_fields_provided_in_config() { + var manager = createConfiguredManager("file:src/test/document/documentmanager.singlereference.cfg"); + var docType = manager.getDocumentType("type_with_ref"); + + assertNotNull(docType.getImportedFieldNames()); + assertEquals(docType.getImportedFieldNames().size(), 0); + assertFalse(docType.hasImportedField("foo")); + } + + @Test + public void imported_fields_are_populated_from_config() { + var manager = createConfiguredManager("file:src/test/document/documentmanager.importedfields.cfg"); + var docType = manager.getDocumentType("type_with_ref"); + + var expectedFields = new HashSet<String>(); + expectedFields.add("my_cool_imported_field"); + expectedFields.add("my_awesome_imported_field"); + assertEquals(docType.getImportedFieldNames(), expectedFields); + + assertTrue(docType.hasImportedField("my_cool_imported_field")); + assertTrue(docType.hasImportedField("my_awesome_imported_field")); + assertFalse(docType.hasImportedField("a_missing_imported_field")); + } + + // TODO test clone(). Also fieldSets not part of clone()..! + + // TODO add imported field to equals()/hashCode() for DocumentType? fieldSets not part of this... + // TODO test reference to own doc type } |