aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/fieldpathupdate/AddFieldPathUpdate.java
blob: 1efee8616a92bf703e9dc30afe57b8f8a99a14fd (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.fieldpathupdate;

import com.yahoo.document.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.CollectionFieldValue;
import com.yahoo.document.datatypes.FieldPathIteratorHandler;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.serialization.DocumentUpdateReader;
import com.yahoo.document.serialization.VespaDocumentSerializer6;

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

    class IteratorHandler extends FieldPathIteratorHandler {
        Array newValues;

        IteratorHandler(Array newValues) {
            this.newValues = newValues;
        }

        @SuppressWarnings({ "unchecked" })
        @Override
        public ModificationStatus doModify(FieldValue fv) {
            for (Object newValue : newValues.getValues()) {
                ((CollectionFieldValue)fv).add((FieldValue) newValue);
            }
            return ModificationStatus.MODIFIED;
        }

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

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

        public Array getNewValues() {
            return newValues;
        }

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

            IteratorHandler that = (IteratorHandler) o;

            if (!newValues.equals(that.newValues)) return false;

            return true;
        }

        @Override
        public int hashCode() {
            return newValues.hashCode();
        }
    }

    IteratorHandler handler;

    public AddFieldPathUpdate(DocumentType type, String fieldPath, String whereClause, Array newValues) {
        super(FieldPathUpdate.Type.ADD, type, fieldPath, whereClause);
        setNewValues(newValues);
    }

    public AddFieldPathUpdate(DocumentType type, String fieldPath, Array newValues) {
        super(FieldPathUpdate.Type.ADD, type, fieldPath, null);
        setNewValues(newValues);
    }

    public AddFieldPathUpdate(DocumentType type, DocumentUpdateReader reader) {
        super(FieldPathUpdate.Type.ADD, type, reader);
        reader.read(this);
    }

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

    public void setNewValues(Array value) {
        handler = new IteratorHandler(value);
    }

    public Array getNewValues() {
        return handler.getNewValues();
    }

    public FieldPathIteratorHandler getIteratorHandler(Document doc) {
        return handler;
    }

    @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;

        AddFieldPathUpdate that = (AddFieldPathUpdate) o;

        if (!handler.equals(that.handler)) return false;

        return true;
    }

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

    @Override
    public String toString() {
        return "Add: " + super.toString() + " : " + handler.getNewValues();
    }
}