aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/diskindex/zcpostingiterators.h
blob: c57870baab2e3de4539bf0320a85bc79087f1284 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/searchlib/index/postinglistfile.h>
#include <vespa/searchlib/bitcompression/compression.h>
#include <vespa/searchlib/queryeval/iterators.h>

namespace search::diskindex {

using bitcompression::Position;

#define ZCDECODE(valI, resop)                                \
do {                                                         \
    if (__builtin_expect(valI[0] < (1 << 7), true)) {        \
    resop valI[0];                                           \
    valI += 1;                                               \
    } else if (__builtin_expect(valI[1] < (1 << 7), true)) { \
        resop (valI[0] & ((1 << 7) - 1)) +                   \
              (valI[1] << 7);                                \
        valI += 2;                                           \
    } else if (__builtin_expect(valI[2] < (1 << 7), true)) { \
        resop (valI[0] & ((1 << 7) - 1)) +                   \
              ((valI[1] & ((1 << 7) - 1)) << 7) +            \
              (valI[2] << 14);                               \
        valI += 3;                                           \
    } else if (__builtin_expect(valI[3] < (1 << 7), true)) { \
        resop (valI[0] & ((1 << 7) - 1)) +                   \
              ((valI[1] & ((1 << 7) - 1)) << 7) +            \
              ((valI[2] & ((1 << 7) - 1)) << 14) +           \
              (valI[3] << 21);                               \
        valI += 4;                                           \
    } else {                                                 \
        resop (valI[0] & ((1 << 7) - 1)) +                   \
              ((valI[1] & ((1 << 7) - 1)) << 7) +            \
              ((valI[2] & ((1 << 7) - 1)) << 14) +           \
              ((valI[3] & ((1 << 7) - 1)) << 21) +           \
              (valI[4] << 28);                               \
        valI += 5;                                           \
    }                                                        \
} while (0)

class ZcIteratorBase : public queryeval::RankedSearchIteratorBase
{
protected:
    ZcIteratorBase(fef::TermFieldMatchDataArray matchData, Position start, uint32_t docIdLimit);
    virtual void readWordStart(uint32_t docIdLimit) = 0;
    virtual void rewind(Position start) = 0;
    void initRange(uint32_t beginid, uint32_t endid) override;
    uint32_t getDocIdLimit() const { return _docIdLimit; }
    Trinary is_strict() const override { return Trinary::True; }
private:
    uint32_t   _docIdLimit;
    Position   _start;
};

template <bool bigEndian>
class ZcRareWordPostingIteratorBase : public ZcIteratorBase
{
private:
    using ParentClass = ZcIteratorBase;

public:
    using DecodeContextBase = bitcompression::FeatureDecodeContext<bigEndian>;
    using DocIdAndFeatures = index::DocIdAndFeatures;
    DecodeContextBase *_decodeContext;
    unsigned int       _residue;
    uint32_t           _prevDocId;  // Previous document id
    uint32_t           _numDocs;    // Documents in chunk or word
    bool               _decode_normal_features;
    bool               _decode_interleaved_features;
    bool               _unpack_normal_features;
    bool               _unpack_interleaved_features;
    uint32_t           _field_length;
    uint32_t           _num_occs;

    ZcRareWordPostingIteratorBase(fef::TermFieldMatchDataArray matchData, Position start, uint32_t docIdLimit,
                                  bool decode_normal_features, bool decode_interleaved_features,
                                  bool unpack_normal_features, bool unpack_interleaved_features);

