summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/btree/btree_store/btree_store_test.cpp
blob: e7d923d0e87ad0841343820a4af8cd0a38c6cf8c (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/btree/btreestore.h>
#include <vespa/vespalib/btree/btreenodeallocator.hpp>
#include <vespa/vespalib/btree/btreeroot.hpp>
#include <vespa/vespalib/btree/btreestore.hpp>
#include <vespa/vespalib/datastore/buffer_type.hpp>
#include <vespa/vespalib/gtest/gtest.h>

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

namespace vespalib::btree {

using MyTraits = BTreeTraits<4, 4, 31, false>;
using TreeStore = BTreeStore<int, int, btree::NoAggregated, std::less<int>, MyTraits>;

class BTreeStoreTest : public ::testing::Test {
protected:
    GenerationHandler _gen_handler;
    TreeStore _store;
    
    BTreeStoreTest();
    ~BTreeStoreTest();

    void inc_generation()
    {
        _store.freeze();
        _store.transferHoldLists(_gen_handler.getCurrentGeneration());
        _gen_handler.incGeneration();
        _store.trimHoldLists(_gen_handler.getFirstUsedGeneration());
    }

    EntryRef add_sequence(int start_key, int end_key)
    {
        std::vector<TreeStore::KeyDataType> additions;
        std::vector<TreeStore::KeyType> removals;
        EntryRef root;
        for (int i = start_key; i < end_key; ++i) {
            additions.emplace_back(i, 0);
        }
        _store.apply(root,
                     &additions[0], &additions[0] + additions.size(),
                     &removals[0], &removals[0] + 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 test_compact_sequence(uint32_t sequence_length);
};

BTreeStoreTest::BTreeStoreTest()
    : _gen_handler(),
      _store()
{
}

BTreeStoreTest::~BTreeStoreTest()
{
    _store.clearBuilder();
    inc_generation();
}

void
BTreeStoreTest::test_compact_sequence(uint32_t sequence_length)
{
    auto &store = _store;
    EntryRef ref1 = add_sequence(4, 4 + sequence_length);
    EntryRef ref2 = add_sequence(5, 5 + sequence_length);
    EntryRef old_ref1 = ref1;
    EntryRef old_ref2 = ref2;
    std::vector<EntryRef> refs;
    for (int i = 0; i < 1000; ++i) {
        refs.emplace_back(add_sequence(i + 6, i + 6 + sequence_length));
    }
    for (auto& ref : refs) {
        store.clear(ref);
    }
    inc_generation();
    auto usage_before = store.getMemoryUsage();
    for (uint32_t pass = 0; pass < 15; ++pass) {
        auto to_hold = store.start_compact_worst_buffers();
        ref1 = store.move(ref1);
        ref2 = store.move(ref2);
        store.finishCompact(to_hold);
        inc_generation();
    }
    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());
    store.clear(ref1);
    store.clear(ref2);
}

TEST_F(BTreeStoreTest, require_that_nodes_for_multiple_btrees_are_compacted)
{
    auto &store = this->_store;
    EntryRef ref1 = add_sequence(4, 40);
    EntryRef ref2 = add_sequence(100, 130);
    store.clear(add_sequence(1000, 20000));
    inc_generation();
    auto usage_before = store.getMemoryUsage();
    for (uint32_t pass = 0; pass < 15; ++pass) {
        auto to_hold = store.start_compact_worst_btree_nodes();
        store.move_btree_nodes(ref1);
        store.move_btree_nodes(ref2);
        store.finish_compact_worst_btree_nodes(to_hold);
        inc_generation();
    }
    EXPECT_EQ(make_exp_sequence(4, 40), get_sequence(ref1));
    EXPECT_EQ(make_exp_sequence(100, 130), get_sequence(ref2));
    auto usage_after = store.getMemoryUsage();
    EXPECT_GT(usage_before.deadBytes(), usage_after.deadBytes());
    store.clear(ref1);
    store.clear(ref2);
}

TEST_F(BTreeStoreTest, require_that_short_arrays_are_compacted)
{
    test_compact_sequence(4);
}

TEST_F(BTreeStoreTest, require_that_btree_roots_are_compacted)
{
    test_compact_sequence(10);
}

}

GTEST_MAIN_RUN_ALL_TESTS()