summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorArne H Juul <arnej@yahooinc.com>2022-03-13 08:47:59 +0000
committerArne H Juul <arnej@yahooinc.com>2022-03-13 08:58:47 +0000
commit145aa634b4f85849c25b7d16b17adfe50c5ecead (patch)
treeefcfc7491cbf65f507d6364425cf5f47a89ec057 /document
parent75c18d52ef575b983496df8bf43a0814c5ac2fb5 (diff)
add getDeclaredStructType in DocumentType
* getType() in DocumentTypeManager will be deprecated soon, this will be the recommended replacement.
Diffstat (limited to 'document')
-rw-r--r--document/abi-spec.json3
-rwxr-xr-xdocument/src/main/java/com/yahoo/document/DocumentType.java34
-rw-r--r--document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java3
-rw-r--r--document/src/test/document/documentmanager.declstruct.cfg89
-rw-r--r--document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java44
5 files changed, 170 insertions, 3 deletions
diff --git a/document/abi-spec.json b/document/abi-spec.json
index 83a56d4fb5e..e9bf2cb6430 100644
--- a/document/abi-spec.json
+++ b/document/abi-spec.json
@@ -437,6 +437,7 @@
"public java.lang.Class getValueClass()",
"public boolean isValueCompatible(com.yahoo.document.datatypes.FieldValue)",
"public com.yahoo.document.StructDataType contentStruct()",
+ "public com.yahoo.document.StructDataType getDeclaredStructType(java.lang.String)",
"public com.yahoo.document.StructDataType getHeaderType()",
"protected void register(com.yahoo.document.DocumentTypeManager, java.util.List)",
"public boolean isA(java.lang.String)",
@@ -3154,8 +3155,8 @@
"public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.datatypes.StringFieldValue)",
"public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.datatypes.TensorFieldValue)",
"public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.datatypes.ReferenceFieldValue)",
- "public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.datatypes.Struct)",
"public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.datatypes.StructuredFieldValue)",
+ "public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.datatypes.Struct)",
"public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.datatypes.WeightedSet)",
"public void write(com.yahoo.vespa.objects.FieldBase, com.yahoo.document.annotation.AnnotationReference)",
"public void write(com.yahoo.document.DocumentId)",
diff --git a/document/src/main/java/com/yahoo/document/DocumentType.java b/document/src/main/java/com/yahoo/document/DocumentType.java
index 15af1b3360b..6cfbb96ae5f 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 List<DocumentType> inherits = new ArrayList<>(1);
private Map<String, Set<Field>> fieldSets = new HashMap<>();
private final Set<String> importedFieldNames;
+ private Map<String, StructDataType> declaredStructTypes = new HashMap<>();
/**
* Creates a new document type and registers it with the document type manager.
@@ -138,6 +139,39 @@ public class DocumentType extends StructuredDataType {
return headerType;
}
+ /**
+ * Get a struct declared in this document (or any inherited
+ * document). Returns null if no such struct was found.
+ *
+ * @param name the name of the struct
+ * @return reference to a struct data type, or null
+ **/
+ public StructDataType getDeclaredStructType(String name) {
+ var mine = declaredStructTypes.get(name);
+ if (mine != null) {
+ return mine;
+ }
+ for (DocumentType inheritedType : inherits) {
+ var fromParent = inheritedType.getDeclaredStructType(name);
+ if (fromParent == null) {
+ continue;
+ } else if (mine == null) {
+ mine = fromParent;
+ } else if (mine != fromParent) {
+ throw new IllegalArgumentException("Found multiple conflicting struct types for "+name);
+ }
+ }
+ return mine;
+ }
+
+ /** only used during configuration */
+ void addDeclaredStructType(String name, StructDataType struct) {
+ var old = declaredStructTypes.put(name, struct);
+ if (old != null) {
+ throw new IllegalArgumentException("Already had declared struct for "+name);
+ }
+ }
+
/** @deprecated use contentStruct instead */
@Deprecated // TODO: Remove on Vespa 8
public StructDataType getHeaderType() {
diff --git a/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java b/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java
index 9325e374daa..cb615c27116 100644
--- a/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java
+++ b/document/src/main/java/com/yahoo/document/DocumentTypeManagerConfigurer.java
@@ -542,6 +542,9 @@ public class DocumentTypeManagerConfigurer implements ConfigSubscriber.SingleSub
DataType fieldType = getOrCreateType(fieldCfg.type());
type.addField(new Field(fieldCfg.name(), fieldCfg.internalid(), fieldType));
}
+ if (docType != DataType.DOCUMENT) {
+ docType.addDeclaredStructType(structCfg.name(), type);
+ }
}
}
void fillDocument() {
diff --git a/document/src/test/document/documentmanager.declstruct.cfg b/document/src/test/document/documentmanager.declstruct.cfg
new file mode 100644
index 00000000000..d9c48ddc42f
--- /dev/null
+++ b/document/src/test/document/documentmanager.declstruct.cfg
@@ -0,0 +1,89 @@
+enablecompression false
+usev8geopositions false
+doctype[0].name "document"
+doctype[0].idx 10000
+doctype[0].contentstruct 10001
+doctype[0].primitivetype[0].idx 10002
+doctype[0].primitivetype[0].name "bool"
+doctype[0].primitivetype[1].idx 10003
+doctype[0].primitivetype[1].name "byte"
+doctype[0].primitivetype[2].idx 10004
+doctype[0].primitivetype[2].name "double"
+doctype[0].primitivetype[3].idx 10005
+doctype[0].primitivetype[3].name "float"
+doctype[0].primitivetype[4].idx 10006
+doctype[0].primitivetype[4].name "float16"
+doctype[0].primitivetype[5].idx 10007
+doctype[0].primitivetype[5].name "int"
+doctype[0].primitivetype[6].idx 10008
+doctype[0].primitivetype[6].name "long"
+doctype[0].primitivetype[7].idx 10010
+doctype[0].primitivetype[7].name "predicate"
+doctype[0].primitivetype[8].idx 10011
+doctype[0].primitivetype[8].name "raw"
+doctype[0].primitivetype[9].idx 10012
+doctype[0].primitivetype[9].name "string"
+doctype[0].primitivetype[10].idx 10014
+doctype[0].primitivetype[10].name "uri"
+doctype[0].wsettype[0].idx 10013
+doctype[0].wsettype[0].elementtype 10012
+doctype[0].wsettype[0].createifnonexistent true
+doctype[0].wsettype[0].removeifzero true
+doctype[0].structtype[0].idx 10001
+doctype[0].structtype[0].name "document.header"
+doctype[0].structtype[1].idx 10009
+doctype[0].structtype[1].name "position"
+doctype[0].structtype[1].field[0].name "x"
+doctype[0].structtype[1].field[0].internalid 914677694
+doctype[0].structtype[1].field[0].type 10007
+doctype[0].structtype[1].field[1].name "y"
+doctype[0].structtype[1].field[1].internalid 900009410
+doctype[0].structtype[1].field[1].type 10007
+doctype[1].name "common"
+doctype[1].idx 10015
+doctype[1].inherits[0].idx 10000
+doctype[1].contentstruct 10016
+doctype[1].structtype[0].idx 10016
+doctype[1].structtype[0].name "common.header"
+doctype[1].structtype[1].idx 10017
+doctype[1].structtype[1].name "mystruct"
+doctype[1].structtype[1].field[0].name "f0"
+doctype[1].structtype[1].field[0].internalid 111558427
+doctype[1].structtype[1].field[0].type 10012
+doctype[2].name "foo"
+doctype[2].idx 10018
+doctype[2].inherits[0].idx 10000
+doctype[2].inherits[1].idx 10015
+doctype[2].contentstruct 10019
+doctype[2].structtype[0].idx 10019
+doctype[2].structtype[0].name "foo.header"
+doctype[2].structtype[1].idx 10020
+doctype[2].structtype[1].name "mystructinfoo"
+doctype[2].structtype[1].field[0].name "f1"
+doctype[2].structtype[1].field[0].internalid 1911889118
+doctype[2].structtype[1].field[0].type 10012
+doctype[3].name "bar"
+doctype[3].idx 10021
+doctype[3].inherits[0].idx 10000
+doctype[3].inherits[1].idx 10015
+doctype[3].contentstruct 10022
+doctype[3].structtype[0].idx 10022
+doctype[3].structtype[0].name "bar.header"
+doctype[3].structtype[1].idx 10023
+doctype[3].structtype[1].name "mystructinbar"
+doctype[3].structtype[1].field[0].name "f2"
+doctype[3].structtype[1].field[0].internalid 84639357
+doctype[3].structtype[1].field[0].type 10012
+doctype[4].name "foobar"
+doctype[4].idx 10024
+doctype[4].inherits[0].idx 10000
+doctype[4].inherits[1].idx 10018
+doctype[4].inherits[2].idx 10021
+doctype[4].contentstruct 10025
+doctype[4].structtype[0].idx 10025
+doctype[4].structtype[0].name "foobar.header"
+doctype[4].structtype[1].idx 10026
+doctype[4].structtype[1].name "mystructinfoobar"
+doctype[4].structtype[1].field[0].name "f3"
+doctype[4].structtype[1].field[0].internalid 63940691
+doctype[4].structtype[1].field[0].type 10012
diff --git a/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java b/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java
index b89ed2b6b08..622c7b2237f 100644
--- a/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java
+++ b/document/src/test/java/com/yahoo/document/DocumentTypeManagerTestCase.java
@@ -599,10 +599,50 @@ search annotationsimplicitstruct {
assertTrue(array.getNestedType() instanceof StructDataType);
}
+ @Test
+ public void declared_struct_types_available() {
+ var manager = DocumentTypeManager.fromFile("src/test/document/documentmanager.declstruct.cfg");
+ var docType = manager.getDocumentType("foo");
+ var struct = docType.getDeclaredStructType("mystructinfoo");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f1"));
+ struct = docType.getDeclaredStructType("mystructinbar");
+ assertNull(struct);
+ struct = docType.getDeclaredStructType("mystructinfoobar");
+ assertNull(struct);
+ struct = docType.getDeclaredStructType("mystruct");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f0"));
+
+ docType = manager.getDocumentType("bar");
+ struct = docType.getDeclaredStructType("mystructinfoo");
+ assertNull(struct);
+ struct = docType.getDeclaredStructType("mystructinbar");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f2"));
+ struct = docType.getDeclaredStructType("mystructinfoobar");
+ assertNull(struct);
+ struct = docType.getDeclaredStructType("mystruct");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f0"));
+
+ docType = manager.getDocumentType("foobar");
+ struct = docType.getDeclaredStructType("mystructinfoo");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f1"));
+ struct = docType.getDeclaredStructType("mystructinbar");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f2"));
+ struct = docType.getDeclaredStructType("mystructinfoobar");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f3"));
+ struct = docType.getDeclaredStructType("mystruct");
+ assertNotNull(struct);
+ assertNotNull(struct.getField("f0"));
+ }
+
// 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
-
}