aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/extendableattributes.h
blob: 86e6be8eff7a879c3d009d4e9ef940ef7b5ac068 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @class search::SearchVisitor
 *
 * @brief Visitor that applies a search query to visitor data and converts them to a SearchResultCommand
 */
#pragma once

#include "attrvector.h"
#include "attrvector.hpp"
#include <vespa/searchcommon/attribute/i_multi_value_attribute.h>

namespace search {

// Translates the actual value type to the type required by IExtendAttribute.
template <typename T> struct AddValueType {
    typedef int64_t Type;
};
template <> struct AddValueType<double> {
    typedef double Type;
};

//******************** CollectionType::SINGLE ********************//

template <typename T> struct AttributeTemplate {
    typedef search::IntegerAttributeTemplate<T> Type;
};
template <> struct AttributeTemplate<double> {
    typedef search::FloatingPointAttributeTemplate<double> Type;
};

template <typename T>
class SingleExtAttribute
    : public NumericDirectAttrVector<AttrVector::Features<false>,
                                     typename AttributeTemplate<T>::Type>,
      public IExtendAttribute
{
    using Super = typename SingleExtAttribute<T>::NumDirectAttrVec;
    using Config =  typename Super::Config;
    using BasicType = typename Super::BasicType;
    using QueryTermSimpleUP = typename Super::QueryTermSimpleUP;

    std::unique_ptr<attribute::SearchContext>
    getSearch(QueryTermSimpleUP term, const attribute::SearchContextParams & params) const override
    {
        (void) term;
        (void) params;
        return {};
    }
    IExtendAttribute * getExtendInterface() override { return this; }
public:
    SingleExtAttribute(const vespalib::string &name)
        : Super(name, Config(BasicType::fromType(T()),
                             attribute::CollectionType::SINGLE)) {}

    bool addDoc(typename Super::DocId &docId) override {
        docId = this->_data.size();
        this->_data.push_back(attribute::getUndefined<T>());
        this->incNumDocs();
        this->setCommittedDocIdLimit(this->getNumDocs());
        return true;
    }
    bool add(typename AddValueType<T>::Type v, int32_t = 1) override {
        this->_data.back() = v;
        return true;
    }
    bool onLoad(vespalib::Executor *) override {
        return false; // Emulate that this attribute is never loaded
    }
    void onAddDocs(typename Super::DocId lidLimit) override {
        this->_data.reserve(lidLimit);
    }
};

typedef SingleExtAttribute<int8_t> SingleInt8ExtAttribute;
typedef SingleExtAttribute<int16_t> SingleInt16ExtAttribute;
typedef SingleExtAttribute<int32_t> SingleInt32ExtAttribute;
typedef SingleExtAttribute<int64_t> SingleInt64ExtAttribute;
typedef SingleExtAttribute<double> SingleFloatExtAttribute;

typedef SingleInt64ExtAttribute SingleIntegerExtAttribute;

class SingleStringExtAttribute
    : public StringDirectAttrVector< AttrVector::Features<false> >,
      public IExtendAttribute
{
    IExtendAttribute * getExtendInterface() override { return this; }
public:
    SingleStringExtAttribute(const vespalib::string & name);
    bool addDoc(DocId & docId) override;
    bool add(const char * v, int32_t w = 1) override;
    bool onLoad(vespalib::Executor *) override {
        return false; // Emulate that this attribute is never loaded
    }
    void onAddDocs(DocId ) override { }
};

//******************** CollectionType::ARRAY ********************//

template <typename T>
class MultiExtAttribute
    : public NumericDirectAttrVector<AttrVector::Features<true>, typename AttributeTemplate<T>::Type>,
      public IExtendAttribute,
      public attribute::IMultiValueAttribute
{
protected:
    typedef typename MultiExtAttribute<T>::NumDirectAttrVec Super;
    typedef typename Super::Config Config;
    typedef typename Super::BasicType BasicType;
    using QueryTermSimpleUP = AttributeVector::QueryTermSimpleUP;

    MultiExtAttribute(const vespalib::string &name, const attribute::CollectionType &ctype)
        : Super(name, Config(BasicType::fromType(T()), ctype))
    { }
private:
    std::unique_ptr<attribute::SearchContext>
    getSearch(QueryTermSimpleUP term, const attribute::SearchContextParams & params) const override
    {
        (void) term;
        (void) params;
        return {};
    }
    IExtendAttribute * getExtendInterface() override { return this; }

public:
    MultiExtAttribute(const vespalib::string &name)
        : Super(name, Config(BasicType::fromType(static_cast<T>(0)),
                             attribute::CollectionType::ARRAY)) {}
    ~MultiExtAttribute() override;

    bool addDoc(typename Super::DocId &docId) override {
        docId = this->_idx.size() - 1;
        this->_idx.push_back(this->_idx.back());
        this->incNumDocs();
        this->setCommittedDocIdLimit(this->getNumDocs());
        return true;
    }
    bool add(typename AddValueType<T>::Type v, int32_t = 1) override {
        this->_data.push_back(v);
        std::vector<uint32_t> &idx = this->_idx;
        idx.back()++;
        this->checkSetMaxValueCount(idx.back() - idx[idx.size() - 2]);
        return true;
    }
    bool onLoad(vespalib::Executor *) override {
        return false; // Emulate that this attribute is never loaded
    }
    void onAddDocs(uint32_t lidLimit) override {
        this->_data.reserve(lidLimit);
    }
    const attribute::IMultiValueAttribute* as_multi_value_attribute() const override;

    // Implements attribute::IMultiValueAttribute
    const attribute::IArrayReadView<T>* make_read_view(attribute::IMultiValueAttribute::ArrayTag<T>, vespalib::Stash& stash) const override;
    const attribute::IWeightedSetReadView<T>* make_read_view(attribute::IMultiValueAttribute::WeightedSetTag<T>, vespalib::Stash& stash) const override;
};

template <typename T>
MultiExtAttribute<T>::~MultiExtAttribute() = default;

typedef MultiExtAttribute<int8_t> MultiInt8ExtAttribute;
typedef MultiExtAttribute<int16_t> MultiInt16ExtAttribute;
typedef MultiExtAttribute<int32_t> MultiInt32ExtAttribute;
typedef MultiExtAttribute<int64_t> MultiInt64ExtAttribute;
typedef MultiExtAttribute<double> MultiFloatExtAttribute;

typedef MultiInt64ExtAttribute MultiIntegerExtAttribute;

class MultiStringExtAttribute :
    public StringDirectAttrVector< AttrVector::Features<true> >,
    public IExtendAttribute,
    public attribute::IMultiValueAttribute
{
    IExtendAttribute * getExtendInterface() override { return this; }
protected:
    MultiStringExtAttribute(const vespalib::string & name, const attribute::CollectionType & ctype);

public:
    MultiStringExtAttribute(const vespalib::string & name);
    bool addDoc(DocId & docId) override;
    bool add(const char * v, int32_t w = 1) override;
    bool onLoad(vespalib::Executor *) override {
        return false; // Emulate that this attribute is never loaded
    }
    void onAddDocs(DocId ) override { }
    const attribute::IMultiValueAttribute* as_multi_value_attribute() const override;
    // Implements attribute::IMultiValueAttribute
    const attribute::IArrayReadView<const char*>* make_read_view(attribute::IMultiValueAttribute::ArrayTag<const char*>, vespalib::Stash& stash) const override;
    const attribute::IWeightedSetReadView<const char*>* make_read_view(attribute::IMultiValueAttribute::WeightedSetTag<const char*>, vespalib::Stash& stash) const override;
};


//******************** CollectionType::WSET ********************//

template <typename B>
class WeightedSetExtAttributeBase : public B
{
private:
    std::vector<int32_t> _weights;

protected:
    void addWeight(int32_t w) {
        _weights.push_back(w);
    }
    int32_t getWeightHelper(AttributeVector::DocId docId, uint32_t idx) const {
        return _weights[this->_idx[docId] + idx];
    }
    WeightedSetExtAttributeBase(const vespalib::string & name) :
        B(name, attribute::CollectionType::WSET),
        _weights()
    {}
    ~WeightedSetExtAttributeBase() {}
    const std::vector<int32_t>& get_weights() const noexcept { return _weights; }
};

class WeightedSetIntegerExtAttribute
    : public WeightedSetExtAttributeBase<MultiIntegerExtAttribute>
{
    std::unique_ptr<attribute::SearchContext>
    getSearch(QueryTermSimpleUP term, const attribute::SearchContextParams & params) const override
    {
        (void) term;
        (void) params;
        return {};
    }
public:
    WeightedSetIntegerExtAttribute(const vespalib::string & name);
    ~WeightedSetIntegerExtAttribute();
    bool add(int64_t v, int32_t w = 1) override;
    uint32_t get(DocId doc, AttributeVector::WeightedInt * v, uint32_t sz) const override;
    // Implements attribute::IMultiValueAttribute
    const attribute::IWeightedSetReadView<int64_t>* make_read_view(attribute::IMultiValueAttribute::WeightedSetTag<int64_t>, vespalib::Stash& stash) const override;
};

class WeightedSetFloatExtAttribute
    : public WeightedSetExtAttributeBase<MultiFloatExtAttribute>
{
    std::unique_ptr<attribute::SearchContext>
    getSearch(QueryTermSimpleUP term, const attribute::SearchContextParams & params) const override
    {
        (void) term;
        (void) params;
        return {};
    }
public:
    WeightedSetFloatExtAttribute(const vespalib::string & name);
    ~WeightedSetFloatExtAttribute();
    bool add(double v, int32_t w = 1) override;
    uint32_t get(DocId doc, AttributeVector::WeightedFloat * v, uint32_t sz) const override;
    // Implements attribute::IMultiValueAttribute
    const attribute::IWeightedSetReadView<double>* make_read_view(attribute::IMultiValueAttribute::WeightedSetTag<double>, vespalib::Stash& stash) const override;
};

class WeightedSetStringExtAttribute
    : public WeightedSetExtAttributeBase<MultiStringExtAttribute>
{
private:
    const char * getHelper(DocId doc, int idx) const {
        return &_buffer[_offsets[_idx[doc] + idx]];
    }
    template <typename T>
    uint32_t getAllHelper(DocId doc, T * v, uint32_t sz) const
    {
        uint32_t valueCount = _idx[doc + 1] - _idx[doc];
        uint32_t num2Read = std::min(valueCount, sz);
        for (uint32_t i = 0; i < num2Read; ++i) {
            v[i] = T(getHelper(doc, i), getWeightHelper(doc, i));
        }
        return valueCount;
    }

public:
    WeightedSetStringExtAttribute(const vespalib::string & name);
    ~WeightedSetStringExtAttribute();
    bool add(const char * v, int32_t w = 1) override;
    uint32_t get(DocId doc, AttributeVector::WeightedString * v, uint32_t sz) const override;
    uint32_t get(DocId doc, AttributeVector::WeightedConstChar * v, uint32_t sz) const override;
    // Implements attribute::IMultiValueAttribute
    const attribute::IWeightedSetReadView<const char*>* make_read_view(attribute::IMultiValueAttribute::WeightedSetTag<const char*>, vespalib::Stash& stash) const override;
};

}  // namespace search