aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/update/arithmeticvalueupdate.cpp
blob: 06f90d59d85250c19ee3c8d353b11a2fa66afa18 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "arithmeticvalueupdate.h"
#include <vespa/document/base/field.h>
#include <vespa/document/fieldvalue/fieldvalues.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/xmlstream.h>
#include <ostream>

using vespalib::IllegalArgumentException;
using vespalib::IllegalStateException;
using namespace vespalib::xml;

namespace document {

// Declare string representations for operator names.
static const char * operatorName[]  = { "add", "div", "mul", "sub" };
static const char * operatorNameC[] = { "Add", "Div", "Mul", "Sub" };

bool
ArithmeticValueUpdate::operator==(const ValueUpdate& other) const
{
    if (other.getType() != Arithmetic) return false;
    const ArithmeticValueUpdate& o(static_cast<const ArithmeticValueUpdate&>(other));
    if (_operator != o._operator) return false;
    if (_operand != o._operand) return false;
    return true;
}

// Ensure that this update is compatible with given field.
void
ArithmeticValueUpdate::checkCompatibility(const Field& field) const
{
    if ( ! field.getDataType().isNumeric()) {
        throw IllegalArgumentException(vespalib::make_string(
                "Can not perform arithmetic update on non-numeric field '%s'.",
                field.getName().data()), VESPA_STRLOC);
    }
}

// Apply this update.
bool
ArithmeticValueUpdate::applyTo(FieldValue& value) const
{
    if (value.isA(FieldValue::Type::BYTE)) {
        ByteFieldValue& bValue = static_cast<ByteFieldValue&>(value);
        bValue.setValue((int)applyTo(static_cast<int64_t>(bValue.getAsInt())));
    } else if (value.isA(FieldValue::Type::DOUBLE)) {
        DoubleFieldValue& dValue = static_cast<DoubleFieldValue&>(value);
        dValue.setValue(applyTo(dValue.getAsDouble()));
    } else if (value.isA(FieldValue::Type::FLOAT)) {
        FloatFieldValue& fValue = static_cast<FloatFieldValue&>(value);
        fValue.setValue((float)applyTo(fValue.getAsFloat()));
    } else if (value.isA(FieldValue::Type::INT)) {
        IntFieldValue& iValue = static_cast<IntFieldValue&>(value);
        iValue.setValue((int)applyTo(static_cast<int64_t>(iValue.getAsInt())));
    } else if (value.isA(FieldValue::Type::LONG)) {
        LongFieldValue& lValue = static_cast<LongFieldValue&>(value);
        lValue.setValue(applyTo(lValue.getAsLong()));
    } else {
        vespalib::string err = vespalib::make_string(
                "Unable to perform an arithmetic update on a \"%s\" field "
                "value.", value.className());
        throw IllegalStateException(err, VESPA_STRLOC);
    }
    return true;
}

// Perform the contained operation on the given value.
double
ArithmeticValueUpdate::applyTo(double value) const
{
    switch(_operator) {
    case Add:
        return value + _operand;
    case Div:
        return value / _operand;
    case Mul:
        return value * _operand;
    case Sub:
        return value - _operand;
    default:
        return 0;
    }
}

// Perform the contained operation on the given value.
long
ArithmeticValueUpdate::applyTo(int64_t value) const
{
    switch(_operator) {
    case Add:
        return (long)(value + _operand);
    case Div:
        return (long)(value / _operand);
    case Mul:
        return (long)(value * _operand);
    case Sub:
        return (long)(value - _operand);
    default:
        return 0;
    }
}

// Perform the contained operation on the given value.
std::string
ArithmeticValueUpdate::applyTo(const std::string & value) const
{
    return value;
}

// Print this update as a human readable string.
void
ArithmeticValueUpdate::print(std::ostream& out, bool, const std::string& indent) const
{
    out << indent << "ArithmeticValueUpdate(" << operatorNameC[_operator] << " " << _operand << ")";
}

void
ArithmeticValueUpdate::printXml(XmlOutputStream& xos) const
{
    xos << XmlTag(operatorName[_operator])
        << XmlAttribute("by", _operand)
        << XmlEndTag();
}

// Deserialize this update from the given buffer.
void
ArithmeticValueUpdate::deserialize(const DocumentTypeRepo&, const DataType&, nbostream & stream)
{
    int32_t opt;
    stream >> opt >>_operand;
    _operator = static_cast<ArithmeticValueUpdate::Operator>(opt);
}

} // document