summaryrefslogtreecommitdiffstats
path: root/config-model
diff options
context:
space:
mode:
authorArne H Juul <arnej@yahooinc.com>2022-04-07 12:58:11 +0000
committerArne H Juul <arnej@yahooinc.com>2022-04-07 14:01:29 +0000
commitef92091aff428b706e199c39e21be3a818f60b5b (patch)
tree0da0e4476ae01a92d6592a670dca321ca9201cf3 /config-model
parentad0d0f7875890539ecaee79a925f0d5247ca8682 (diff)
add DataTypeRecognizer
Diffstat (limited to 'config-model')
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/configmodel/producers/DataTypeRecognizer.java127
1 files changed, 127 insertions, 0 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/configmodel/producers/DataTypeRecognizer.java b/config-model/src/main/java/com/yahoo/vespa/configmodel/producers/DataTypeRecognizer.java
new file mode 100644
index 00000000000..cce18be2522
--- /dev/null
+++ b/config-model/src/main/java/com/yahoo/vespa/configmodel/producers/DataTypeRecognizer.java
@@ -0,0 +1,127 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.configmodel.producers;
+
+import com.yahoo.document.ArrayDataType;
+import com.yahoo.document.DataType;
+import com.yahoo.document.DocumentType;
+import com.yahoo.document.MapDataType;
+import com.yahoo.document.NumericDataType;
+import com.yahoo.document.PositionDataType;
+import com.yahoo.document.PrimitiveDataType;
+import com.yahoo.document.StructDataType;
+import com.yahoo.document.TensorDataType;
+import com.yahoo.document.WeightedSetDataType;
+import com.yahoo.document.annotation.AnnotationType;
+import com.yahoo.document.annotation.AnnotationReferenceDataType;
+import com.yahoo.document.internal.GeoPosType;
+import com.yahoo.documentmodel.DataTypeCollection;
+import com.yahoo.documentmodel.NewDocumentReferenceDataType;
+import com.yahoo.documentmodel.NewDocumentType;
+import com.yahoo.documentmodel.OwnedStructDataType;
+import com.yahoo.documentmodel.VespaDocumentType;
+import com.yahoo.searchdefinition.document.annotation.SDAnnotationType;
+
+import java.util.*;
+
+/**
+ * Class to produce unique names for DataType instances that have
+ * different contents even when their getName() and getId() may be
+ * equal.
+ *
+ * @author arnej
+ **/
+public class DataTypeRecognizer {
+ private Map<Object, String> toUniqueNames = new IdentityHashMap<>();
+
+ DataTypeRecognizer() {
+ }
+
+ DataTypeRecognizer(DataTypeCollection dtc) {
+ for (var type : dtc.getTypes()) {
+ System.err.println("added: "+nameOf(type));
+ }
+ }
+
+ String nameOf(Object type) {
+ return toUniqueNames.computeIfAbsent(type, t -> makeUniqueName(t));
+ }
+
+ private String makeUniqueName(Object type) {
+ if (type == PositionDataType.INSTANCE) {
+ return "{builtin position}";
+ }
+ if (type == DataType.DOCUMENT) {
+ return "{builtin root document}";
+ }
+ if (type == VespaDocumentType.INSTANCE) {
+ return "{builtin vespa document}";
+ }
+ var typeClass = type.getClass();
+ if (typeClass == AnnotationReferenceDataType.class) {
+ var t = (AnnotationReferenceDataType) type;
+ var ann = t.getAnnotationType();
+ return "annotationreference<" + ann.getName() + ">";
+ }
+ if (typeClass == AnnotationType.class) {
+ var t = (AnnotationType) type;
+ return "annotation<" + t.getName() + ">";
+ }
+ if (typeClass == SDAnnotationType.class) {
+ var t = (SDAnnotationType) type;
+ return "annotation<" + t.getName() + ">";
+ }
+ if (typeClass == ArrayDataType.class) {
+ var t = (ArrayDataType) type;
+ var nt = t.getNestedType();
+ return "array<" + nameOf(nt) + ">";
+ }
+ if (typeClass == DocumentType.class) {
+ var t = (DocumentType) type;
+ return "{document "+t.getName()+"}";
+ }
+ if (typeClass == MapDataType.class) {
+ var t = (MapDataType) type;
+ var kt = t.getKeyType();
+ var vt = t.getValueType();
+ return "map<" + nameOf(kt) + ", " + nameOf(vt) + ">";
+ }
+ if (typeClass == NewDocumentReferenceDataType.class) {
+ var t = (NewDocumentReferenceDataType) type;
+ return "reference<" + t.getTargetTypeName() + ">";
+ }
+ if (typeClass == NewDocumentType.class) {
+ var t = (NewDocumentType) type;
+ return "{new-document "+t.getName()+"}";
+ }
+ if (typeClass == OwnedStructDataType.class) {
+ var t = (OwnedStructDataType) type;
+ return "{owned-struct " + t.getName() + " @ " + t.getOwnerName() + "}";
+ }
+ if (typeClass == NumericDataType.class) {
+ var t = (NumericDataType) type;
+ return "{numeric " + t.getName() + "}";
+ }
+ if (typeClass == PrimitiveDataType.class) {
+ var t = (PrimitiveDataType) type;
+ return "{primitive " + t.getName() + "}";
+ }
+ if (typeClass == StructDataType.class) {
+ var t = (StructDataType) type;
+ return "{struct "+t.getName()+"}";
+ }
+ if (typeClass == TensorDataType.class) {
+ return "{tensor}";
+ }
+ if (typeClass == WeightedSetDataType.class) {
+ var t = (WeightedSetDataType) type;
+ var nt = t.getNestedType();
+ String prefix = "weightedset<";
+ String cine = t.createIfNonExistent() ? " [createIfNonExistent]" : "";
+ String riz = t.removeIfZero() ? " [removeIfZero]" : "";
+ String suffix = ">";
+ return prefix + nameOf(nt) + cine + riz + suffix;
+ }
+ throw new IllegalArgumentException("unknown type class: "+typeClass);
+ }
+
+}