aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/vespaxmlparser/VespaXMLUpdateReader.java
blob: 656fb8dd0337dbdbbe000b1235891ac8fb51560d (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.vespaxmlparser;

import com.yahoo.document.*;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.document.fieldpathupdate.AddFieldPathUpdate;
import com.yahoo.document.fieldpathupdate.AssignFieldPathUpdate;
import com.yahoo.document.fieldpathupdate.FieldPathUpdate;
import com.yahoo.document.fieldpathupdate.RemoveFieldPathUpdate;
import com.yahoo.document.select.parser.ParseException;
import com.yahoo.document.serialization.DocumentUpdateReader;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;

public class VespaXMLUpdateReader extends VespaXMLFieldReader implements DocumentUpdateReader {
    public VespaXMLUpdateReader(String fileName, DocumentTypeManager docTypeManager) throws Exception {
        super(fileName, docTypeManager);
    }

    public VespaXMLUpdateReader(InputStream stream, DocumentTypeManager docTypeManager) throws Exception {
        super(stream, docTypeManager);
    }

    public VespaXMLUpdateReader(XMLStreamReader reader, DocumentTypeManager docTypeManager) {
        super(reader, docTypeManager);
    }

    private Optional<String> condition = Optional.empty();

    public Optional<String> getCondition() {
        return condition;
    }

    public boolean hasFieldPath() {
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if (reader.getAttributeName(i).toString().equals("fieldpath")) {
                return true;
            }
        }

        return false;
    }

    public void read(DocumentUpdate update) {
       try {
           // First fetch attributes.
           DocumentType doctype = null;

           for (int i = 0; i < reader.getAttributeCount(); i++) {
               final String attributeName = reader.getAttributeName(i).toString();
               final String attributeValue = reader.getAttributeValue(i);

               if ("documentid".equals(attributeName) || "id".equals(attributeName)) {
                   update.setId(new DocumentId(attributeValue));
               } else if ("documenttype".equals(attributeName) || "type".equals(attributeName)) {
                   doctype = docTypeManager.getDocumentType(attributeValue);
                   update.setDocumentType(doctype);
               } else if ("create-if-non-existent".equals(attributeName)) {
                   if ("true".equals(attributeValue)) {
                       update.setCreateIfNonExistent(true);
                   } else if ("false".equals(attributeValue)) {
                       update.setCreateIfNonExistent(false);
                   } else {
                       throw newDeserializeException("'create-if-non-existent' must be either 'true' or 'false', was '" + attributeValue +"'");
                   }
               } else if ("condition".equals(attributeName)) {
                   condition = Optional.of(attributeValue);
               }
           }

           if (doctype == null) {
               throw newDeserializeException("Must specify document type. " + reader.getLocation());
           }

           // Then fetch fields
           while (reader.hasNext()) {
               int type = reader.next();

               if (type == XMLStreamReader.START_ELEMENT) {
                   final String currentName = reader.getName().toString();
                   if (hasFieldPath()) {
                       if ("assign".equals(currentName)) {
                           update.addFieldPathUpdate(new AssignFieldPathUpdate(doctype, this));
                           skipToEnd("assign");
                       } else if ("add".equals(currentName)) {
                           update.addFieldPathUpdate(new AddFieldPathUpdate(doctype, this));
                           skipToEnd("add");
                       } else if ("remove".equals(currentName)) {
                           update.addFieldPathUpdate(new RemoveFieldPathUpdate(doctype, this));
                           skipToEnd("remove");
                       } else {
                           throw newDeserializeException("Unknown field path update operation " + reader.getName());
                       }
                   } else {
                       if ("assign".equals(currentName)) {
                           update.addFieldUpdate(readAssign(update));
                           skipToEnd("assign");
                       } else if ("add".equals(currentName)) {
                           update.addFieldUpdate(readAdd(update));
                           skipToEnd("add");
                       } else if ("remove".equals(currentName)) {
                           update.addFieldUpdate(readRemove(update));
                           skipToEnd("remove");
                       } else if ("alter".equals(currentName)) {
                           update.addFieldUpdate(readAlter(update));
                           skipToEnd("alter");
                       } else if ("increment".equals(currentName) ||
                               "decrement".equals(currentName) ||
                               "multiply".equals(currentName) ||
                               "divide".equals(currentName)) {
                           update.addFieldUpdate(readArithmeticField(update, currentName));
                           skipToEnd(currentName);
                       } else {
                           throw newDeserializeException("Unknown update operation " + reader.getName());
                       }
                   }
               } else if (type == XMLStreamReader.END_ELEMENT) {
                   return;
               }
           }
       } catch (XMLStreamException e) {
           throw newException(e);
       }
    }

    FieldUpdate readAdd(DocumentUpdate update) throws XMLStreamException {
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if ("field".equals(reader.getAttributeName(i).toString())) {
                Field f = update.getDocumentType().getField(reader.getAttributeValue(i));

                FieldValue value = f.getDataType().createFieldValue();
                value.deserialize(f, this);

                if (value instanceof Array) {
                    List<FieldValue> l = ((Array)value).getValues();
                    return FieldUpdate.createAddAll(f, l);
                } else if (value instanceof WeightedSet) {
                    return FieldUpdate.createAddAll(f, ((WeightedSet) value));
                } else {
                    throw newDeserializeException("Add operation only applicable to multivalue lists");
                }

            }
        }
        throw newDeserializeException("Add update without field attribute");
    }


    FieldUpdate readRemove(DocumentUpdate update) throws XMLStreamException {
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if ("field".equals(reader.getAttributeName(i).toString())) {
                Field f = update.getDocumentType().getField(reader.getAttributeValue(i));

                FieldValue value = f.getDataType().createFieldValue();
                value.deserialize(f, this);

                if (value instanceof Array) {
                    List<FieldValue> l = ((Array)value).getValues();
                    return FieldUpdate.createRemoveAll(f, l);
                } else if (value instanceof WeightedSet) {
                    return FieldUpdate.createRemoveAll(f, ((WeightedSet)value));
                } else {
                    throw newDeserializeException("Remove operation only applicable to multivalue lists");
                }

            }
        }
        throw newDeserializeException("Remove update without field attribute");
    }

    FieldUpdate readAssign(DocumentUpdate update) throws XMLStreamException {
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if ("field".equals(reader.getAttributeName(i).toString())) {
                Field f = update.getDocumentType().getField(reader.getAttributeValue(i));

                if (f == null) {
                    throw newDeserializeException("Field " + reader.getAttributeValue(i) + " not found.");
                }

                FieldValue value = f.getDataType().createFieldValue();
                value.deserialize(f, this);
                return FieldUpdate.createAssign(f, value);
            }
        }
        throw newDeserializeException("Assignment update without field attribute");
    }


    FieldUpdate readAlter(DocumentUpdate update) throws XMLStreamException {
        Field f = null;
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if ("field".equals(reader.getAttributeName(i).toString())) {
                f = update.getDocumentType().getField(reader.getAttributeValue(i));
            }
        }

        if (f == null) {
            throw newDeserializeException("Alter update without \"field\" attribute");
        }

        FieldUpdate fu = FieldUpdate.create(f);

        while (reader.hasNext()) {
            int type = reader.next();
            if (type == XMLStreamReader.START_ELEMENT) {
                if ("increment".equals(reader.getName().toString()) ||
                    "decrement".equals(reader.getName().toString()) ||
                    "multiply".equals(reader.getName().toString()) ||
                    "divide".equals(reader.getName().toString())) {
                    update.addFieldUpdate(readArithmetic(update, reader.getName().toString(), f, fu));
                    skipToEnd(reader.getName().toString());
                } else {
                    throw newDeserializeException("Element \"" + reader.getName() + "\" not appropriate within alter element");
                }
            } else if (type == XMLStreamReader.END_ELEMENT) {
                break;
            }
        }

        return fu;
    }

    FieldUpdate readArithmeticField(DocumentUpdate update, String type) throws XMLStreamException {
        Field f = null;
        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if ("field".equals(reader.getAttributeName(i).toString())) {
                f = update.getDocumentType().getField(reader.getAttributeValue(i));
            }
        }

        if (f == null) {
            throw newDeserializeException("Assignment update without \"field\" attribute");
        }

        FieldUpdate fu = FieldUpdate.create(f);
        readArithmetic(update, type, f, fu);
        return fu;
    }

    FieldUpdate readArithmetic(DocumentUpdate update, String type, Field f, FieldUpdate fu) throws XMLStreamException {
        Double by = null;

        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if ("by".equals(reader.getAttributeName(i).toString())) {
                by = Double.parseDouble(reader.getAttributeValue(i));
            }
        }

        if (by == null) {
            throw newDeserializeException("Assignment update without \"by\" attribute");
        }

        FieldValue key = null;
        do {
            reader.next();
            if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
                if ("key".equals(reader.getName().toString())) {
                    if (f.getDataType() instanceof WeightedSetDataType) {
                        DataType nestedType = ((WeightedSetDataType)f.getDataType()).getNestedType();
                        key = nestedType.createFieldValue();
                        key.deserialize(this);
                    } else if (f.getDataType() instanceof MapDataType) {
                        key = ((MapDataType)f.getDataType()).getKeyType().createFieldValue();
                        key.deserialize(this);
                    } else if (f.getDataType() instanceof ArrayDataType) {
                        key = new IntegerFieldValue(Integer.parseInt(reader.getElementText()));
                    } else {
                        throw newDeserializeException("Key tag only applicable for weighted sets and maps");
                    }
                    skipToEnd("key");
                } else {
                    throw newDeserializeException("\"" + reader.getName() + "\" not appropriate within " + type + " element.");
                }
            }
        } while (reader.getEventType() != XMLStreamReader.END_ELEMENT);

        if (key != null) {
            if ("increment".equals(type)) { fu.addValueUpdate(ValueUpdate.createIncrement(key, by)); }
            if ("decrement".equals(type)) { fu.addValueUpdate(ValueUpdate.createDecrement(key, by)); }
            if ("multiply".equals(type)) { fu.addValueUpdate(ValueUpdate.createMultiply(key, by)); }
            if ("divide".equals(type)) { fu.addValueUpdate(ValueUpdate.createDivide(key, by)); }
        } else {
            if ("increment".equals(type)) { fu.addValueUpdate(ValueUpdate.createIncrement(by)); }
            if ("decrement".equals(type)) { fu.addValueUpdate(ValueUpdate.createDecrement(by)); }
            if ("multiply".equals(type)) { fu.addValueUpdate(ValueUpdate.createMultiply(by)); }
            if ("divide".equals(type)) { fu.addValueUpdate(ValueUpdate.createDivide(by)); }
        }

        return fu;
    }

    public void read(FieldUpdate update) {
    }

    public void read(FieldPathUpdate update) {
        String whereClause = null;
        String fieldPath = null;

        for (int i = 0; i < reader.getAttributeCount(); i++) {
            if (reader.getAttributeName(i).toString().equals("where")) {
                whereClause = reader.getAttributeValue(i);
            } else if (reader.getAttributeName(i).toString().equals("fieldpath")) {
                fieldPath = reader.getAttributeValue(i);
            }
        }

        if (fieldPath != null) {
            update.setFieldPath(fieldPath);
        } else {
            throw newDeserializeException("Field path is required for document updates.");
        }

        if (whereClause != null) {
            try {
                update.setWhereClause(whereClause);
            } catch (ParseException e) {
                throw newException(e);
            }
        }
    }

    public void read(AssignFieldPathUpdate update) {
        try {
            for (int i = 0; i < reader.getAttributeCount(); i++) {
                if (reader.getAttributeName(i).toString().equals("removeifzero")) {
                    update.setRemoveIfZero(Boolean.parseBoolean(reader.getAttributeValue(i)));
                } else if (reader.getAttributeName(i).toString().equals("createmissingpath")) {
                    update.setCreateMissingPath(Boolean.parseBoolean(reader.getAttributeValue(i)));
                }
            }
            DataType dt = update.getFieldPath().getResultingDataType();

            if (dt instanceof NumericDataType) {
                update.setExpression(reader.getElementText());
            } else {
                FieldValue fv = dt.createFieldValue();
                fv.deserialize(resolveField(update), this);
                update.setNewValue(fv);
            }
        } catch (XMLStreamException e) {
            throw newException(e);
        }
    }

    public void read(AddFieldPathUpdate update) {
        DataType dt = update.getFieldPath().getResultingDataType();
        FieldValue fv = dt.createFieldValue();
        fv.deserialize(resolveField(update), this);
        update.setNewValues((Array)fv);
    }

    public void read(RemoveFieldPathUpdate update) {
    }

    private static Field resolveField(FieldPathUpdate update) {
        String orig = update.getOriginalFieldPath();
        if (orig == null) {
            return null;
        }
        FieldPath path = update.getFieldPath();
        if (path == null) {
            return null;
        }
        DataType type = path.getResultingDataType();
        if (type == null) {
            return null;
        }
        return new Field(orig, type);
    }
}