summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/grouping/mergingmanager.h
blob: ea2c62909b9afe256444285b64276a903cb7e9fd (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <memory>
#include <vector>

namespace search {
namespace grouping {

/**
 * Wrapper class used to handle merging of grouping results. All input
 * data is assumed to be kept alive by the user.
 **/
class MergingManager
{
public:
    /**
     * Simple wrapper for all the grouping results from a single
     * search/fdispatch node.
     **/
    struct Entry {
        uint32_t    partId;
        uint32_t    rowId;
        bool        mld;
        const char *data;
        size_t      length;

        Entry(uint32_t part, uint32_t row, bool m, const char *pt, size_t len)
            : partId(part), rowId(row), mld(m), data(pt), length(len) {}
    };

private:
    MergingManager(const MergingManager &);
    MergingManager &operator=(const MergingManager &);
    void fullMerge();
    bool needMerge() const;

    uint32_t           _partBits;
    uint32_t           _rowBits;
    std::vector<Entry> _input;
    char              *_result;
    size_t             _resultLen;

public:
    /**
     * Create a new merging manager.
     *
     * @param partBits how many bits to be used to encode partId into path
     * @param rowBits how many bits to be used to encode rowId into path
     **/
    MergingManager(uint32_t partBits, uint32_t rowBits);

    /**
     * Release resources
     **/
    ~MergingManager();

    /**
     * Register an additional grouping result that should be part of
     * the upcoming merge operation.
     *
     * @param partId which partition these results came from
     * @param rowId which row these results came from
     * @param mld true if the node below is a dispatch node
     * @param groupSpec group spec
     * @param groupSpecLen length of the group spec
     **/
    void addResult(uint32_t partId, uint32_t rowId, bool mld,
                   const char *groupResult, size_t groupResultLen);

    /**
     * Perform actual merging of all the registered grouping results.
     **/
    void merge();

    /**
     * Obtain the size of the grouping result
     *
     * @return grouping result size
     **/
    size_t getGroupResultLen() const;

    /**
     * Obtain the grouping result.
     *
     * @return grouping result
     **/
    const char *getGroupResult() const;

    /**
     * Steal the grouping result. Invoking this method will take
     * overship of the grouping result blob returned by this
     * method. Use 'free' to release the memory when you are done with
     * it.
     *
     * @return grouping result that have just been stolen
     **/
    char *stealGroupResult();
};

} // namespace search::grouping
} // namespace search