aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/attribute_operation.cpp
blob: 55e104560a79ef98292a47b6b7db6285ed41d6a6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "attribute_operation.h"
#include "singlenumericattribute.h"
#include <vespa/searchlib/common/bitvector.h>
#include <vespa/searchcommon/attribute/basictype.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/util/array.hpp>

#include <vespa/log/log.h>
LOG_SETUP(".proton.matching.attribute_operation");

namespace search::attribute {

namespace {

template <typename T>
struct Inc {
    using V = T;
    Inc(T ) {}
    T operator()(T oldVal) const { return oldVal + 1; }
};

template <typename T>
struct Dec {
    using V = T;
    Dec(T ) {}
    T operator()(T oldVal) const { return oldVal - 1; }
};

template <typename T>
struct Add {
    using V = T;
    Add(T m) : _m(m) {}
    T _m;
    T operator()(T oldVal) const { return oldVal + _m; }
};

template <typename T>
struct Mul {
    using V = T;
    Mul(T m) : _m(m) {}
    T _m;
    T operator()(T oldVal) const { return oldVal * _m; }
};

template <typename T>
struct Div {
    using V = T;
    Div(T m) : _m(m) {}
    T _m;
    T operator()(T oldVal) const { return oldVal / _m; }
};

template <typename T>
struct Mod {
    using V = T;
    Mod(T m) : _m(m) {}
    T _m;
    T operator()(T oldVal) const { return oldVal % static_cast<int64_t>(_m); }
};

template <>
struct Mod<double> {
    using V = double;
    Mod(double ) {}
    double operator()(double oldVal) const { return oldVal; }
};

template <>
struct Mod<float> {
    using V = float;
    Mod(float ) {}
    float operator()(float oldVal) const { return oldVal; }
};

template <typename T>
struct Set {
    using V = T;
    Set(T m) : _m(m) {}
    T _m;
    T operator()(T) const { return  _m; }
};

template <typename T, typename OP>
struct UpdateFast {
    using A = SingleValueNumericAttribute<T>;
    using F = OP;
    A * attr;
    F op;
    using ValueType = typename T::LoadedValueType;
    UpdateFast(IAttributeVector &attr_in, typename F::V operand)
        : attr(dynamic_cast<A *>(&attr_in)),
          op(operand)
    {}
    void operator()(uint32_t docid) { attr->set(docid, op(attr->getFast(docid))); }
    bool valid() const {
        return (attr != nullptr) &&
               (attr->isMutable()); }
};

template <typename OP>
class OperateOverResultSet : public AttributeOperation {
public:
    OperateOverResultSet(FullResult && result, typename OP::F::V operand)
        : _operand(operand),
          _result(std::move(result))
    {}

    void operator()(IAttributeVector &attributeVector) override {
        OP op(attributeVector, _operand);
        if (op.valid()) {
            const RankedHit *hits = _result.second.data();
            size_t numHits   = _result.second.size();
            std::for_each(hits, hits+numHits,  [&op](RankedHit hit) { op(hit.getDocId()); });
            if (_result.first) {
                _result.first->foreach_truebit([&op](uint32_t docId) { op(docId); });
            }
        }
    }
private:
    typename OP::F::V _operand;
    FullResult _result;
};

template<typename OP>
class OperateOverHits : public AttributeOperation {
public:
    using Hit = AttributeOperation::Hit;
    OperateOverHits(std::vector<Hit> reRanked, typename OP::F::V operand)
        : _operand(operand),
          _reRanked(std::move(reRanked))
    {}

    void operator()(IAttributeVector &attributeVector) override {
        OP op(attributeVector, _operand);
        if (op.valid()) {
            std::for_each(_reRanked.begin(), _reRanked.end(), [&op](Hit hit) { op(hit.first); });
        }
    }
private:
    typename OP::F::V _operand;
    std::vector<Hit> _reRanked;
};

template<typename OP>
class OperateOverDocIds : public AttributeOperation {
public:
    OperateOverDocIds(std::vector<uint32_t> docIds, typename OP::F::V operand)
        : _operand(operand),
          _docIds(std::move(docIds))
    {}

