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

import com.yahoo.document.Document;
import com.yahoo.document.DocumentCalculator;
import com.yahoo.document.DocumentType;
import com.yahoo.document.datatypes.FieldPathIteratorHandler;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.NumericFieldValue;
import com.yahoo.document.select.parser.ParseException;
import com.yahoo.document.serialization.DocumentUpdateReader;
import com.yahoo.document.serialization.VespaDocumentSerializer6;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Thomas Gundersen
 */
public class AssignFieldPathUpdate extends FieldPathUpdate {

    class SimpleAssignIteratorHandler extends FieldPathIteratorHandler {
        FieldValue newValue;
        boolean removeIfZero;
        boolean createMissingPath;

        SimpleAssignIteratorHandler(FieldValue newValue, boolean removeIfZero, boolean createMissingPath) {
            this.newValue = newValue;
            this.removeIfZero = removeIfZero;
            this.createMissingPath = createMissingPath;
        }

        @Override
        public ModificationStatus doModify(FieldValue fv) {
            if (!fv.getDataType().equals(newValue.getDataType())) {
                throw new IllegalArgumentException("Trying to assign " + newValue + " of type " + newValue.getDataType() + " to an instance of " + fv.getDataType());
            } else {
                if (removeIfZero && (newValue instanceof NumericFieldValue) && ((NumericFieldValue)newValue).getNumber().longValue() == 0) {
                    return ModificationStatus.REMOVED;
                }
                fv.assign(newValue);
            }
            return ModificationStatus.MODIFIED;
        }

        @Override
        public boolean createMissingPath() {
            return createMissingPath;
        }

        @Override
        public boolean onComplex(FieldValue fv) {
            return false;
        }
    }

    class MathAssignIteratorHandler extends FieldPathIteratorHandler {
        DocumentCalculator calc;
        Document doc;
        boolean removeIfZero;
        boolean createMissingPath;

        MathAssignIteratorHandler(String expression, Document doc, boolean removeIfZero, boolean createMissingPath) throws ParseException {
            this.calc = new DocumentCalculator(expression);
            this.doc = doc;
            this.removeIfZero = removeIfZero;
            this.createMissingPath = createMissingPath;
        }

        @Override
        public ModificationStatus doModify(FieldValue fv) {
            if (fv instanceof NumericFieldValue) {
                Map<String, Object> vars = new HashMap<String, Object>();
                for (Map.Entry<String, IndexValue> entry : getVariables().entrySet()) {
                    if (entry.getValue().getKey() != null && entry.getValue().getKey() instanceof NumericFieldValue) {
                        vars.put(entry.getKey(), ((NumericFieldValue)entry.getValue().getKey()).getNumber());
                    } else {
                        vars.put(entry.getKey(), entry.getValue().getIndex());
                    }
                }
                vars.put("value", ((NumericFieldValue)fv).getNumber());

                try {
                    Number d = calc.evaluate(doc, vars);
                    if (removeIfZero && d.longValue() == 0) {
                        return ModificationStatus.REMOVED;
                    } else {
                        fv.assign(calc.evaluate(doc, vars));
                    }
                } catch (IllegalArgumentException e) {
                    // Ignore divide by zero
                    return ModificationStatus.NOT_MODIFIED;
                }
            } else {
                throw new IllegalArgumentException("Trying to perform arithmetic on " + fv + " of type " + fv.getDataType());
            }
            return ModificationStatus.MODIFIED;
        }

        @Override
        public boolean createMissingPath() {
            return createMissingPath;
        }

        @Override
        public boolean onComplex(FieldValue fv) {
            return false;
        }
    }

    FieldValue fieldValue = null;
    String expression = null;
    boolean createMissingPath = true;
    boolean removeIfZero = false;

    // Flag bits
    public static final int ARITHMETIC_EXPRESSION = 1;
    public static final int REMOVE_IF_ZERO = 2;
    public static final int CREATE_MISSING_PATH = 4;

    /**
     * Creates an assignment update that overwrites the old value with the given new value.
     *
     * @param type The document type the assignment works on.
     * @param fieldPath The field path of the field to be overwritten.
     * @param whereClause A document selection string that selects documents and variables to be updated.
     * @param newValue The new value of the assignment.
     */
    public AssignFieldPathUpdate(DocumentType type, String fieldPath, String whereClause, FieldValue newValue) {
        super(FieldPathUpdate.Type.ASSIGN, type, fieldPath, whereClause);
        setNewValue(newValue);
    }

