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

#pragma once

#include <vector>

namespace search {

/*
 * Provide priority queue semantics for a set of posting readers.
 */
template <class Reader>
class PostingPriorityQueue
{
protected:
    class Ref
    {
        Reader *_ref;
    public:
        Ref(Reader *ref)
            : _ref(ref)
        {
        }

        bool operator<(const Ref &rhs) const { return *_ref < *rhs._ref; }
        Reader *get() const noexcept { return _ref; }
    };

    using Vector = std::vector<Ref>;
    Vector _vec;
    uint32_t _heap_limit;
    uint32_t _merge_chunk;

public:
    PostingPriorityQueue()
        : _vec(),
          _heap_limit(0u),
          _merge_chunk(0u)
    {
    }

    bool empty() const { return _vec.empty(); }
    void clear() { _vec.clear(); }
    void initialAdd(Reader *it) { _vec.push_back(Ref(it)); }

    /*
     * Sort vector after a set of initial add operations, so lowest()
     * and adjust() can be used. Skip sort if _vec.size() < heap_limit
     * since merging with few elements don't use heap.
     */
    void setup(uint32_t heap_limit);

    /*
     * Return lowest value.  Assumes vector is sorted.
     */
    Reader *lowest() const { return _vec.front().get(); }

    /*
     * The vector might no longer be sorted since the first element has changed
     * value.  Perform adjustments to make vector sorted again.
     */
    void adjust();
};

}