summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/attribute/direct_posting_store/direct_posting_store_test.cpp
blob: d6b0ff2a4b6aad10111787103d69fb7bb7e17558 (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
// 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/searchlib/attribute/attribute.h>
#include <vespa/searchlib/attribute/attribute_read_guard.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/attribute/attributeguard.h>
#include <vespa/searchlib/attribute/attributememorysavetarget.h>
#include <vespa/searchlib/attribute/i_docid_with_weight_posting_store.h>
#include <vespa/searchlib/index/dummyfileheadercontext.h>
#include <vespa/searchlib/queryeval/docid_with_weight_search_iterator.h>
#include <vespa/searchlib/test/searchiteratorverifier.h>
#include <vespa/searchlib/util/randomgenerator.h>
#include <vespa/vespalib/test/insertion_operators.h>
#include <vespa/vespalib/testkit/test_kit.h>

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

using namespace search;
using namespace search::attribute;

AttributeVector::SP make_attribute(BasicType type, CollectionType collection, bool fast_search) {
    Config cfg(type, collection);
    cfg.setFastSearch(fast_search);
    return AttributeFactory::createAttribute("my_attribute", cfg);
}

void add_docs(AttributeVector::SP attr_ptr, size_t limit = 1000) {
    AttributeVector::DocId docid;
    for (size_t i = 0; i < limit; ++i) {
        attr_ptr->addDoc(docid);
    }
    attr_ptr->commit();
    ASSERT_EQUAL((limit - 1), docid);
}

template <typename ATTR, typename KEY>
void set_doc(ATTR *attr, uint32_t docid, KEY key, int32_t weight) {
    attr->clearDoc(docid);
    attr->append(docid, key, weight);
    attr->commit();
}

void populate_long(AttributeVector::SP attr_ptr) {
    IntegerAttribute *attr = static_cast<IntegerAttribute *>(attr_ptr.get());
    set_doc(attr, 1, int64_t(111), 20);
    set_doc(attr, 5, int64_t(111), 5);
    set_doc(attr, 7, int64_t(111), 10);
}

void populate_string(AttributeVector::SP attr_ptr) {
    StringAttribute *attr = static_cast<StringAttribute *>(attr_ptr.get());
    set_doc(attr, 1, "foo", 20);
    set_doc(attr, 5, "foo", 5);
    set_doc(attr, 7, "foo", 10);
}

struct LongFixture {
    AttributeVector::SP attr;
    const IDocidWithWeightPostingStore *api;
    LongFixture() : attr(make_attribute(BasicType::INT64, CollectionType::WSET, true)),
                    api(attr->as_docid_with_weight_posting_store())
    {
        ASSERT_TRUE(api != nullptr);
        add_docs(attr);
        populate_long(attr);
    }
};

struct StringFixture {
    AttributeVector::SP attr;
    const IDocidWithWeightPostingStore *api;
    StringFixture() : attr(make_attribute(BasicType::STRING, CollectionType::WSET, true)),
                      api(attr->as_docid_with_weight_posting_store())
    {
        ASSERT_TRUE(api != nullptr);
        add_docs(attr);
        populate_string(attr);
    }
};

TEST("require that appropriate attributes support the IDocidWithWeightPostingStore interface") {
    EXPECT_TRUE(make_attribute(BasicType::INT64, CollectionType::WSET, true)->as_docid_with_weight_posting_store() != nullptr);
    EXPECT_TRUE(make_attribute(BasicType::STRING, CollectionType::WSET, true)->as_docid_with_weight_posting_store() != nullptr);
}

TEST("require that inappropriate attributes do not support the IDocidWithWeightPostingStore interface") {
    EXPECT_TRUE(make_attribute(BasicType::INT64, CollectionType::SINGLE, false)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::INT64, CollectionType::ARRAY, false)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::INT64, CollectionType::WSET, false)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::INT64, CollectionType::SINGLE, true)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::INT64, CollectionType::ARRAY, true)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::STRING, CollectionType::SINGLE, false)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::STRING, CollectionType::ARRAY, false)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::STRING, CollectionType::WSET, false)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::STRING, CollectionType::SINGLE, true)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::STRING, CollectionType::ARRAY, true)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::INT32, CollectionType::WSET, true)->as_docid_with_weight_posting_store() == nullptr);
    EXPECT_TRUE(make_attribute(BasicType::DOUBLE, CollectionType::WSET, true)->as_docid_with_weight_posting_store() == nullptr);
}

void verify_valid_lookup(IDirectPostingStore::LookupResult result) {
    EXPECT_TRUE(result.posting_idx.valid());
    EXPECT_EQUAL(3u, result.posting_size);
    EXPECT_EQUAL(5, result.min_weight);
    EXPECT_EQUAL(20, result.max_weight);
}

void verify_invalid_lookup(IDirectPostingStore::LookupResult result) {
    EXPECT_FALSE(result.posting_idx.valid());
    EXPECT_EQUAL(0u, result.posting_size);
    EXPECT_EQUAL(0, result.min_weight);
    EXPECT_EQUAL(0, result.max_weight);
}

