aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/attribute/posting_list_merger/posting_list_merger_test.cpp
blob: fb733db5f71fbecfca3589427f01db831f152ba4 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/attribute/posting_list_merger.h>
#include <vespa/vespalib/test/insertion_operators.h>
#include <vespa/vespalib/util/size_literals.h>
#include <algorithm>

using vespalib::btree::BTreeNoLeafData;
using search::attribute::PostingListMerger;

struct Posting {
    uint32_t lid;
    int32_t weight;
    Posting(uint32_t lid_, int32_t weight_) noexcept
        : lid(lid_),
          weight(weight_)
    {
    }

    bool operator==(const Posting &rhs) const noexcept {
        return ((lid == rhs.lid) && (weight == rhs.weight));
    }

    bool operator<(const Posting &rhs) const noexcept  { return lid < rhs.lid; }
};

std::ostream &operator<<(std::ostream &os, const Posting &posting)
{
    os << "{" << posting.lid << "," << posting.weight << "}";
    return os;
}


class WeightedPostingList
{
    std::vector<Posting> _entries;
public:
    WeightedPostingList(std::vector<Posting> entries) noexcept
        : _entries(std::move(entries))
    {
    }
    ~WeightedPostingList() = default;

    template <typename Func>
    void foreach(Func func) const {
        for (const auto &posting : _entries) {
            func(posting.lid, posting.weight);
        }
    }

    template <typename Func>
    void foreach_key(Func func) const {
        for (const auto &posting : _entries) {
            func(posting.lid);
        }
    }
};

constexpr uint32_t docIdLimit = 16_Ki;

struct WeightedFixture
{
    PostingListMerger<int32_t> _merger;

    WeightedFixture()
        : _merger(docIdLimit)
    {
    }

    ~WeightedFixture() = default;

    void reserveArray(uint32_t postingsCount, size_t postingsSize) { _merger.reserveArray(postingsCount, postingsSize); }

    std::vector<Posting> asArray() {
        const auto &llArray = _merger.getArray();
        std::vector<Posting> result;
        result.reserve(llArray.size());
        for (auto &entry : llArray) {
            result.emplace_back(entry._key, entry.getData());
        }
        return result;
    }

    std::vector<uint32_t> bvAsArray() {
        const auto &bv = *_merger.getBitVector();
        std::vector<uint32_t> result;
        uint32_t lid = bv.getNextTrueBit(0);
        while (lid + 1 < docIdLimit) {
            result.emplace_back(lid);
            lid = bv.getNextTrueBit(lid + 1);
        }
        return result;
    }

    void assertArray(std::vector<Posting> exp)
    {
        EXPECT_EQUAL(exp, asArray());
    }

    void assertBitVector(std::vector<uint32_t> exp)
    {
        EXPECT_EQUAL(exp, bvAsArray());
    }
};

TEST_F("Single weighted array", WeightedFixture)
{
    f._merger.reserveArray(1, 4);
    f._merger.addToArray(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}}));
    f._merger.merge();
    TEST_DO(f.assertArray({{2, 102}, {3, 103}, {5, 105}, {9, 109}}));
}

TEST_F("Merge array", WeightedFixture)
{
    f._merger.reserveArray(2, 8);
    f._merger.addToArray(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}}));
    f._merger.addToArray(WeightedPostingList({{ 6, 106}, {8, 108}, { 14, 114}, {17, 117}}));
    f._merger.merge();
    TEST_DO(f.assertArray({{2, 102}, {3, 103}, {5, 105}, {6, 106}, {8, 108}, {9, 109}, {14, 114}, {17, 117}}));
}

TEST_F("Merge many arrays", WeightedFixture)
{
    std::vector<Posting> res;
    f._merger.reserveArray(10, 100);
    for (uint32_t i = 0; i < 10; ++i) {
        std::vector<Posting> fragment;
        for (uint32_t j = 0; j < 10; ++j) {
            fragment.emplace_back(j * 100 + i, j * 200 + i);
        }
        f._merger.addToArray(WeightedPostingList(fragment));
        res.insert(res.end(), fragment.begin(), fragment.end());
    }
    f._merger.merge();
    std::sort(res.begin(), res.end());
    TEST_DO(f.assertArray(res));
}

TEST_F("Merge single posting list into bitvector", WeightedFixture)
{
    f._merger.allocBitVector();
    f._merger.addToBitVector(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}}));
    f._merger.merge();
    TEST_DO(f.assertBitVector({2, 3, 5, 9}));
}

TEST_F("Merge multiple posting lists into bitvector", WeightedFixture)
{
    f._merger.allocBitVector();
    f._merger.addToBitVector(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}}));
    f._merger.addToBitVector(WeightedPostingList({{ 6, 106}, {8, 108}, { 14, 114}, {17, 117}}));
    f._merger.merge();
    TEST_DO(f.assertBitVector({2, 3, 5, 6, 8, 9, 14, 17}));
}

TEST_MAIN() { TEST_RUN_ALL(); }