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

#include <vespa/searchcommon/attribute/config.h>
#include <vespa/searchcommon/attribute/status.h>
#include <vespa/searchlib/attribute/postingstore.h>
#include <vespa/searchlib/attribute/enumstore.hpp>
#include <vespa/vespalib/btree/btreerootbase.hpp>
#include <vespa/searchlib/attribute/postingstore.hpp>
#include <vespa/vespalib/datastore/buffer_type.hpp>
#include <vespa/vespalib/gtest/gtest.h>
#include <ostream>

using vespalib::GenerationHandler;
using vespalib::datastore::CompactionStrategy;
using vespalib::datastore::EntryRef;

namespace search::attribute {

using MyValueStore = EnumStoreT<int32_t>;
using MyPostingStore = PostingStore<int32_t>;

namespace {

constexpr uint32_t lid_limit = 20000;
constexpr uint32_t huge_sequence_length = 800;

struct PostingStoreSetup {
    bool enable_only_bitvector;
    explicit PostingStoreSetup(bool enable_only_bitvector_in)
        : enable_only_bitvector(enable_only_bitvector_in)
    {
    }
};

std::ostream& operator<<(std::ostream& os, const PostingStoreSetup setup)
{
    os << (setup.enable_only_bitvector ? "onlybv" : "mixed");
    return os;
}

Config make_config(PostingStoreSetup param) {
    Config cfg;
    cfg.setIsFilter(param.enable_only_bitvector);
    return cfg;
}

}

class PostingStoreTest : public ::testing::TestWithParam<PostingStoreSetup>
{
protected:
    GenerationHandler _gen_handler;
    Config            _config;
    Status            _status;
    MyValueStore      _value_store;
    MyPostingStore    _store;

    PostingStoreTest();
    ~PostingStoreTest() override;

    void inc_generation()
    {
        _value_store.freeze_dictionary();
        _store.freeze();
        _value_store.assign_generation(_gen_handler.getCurrentGeneration());
        _store.assign_generation(_gen_handler.getCurrentGeneration());
        _gen_handler.incGeneration();
        _value_store.reclaim_memory(_gen_handler.get_oldest_used_generation());
        _store.reclaim_memory(_gen_handler.get_oldest_used_generation());
    }

    EntryRef add_sequence(int start_key, int end_key)
    {
        std::vector<MyPostingStore::KeyDataType> additions;
        std::vector<MyPostingStore::KeyType> removals;
        EntryRef root;
        for (int i = start_key; i < end_key; ++i) {
            additions.emplace_back(i, 0);
        }
        _store.apply(root,
                     additions.data(), additions.data() + additions.size(),
                     removals.data(), removals.data() + removals.size());
        return root;
    }
    static std::vector<int> make_exp_sequence(int start_key, int end_key)
    {
        std::vector<int> sequence;
        for (int i = start_key; i < end_key; ++i) {
            sequence.emplace_back(i);
        }
        return sequence;
    }
    std::vector<int> get_sequence(EntryRef root) const {
        std::vector<int> sequence;
        _store.foreach_frozen_key(root, [&sequence](int key) { sequence.emplace_back(key); });
        return sequence;
    }

