aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/update/assignfieldpathupdate.cpp
blob: 649b1c1b6b8ac831b8112dbba7b1cbb96cf6b221 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "assignfieldpathupdate.h"
#include <vespa/document/fieldvalue/fieldvalues.h>
#include <vespa/document/fieldvalue/iteratorhandler.h>
#include <vespa/document/select/variablemap.h>
#include <vespa/document/serialization/vespadocumentdeserializer.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/util/exceptions.h>
#include <boost/numeric/conversion/cast.hpp>
#include <ostream>

#include <vespa/log/log.h>
LOG_SETUP(".document.update.fieldpathupdate");

using vespalib::nbostream;

namespace document {

using namespace fieldvalue;

AssignFieldPathUpdate::AssignFieldPathUpdate()
    : FieldPathUpdate(Assign),
      _newValue(),
      _expression(),
      _removeIfZero(false),
      _createMissingPath(false)
{ }


AssignFieldPathUpdate::AssignFieldPathUpdate(
        const DataType& type,
        stringref fieldPath,
        stringref whereClause,
        std::unique_ptr<FieldValue> newValue)
    : FieldPathUpdate(Assign, fieldPath, whereClause),
      _newValue(std::move(newValue)),
      _expression(),
      _removeIfZero(false),
      _createMissingPath(true)
{
    checkCompatibility(*_newValue, type);
}

AssignFieldPathUpdate::AssignFieldPathUpdate(stringref fieldPath, stringref whereClause, stringref expression)
    : FieldPathUpdate(Assign, fieldPath, whereClause),
      _newValue(),
      _expression(expression),
      _removeIfZero(false),
      _createMissingPath(true)
{
    if (_expression.empty()) {
        throw vespalib::IllegalArgumentException("Cannot create an arithmetic "
                "assignment update with an empty expression", VESPA_STRLOC);
    }
}

AssignFieldPathUpdate::~AssignFieldPathUpdate() = default;

namespace {

class AssignValueIteratorHandler : public IteratorHandler
{
public:
    AssignValueIteratorHandler(const FieldValue& newValue,
                               bool removeIfZero,
                               bool createMissingPath_)
            : _newValue(newValue), _removeIfZero(removeIfZero),
              _createMissingPath(createMissingPath_)
    {}

    ModificationStatus doModify(FieldValue& fv) override;
    bool onComplex(const Content&) override { return false; }
    bool createMissingPath() const override { return _createMissingPath; }

private:
    const FieldValue& _newValue;
    bool _removeIfZero;
    bool _createMissingPath;
};

class AssignExpressionIteratorHandler : public IteratorHandler
{
public:
    AssignExpressionIteratorHandler(
            const DocumentTypeRepo& repo,
            Document& doc,
            const vespalib::string& expression,
            bool removeIfZero,
            bool createMissingPath_)
            : _calc(repo, expression),
              _doc(doc),
              _removeIfZero(removeIfZero),
              _createMissingPath(createMissingPath_)
    {}

