summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/singlesmallnumericattribute.h
blob: 77c4133817cb95d1bff9cb28dc53863f7a3ff2c4 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "integerbase.h"
#include "floatbase.h"
#include "numeric_range_matcher.h"
#include "search_context.h"
#include <vespa/vespalib/util/atomic.h>
#include <vespa/vespalib/util/rcuvector.h>
#include <limits>

namespace search {

class SingleValueSmallNumericAttribute : public IntegerAttributeTemplate<int8_t>
{
private:
//    friend class attribute::SearchContext;
    typedef IntegerAttributeTemplate<int8_t> B;
    typedef B::BaseType      T;
    typedef B::DocId         DocId;
    typedef B::EnumHandle    EnumHandle;
    typedef B::largeint_t    largeint_t;
    typedef B::Weighted      Weighted;
    typedef B::WeightedInt   WeightedInt;
    typedef B::WeightedFloat WeightedFloat;
    typedef B::WeightedEnum  WeightedEnum;
    typedef B::generation_t generation_t;

protected:
    typedef uint32_t Word;  // Large enough to contain numDocs.
private:
    Word _valueMask;            // 0x01, 0x03 or 0x0f
    uint32_t _valueShiftShift;  // 0x00, 0x01 or 0x02
    uint32_t _valueShiftMask;   // 0x1f, 0x0f or 0x07
    uint32_t _wordShift;        // 0x05, 0x04 or 0x03

    typedef vespalib::RcuVectorBase<Word> DataVector;
    DataVector _wordData;

    T getFromEnum(EnumHandle) const override {
        return T();
    }

protected:
    bool findEnum(T, EnumHandle &) const override {
        return false;
    }

    void set(DocId doc, T v) {
        Word &word_ref = _wordData[doc >> _wordShift];
        uint32_t valueShift = (doc & _valueShiftMask) << _valueShiftShift;
        Word word = vespalib::atomic::load_ref_relaxed(word_ref);
        word = (word & ~(_valueMask << valueShift)) |
               ((v & _valueMask) << valueShift);
        vespalib::atomic::store_ref_relaxed(word_ref, word);
    }


public:
    /*
     * Specialization of SearchContext
     */
    class SingleSearchContext : public attribute::NumericRangeMatcher<T>, public attribute::SearchContext
    {
    private:
        const Word *_wordData;
        Word _valueMask;
        uint32_t _valueShiftShift;
        uint32_t _valueShiftMask;
        uint32_t _wordShift;

        int32_t onFind(DocId docId, int32_t elementId, int32_t & weight) const override {
            return find(docId, elementId, weight);
        }

        int32_t onFind(DocId docId, int32_t elementId) const override {
            return find(docId, elementId);
        }

        bool valid() const override;

    public:
        SingleSearchContext(std::unique_ptr<QueryTermSimple> qTerm, const SingleValueSmallNumericAttribute & toBeSearched);

        int32_t find(DocId docId, int32_t elemId, int32_t & weight) const {
            if ( elemId != 0) return -1;
            const Word &word = _wordData[docId >> _wordShift];
            uint32_t valueShift = (docId & _valueShiftMask) << _valueShiftShift;
            T v = (vespalib::atomic::load_ref_relaxed(word) >> valueShift) & _valueMask;
            weight = 1;
            return match(v) ? 0 : -1;
        }

        int32_t find(DocId docId, int32_t elemId) const {
            if ( elemId != 0) return -1;
            const Word &word = _wordData[docId >> _wordShift];
            uint32_t valueShift = (docId & _valueShiftMask) << _valueShiftShift;
            T v = (vespalib::atomic::load_ref_relaxed(word) >> valueShift) & _valueMask;
            return match(v) ? 0 : -1;
        }

        Int64Range getAsIntegerTerm() const override;

        std::unique_ptr<queryeval::SearchIterator>
        createFilterIterator(fef::TermFieldMatchData * matchData, bool strict) override;
    };