    public AssignFieldPathUpdate(DocumentType type, String fieldPath, FieldValue newValue) {
        super(FieldPathUpdate.Type.ASSIGN, type, fieldPath, null);
        setNewValue(newValue);
    }

    /**
     * Creates an assign statement based on a mathematical expression.
     *
     * @param type The document type the assignment works on.
     * @param fieldPath The field path of the field to be overwritten.
     * @param whereClause A document selection string that selects documents and variables to be updated.
     * @param expression The mathematical expression to apply. Use $value to signify the previous value of the field.
     */
    public AssignFieldPathUpdate(DocumentType type, String fieldPath, String whereClause, String expression) {
        super(FieldPathUpdate.Type.ASSIGN, type, fieldPath, whereClause);
        setExpression(expression);
    }

    /**
     * Creates an assign update from a serialized object.
     *
     * @param type The document type the assignment will work on.
     * @param reader A reader that can deserialize something into this object.
     */
    public AssignFieldPathUpdate(DocumentType type, DocumentUpdateReader reader) {
        super(FieldPathUpdate.Type.ASSIGN, type, reader);
        reader.read(this);
    }

    public AssignFieldPathUpdate(DocumentType type, String fieldPath) {
        super(FieldPathUpdate.Type.ASSIGN, type, fieldPath, null);
    }

    /**
     * Turns this assignment into a literal one.
     *
     * @param value The new value to assign to the document.
     */
    public void setNewValue(FieldValue value) {
        fieldValue = value;
        expression = null;
    }

    /**
     *
     * @return Returns the value to assign, or null if this is a mathematical expression.
     */
    public FieldValue getNewValue() {
        return fieldValue;
    }

    /**
     * Turns this assignment into a mathematical expression assignment.
     *
     * @param value The expression to use for assignment.
     */
    public void setExpression(String value) {
        expression = value;
        fieldValue = null;
    }

    /**
     *
     * @return Returns the arithmetic expression to assign, or null if this is not a mathematical expression.
     */
    public String getExpression() {
        return expression;
    }

    /**
     * If set to true, and the new value assigned evaluates to a numeric value of 0, removes the value instead of setting it.
     * Default is false.
     */
    public void setRemoveIfZero(boolean removeIfZero) {
        this.removeIfZero = removeIfZero;
    }

    /**
     * If set to true, and any part of the field path specified does not exist (except for array indexes), we create the path as necessary.
     * Default is true.
     */
    public void setCreateMissingPath(boolean createMissingPath) {
        this.createMissingPath = createMissingPath;
    }

    /**
     *
     * @return Returns true if this assignment is an arithmetic operation.
     */
    public boolean isArithmetic() {
        return expression != null;
    }

    FieldPathIteratorHandler getIteratorHandler(Document doc) {
        if (expression != null) {
            try {
                return new MathAssignIteratorHandler(expression, doc, removeIfZero, createMissingPath);
            } catch (ParseException e) {
                return null;
            }
        } else {
            return new SimpleAssignIteratorHandler(fieldValue, removeIfZero, createMissingPath);
        }
    }

    @Override
    public void serialize(VespaDocumentSerializer6 data) {
        data.write(this);
    }

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

        AssignFieldPathUpdate that = (AssignFieldPathUpdate) o;

        if (createMissingPath != that.createMissingPath) return false;
        if (removeIfZero != that.removeIfZero) return false;
        if (expression != null ? !expression.equals(that.expression) : that.expression != null) return false;
        if (fieldValue != null ? !fieldValue.equals(that.fieldValue) : that.fieldValue != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (fieldValue != null ? fieldValue.hashCode() : 0);
        result = 31 * result + (expression != null ? expression.hashCode() : 0);
        result = 31 * result + (createMissingPath ? 1 : 0);
        result = 31 * result + (removeIfZero ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Assign: " + super.toString() + " : " + (isArithmetic() ? getExpression() : getNewValue().toString());
    }

    public boolean getCreateMissingPath() {
        return createMissingPath;
    }

    public boolean getRemoveIfZero() {
        return removeIfZero;
    }

    public FieldValue getFieldValue() {
        return fieldValue;
    }

}