    void doUnpack(uint32_t docId) override;
    void rewind(Position start) override;
};

template <bool dynamic_k> class ZcPostingDocIdKParam;

template <>
class ZcPostingDocIdKParam<false>
{
public:
    ZcPostingDocIdKParam() { }
    constexpr static uint32_t get_doc_id_k() { return K_VALUE_ZCPOSTING_DELTA_DOCID; }
    void setup(uint32_t, uint32_t) { }
};

template <>
class ZcPostingDocIdKParam<true>
{
    uint32_t _doc_id_k;
public:
    ZcPostingDocIdKParam() : _doc_id_k(0) { }
    uint32_t get_doc_id_k() const { return _doc_id_k; }
    void setup(uint32_t num_docs, uint32_t doc_id_limit) {
        using EC = bitcompression::FeatureEncodeContext<true>;
        _doc_id_k = EC::calcDocIdK(num_docs, doc_id_limit);
    }
};


template <bool bigEndian, bool dynamic_k>
class ZcRareWordPostingIterator : public ZcRareWordPostingIteratorBase<bigEndian>
{
    using ParentClass = ZcRareWordPostingIteratorBase<bigEndian>;
    using ParentClass::getDocId;
    using ParentClass::getUnpacked;
    using ParentClass::clearUnpacked;
    using ParentClass::_residue;
    using ParentClass::setDocId;
    using ParentClass::setAtEnd;
    using ParentClass::_numDocs;
    using ParentClass::_decode_normal_features;
    using ParentClass::_decode_interleaved_features;
    using ParentClass::_unpack_normal_features;
    using ParentClass::_unpack_interleaved_features;
    using ParentClass::_field_length;
    using ParentClass::_num_occs;
    ZcPostingDocIdKParam<dynamic_k> _doc_id_k_param;
public:
    using ParentClass::_decodeContext;
    ZcRareWordPostingIterator(fef::TermFieldMatchDataArray matchData, Position start, uint32_t docIdLimit,
                              bool decode_normal_features, bool decode_interleaved_features,
                              bool unpack_normal_features, bool unpack_interleaved_features);
    void doSeek(uint32_t docId) override;
    void readWordStart(uint32_t docIdLimit) override;
};

class ZcPostingIteratorBase : public ZcIteratorBase
{
protected:
    const uint8_t *_valI;     // docid deltas
    const uint8_t *_valIBase; // start of docid deltas
    uint64_t _featureSeekPos;

    // Helper class for L1 skip info
    class L1Skip
    {
    public:
        uint32_t _skipDocId;
        const uint8_t *_valI;
        const uint8_t *_docIdPos;
        uint64_t _skipFeaturePos;
        const uint8_t *_valIBase;

        L1Skip()
            : _skipDocId(0),
              _valI(nullptr),
              _docIdPos(nullptr),
              _skipFeaturePos(0),
              _valIBase(nullptr)
        {
        }

        void setup(uint32_t prevDocId, uint32_t lastDocId, const uint8_t *&bcompr, uint32_t skipSize) {
            if (skipSize != 0) {
                _valI = _valIBase = bcompr;
                bcompr += skipSize;
                _skipDocId = prevDocId + 1;
                ZCDECODE(_valI, _skipDocId +=);
            } else {
                _valI = _valIBase = nullptr;
                _skipDocId = lastDocId;
            }
            _skipFeaturePos = 0;
        }
        void postSetup(const ZcPostingIteratorBase &l0) {
            _docIdPos = l0._valIBase;
        }
        void decodeSkipEntry(bool decode_normal_features) {
            ZCDECODE(_valI, _docIdPos += 1 +);
            if (decode_normal_features) {
                ZCDECODE(_valI, _skipFeaturePos += 1 +);
            }
        }
        void nextDocId() {
            ZCDECODE(_valI, _skipDocId += 1 +);
        }
    };

    // Helper class for L2 skip info
    class L2Skip : public L1Skip
    {
    public:
        const uint8_t *_l1Pos;

        L2Skip()
            : L1Skip(),
              _l1Pos(nullptr)
        {
        }

        void postSetup(const L1Skip &l1) {
            _docIdPos = l1._docIdPos;
            _l1Pos = l1._valIBase;
        }
        void decodeSkipEntry(bool decode_normal_features) {
            L1Skip::decodeSkipEntry(decode_normal_features);
            ZCDECODE(_valI, _l1Pos += 1 + );
        }
    };

    // Helper class for L3 skip info
    class L3Skip : public L2Skip
    {
    public:
        const uint8_t *_l2Pos;

        L3Skip()
            : L2Skip(),
              _l2Pos(nullptr)
        {
        }

        void postSetup(const L2Skip &l2) {
            _docIdPos = l2._docIdPos;
            _l1Pos = l2._l1Pos;
            _l2Pos = l2._valIBase;
        }
        void decodeSkipEntry(bool decode_normal_features) {
            L2Skip::decodeSkipEntry(decode_normal_features);
            ZCDECODE(_valI, _l2Pos += 1 + );
        }
    };