    void operator()(IAttributeVector &attributeVector) override {
        OP op(attributeVector, _operand);
        if (op.valid()) {
            std::for_each(_docIds.begin(), _docIds.end(), [&op](uint32_t docId) { op(docId); });
        }
    }
private:
    typename OP::F::V _operand;
    std::vector<uint32_t> _docIds;
};

struct Operation {
    enum class Type { INC, DEC, ADD, SUB, MUL, DIV, MOD, SET, BAD };
    Operation(Type operation_in, vespalib::stringref operand_in) : operation(operation_in), operand(operand_in) { }
    template <typename V>
    std::unique_ptr<AttributeOperation> create(BasicType type, V vector) const;
    template <typename IT, typename V>
    std::unique_ptr<AttributeOperation> create(V vector) const;
    bool valid() const { return operation != Type::BAD; }
    bool hasArgument() const { return valid() && (operation != Type::INC) && (operation != Type::DEC); }
    Type operation;
    vespalib::stringref operand;
    static Operation create(vespalib::stringref s);
};

Operation
Operation::create(vespalib::stringref s)
{
    Type op = Type::BAD;
    if (s.size() >= 2) {
        if ((s[0] == '+') && (s[1] == '+')) {
            op = Type::INC;
        } else if ((s[0] == '-') && (s[1] == '-')) {
            op = Type::DEC;
        } else if ((s[0] == '+') && (s[1] == '=')) {
            op = Type::ADD;
        } else if ((s[0] == '-') && (s[1] == '=')) {
            op = Type::SUB;
        } else if ((s[0] == '*') && (s[1] == '=')) {
            op = Type::MUL;
        } else if ((s[0] == '/') && (s[1] == '=')) {
            op = Type::DIV;
        } else if ((s[0] == '%') && (s[1] == '=')) {
            op = Type::MOD;
        } else if (s[0] == '=') {
            op = Type::SET;
        }
        if (op == Type::SET) {
            s = s.substr(1);
        } else if (op == Type::BAD) {
        } else {
            s = s.substr(2);
        }
    }
    return Operation(op, s);
}

template<typename T, typename OP>
std::unique_ptr<AttributeOperation>
createOperation(std::vector<uint32_t> vector, T operand) {
    return std::make_unique<OperateOverDocIds<OP>>(std::move(vector), operand);
}

template<typename T, typename OP>
std::unique_ptr<AttributeOperation>
createOperation(std::vector<AttributeOperation::Hit> vector, T operand) {
    return std::make_unique<OperateOverHits<OP>>(std::move(vector), operand);
}

template<typename T, typename OP>
std::unique_ptr<AttributeOperation>
createOperation(AttributeOperation::FullResult && result, T operand) {
    return std::make_unique<OperateOverResultSet<OP>>(std::move(result), operand);
}

template <typename T_IN, typename V>
std::unique_ptr<AttributeOperation>
Operation::create(V vector) const {
    using T = typename T_IN::T;
    using A = typename T_IN::A;
    T value(0);
    Type validOp = operation;
    if (hasArgument()) {
        vespalib::asciistream is(operand);
        try {
            is >> value;
            if (!is.empty()) {
                LOG(warning, "Invalid operand, unable to consume all of (%s). (%s) is unconsumed.", operand.data(), is.c_str());
                validOp = Type::BAD;
            }
            if (((validOp == Type::DIV) || (validOp == Type::MOD)) && (value == 0)) {
                LOG(warning, "Division by zero is not acceptable (%s).", operand.data());
                validOp = Type::BAD;
            }
        } catch (vespalib::IllegalArgumentException & e) {
            LOG(warning, "Invalid operand, ignoring : %s", e.what());
            validOp = Type::BAD;
        }
    }
    switch (validOp) {
        case Type::INC:
            return createOperation<T, UpdateFast<A, Inc<T>>>(std::move(vector), value);
        case Type::DEC:
            return createOperation<T, UpdateFast<A, Dec<T>>>(std::move(vector), value);
        case Type::ADD:
            return createOperation<T, UpdateFast<A, Add<T>>>(std::move(vector), value);
        case Type::SUB:
            return createOperation<T, UpdateFast<A, Add<T>>>(std::move(vector), -value);
        case Type::MUL:
            return createOperation<T, UpdateFast<A, Mul<T>>>(std::move(vector), value);
        case Type::DIV:
            return createOperation<T, UpdateFast<A, Div<T>>>(std::move(vector), value);
        case Type::MOD:
            return createOperation<T, UpdateFast<A, Mod<T>>>(std::move(vector), value);
        case Type::SET:
            return createOperation<T, UpdateFast<A, Set<T>>>(std::move(vector), value);
        default:
            return std::unique_ptr<AttributeOperation>();
    }
}

struct Int64T {
    using T = int64_t;
    using A = IntegerAttributeTemplate<int64_t>;
};

struct Int32T {
    using T = int64_t;
    using A = IntegerAttributeTemplate<int32_t>;
};

struct Int8T {
    using T = int64_t;
    using A = IntegerAttributeTemplate<int8_t>;
};

struct DoubleT {
    using T = double;
    using A = FloatingPointAttributeTemplate<double>;
};
struct FloatT {
    using T = double;
    using A = FloatingPointAttributeTemplate<float>;
};

template <typename V>
std::unique_ptr<AttributeOperation>
Operation::create(BasicType type, V hits) const {
    if ( ! valid()) {
        return std::unique_ptr<AttributeOperation>();
    }
    switch (type.type()) {
        case BasicType::INT64:
            return create<Int64T, V>(std::move(hits));
        case BasicType::INT32:
            return create<Int32T, V>(std::move(hits));
        case BasicType::INT8:
            return create<Int8T, V>(std::move(hits));
        case BasicType::DOUBLE:
            return create<DoubleT, V>(std::move(hits));
        case BasicType::FLOAT:
            return create<FloatT, V>(std::move(hits));
        default:
            return std::unique_ptr<AttributeOperation>();
    }
}

}

template <typename Hits>
std::unique_ptr<AttributeOperation>
AttributeOperation::create(BasicType type, const vespalib::string & operation, Hits docs) {
    Operation op = Operation::create(operation);
    return op.create<Hits>(type, std::move(docs));
}

template std::unique_ptr<AttributeOperation>
AttributeOperation::create(BasicType, const vespalib::string &, std::vector<uint32_t>);

template std::unique_ptr<AttributeOperation>
AttributeOperation::create(BasicType, const vespalib::string &, std::vector<Hit>);

template std::unique_ptr<AttributeOperation>
AttributeOperation::create(BasicType, const vespalib::string &, FullResult);

}

template class vespalib::Array<search::RankedHit>;