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

import com.yahoo.document.ArrayDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.document.StructuredDataType;
import com.yahoo.document.WeightedSetDataType;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.document.serialization.DocumentUpdateWriter;

/**
 * <p>Value update that represents performing an encapsulated value update on a subvalue. Currently, there are
 * two multi-value data types in Vespa, <em>array</em> and <em>weighted set</em>. </p>
 *
 * <ul>
 * <li>For an array, the value
 * must be an Integer, and the update must represent a legal operation on the subtype of the array. </li>
 * <li>For a
 * weighted set, the value must be a key of the same type as the subtype of the weighted set, and the update
 * must represent a legal operation on an integer value.</li>
 * </ul>
 *
 * @author Einar M R Rosenvinge
 */
public class MapValueUpdate extends ValueUpdate {
    protected FieldValue value;
    protected ValueUpdate update;

    public MapValueUpdate(FieldValue value, ValueUpdate update) {
        super(ValueUpdateClassID.MAP);
        this.value = value;
        this.update = update;
    }

    /** Returns the key of the nested update */
    public FieldValue getValue() {
        return value;
    }

    /** Sets the key of the nested update */
    public void setValue(FieldValue value) {
        this.value=value;
    }

    public ValueUpdate getUpdate() {
        return update;
    }

    @Override
    public FieldValue applyTo(FieldValue fval) {
        if (fval instanceof Array) {
            Array array = (Array) fval;
            FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger());
            element = update.applyTo(element);
            array.set(((IntegerFieldValue) value).getInteger(), element);
        } else if (fval instanceof WeightedSet) {
            WeightedSet wset = (WeightedSet) fval;
            WeightedSetDataType wtype = wset.getDataType();
            Integer weight = wset.get(value);
            if (weight == null) {
                if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) {
                    weight = 0;
                } else {
                    return fval;
                }
            }
            weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue();
            wset.put(value, weight);
            if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) {
                wset.remove(value);
            }
        }
        return fval;
    }

    @Override
    protected void checkCompatibility(DataType fieldType) {
        if (fieldType instanceof ArrayDataType) {
            if (!(value instanceof IntegerFieldValue)) {
                throw new IllegalArgumentException("Expected integer, got " + value.getClass().getName() + ".");
            }
            update.checkCompatibility(((ArrayDataType)fieldType).getNestedType());
        } else if (fieldType instanceof WeightedSetDataType) {
            ((WeightedSetDataType)fieldType).getNestedType().createFieldValue().assign(value);
            update.checkCompatibility(DataType.INT);
        } else if (fieldType instanceof StructuredDataType) {
            if (!(value instanceof StringFieldValue)) {
                throw new IllegalArgumentException("Expected string, got " + value.getClass().getName() + ".");
            }
            Field field = ((StructuredDataType)fieldType).getField(((StringFieldValue)value).getString());
            if (field == null) {
                throw new IllegalArgumentException("Field '" + value + "' not found.");
            }
            update.checkCompatibility(field.getDataType());
        } else {
            throw new UnsupportedOperationException("Field type " + fieldType.getName() + " not supported.");
        }
    }


    @Override
    public void serialize(DocumentUpdateWriter data, DataType superType) {
        data.write(this, superType);
    }

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

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

    @Override
    public String toString() {
        return super.toString() + " " + value + " " + update;
    }

}