    // Helper class for L4 skip info
    class L4Skip : public L3Skip
    {
    public:
        const uint8_t *_l3Pos;

        L4Skip()
            : L3Skip(),
              _l3Pos(nullptr)
        {
        }

        void postSetup(const L3Skip &l3) {
            _docIdPos = l3._docIdPos;
            _l1Pos = l3._l1Pos;
            _l2Pos = l3._l2Pos;
            _l3Pos = l3._valIBase;
        }

        void decodeSkipEntry(bool decode_normal_features) {
            L3Skip::decodeSkipEntry(decode_normal_features);
            ZCDECODE(_valI, _l3Pos += 1 + );
        }
    };

    // Helper class for chunk skip info
    class ChunkSkip {
    public:
        uint32_t _lastDocId;

        ChunkSkip()
            : _lastDocId(0)
        {
        }
    };

    L1Skip _l1;
    L2Skip _l2;
    L3Skip _l3;
    L4Skip _l4;
    ChunkSkip _chunk;
    uint64_t _featuresSize;
    bool     _hasMore;
    bool     _decode_normal_features;
    bool     _decode_interleaved_features;
    bool     _unpack_normal_features;
    bool     _unpack_interleaved_features;
    uint32_t _chunkNo;
    uint32_t _field_length;
    uint32_t _num_occs;

    void nextDocId(uint32_t prevDocId) {
        uint32_t docId = prevDocId + 1;
        ZCDECODE(_valI, docId +=);
        setDocId(docId);
        if (_decode_interleaved_features) {
            ZCDECODE(_valI, _field_length = 1 +);
            ZCDECODE(_valI, _num_occs = 1 +);
        }
    }
    virtual void featureSeek(uint64_t offset) = 0;
    VESPA_DLL_LOCAL void doChunkSkipSeek(uint32_t docId);
    VESPA_DLL_LOCAL void doL4SkipSeek(uint32_t docId);
    VESPA_DLL_LOCAL void doL3SkipSeek(uint32_t docId);
    VESPA_DLL_LOCAL void doL2SkipSeek(uint32_t docId);
    VESPA_DLL_LOCAL void doL1SkipSeek(uint32_t docId);
    void doSeek(uint32_t docId) override;
public:
    ZcPostingIteratorBase(fef::TermFieldMatchDataArray matchData, Position start, uint32_t docIdLimit,
                          bool decode_normal_features, bool decode_interleaved_features,
                          bool unpack_normal_features, bool unpack_interleaved_features);
};

template <bool bigEndian>
class ZcPostingIterator : public ZcPostingIteratorBase
{
private:
    using ParentClass = ZcPostingIteratorBase;
    using ParentClass::getDocId;

public:

    using DecodeContextBase = bitcompression::FeatureDecodeContext<bigEndian>;
    using DocIdAndFeatures = index::DocIdAndFeatures;
    using PostingListCounts = index::PostingListCounts;
    DecodeContextBase *_decodeContext;
    uint32_t _minChunkDocs;
    uint32_t _docIdK;
    bool     _dynamicK;
    uint32_t _numDocs;
    // Start of current features block, needed for seeks
    const uint64_t *_featuresValI;
    int _featuresBitOffset;
    // Counts used for assertions
    const PostingListCounts &_counts;

    ZcPostingIterator(uint32_t minChunkDocs, bool dynamicK, const PostingListCounts &counts,
                      search::fef::TermFieldMatchDataArray matchData, Position start, uint32_t docIdLimit,
                      bool decode_normal_features, bool decode_interleaved_features,
                      bool unpack_normal_features, bool unpack_interleaved_features);


    void doUnpack(uint32_t docId) override;
    void readWordStart(uint32_t docIdLimit) override;
    void rewind(Position start) override;

    void featureSeek(uint64_t offset) override {
        _decodeContext->_valI = _featuresValI + (_featuresBitOffset + offset) / 64;
        _decodeContext->setupBits((_featuresBitOffset + offset) & 63);
    }
};


extern template class ZcRareWordPostingIterator<false, false>;
extern template class ZcRareWordPostingIterator<false, true>;
extern template class ZcRareWordPostingIterator<true, false>;
extern template class ZcRareWordPostingIterator<true, true>;

extern template class ZcPostingIterator<true>;
extern template class ZcPostingIterator<false>;

}