aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/predicate/simple_index_test.cpp
blob: 7bf5268078247d92d4b5049c34e2c51627d471a1 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Unit tests for simple_index.

#include <vespa/searchlib/predicate/simple_index.hpp>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/attribute/predicate_attribute.h>
#include <vespa/vespalib/btree/btree.hpp>
#include <vespa/vespalib/btree/btreeroot.hpp>
#include <vespa/vespalib/btree/btreeiterator.hpp>
#include <vespa/vespalib/btree/btreestore.hpp>
#include <vespa/vespalib/btree/btreenodeallocator.hpp>
#include <vespa/vespalib/datastore/buffer_type.hpp>
#include <vespa/vespalib/util/rcuvector.hpp>
#include <map>

#include <vespa/log/log.h>
LOG_SETUP("simple_index_test");

using namespace search;
using namespace search::predicate;
using vespalib::GenerationHolder;

namespace {

struct MyData {
    uint32_t data;
    MyData() : data(0) {}
    MyData(uint32_t d) : data(d) {}
    bool valid() const {
        return data != 0;
    }
};

struct MyDataSerializer : PostingSerializer<MyData> {
    void serialize(const MyData &data, vespalib::DataBuffer& buffer) const override {
        buffer.writeInt32(data.data);
    }
};

struct MyDataDeserializer : PostingDeserializer<MyData> {
    MyData deserialize(vespalib::DataBuffer& buffer) override {
        return {buffer.readInt32()};
    }
};

struct SimpleDocIdLimitProvider : public DocIdLimitProvider {
    uint32_t _doc_id_limit = 1;
    uint32_t _committed_doc_id_limit = 1;
    virtual uint32_t getDocIdLimit() const override { return _doc_id_limit; }
    virtual uint32_t getCommittedDocIdLimit() const override { return _committed_doc_id_limit; }
};

constexpr uint64_t key = 0x123456;
constexpr uint32_t doc_id = 42;
const MyData data{100};

constexpr double UPPER_DOCID_FREQ_THRESHOLD = 0.5;
constexpr double LOWER_DOCID_FREQ_THRESHOLD = 0.25;
constexpr size_t UPPER_VECTOR_SIZE_THRESHOLD = 10;
constexpr size_t LOWER_VECTOR_SIZE_THRESHOLD = 8;
constexpr size_t VECTOR_PRUNE_FREQUENCY = 1;
constexpr double FOREACH_VECTOR_THRESHOLD = 0.0;
const auto config = SimpleIndexConfig(UPPER_DOCID_FREQ_THRESHOLD,
                                      LOWER_DOCID_FREQ_THRESHOLD,
                                      UPPER_VECTOR_SIZE_THRESHOLD,
                                      LOWER_VECTOR_SIZE_THRESHOLD,
                                      VECTOR_PRUNE_FREQUENCY,
                                      FOREACH_VECTOR_THRESHOLD,
                                      vespalib::GrowStrategy());
struct Fixture {
    GenerationHolder _generation_holder;
    SimpleDocIdLimitProvider _limit_provider;
    SimpleIndex<MyData> _index;
    Fixture() : _generation_holder(), _limit_provider(),
                _index(_generation_holder, _limit_provider, config) {}
    ~Fixture() {
        _generation_holder.reclaim_all();
    }
    SimpleIndex<MyData> &index() {
        return _index;
    }
    void addPosting(uint64_t k, uint32_t id, const MyData &d) {
        if (id >= _limit_provider._doc_id_limit) {
            _limit_provider._doc_id_limit = id + 1;
        }
        _index.addPosting(k, id, d);
    }
    SimpleIndex<MyData>::DictionaryIterator lookup(uint64_t k) {
        return _index.lookup(k);
    }
    bool hasKey(uint64_t k) {
        return lookup(k).valid();
    }
    std::pair<MyData, bool> removeFromPostingList(uint64_t k, uint32_t id) {
        return _index.removeFromPostingList(k, id);
    }
    bool hasVectorPostingList(uint64_t k) {
        return _index.getVectorPostingList(k).operator bool();
    }
    SimpleIndex<MyData>::VectorIterator getVectorPostingList(uint64_t k) {
        return *_index.getVectorPostingList(k);
    }
    SimpleIndex<MyData>::BTreeIterator getBTreePostingList(vespalib::datastore::EntryRef ref) {
        return _index.getBTreePostingList(ref);
    }
    void commit() {
        _index.commit();
        _limit_provider._committed_doc_id_limit = _limit_provider._doc_id_limit;
    }
};

TEST_F("require that SimpleIndex can insert and remove a value.", Fixture) {
    f.addPosting(key, doc_id, data);
    f.commit();
    auto it = f.lookup(key);
    ASSERT_TRUE(it.valid());
    vespalib::datastore::EntryRef ref = it.getData();
    auto posting_it = f.getBTreePostingList(ref);
    ASSERT_TRUE(posting_it.valid());
    EXPECT_EQUAL(doc_id, posting_it.getKey());
    EXPECT_EQUAL(data.data, posting_it.getData().data);

    auto result = f.removeFromPostingList(key, doc_id);
    EXPECT_TRUE(result.second);
    EXPECT_EQUAL(data.data, result.first.data);
    f.commit();

    result = f.removeFromPostingList(key, doc_id);
    EXPECT_FALSE(result.second);
    EXPECT_FALSE(result.first.valid());

    ASSERT_FALSE(f.hasKey(key));
}

TEST_F("require that SimpleIndex can insert and remove many values.", Fixture) {
    for (uint32_t id = 1; id < 100; ++id) {
        f.addPosting(key, id, {id});
    }
    f.commit();
    auto it = f.lookup(key);
    ASSERT_TRUE(it.valid());
    vespalib::datastore::EntryRef ref = it.getData();
    auto posting_it = f.getBTreePostingList(ref);
    for (size_t id = 1; id < 100; ++id) {
        ASSERT_TRUE(posting_it.valid());
        EXPECT_EQUAL(id, posting_it.getKey());
        EXPECT_EQUAL(id, posting_it.getData().data);
        ++posting_it;
    }
    ASSERT_FALSE(posting_it.valid());
    for (uint32_t id = 1; id < 100; ++id) {
        it = f.lookup(key);
        ASSERT_TRUE(it.valid());
        ref = it.getData();
        auto result = f.removeFromPostingList(key, id);
        EXPECT_TRUE(result.second);
        EXPECT_EQUAL(id, result.first.data);
    }
    f.commit();
    ASSERT_FALSE(f.hasKey(key));
}

struct MyObserver : SimpleIndexDeserializeObserver<> {
    std::map<uint32_t, uint64_t> features;
    void notifyInsert(uint64_t my_key, uint32_t my_doc_id, uint32_t) override {
        features[my_doc_id] = my_key;
    }
    bool hasSeenDoc(uint32_t doc) {
        return features.find(doc) != features.end();
    }
};

TEST_FF("require that SimpleIndex can be serialized and deserialized.", Fixture, Fixture) {
    for (uint32_t id = 1; id < 100; ++id) {
        f1.addPosting(key, id, {id});
    }
    f1.commit();
    vespalib::DataBuffer buffer;
    f1.index().serialize(buffer, MyDataSerializer());
    MyObserver observer;
    MyDataDeserializer deserializer;
    f2.index().deserialize(buffer, deserializer, observer, PredicateAttribute::PREDICATE_ATTRIBUTE_VERSION);

    auto it = f2.lookup(key);
    ASSERT_TRUE(it.valid());
    vespalib::datastore::EntryRef ref = it.getData();
    auto posting_it = f1.getBTreePostingList(ref);
    for (uint32_t id = 1; id < 100; ++id) {
        ASSERT_TRUE(posting_it.valid());
        EXPECT_EQUAL(id, posting_it.getKey());
        EXPECT_EQUAL(id, posting_it.getData().data);
        EXPECT_TRUE(observer.hasSeenDoc(id));
        ++posting_it;
    }
    EXPECT_FALSE(posting_it.valid());
}

TEST_F("require that SimpleIndex can update by inserting the same key twice.", Fixture) {
    f.addPosting(key, doc_id, data);

    MyData new_data{42};
    f.addPosting(key, doc_id, new_data);
    f.commit();

    auto it = f.lookup(key);
    ASSERT_TRUE(it.valid());
    vespalib::datastore::EntryRef ref = it.getData();
    auto posting_it = f.getBTreePostingList(ref);
    ASSERT_TRUE(posting_it.valid());
    EXPECT_EQUAL(doc_id, posting_it.getKey());
    EXPECT_EQUAL(new_data.data, posting_it.getData().data);
}

TEST_F("require that only that btrees exceeding size threshold is promoted to vector", Fixture) {
    for (uint32_t i = 1; i < 10; ++i) {
        f.addPosting(key, i, {i});
    }
    f.commit();
    ASSERT_TRUE(f.hasKey(key));
    EXPECT_FALSE(f.hasVectorPostingList(key));
    f.addPosting(key, 10, {10});
    f.commit();
    ASSERT_TRUE(f.hasVectorPostingList(key));
}

TEST_F("require that vectors below size threshold is pruned", Fixture) {
    for (uint32_t i = 1; i <= 10; ++i) {
        f.addPosting(key, i, {i});
    }
    f.commit();
    auto it = f.lookup(key);
    ASSERT_TRUE(it.valid());
    for (uint32_t i = 10; i > 8; --i) {
        f.removeFromPostingList(key, i);
    }
    f.commit();
    EXPECT_TRUE(f.hasVectorPostingList(key));
    f.removeFromPostingList(key, 8);
    f.commit();
    EXPECT_FALSE(f.hasVectorPostingList(key));
}

TEST_F("require that only btrees with high enough doc frequency is promoted to vector", Fixture) {
    for (uint32_t i = 100; i > 51; --i) {
        f.addPosting(key, i, {i});
    }
    f.commit();
    auto it = f.lookup(key);
    ASSERT_TRUE(it.valid());
    EXPECT_FALSE(f.hasVectorPostingList(key));
    f.addPosting(key, 51, {51});
    f.commit();
    ASSERT_TRUE(f.hasVectorPostingList(key));
}

TEST_F("require that vectors below doc frequency is pruned by removeFromPostingList", Fixture) {
    for (uint32_t i = 1; i <= 100; ++i) {
        f.addPosting(key, i, {i});
    }
    f.commit();
    ASSERT_TRUE(f.hasKey(key));
    EXPECT_TRUE(f.hasVectorPostingList(key));
    for (uint32_t i = 100; i > 25; --i) {
        f.removeFromPostingList(key, i);
    }
    f.commit();
    EXPECT_TRUE(f.hasVectorPostingList(key));
    f.removeFromPostingList(key, 25);
    f.commit();
    EXPECT_FALSE(f.hasVectorPostingList(key));
}

TEST_F("require that vectors below doc frequency is pruned by addPosting", Fixture) {
    for (uint32_t i = 1; i <= 10; ++i) {
        f.addPosting(key, i, {i});
    }
    f.commit();
    ASSERT_TRUE(f.hasKey(key));
    EXPECT_TRUE(f.hasVectorPostingList(key));
    for (uint32_t i = 1; i <= 100; ++i) {
        f.addPosting(key + 1, i, {i});
    }
    f.commit();
    EXPECT_FALSE(f.hasVectorPostingList(key));
}

TEST_F("require that promoteOverThresholdVectors promotes posting lists over threshold to vectors", Fixture) {
    f._limit_provider._doc_id_limit = 100;
    for (uint32_t i = 1; i <= 20; ++i) {
        f.addPosting(key + 0, i, {i});
        f.addPosting(key + 1, i, {i});
        f.addPosting(key + 2, i, {i});
    }
    for (uint32_t i = 21; i <= 40; ++i) {
        f.addPosting(key + 0, i, {i});
        f.addPosting(key + 2, i, {i});
    }
    f.commit();
    EXPECT_FALSE(f.hasVectorPostingList(key + 0));
    EXPECT_FALSE(f.hasVectorPostingList(key + 1));
    EXPECT_FALSE(f.hasVectorPostingList(key + 2));
    f._limit_provider._doc_id_limit = 50;
    f.index().promoteOverThresholdVectors();
    f.commit();
    EXPECT_TRUE(f.hasVectorPostingList(key + 0));
    EXPECT_FALSE(f.hasVectorPostingList(key + 1));
    EXPECT_TRUE(f.hasVectorPostingList(key + 2));
}

TEST_F("require that vector contains correct postings", Fixture) {
    for (uint32_t i = 1; i <= 100; ++i) {
        f.addPosting(key, i, i % 5 > 0 ? MyData{i * 2} : MyData{0});
    }
    f.commit();
    ASSERT_TRUE(f.hasKey(key));
    ASSERT_TRUE(f.hasVectorPostingList(key));
    auto v = f.getVectorPostingList(key);

    EXPECT_EQUAL(1u, v.getKey());
    EXPECT_EQUAL(2u, v.getData().data);

    for (uint32_t i = 1; i < 100; ++i) {
        v.linearSeek(i);
        ASSERT_TRUE(v.valid());
        if (i % 5 == 0) {
            EXPECT_EQUAL(i + 1, v.getKey());
            EXPECT_EQUAL((i + 1) * 2, v.getData().data);
        } else {
            EXPECT_EQUAL(i, v.getKey());
            EXPECT_EQUAL(i * 2, v.getData().data);
        }
    }
    v.linearSeek(100);
    EXPECT_FALSE(v.valid());
}

}  // namespace

TEST_MAIN() { TEST_RUN_ALL(); }