aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/readers/MapReader.java
blob: b45a0001fd1d1387d5140568a401b4b6a1000b29 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json.readers;

import com.fasterxml.jackson.core.JsonToken;
import com.google.common.base.Preconditions;
import com.yahoo.document.ArrayDataType;
import com.yahoo.document.CollectionDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.document.MapDataType;
import com.yahoo.document.WeightedSetDataType;
import com.yahoo.document.datatypes.CollectionFieldValue;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.MapFieldValue;
import com.yahoo.document.json.TokenBuffer;
import com.yahoo.document.update.MapValueUpdate;
import com.yahoo.document.update.ValueUpdate;

import static com.yahoo.document.json.readers.JsonParserHelpers.expectArrayStart;
import static com.yahoo.document.json.readers.JsonParserHelpers.expectObjectEnd;
import static com.yahoo.document.json.readers.JsonParserHelpers.expectObjectStart;
import static com.yahoo.document.json.readers.SingleValueReader.readAtomic;
import static com.yahoo.document.json.readers.SingleValueReader.readSingleUpdate;
import static com.yahoo.document.json.readers.SingleValueReader.readSingleValue;

public class MapReader {

    public static final String MAP_KEY = "key";
    public static final String MAP_VALUE = "value";
    public static final String UPDATE_ELEMENT = "element";
    public static final String UPDATE_MATCH = "match";

    public static void fillMap(TokenBuffer buffer, MapFieldValue parent, boolean ignoreUndefinedFields) {
        if (buffer.current() == JsonToken.START_ARRAY) {
            MapReader.fillMapFromArray(buffer, parent, ignoreUndefinedFields);
        } else {
            MapReader.fillMapFromObject(buffer, parent, ignoreUndefinedFields);
        }
    }

    @SuppressWarnings({ "rawtypes", "cast", "unchecked" })
    public static void fillMapFromArray(TokenBuffer buffer, MapFieldValue parent, boolean ignoreUndefinedFields) {
        JsonToken token = buffer.current();
        int initNesting = buffer.nesting();
        expectArrayStart(token);
        token = buffer.next();
        DataType keyType = parent.getDataType().getKeyType();
        DataType valueType = parent.getDataType().getValueType();
        while (buffer.nesting() >= initNesting) {
            FieldValue key = null;
            FieldValue value = null;
            expectObjectStart(token);
            token = buffer.next();
            for (int i = 0; i < 2; ++i) {
                if (MAP_KEY.equals(buffer.currentName())) {
                    key = readSingleValue(buffer, keyType, ignoreUndefinedFields);
                } else if (MAP_VALUE.equals(buffer.currentName())) {
                    value = readSingleValue(buffer, valueType, ignoreUndefinedFields);
                }
                token = buffer.next();
            }
            Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
            parent.put(key, value);

            expectObjectEnd(token);
            token = buffer.next(); // array end or next entry
        }
    }

    @SuppressWarnings({ "rawtypes", "cast", "unchecked" })
    public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent, boolean ignoreUndefinedFields) {
        JsonToken token = buffer.current();
        int initNesting = buffer.nesting();
        expectObjectStart(token);
        token = buffer.next();
        DataType keyType = parent.getDataType().getKeyType();
        DataType valueType = parent.getDataType().getValueType();
        while (buffer.nesting() >= initNesting) {
            FieldValue key = readAtomic(buffer.currentName(), keyType);
            FieldValue value = readSingleValue(buffer, valueType, ignoreUndefinedFields);

            Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
            parent.put(key, value);
            token = buffer.next();
        }
        expectObjectEnd(token);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static ValueUpdate createMapUpdate(TokenBuffer buffer,
                                              DataType currentLevel,
                                              boolean ignoreUndefinedFields) {
        if ( ! JsonToken.START_OBJECT.equals(buffer.current()))
            throw new IllegalArgumentException("Expected object for match update, got " + buffer.current());
        buffer.next();

        FieldValue key = null;
        ValueUpdate update;

        if (UPDATE_ELEMENT.equals(buffer.currentName())) {
            key = keyTypeForMapUpdate(buffer.currentText(), currentLevel);
            buffer.next();
        }

        update = UPDATE_MATCH.equals(buffer.currentName()) ? createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), ignoreUndefinedFields)
                                                           : readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName(), ignoreUndefinedFields);
        buffer.next();

        if (key == null) {
            if ( ! UPDATE_ELEMENT.equals(buffer.currentName()))
                throw new IllegalArgumentException("Expected match element, got " + buffer.current());
            key = keyTypeForMapUpdate(buffer.currentText(), currentLevel);
            buffer.next();
        }

        if ( ! JsonToken.END_OBJECT.equals(buffer.current()))
            throw new IllegalArgumentException("Expected object end for match update, got " + buffer.current());

        return ValueUpdate.createMap(key, update);
    }

    @SuppressWarnings("rawtypes")
    public static ValueUpdate createMapUpdate(TokenBuffer buffer, Field field, boolean ignoreUndefinedFields) {
        return MapReader.createMapUpdate(buffer, field.getDataType(), ignoreUndefinedFields);
    }

    private static DataType valueTypeForMapUpdate(DataType parentType) {
        if (parentType instanceof WeightedSetDataType) {
            return DataType.INT;
        } else if (parentType instanceof CollectionDataType) {
            return ((CollectionDataType) parentType).getNestedType();
        } else if (parentType instanceof MapDataType) {
            return ((MapDataType) parentType).getValueType();
        } else {
            throw new UnsupportedOperationException("Unexpected parent type: " + parentType);
        }
    }

    private static FieldValue keyTypeForMapUpdate(String elementText, DataType expectedType) {
        FieldValue v;
        if (expectedType instanceof ArrayDataType) {
            v = new IntegerFieldValue(Integer.valueOf(elementText));
        } else if (expectedType instanceof WeightedSetDataType) {
            v = ((WeightedSetDataType) expectedType).getNestedType().createFieldValue(elementText);
        } else if (expectedType instanceof MapDataType) {
            v = ((MapDataType) expectedType).getKeyType().createFieldValue(elementText);
        } else {
            throw new IllegalArgumentException("Container type " + expectedType + " not supported for match update.");
        }
        return v;
    }
}