aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/attributevector.hpp
blob: 823547276b58687af3efbcee42ea250cb32f0d64 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "attributevector.h"
#include "integerbase.h"
#include <vespa/document/fieldvalue/intfieldvalue.h>
#include <vespa/document/update/arithmeticvalueupdate.h>
#include <vespa/document/update/assignvalueupdate.h>
#include <cmath>

namespace search {

template<typename T>
bool
AttributeVector::adjustWeight(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc, const T & v,
                              const ArithmeticValueUpdate & wd)
{
    bool retval(hasWeightedSetType() && (doc < getNumDocs()));
    if (retval) {
        size_t oldSz(changes.size());
        ArithmeticValueUpdate::Operator op(wd.getOperator());
        int32_t w(static_cast<int32_t>(wd.getOperand()));
        if (op == ArithmeticValueUpdate::Add) {
            changes.push_back(ChangeTemplate<T>(ChangeBase::INCREASEWEIGHT, doc, v, w));
        } else if (op == ArithmeticValueUpdate::Sub) {
            changes.push_back(ChangeTemplate<T>(ChangeBase::INCREASEWEIGHT, doc, v, -w));
        } else if (op == ArithmeticValueUpdate::Mul) {
            changes.push_back(ChangeTemplate<T>(ChangeBase::MULWEIGHT, doc, v, w));
        } else if (op == ArithmeticValueUpdate::Div) {
            if (w != 0) {
                changes.push_back(ChangeTemplate<T>(ChangeBase::DIVWEIGHT, doc, v, w));
            } else {
                divideByZeroWarning();
            }
        } else {
            retval = false;
        }
        if (retval) {
            const size_t diff = changes.size() - oldSz;
            _status.incNonIdempotentUpdates(diff);
            _status.incUpdates(diff);
        }
    }
    return retval;
}

template<typename T>
bool
AttributeVector::adjustWeight(ChangeVectorT< ChangeTemplate<T> >& changes, DocId doc, const T& v, const document::AssignValueUpdate& wu)
{
    bool retval(hasWeightedSetType() && (doc < getNumDocs()));
    if (retval) {
        size_t oldSz(changes.size());
        if (wu.hasValue()) {
            const FieldValue &wv = wu.getValue();
            if (wv.isA(FieldValue::Type::INT)) {
                changes.push_back(ChangeTemplate<T>(ChangeBase::SETWEIGHT, doc, v, wv.getAsInt()));
            } else {
                retval = false;
            }
        } else {
            retval = false;
        }
        if (retval) {
            const size_t diff = changes.size() - oldSz;
            _status.incNonIdempotentUpdates(diff);
            _status.incUpdates(diff);
        }
    }
    return retval;
}

template<typename T>
bool
AttributeVector::applyArithmetic(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc, const T &,
                                 const ArithmeticValueUpdate & arithm)
{
    if (hasMultiValue() || (doc >= getNumDocs())) return false;

    size_t oldSz(changes.size());
    ArithmeticValueUpdate::Operator op(arithm.getOperator());
    double aop = arithm.getOperand();
    if (op == ArithmeticValueUpdate::Add) {
        changes.push_back(ChangeTemplate<T>(ChangeBase::ADD, doc, 0, 0));
    } else if (op == ArithmeticValueUpdate::Sub) {
        changes.push_back(ChangeTemplate<T>(ChangeBase::SUB, doc, 0, 0));
    } else if (op == ArithmeticValueUpdate::Mul) {
        changes.push_back(ChangeTemplate<T>(ChangeBase::MUL, doc, 0, 0));
    } else if (op == ArithmeticValueUpdate::Div) {
        if ((aop == 0) && isIntegerType()) {
            divideByZeroWarning();
        } else {
            changes.push_back(ChangeTemplate<T>(ChangeBase::DIV, doc, 0, 0));
        }
    } else {
        return false;
    }
    const size_t diff = changes.size() - oldSz;
    _status.incNonIdempotentUpdates(diff);
    _status.incUpdates(diff);
    if (diff > 0) {
        changes.back()._data.setArithOperand(aop);
    }
    return true;
}

template<typename T>
bool AttributeVector::clearDoc(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc) {
    bool retval(doc < getNumDocs());
    if (retval) {
        changes.push_back(ChangeTemplate<T>(ChangeBase::CLEARDOC, doc, T()));
        _status.incUpdates();
        updateUncommittedDocIdLimit(doc);
    }
    return retval;
}

template<typename T>
bool AttributeVector::update(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc, const T & v) {
    bool retval(doc < getNumDocs());
    if (retval) {
        if (hasMultiValue()) {
            clearDoc(doc);
            retval = append(changes, doc, v, 1, false);
        } else {
            changes.push_back(ChangeTemplate<T>(ChangeBase::UPDATE, doc, v));
            _status.incUpdates();
            updateUncommittedDocIdLimit(doc);
        }
    }
    return retval;
}

template<typename T>
bool AttributeVector::append(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc, const T & v, int32_t w, bool doCount) {
    bool retval(hasMultiValue() && (doc < getNumDocs()));
    if (retval) {
        changes.push_back(ChangeTemplate<T>(ChangeBase::APPEND, doc, v, w));
        _status.incUpdates();
        updateUncommittedDocIdLimit(doc);
        if ( hasArrayType() && doCount) {
            _status.incNonIdempotentUpdates();
        }
    }
    return retval;
}

template<typename T, typename Accessor>
bool AttributeVector::append(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc, Accessor & ac) {
    bool retval(hasMultiValue() && (doc < getNumDocs()));
    if (retval) {
        changes.push_back(doc, ac);
        _status.incUpdates(ac.size());
        updateUncommittedDocIdLimit(doc);
        if ( hasArrayType() ) {
            _status.incNonIdempotentUpdates(ac.size());
        }
    }
    return retval;
}

template<typename T>
bool AttributeVector::remove(ChangeVectorT< ChangeTemplate<T> > & changes, DocId doc, const T & v, int32_t w) {
    bool retval(hasMultiValue() && (doc < getNumDocs()));
    if (retval) {
        changes.push_back(ChangeTemplate<T>(ChangeBase::REMOVE, doc, v, w));
        _status.incUpdates();
        updateUncommittedDocIdLimit(doc);
        if ( hasArrayType() ) {
            _status.incNonIdempotentUpdates();
        }
    }
    return retval;
}

}