aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/StructuredDataType.java
blob: 57e53eda9fb047431e07b85e8be284d2e10eaacc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StructuredFieldValue;
import com.yahoo.vespa.objects.Ids;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author Håkon Humberset
 */
public abstract class StructuredDataType extends DataType {

    public static final int classId = registerClass(Ids.document + 56, StructuredDataType.class);

    protected static int createId(String name) {
        if (name.equals("document")) return 8;

        // This is broken really because we now depend on String.hashCode staying the same in Java vm's
        // which is likely for pragmatic reasons but not by contract
        return (name+".0").hashCode(); // the ".0" must be preserved to keep hashCodes the same after we removed version
    }

    public StructuredDataType(String name) {
        super(name, createId(name));
    }

    public StructuredDataType(int id, String name) {
        super(name, id);
    }

    @Override
    public abstract StructuredFieldValue createFieldValue();

    @Override
    protected FieldValue createByReflection(Object arg) { return null; }

    /**
     * Returns the name of this as a DataTypeName
     *
     * @return Return the Documentname of this doumenttype.
     */
    public DataTypeName getDataTypeName() {
        return new DataTypeName(getName());
    }

    /**
     * Gets the field  matching a given name.
     *
     * @param name The name of a field.
     * @return Returns the matching field, or null if not found.
     */
    public abstract Field getField(String name);

    /**
     * Gets the field with the specified id.
     *
     * @param id the id of the field to return.
     * @return the matching field, or null if not found.
     */
    public abstract Field getField(int id);

    public abstract Collection<Field> getFields();

    @Override
    public boolean equals(Object o) {
        return ((o instanceof StructuredDataType) && super.equals(o));
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    protected void register(DocumentTypeManager manager, List<DataType> seenTypes) {
        seenTypes.add(this);
        for (Field field : getFields()) {
            if (!seenTypes.contains(field.getDataType())) {
                //we haven't seen this one before, register it:
                field.getDataType().register(manager, seenTypes);
            }
        }
        super.register(manager, seenTypes);
    }

    @Override
    public FieldPath buildFieldPath(String remainFieldName) {
        if (remainFieldName.length() == 0) {
            return new FieldPath();
        }

        String currFieldName = remainFieldName;
        String subFieldName = "";

        for (int i = 0; i < remainFieldName.length(); i++) {
            if (remainFieldName.charAt(i) == '.') {
                currFieldName = remainFieldName.substring(0, i);
                subFieldName = remainFieldName.substring(i + 1);
                break;
            } else if (remainFieldName.charAt(i) == '{' || remainFieldName.charAt(i) == '[') {
                currFieldName = remainFieldName.substring(0, i);
                subFieldName = remainFieldName.substring(i);
                break;
            }
        }

        Field f = getField(currFieldName);
        if (f != null) {
            FieldPath fieldPath = f.getDataType().buildFieldPath(subFieldName);
            List<FieldPathEntry> tmpPath = new ArrayList<FieldPathEntry>(fieldPath.getList());
            tmpPath.add(0, FieldPathEntry.newStructFieldEntry(f));
            return new FieldPath(tmpPath);
        } else {
            throw new IllegalArgumentException("Field '" + currFieldName + "' not found in type " + this);
        }
    }

}