aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/datatypes/Struct.java
blob: 0b1bbf5d3cab0014ce65f873c1b3ca459c32651f (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.datatypes;

import com.yahoo.collections.Hashlet;
import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.Field;
import com.yahoo.document.PositionDataType;
import com.yahoo.document.StructDataType;
import com.yahoo.document.serialization.FieldReader;
import com.yahoo.document.serialization.FieldWriter;
import com.yahoo.document.serialization.XmlSerializationHelper;
import com.yahoo.document.serialization.XmlStream;
import com.yahoo.vespa.objects.Ids;

import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;


/**
 * @author Håkon Humberset
 */
public class Struct extends StructuredFieldValue {

    public static final int classId = registerClass(Ids.document + 33, Struct.class);
    private Hashlet<Integer, FieldValue> values = new Hashlet<>();
    private int [] order = null;

    private int version;

    private int [] getInOrder() {
        if (order == null) {
            order = new int[values.size()];
            for (int i = 0; i < values.size(); i++) {
                order[i] = values.key(i);
            }
            Arrays.sort(order);
        }
        return order;
    }

    private void invalidateOrder() {
        order = null;
    }

    public Struct(DataType type) {
        super((StructDataType) type);
        this.version = Document.SERIALIZED_VERSION;
    }

    @Override
    public StructDataType getDataType() {
        return (StructDataType)super.getDataType();
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public int getVersion() {
        return this.version;
    }

    @Override
    public Struct clone() {
        Struct struct = (Struct) super.clone();
        struct.values = new Hashlet<>();
        struct.values.reserve(values.size());
        for (int i = 0; i < values.size(); i++) {
            struct.values.put(values.key(i), values.value(i).clone());
        }
        return struct;
    }

    @Override
    public void clear() {
        values = new Hashlet<>();
        invalidateOrder();
    }

    @Override
    public Iterator<Map.Entry<Field, FieldValue>> iterator() {
        return new FieldSet().iterator();
    }

    public Set<Map.Entry<Field, FieldValue>> getFields() {
        return new FieldSet();
    }

    @Override
    @Deprecated
    public void printXml(XmlStream xml) {
        if (getDataType().equals(PositionDataType.INSTANCE)) {
            try {
                PositionDataType.renderXml(this, xml);
                return;
            } catch (RuntimeException e) {
                // fallthrough to handling below
            }
        }
        XmlSerializationHelper.printStructXml(this, xml);
    }

    @Override
    public FieldValue getFieldValue(Field field) {
        return values.get(field.getId());
    }


    @Override
    public Field getField(String fieldName) {
        return getDataType().getField(fieldName);
    }

    @Override
    public int getFieldCount() {
        return values.size();
    }

    @Override
    protected void doSetFieldValue(Field field, FieldValue value) {
        if (field == null) {
            throw new IllegalArgumentException("Invalid null field pointer");
        }
        Field myField = getDataType().getField(field.getId());
        if (myField==null) {
            throw new IllegalArgumentException("No such field in "+getDataType()+" : "+field.getName());
        }
        if (!myField
                   .getDataType().isValueCompatible(value)) {
            throw new IllegalArgumentException(
                    "Incompatible data types. Got " + value.getDataType()
                    + ", expected "
                    + myField.getDataType());
        }

        if (myField.getId()
                != field.getId()) {
            throw new IllegalArgumentException(
                    "Inconsistent field: " + field);
        }

        int index = values.getIndexOfKey(field.getId());
        if (index == -1) {
            values.put(field.getId(), value);
            invalidateOrder();
        } else {
            values.setValue(index, value);
        }
    }

    @Override
    public FieldValue removeFieldValue(Field field) {
        FieldValue found = values.get(field.getId());
        if (found != null) {
            Hashlet<Integer, FieldValue> copy = new Hashlet<>();
            copy.reserve(values.size() - 1);
            for (int i=0; i < values.size(); i++) {
                if (values.key(i) != field.getId()) {
                    copy.put(values.key(i), values.value(i));
                }
            }
            values = copy;
            invalidateOrder();
        }
        return found;
    }

    @Override
    public void assign(Object o) {
        if ((o instanceof Struct) && ((Struct) o).getDataType().equals(getDataType())) {
            clear();
            Iterator<Map.Entry<Field,FieldValue>> otherValues = ((Struct) o).iterator();
            while (otherValues.hasNext()) {
                Map.Entry<Field, FieldValue> otherEntry = otherValues.next();
                setFieldValue(otherEntry.getKey(), otherEntry.getValue());
            }
        } else {
            throw new IllegalArgumentException("Type " + o.getClass() + " can not specify a " + getClass() + " instance");
        }
    }

    /**
     * Clears this and assigns from the given {@link StructuredFieldValue}
     */
    public void assignFrom(StructuredFieldValue sfv) {
        clear();
        Iterator<Map.Entry<Field,FieldValue>> otherValues = sfv.iterator();
        while (otherValues.hasNext()) {
            Map.Entry<Field, FieldValue> otherEntry = otherValues.next();
            setFieldValue(otherEntry.getKey(), otherEntry.getValue());
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Struct)) return false;
        if (!super.equals(o)) return false;

        Struct struct = (Struct) o;
        return values.equals(struct.values);
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + values.hashCode();
        return result;
    }

    @Override
    public String toString() {
        StringBuilder retVal = new StringBuilder();
        retVal.append("Struct (").append(getDataType()).append("): ");
        int [] increasing = getInOrder();
        for (int i = 0; i < increasing.length; i++) {
            int id = increasing[i];
            retVal.append(getDataType().getField(id)).append("=").append(values.get(id)).append(", ");
        }
        return retVal.toString();
    }

    @Override
    public void serialize(Field field, FieldWriter writer) {
        writer.write(field, this);
    }

    @Override
    public int compareTo(FieldValue obj) {
        int cmp = super.compareTo(obj);
        if (cmp != 0) {
            return cmp;
        }
        Struct rhs = (Struct)obj;
        cmp = values.size() - rhs.values.size();
        if (cmp != 0) {
            return cmp;
        }
        StructDataType type = getDataType();
        for (Field field : type.getFields()) {
            FieldValue lhsField = getFieldValue(field);
            FieldValue rhsField = rhs.getFieldValue(field);
            if (lhsField != null && rhsField != null) {
                cmp = lhsField.compareTo(rhsField);
                if (cmp != 0) {
                    return cmp;
                }
            } else if (lhsField != null || rhsField != null) {
                return (lhsField != null ? -1 : 1);
            }
        }
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.yahoo.document.datatypes.FieldValue#deserialize(com.yahoo.document.Field, com.yahoo.document.serialization.FieldReader)
     */
    @Override
    public void deserialize(Field field, FieldReader reader) {
        reader.read(field, this);
    }

    private class FieldEntry implements Map.Entry<Field, FieldValue> {
        private int id;

        private FieldEntry(int id) {
            this.id = id;
        }

        public Field getKey() {
            return getDataType().getField(id);
        }

        public FieldValue getValue() {
            return values.get(id);
        }

        public FieldValue setValue(FieldValue value) {
            if (value == null) {
                throw new NullPointerException("Null values in Struct not supported, use removeFieldValue() to remove value instead.");
            }

            int index = values.getIndexOfKey(id);
            FieldValue retVal = null;
            if (index == -1) {
                values.put(id, value);
                invalidateOrder();
            } else {
                retVal = values.value(index);
                values.setValue(index, value);
            }

            return retVal;
        }

        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof FieldEntry)) return false;

            FieldEntry that = (FieldEntry) o;
            return (id == that.id);
        }

        public int hashCode() {
            return id;
        }
    }

    private class FieldSet extends AbstractSet<Map.Entry<Field, FieldValue>> {
        @Override
        public int size() {
            return values.size();
        }

        @Override
        public Iterator<Map.Entry<Field, FieldValue>> iterator() {
            return new FieldSetIterator();
        }


    }

    private class FieldSetIterator implements Iterator<Map.Entry<Field, FieldValue>> {
        private int position = 0;
        private int [] increasing = getInOrder();

        public boolean hasNext() {
            return (position < increasing.length);
        }

        public Map.Entry<Field, FieldValue> next() {
            if (position >= increasing.length) {
                throw new NoSuchElementException("No more elements in collection");
            }
            FieldEntry retval = new FieldEntry(increasing[position]);
            position++;
            return retval;
        }

        public void remove() {
            throw new UnsupportedOperationException("The set of fields and values of this struct is unmodifiable when accessed through this method.");
        }
    }

    public static <T> T getFieldValue(FieldValue struct, DataType structType, String fieldName, Class<T> fieldType) {
        if (!(struct instanceof Struct)) {
            return null;
        }
        if (!struct.getDataType().equals(structType)) {
            return null;
        }
        FieldValue fieldValue = ((Struct)struct).getFieldValue(fieldName);
        if (!fieldType.isInstance(fieldValue)) {
            return null;
        }
        return fieldType.cast(fieldValue);
    }

    public static <T> T getFieldValue(FieldValue struct, DataType structType, Field field, Class<T> fieldType) {
        if (!(struct instanceof Struct)) {
            return null;
        }
        if (!struct.getDataType().equals(structType)) {
            return null;
        }
        FieldValue fieldValue = ((Struct)struct).getFieldValue(field);
        if (!fieldType.isInstance(fieldValue)) {
            return null;
        }
        return fieldType.cast(fieldValue);
    }

}