aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/fieldpathupdate/FieldPathUpdate.java
blob: 84994de7069ee0a60bb073324f459df0bf776d81 (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
// Copyright Yahoo. 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.DocumentPut;
import com.yahoo.document.DocumentType;
import com.yahoo.document.FieldPath;
import com.yahoo.document.datatypes.FieldPathIteratorHandler;
import com.yahoo.document.select.DocumentSelector;
import com.yahoo.document.select.Result;
import com.yahoo.document.select.ResultList;
import com.yahoo.document.select.parser.ParseException;
import com.yahoo.document.serialization.DocumentUpdateReader;
import com.yahoo.document.serialization.VespaDocumentSerializer6;
import java.util.ListIterator;

/**
 * @author Thomas Gundersen
 */
public abstract class FieldPathUpdate {

    public enum Type {
        ASSIGN(0),
        REMOVE(1),
        ADD(2);

        private final int code;

        private Type(int code) {
            this.code = code;
        }

        public static Type valueOf(int code) {
            for (Type type : values()) {
                if (type.code == code) {
                    return type;
                }
            }
            throw new IllegalArgumentException("Field path update type " + code + " not supported.");
        }

        public int getCode() {
            return code;
        }
    }

    private FieldPath fieldPath;
    private DocumentSelector selector;
    private String originalFieldPath;
    private String whereClause;
    private Type updType;
    private DocumentType docType;

    public FieldPathUpdate(Type updType, DocumentType docType, String fieldPath, String whereClause) {
        this.updType = updType;
        this.docType = docType;

        try {
            setWhereClause(whereClause);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
        setFieldPath(fieldPath);
    }

    public FieldPathUpdate(Type updType, DocumentType docType, DocumentUpdateReader reader) {
        this(updType, docType);
        reader.read(this);
    }

    public FieldPathUpdate(Type updType, DocumentType docType) {
        this.updType = updType;
        this.docType = docType;
    }

    public Type getUpdateType() {
        return updType;
    }

    public DocumentType getDocumentType() {
        return docType;
    }

    public void setFieldPath(String fieldPath) {
        originalFieldPath = fieldPath;
        this.fieldPath = docType.buildFieldPath(fieldPath);
    }

    public FieldPath getFieldPath() {
        return fieldPath;
    }

    public String getOriginalFieldPath() {
        return originalFieldPath;
    }

    public void setWhereClause(String whereClause) throws ParseException {
        this.whereClause = whereClause;
        selector = null;
        if (whereClause != null && !whereClause.equals("")) {
            selector = new DocumentSelector(whereClause);
        }
    }

    public DocumentSelector getWhereClause() {
        return selector;
    }

    public String getOriginalWhereClause() {
        return whereClause;
    }

    public void applyTo(Document doc) {
        if (selector == null) {
            FieldPathIteratorHandler handler = getIteratorHandler(doc);
            doc.iterateNested(fieldPath, 0, handler);
        } else {
            ResultList results = selector.getMatchingResultList(new DocumentPut(doc));
            ListIterator<ResultList.ResultPair> resultIter = results.getResults().listIterator(results.getResults().size());
            while (resultIter.hasPrevious()) {
                ResultList.ResultPair rp = resultIter.previous();
                if (rp.getResult() == Result.TRUE) {
                    FieldPathIteratorHandler handler = getIteratorHandler(doc);
                    handler.getVariables().clear();
                    handler.getVariables().putAll(rp.getVariables());

                    doc.iterateNested(fieldPath, 0, handler);
                }
            }
        }
    }

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

    public static FieldPathUpdate create(Type type, DocumentType docType, DocumentUpdateReader reader) {
        switch (type) {
        case ASSIGN:
            return new AssignFieldPathUpdate(docType, reader);
        case ADD:
            return new AddFieldPathUpdate(docType, reader);
        case REMOVE:
            return new RemoveFieldPathUpdate(docType, reader);
        }
        throw new IllegalArgumentException("Field path update type '" + type + "' not supported.");
    }

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

        FieldPathUpdate that = (FieldPathUpdate) o;

        if (docType != null ? !docType.equals(that.docType) : that.docType != null) return false;
        if (originalFieldPath != null ? !originalFieldPath.equals(that.originalFieldPath) : that.originalFieldPath != null)
            return false;
        if (whereClause != null ? !whereClause.equals(that.whereClause) : that.whereClause != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = originalFieldPath != null ? originalFieldPath.hashCode() : 0;
        result = 31 * result + (whereClause != null ? whereClause.hashCode() : 0);
        result = 31 * result + (docType != null ? docType.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "fieldpath=\"" + originalFieldPath + "\"" + (whereClause != null ? " where=\"" + whereClause + "\"" : "");
    }

    abstract FieldPathIteratorHandler getIteratorHandler(Document doc);
}