aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/unpackinfo.h
blob: eedb907bb29be5064110e67b5bb8e56d78555e29 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/string.h>

namespace search::queryeval {

class MultiSearch;

class UnpackInfo
{
private:
    static constexpr size_t max_size = 31;
    static constexpr size_t max_index = 255;

    uint8_t _size;
    uint8_t _unpack[max_size];

public:
    UnpackInfo();

    // add an index to unpack, will not renumber existing indexes
    UnpackInfo &add(size_t index);

    // insert an index that may need unpacking, will renumber existing indexes
    UnpackInfo &insert(size_t index, bool unpack = true);

    // remove an index and its unpack data, will renumber existing indexes
    UnpackInfo &remove(size_t index);

    UnpackInfo &forceAll() {
        _size = (max_size + 1);
        return *this;
    }

    bool unpackAll() const { return (_size > max_size); }
    bool empty() const { return (_size == 0); }
    bool needUnpack(size_t index) const;

    template <typename F>
    void each(F &&f, size_t n) const {
        if (__builtin_expect(unpackAll(), false)) {
            for (size_t i = 0; i < n; ++i) {
                f(i);
            }
        } else {
            for (size_t i = 0; i < _size; ++i) {
                f(_unpack[i]);
            }
        }
    }

    vespalib::string toString() const;
};

struct NoUnpack {
    void unpack(uint32_t docid, const MultiSearch & search) {
        (void) docid;
        (void) search;
    }
    void onRemove(size_t index) { (void) index; }
    void onInsert(size_t index) { (void) index; }
    bool needUnpack(size_t index) const { (void) index; return false; }
};

}