summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/postinglistbm/posting_list_test.cpp
blob: ad3410b8f92b22b6f1f2bd9ffab77803489b98b0 (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
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchlib/test/fakedata/fake_match_loop.h>
#include <vespa/searchlib/test/fakedata/fakeposting.h>
#include <vespa/searchlib/test/fakedata/fakeword.h>
#include <vespa/searchlib/test/fakedata/fakewordset.h>
#include <vespa/searchlib/test/fakedata/fpfactory.h>
#include <vespa/searchlib/util/rand48.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <cinttypes>

using search::fef::TermFieldMatchData;
using search::fef::TermFieldMatchDataArray;
using search::queryeval::SearchIterator;

using namespace search::index;
using namespace search::fakedata;

using FakeWordUP = std::unique_ptr<FakeWord>;

void
validate_posting_list_for_word(const FakePosting& posting, const FakeWord& word)
{
    TermFieldMatchData md;
    TermFieldMatchDataArray tfmda;
    tfmda.add(&md);

    std::unique_ptr<SearchIterator> iterator(posting.createIterator(tfmda));
    if (posting.hasWordPositions()) {
        word.validate(iterator.get(), tfmda, false);
    } else {
        word.validate(iterator.get(), false);
    }
}

void
test_fake(const std::string& posting_type,
          const Schema& schema,
          const FakeWord& word)
{
    std::unique_ptr<FPFactory> factory(getFPFactory(posting_type, schema));
    std::vector<const FakeWord *> words;
    words.push_back(&word);
    factory->setup(words);
    auto posting = factory->make(word);

    printf("%s.bitsize=%d+%d+%d+%d+%d\n",
           posting->getName().c_str(),
           static_cast<int>(posting->bitSize()),
           static_cast<int>(posting->l1SkipBitSize()),
           static_cast<int>(posting->l2SkipBitSize()),
           static_cast<int>(posting->l3SkipBitSize()),
           static_cast<int>(posting->l4SkipBitSize()));

    validate_posting_list_for_word(*posting, word);

    uint64_t scan_time = 0;
    uint64_t scan_unpack_time = 0;
    int hits1 = FakeMatchLoop::single_posting_scan(*posting, word.getDocIdLimit(), scan_time);
    int hits2 = FakeMatchLoop::single_posting_scan_with_unpack(*posting, word.getDocIdLimit(), scan_unpack_time);

    printf("test_fake: '%s': hits1=%d, hits2=%d, scan_time=%" PRIu64 "(ns), scan_unpack_time=%" PRIu64 "(ns)\n",
           posting->getName().c_str(), hits1, hits2, scan_time, scan_unpack_time);
}

void
test_fake_pair(const std::string& posting_type, const Schema& schema,
               const FakeWord& word1, const FakeWord& word2)
{
    std::unique_ptr<FPFactory> factory(getFPFactory(posting_type, schema));
    std::vector<const FakeWord *> words;
    words.push_back(&word1);
    words.push_back(&word2);
    factory->setup(words);
    auto posting1 = factory->make(word1);
    auto posting2 = factory->make(word2);

    uint64_t scan_time = 0;
    int hits = FakeMatchLoop::and_pair_posting_scan(*posting1, *posting2, word1.getDocIdLimit(), scan_time);
    printf("test_fake_pair: '%s' AND '%s' => %d hits, scan_time=%" PRIu64 " (ns)\n",
           posting1->getName().c_str(), posting2->getName().c_str(), hits, scan_time);
}

struct PostingListTest : public ::testing::Test {
    uint32_t num_docs;
    std::vector<std::string> posting_types;
    FakeWordSet word_set;
    FakeWordUP word1;
    FakeWordUP word2;
    FakeWordUP word3;
    FakeWordUP word4;
    FakeWordUP word5;
    search::Rand48 rnd;

    PostingListTest()
        : num_docs(36000),
          posting_types(getPostingTypes()),
          word_set(),
          word1(),
          word2(),
          word3(),
          word4(),
          word5(),
          rnd()
    {
        rnd.srand48(32);
    }

    void setup(bool has_elements, bool has_element_weights) {
        word_set.setupParams(has_elements, has_element_weights);

        uint32_t w1_freq = 2;
        uint32_t w2_freq = 1000;
        uint32_t w3_freq = 10000;
        uint32_t w4_freq = 19000;
        uint32_t w5_freq = 5000;
        uint32_t w4w5od = 1000;

        word1 = std::make_unique<FakeWord>(num_docs, w1_freq, w1_freq / 2, "word1", rnd,
                                           word_set.getFieldsParams(), word_set.getPackedIndex());

        word2 = std::make_unique<FakeWord>(num_docs, w2_freq, w2_freq / 2, "word2", *word1, 4, rnd,
                                           word_set.getFieldsParams(), word_set.getPackedIndex());

        word3 = std::make_unique<FakeWord>(num_docs, w3_freq, w3_freq / 2, "word3", *word1, 10, rnd,
                                           word_set.getFieldsParams(), word_set.getPackedIndex());

        word4 = std::make_unique<FakeWord>(num_docs, w4_freq, w4_freq / 2, "word4", rnd,
                                           word_set.getFieldsParams(), word_set.getPackedIndex());

        word5 = std::make_unique<FakeWord>(num_docs, w5_freq, w5_freq / 2, "word5", *word4, w4w5od, rnd,
                                           word_set.getFieldsParams(), word_set.getPackedIndex());

    }

    void run() {
        for (const auto& type : posting_types) {
            test_fake(type, word_set.getSchema(), *word1);
            test_fake(type, word_set.getSchema(), *word2);
            test_fake(type, word_set.getSchema(), *word3);
        }

        for (const auto& type : posting_types) {
            test_fake_pair(type, word_set.getSchema(), *word1, *word3);
            test_fake_pair(type, word_set.getSchema(), *word2, *word3);
        }

        for (const auto& type : posting_types) {
            test_fake_pair(type, word_set.getSchema(), *word4, *word5);
        }
    }

};

TEST_F(PostingListTest, verify_posting_list_iterators_over_single_value_field)
{
    setup(false, false);
    run();
}

TEST_F(PostingListTest, verify_posting_list_iterators_over_array_field)
{
    setup(true, false);
    run();
}

TEST_F(PostingListTest, verify_posting_list_iterators_over_weighted_set_field)
{
    setup(true, true);
    run();
}

GTEST_MAIN_RUN_ALL_TESTS()