TEST_F("require that integer lookup works correctly", LongFixture) {
    verify_valid_lookup(f1.api->lookup("111", f1.api->get_dictionary_snapshot()));
    verify_invalid_lookup(f1.api->lookup("222", f1.api->get_dictionary_snapshot()));
}

TEST_F("require string lookup works correctly", StringFixture) {
    verify_valid_lookup(f1.api->lookup("foo", f1.api->get_dictionary_snapshot()));
    verify_invalid_lookup(f1.api->lookup("bar", f1.api->get_dictionary_snapshot()));
}

void verify_posting(const IDocidWithWeightPostingStore &api, const char *term) {
    auto result = api.lookup(term, api.get_dictionary_snapshot());
    ASSERT_TRUE(result.posting_idx.valid());
    std::vector<DocidWithWeightIterator> itr_store;
    api.create(result.posting_idx, itr_store);
    ASSERT_EQUAL(1u, itr_store.size());
    {
        DocidWithWeightIterator &itr = itr_store[0];
        if (itr.valid() && itr.getKey() < 1) {
            itr.linearSeek(1);
        }
        ASSERT_TRUE(itr.valid());
        EXPECT_EQUAL(1u, itr.getKey());  // docid
        EXPECT_EQUAL(20, itr.getData()); // weight
        itr.linearSeek(2);
        ASSERT_TRUE(itr.valid());
        EXPECT_EQUAL(5u, itr.getKey());  // docid
        EXPECT_EQUAL(5, itr.getData());  // weight
        itr.linearSeek(6);
        ASSERT_TRUE(itr.valid());
        EXPECT_EQUAL(7u, itr.getKey());  // docid
        EXPECT_EQUAL(10, itr.getData()); // weight
        itr.linearSeek(8);
        EXPECT_FALSE(itr.valid());
    }
}

TEST_F("require that integer iterators are created correctly", LongFixture) {
    verify_posting(*f1.api, "111");
}

TEST_F("require that string iterators are created correctly", StringFixture) {
    verify_posting(*f1.api, "foo");
}

TEST_F("require that collect_folded works for string", StringFixture)
{
    StringAttribute *attr = static_cast<StringAttribute *>(f1.attr.get());
    set_doc(attr, 2, "bar", 30);
    attr->commit();
    set_doc(attr, 3, "FOO", 30);
    attr->commit();
    auto dictionary_snapshot = f1.api->get_dictionary_snapshot();
    auto lookup1 = f1.api->lookup("foo", dictionary_snapshot);
    std::vector<vespalib::string> folded;
    std::function<void(vespalib::datastore::EntryRef)> save_folded = [&folded,attr](vespalib::datastore::EntryRef enum_idx) { folded.emplace_back(attr->getFromEnum(enum_idx.ref())); };
    f1.api->collect_folded(lookup1.enum_idx, dictionary_snapshot, save_folded);
    std::vector<vespalib::string> expected_folded{"FOO", "foo"};
    EXPECT_EQUAL(expected_folded, folded);
}

TEST_F("require that collect_folded works for integers", LongFixture)
{
    IntegerAttributeTemplate<int64_t> *attr = dynamic_cast<IntegerAttributeTemplate<int64_t> *>(f1.attr.get());
    set_doc(attr, 2, int64_t(112), 30);
    attr->commit();
    auto dictionary_snapshot = f1.api->get_dictionary_snapshot();
    auto lookup1 = f1.api->lookup("111", dictionary_snapshot);
    std::vector<int64_t> folded;
    std::function<void(vespalib::datastore::EntryRef)> save_folded = [&folded,attr](vespalib::datastore::EntryRef enum_idx) { folded.emplace_back(attr->getFromEnum(enum_idx.ref())); };
    f1.api->collect_folded(lookup1.enum_idx, dictionary_snapshot, save_folded);
    std::vector<int64_t> expected_folded{int64_t(111)};
    EXPECT_EQUAL(expected_folded, folded);
}

class Verifier : public search::test::SearchIteratorVerifier {
public:
    Verifier();
    ~Verifier();
    SearchIterator::UP create(bool strict) const override {
        (void) strict;
        const auto* api = _attr->as_docid_with_weight_posting_store();
        ASSERT_TRUE(api != nullptr);
        auto dict_entry = api->lookup("123", api->get_dictionary_snapshot());
        ASSERT_TRUE(dict_entry.posting_idx.valid());
        return std::make_unique<queryeval::DocidWithWeightSearchIterator>(_tfmd, *api, dict_entry);
    }
private:
    mutable fef::TermFieldMatchData _tfmd;
    AttributeVector::SP _attr;
};

Verifier::Verifier()
    : _attr(make_attribute(BasicType::INT64, CollectionType::WSET, true))
{
    add_docs(_attr, getDocIdLimit());
    auto docids = getExpectedDocIds();
    IntegerAttribute *int_attr = static_cast<IntegerAttribute *>(_attr.get());
    for (auto docid: docids) {
        set_doc(int_attr, docid, int64_t(123), 1);
    }
}
Verifier::~Verifier() {}

TEST("verify document weight search iterator") {
    Verifier verifier;
    verifier.verify();
}

TEST_MAIN() { TEST_RUN_ALL(); }