    ModificationStatus doModify(FieldValue& fv) override;
    bool onComplex(const Content&) override { return false; }
    bool createMissingPath() const override { return _createMissingPath; }

private:
    DocumentCalculator _calc;
    Document& _doc;
    bool _removeIfZero;
    bool _createMissingPath;
};

ModificationStatus
AssignValueIteratorHandler::doModify(FieldValue& fv) {
    LOG(spam, "fv = %s", fv.toString().c_str());
    if (!(*fv.getDataType() == *_newValue.getDataType())) {
        vespalib::string err = vespalib::make_string(
                "Trying to assign \"%s\" of type %s to an instance of type %s",
                _newValue.toString().c_str(), _newValue.className(), fv.className());
        throw vespalib::IllegalArgumentException(err, VESPA_STRLOC);
    }
    if (_removeIfZero
        && _newValue.isNumeric()
        && static_cast<const NumericFieldValueBase&>(_newValue).getAsLong() == 0)
    {
        return ModificationStatus::REMOVED;
    }
    fv.assign(_newValue);
    return ModificationStatus::MODIFIED;
}

ModificationStatus
AssignExpressionIteratorHandler::doModify(FieldValue& fv) {
    LOG(spam, "fv = %s", fv.toString().c_str());
    if (fv.isNumeric()) {
        std::unique_ptr<select::VariableMap> varHolder = std::make_unique<select::VariableMap>();
        select::VariableMap & vars = *varHolder;
        for (VariableMap::const_iterator i(getVariables().begin()),
                 e(getVariables().end()); i != e; ++i)
        {
            if (i->second.key.get() && i->second.key->isNumeric()) {
                vars[i->first] = i->second.key->getAsDouble();
            } else {
                vars[i->first] = i->second.index;
            }
        }

        vars["value"] = fv.getAsDouble();

        try {
            double res = _calc.evaluate(_doc, std::move(varHolder));
            if (_removeIfZero && static_cast<uint64_t>(res) == 0) {
                return ModificationStatus::REMOVED;
            } else {
                fv.assign(DoubleFieldValue(res));
            }
        } catch (const vespalib::IllegalArgumentException&) {
            // Divide by zero does not modify the document field
            return ModificationStatus::NOT_MODIFIED;
        } catch (const boost::bad_numeric_cast&) {
            // Underflow/overflow does not modify
            return ModificationStatus::NOT_MODIFIED;
        }
    } else {
        throw vespalib::IllegalArgumentException(
                vespalib::make_string("Trying to perform arithmetic on %s of type %s",
                                      fv.toString().c_str(), fv.getDataType()->toString().c_str()),
                VESPA_STRLOC);
    }
    return ModificationStatus::MODIFIED;
}

}

std::unique_ptr<IteratorHandler>
AssignFieldPathUpdate::getIteratorHandler(Document& doc, const DocumentTypeRepo & repo) const
{
    if (!_expression.empty()) {
        return std::make_unique<AssignExpressionIteratorHandler>(repo, doc, _expression, _removeIfZero, _createMissingPath);
    } else {
        return std::make_unique<AssignValueIteratorHandler>(*_newValue, _removeIfZero, _createMissingPath);
    }
}

bool
AssignFieldPathUpdate::operator==(const FieldPathUpdate& other) const
{
    if (!FieldPathUpdate::operator==(other)) return false;
    const auto & assignOther = static_cast<const AssignFieldPathUpdate&>(other);
    if (assignOther._newValue.get() && _newValue.get()) {
        if (*assignOther._newValue != *_newValue) return false;
    }
    // else: should always have at least 1 with non-empty expression
    return (assignOther._expression == _expression)
            && (assignOther._removeIfZero == _removeIfZero)
            && (assignOther._createMissingPath == _createMissingPath);
}

void
AssignFieldPathUpdate::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    out << "AssignFieldPathUpdate(\n";
    FieldPathUpdate::print(out, verbose, indent + "  ");
    if (_newValue.get()) {
        out << ",\n" << indent << "  " << "newValue=";
        _newValue->print(out, verbose, indent + "  ");
    } else {
        out << ",\n" << indent << "  " << "expression='" << _expression << "'";
    }
    out << ", removeIfZero=" << (_removeIfZero ? "yes" : "no")
        << ", createMissingPath=" << (_createMissingPath ? "yes" : "no")
        << "\n" << indent << ")";
}

void
AssignFieldPathUpdate::deserialize(const DocumentTypeRepo& repo, const DataType& type, nbostream & stream)
{
    FieldPathUpdate::deserialize(repo, type, stream);

    uint8_t flags = 0x00;
    stream >> flags;

    _removeIfZero = (flags & REMOVE_IF_ZERO) != 0;
    _createMissingPath = (flags & CREATE_MISSING_PATH) != 0;

    if (flags & ARITHMETIC_EXPRESSION) {
        _expression = getString(stream);
    } else {
        FieldPath path;
        type.buildFieldPath(path, getOriginalFieldPath());
        _newValue.reset(getResultingDataType(path).createFieldValue().release());
        VespaDocumentDeserializer deserializer(repo, stream, Document::getNewestSerializationVersion());
        deserializer.read(*_newValue);
    }
}

} // ns document