    SingleValueSmallNumericAttribute(const vespalib::string & baseFileName, const Config &c, Word valueMask,
                                     uint32_t valueShiftShift, uint32_t valueShiftMask, uint32_t wordShift);

    ~SingleValueSmallNumericAttribute() override;

    uint32_t getValueCount(DocId doc) const override {
        if (doc >= B::getNumDocs()) {
            return 0;
        }
        return 1;
    }
    void onCommit() override;
    void onAddDocs(DocId docIdLimit) override;
    void onUpdateStat() override;
    void removeOldGenerations(generation_t firstUsed) override;
    void onGenerationChange(generation_t generation) override;
    bool addDoc(DocId & doc) override;
    bool onLoad(vespalib::Executor *executor) override;
    void onSave(IAttributeSaveTarget &saveTarget) override;

    std::unique_ptr<attribute::SearchContext>
    getSearch(std::unique_ptr<QueryTermSimple> term, const attribute::SearchContextParams & params) const override;

    T getFast(DocId doc) const {
        const Word &word = _wordData.acquire_elem_ref(doc >> _wordShift);
        uint32_t valueShift = (doc & _valueShiftMask) << _valueShiftShift;
        return (vespalib::atomic::load_ref_relaxed(word) >> valueShift) & _valueMask;
    }

    //-------------------------------------------------------------------------
    // new read api
    //-------------------------------------------------------------------------
    T get(DocId doc) const override {
        return getFast(doc);
    }
    largeint_t getInt(DocId doc) const override {
        return static_cast<largeint_t>(getFast(doc));
    }
    double getFloat(DocId doc) const override {
        return static_cast<double>(getFast(doc));
    }
    uint32_t getEnum(DocId) const override {
        return std::numeric_limits<uint32_t>::max(); // does not have enum
    }
    uint32_t getAll(DocId doc, T * v, uint32_t sz) const override {
        if (sz > 0) {
            v[0] = getFast(doc);
        }
        return 1;
    }
    uint32_t get(DocId doc, largeint_t * v, uint32_t sz) const override {
        if (sz > 0) {
            v[0] = static_cast<largeint_t>(getFast(doc));
        }
        return 1;
    }
    uint32_t get(DocId doc, double * v, uint32_t sz) const override {
        if (sz > 0) {
            v[0] = static_cast<double>(getFast(doc));
        }
        return 1;
    }
    uint32_t get(DocId doc, EnumHandle * e, uint32_t sz) const override {
        if (sz > 0) {
            e[0] = getEnum(doc);
        }
        return 1;
    }
    uint32_t getAll(DocId, Weighted *, uint32_t) const override { return 0; }
    uint32_t get(DocId doc, WeightedInt * v, uint32_t sz) const override {
        if (sz > 0) {
            v[0] = WeightedInt(static_cast<largeint_t>(getFast(doc)));
        }
        return 1;
    }
    uint32_t get(DocId doc, WeightedFloat * v, uint32_t sz) const override {
        if (sz > 0) {
            v[0] = WeightedFloat(static_cast<double>(getFast(doc)));
        }
        return 1;
    }
    uint32_t get(DocId doc, WeightedEnum * e, uint32_t sz) const override {
        (void) doc; (void) e; (void) sz;
        return 0;
    }

    void clearDocs(DocId lidLow, DocId lidLimit, bool in_shrink_lid_space) override;
    void onShrinkLidSpace() override;
    uint64_t getEstimatedSaveByteSize() const override;
};

class SingleValueSemiNibbleNumericAttribute : public SingleValueSmallNumericAttribute
{
public:
    SingleValueSemiNibbleNumericAttribute(const vespalib::string & baseFileName, const search::GrowStrategy & grow);
};

class SingleValueNibbleNumericAttribute : public SingleValueSmallNumericAttribute
{
public:
    SingleValueNibbleNumericAttribute(const vespalib::string & baseFileName, const search::GrowStrategy & grow);
};

}