    void populate(uint32_t sequence_length);
    EntryRef get_posting_ref(int key);
    void test_compact_btree_nodes(uint32_t sequence_length);
    void test_compact_sequence(uint32_t sequence_length);
};

PostingStoreTest::PostingStoreTest()
    : _gen_handler(),
      _config(make_config(GetParam())),
      _status(),
      _value_store(true, _config.get_dictionary_config()),
      _store(_value_store.get_dictionary(), _status, _config)
{
    _store.resizeBitVectors(lid_limit, lid_limit);
}

PostingStoreTest::~PostingStoreTest()
{
    _value_store.get_dictionary().clear_all_posting_lists([this](EntryRef posting_idx) { _store.clear(posting_idx); });
    _store.clearBuilder();
    inc_generation();
}

void
PostingStoreTest::populate(uint32_t sequence_length)
{
    auto& store = _store;
    auto& dictionary = _value_store.get_dictionary();
    std::vector<EntryRef> refs;
    for (int i = 0; i < 9000; ++i) {
        refs.emplace_back(add_sequence(i + 6, i + 6 + sequence_length));
    }
    dictionary.update_posting_list(_value_store.insert(1), _value_store.get_comparator(), [this, sequence_length](EntryRef) { return add_sequence(4, 4 + sequence_length); });
    dictionary.update_posting_list(_value_store.insert(2), _value_store.get_comparator(), [this, sequence_length](EntryRef) { return add_sequence(5, 5 + sequence_length); });
    for (int i = 9000; i < 11000; ++i) {
        refs.emplace_back(add_sequence(i + 6, i + 6 + sequence_length));
    }
    for (auto& ref : refs) {
        store.clear(ref);
    }
    inc_generation();
}

EntryRef
PostingStoreTest::get_posting_ref(int key)
{
    auto &dictionary = _value_store.get_dictionary();
    auto root = dictionary.get_frozen_root();
    return dictionary.find_posting_list(_value_store.make_comparator(key), root).second;
}

void
PostingStoreTest::test_compact_sequence(uint32_t sequence_length)
{
    populate(sequence_length);
    auto &store = _store;
    EntryRef old_ref1 = get_posting_ref(1);
    EntryRef old_ref2 = get_posting_ref(2);
    auto usage_before = store.getMemoryUsage();
    bool compaction_done = false;
    CompactionStrategy compaction_strategy(0.05, 0.2);
    for (uint32_t pass = 0; pass < 45; ++pass) {
        store.update_stat(compaction_strategy);
        auto guard = _gen_handler.takeGuard();
        if (!store.consider_compact_worst_buffers(compaction_strategy)) {
            compaction_done = true;
            break;
        }
        inc_generation();
        EXPECT_FALSE(store.consider_compact_worst_buffers(compaction_strategy));
        guard = GenerationHandler::Guard();
        inc_generation();
    }
    EXPECT_TRUE(compaction_done);
    EntryRef ref1 = get_posting_ref(1);
    EntryRef ref2 = get_posting_ref(2);
    EXPECT_NE(old_ref1, ref1);
    EXPECT_NE(old_ref2, ref2);
    EXPECT_EQ(make_exp_sequence(4, 4 + sequence_length), get_sequence(ref1));
    EXPECT_EQ(make_exp_sequence(5, 5 + sequence_length), get_sequence(ref2));
    auto usage_after = store.getMemoryUsage();
    EXPECT_GT(usage_before.deadBytes(), usage_after.deadBytes());
}

void
PostingStoreTest::test_compact_btree_nodes(uint32_t sequence_length)
{
    populate(sequence_length);
    auto &store = _store;
    EntryRef old_ref1 = get_posting_ref(1);
    EntryRef old_ref2 = get_posting_ref(2);
    auto usage_before = store.getMemoryUsage();
    bool compaction_done = false;
    CompactionStrategy compaction_strategy(0.05, 0.2);
    for (uint32_t pass = 0; pass < 55; ++pass) {
        store.update_stat(compaction_strategy);
        auto guard = _gen_handler.takeGuard();
        if (!store.consider_compact_worst_btree_nodes(compaction_strategy)) {
            compaction_done = true;
            break;
        }
        inc_generation();
        EXPECT_FALSE(store.consider_compact_worst_btree_nodes(compaction_strategy));
        guard = GenerationHandler::Guard();
        inc_generation();
    }
    EXPECT_TRUE(compaction_done);
    EntryRef ref1 = get_posting_ref(1);
    EntryRef ref2 = get_posting_ref(2);
    EXPECT_EQ(old_ref1, ref1);
    EXPECT_EQ(old_ref2, ref2);
    EXPECT_EQ(make_exp_sequence(4, 4 + sequence_length), get_sequence(ref1));
    EXPECT_EQ(make_exp_sequence(5, 5 + sequence_length), get_sequence(ref2));
    auto usage_after = store.getMemoryUsage();
    if ((sequence_length < huge_sequence_length) || !_config.getIsFilter()) {
        EXPECT_GT(usage_before.deadBytes(), usage_after.deadBytes());
    } else {
        EXPECT_EQ(usage_before.deadBytes(), usage_after.deadBytes());
    }
}

INSTANTIATE_TEST_SUITE_P(PostingStoreMultiTest,
                         PostingStoreTest,
                         testing::Values(PostingStoreSetup(false), PostingStoreSetup(true)), testing::PrintToStringParamName());

TEST_P(PostingStoreTest, require_that_nodes_for_multiple_small_btrees_are_compacted)
{
    test_compact_btree_nodes(30);
}

TEST_P(PostingStoreTest, require_that_nodes_for_multiple_large_btrees_are_compacted)
{
    test_compact_btree_nodes(huge_sequence_length);
}

TEST_P(PostingStoreTest, require_that_short_arrays_are_compacted)
{
    test_compact_sequence(4);
}

TEST_P(PostingStoreTest, require_that_btree_roots_are_compacted)
{
    test_compact_sequence(10);
}

TEST_P(PostingStoreTest, require_that_bitvectors_are_compacted)
{
    test_compact_sequence(huge_sequence_length);
}

}

GTEST_MAIN_RUN_ALL_TESTS()