aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/attrvector.h
blob: 3472f7de5a4fb4fe90c9c0aea27fb3d897601dbe (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "stringbase.h"
#include "integerbase.h"
#include "floatbase.h"
#include <vespa/searchlib/common/rankedhit.h>

//TODO: This one should go.
//
using search::AttributeVector;

//-----------------------------------------------------------------------------

class AttrVector
{
public:
    template <bool MULTI>
    struct Features
    {
        using EnumType = uint32_t;
        static bool IsMultiValue() { return MULTI; }
    };
};

namespace search {

template <typename B>
class NumericDirectAttribute : public B
{
private:
    using EnumHandle = typename B::EnumHandle;
    NumericDirectAttribute(const NumericDirectAttribute &);
    NumericDirectAttribute & operator=(const NumericDirectAttribute &);
    typename B::BaseType getFromEnum(EnumHandle e) const override { return _data[e]; }
protected:
    using BaseType = typename B::BaseType;
    using DocId = typename B::DocId;
    using Change = typename B::Change;
    using largeint_t = typename B::largeint_t;
    using Config = typename B::Config;

    NumericDirectAttribute(const vespalib::string & baseFileName, const Config & c);
    ~NumericDirectAttribute() override;

    bool findEnum(BaseType value, EnumHandle & e) const override;
    void onCommit() override;
    void onUpdateStat() override { }
    bool addDoc(DocId & ) override;

    std::vector<BaseType>   _data;
    std::vector<uint32_t>   _idx;
};

}

template <typename F, typename B>
class NumericDirectAttrVector : public search::NumericDirectAttribute<B>
{
protected:
    using DocId = typename B::DocId;
    using NumDirectAttrVec = NumericDirectAttrVector<F, B>;
private:
    using largeint_t = typename B::largeint_t;
public:
    NumericDirectAttrVector(const vespalib::string & baseFileName);
    NumericDirectAttrVector(const vespalib::string & baseFileName, const AttributeVector::Config & c);
    largeint_t getInt(DocId doc)   const override { return static_cast<largeint_t>(getHelper(doc, 0)); }
    double getFloat(DocId doc)     const override { return getHelper(doc, 0); }
    uint32_t get(DocId doc, largeint_t * v, uint32_t sz)     const override { return getAllHelper<largeint_t, largeint_t>(doc, v, sz); }
    uint32_t get(DocId doc, double * v, uint32_t sz)         const override { return getAllHelper<double, double>(doc, v, sz); }
private:
    using EnumHandle = typename B::EnumHandle;
    using BaseType = typename B::BaseType;
    using Weighted = typename B::Weighted;
    using WeightedEnum = typename B::WeightedEnum;
    using WeightedInt = typename B::WeightedInt;
    using WeightedFloat = typename B::WeightedFloat;
    BaseType get(DocId doc)        const override { return getHelper(doc, 0); }
    EnumHandle getEnum(DocId doc)  const override { return getEnumHelper(doc, 0); }
    uint32_t get(DocId doc, EnumHandle * e, uint32_t sz) const override { return getAllEnumHelper(doc, e, sz); }

    uint32_t getValueCount(DocId doc) const override { return getValueCountHelper(doc); }

    uint32_t getValueCountHelper(DocId doc) const {
        if (F::IsMultiValue()) {
            return this->_idx[doc+1] - this->_idx[doc];
        } else {
            return 1;
        }
    }

    EnumHandle getEnumHelper(DocId doc, int idx) const {
        (void) doc;
        (void) idx;
        return uint32_t(-1);
    }

    BaseType getHelper(DocId doc, int idx) const {
        if (F::IsMultiValue()) {
            return this->_data[this->_idx[doc] + idx];
        } else {
            return this->_data[doc];
        }
    }
    template <typename T, typename C>
    uint32_t getAllHelper(DocId doc, T * v, uint32_t sz) const {
        uint32_t available(getValueCountHelper(doc));
        uint32_t num2Read(std::min(available, sz));
        for (uint32_t i(0); i < num2Read; i++) {
            v[i] = T(static_cast<C>(getHelper(doc, i)));
        }
        return available;
    }
    template <typename T>
    uint32_t getAllEnumHelper(DocId doc, T * v, uint32_t sz) const {
        uint32_t available(getValueCountHelper(doc));
        uint32_t num2Read(std::min(available, sz));
        for (uint32_t i(0); i < num2Read; i++) {
            v[i] = T(getEnumHelper(doc, i));
        }
        return available;
    }

    uint32_t get(DocId doc, WeightedEnum * v, uint32_t sz)  const override { return getAllEnumHelper(doc, v, sz); }
    uint32_t get(DocId doc, WeightedInt * v, uint32_t sz)   const override { return getAllHelper<WeightedInt, largeint_t>(doc, v, sz); }
    uint32_t get(DocId doc, WeightedFloat * v, uint32_t sz) const override { return getAllHelper<WeightedFloat, double>(doc, v, sz); }
    template <bool asc>
    long on_serialize_for_sort(DocId doc, void* serTo, long available) const;
    long onSerializeForAscendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const override;
    long onSerializeForDescendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const override;
};

//-----------------------------------------------------------------------------

namespace search {
class StringDirectAttribute : public StringAttribute
{
private:
    StringDirectAttribute(const StringDirectAttribute &);
    StringDirectAttribute & operator=(const StringDirectAttribute &);
    const char * getFromEnum(EnumHandle e) const override { return &_buffer[e]; }
    const char * getStringFromEnum(EnumHandle e) const override { return &_buffer[e]; }
    std::unique_ptr<attribute::SearchContext> getSearch(QueryTermSimpleUP term, const attribute::SearchContextParams & params) const override;
protected:
    StringDirectAttribute(const vespalib::string & baseFileName, const Config & c);
    ~StringDirectAttribute() override;
    bool findEnum(const char * value, EnumHandle & e) const override;
    std::vector<EnumHandle> findFoldedEnums(const char *) const override;
    void onCommit() override;
    void onUpdateStat() override { }
    bool addDoc(DocId & ) override;

protected:
    std::vector<char>        _buffer;
    OffsetVector             _offsets;
    std::vector<uint32_t>    _idx;
};

}

template <typename F>
class StringDirectAttrVector : public search::StringDirectAttribute
{

public:
    StringDirectAttrVector(const vespalib::string & baseFileName);
    StringDirectAttrVector(const vespalib::string & baseFileName, const Config & c);
    uint32_t get(DocId doc, const char ** v, uint32_t sz)  const override {
        return getAllHelper(doc, v, sz);
    }
    const char * get(DocId doc)  const override { return getHelper(doc, 0); }
private:
    uint32_t get(DocId doc, vespalib::string * v, uint32_t sz)  const override { return getAllHelper(doc, v, sz); }
    uint32_t get(DocId doc, EnumHandle * e, uint32_t sz) const override { return getAllEnumHelper(doc, e, sz); }
    EnumHandle getEnum(DocId doc)  const override { return getEnumHelper(doc, 0); }
    uint32_t getValueCount(DocId doc) const override { return getValueCountHelper(doc); }
    uint32_t get(DocId doc, WeightedEnum * e, uint32_t sz)  const override { return getAllEnumHelper(doc, e, sz); }
    uint32_t get(DocId doc, WeightedString * v, uint32_t sz)    const override { return getAllHelper(doc, v, sz); }
    uint32_t get(DocId doc, WeightedConstChar * v, uint32_t sz) const override { return getAllHelper(doc, v, sz); }

    uint32_t getValueCountHelper(DocId doc) const {
        if (F::IsMultiValue()) {
            return this->_idx[doc+1] - this->_idx[doc];
        } else {
            return 1;
        }
    }

    EnumHandle getEnumHelper(DocId doc, int idx) const {
        if (F::IsMultiValue()) {
            return  this->_offsets[this->_idx[doc] + idx];
        } else {
            return this->_offsets[doc];
        }
        return uint32_t(-1);
    }

    const char *getHelper(DocId doc, int idx) const {
        if (F::IsMultiValue()) {
            return & this->_buffer[this->_offsets[this->_idx[doc] + idx]];
        } else if (idx == 0) {
            return & this->_buffer[this->_offsets[doc]];
        }
        return NULL;
    }
    template <typename T>
    uint32_t getAllHelper(DocId doc, T * v, uint32_t sz) const
    {
        uint32_t available(getValueCountHelper(doc));
        uint32_t num2Read(std::min(available, sz));
        for (uint32_t i(0); i < num2Read; i++) {
            v[i] = T(getHelper(doc, i));
        }
        return available;
    }
    template <typename T>
    uint32_t getAllEnumHelper(DocId doc, T * v, uint32_t sz) const
    {
        uint32_t available(getValueCountHelper(doc));
        uint32_t num2Read(std::min(available, sz));
        for (uint32_t i(0); i < num2Read; i++) {
            v[i] = T(getEnumHelper(doc, i));
        }
        return available;
    }
    long on_serialize_for_sort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc, bool asc) const;
    long onSerializeForAscendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const override;
    long onSerializeForDescendingSort(DocId doc, void* serTo, long available, const search::common::BlobConverter* bc) const override;
};