aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/diskindex/diskindex/diskindex_test.cpp
blob: ba266a85470ef303e3e6aeb97b572954e9c5f5fe (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
340
341
342
343
344
345
346
347
348
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/common/bitvectoriterator.h>
#include <vespa/searchlib/diskindex/disktermblueprint.h>
#include <vespa/searchlib/test/diskindex/testdiskindex.h>
#include <vespa/searchlib/test/searchiteratorverifier.h>
#include <vespa/searchlib/test/fakedata/fakeword.h>
#include <vespa/searchlib/diskindex/zcposocciterators.h>
#include <vespa/searchlib/query/tree/simplequery.h>
#include <vespa/searchlib/queryeval/booleanmatchiteratorwrapper.h>
#include <vespa/searchlib/queryeval/leaf_blueprints.h>
#include <vespa/searchlib/queryeval/emptysearch.h>
#include <vespa/searchlib/queryeval/fake_requestcontext.h>
#include <vespa/searchlib/index/dummyfileheadercontext.h>
#include <vespa/searchlib/test/fakedata/fpfactory.h>
#include <vespa/vespalib/io/fileutil.h>
#include <iostream>
#include <set>

using search::BitVectorIterator;
using namespace search::fef;
using namespace search::index;
using namespace search::query;
using namespace search::queryeval;
using namespace search::queryeval::blueprint;
using search::test::SearchIteratorVerifier;
using namespace search::fakedata;

namespace search {
namespace diskindex {

typedef DiskIndex::LookupResult LookupResult;

std::string
toString(SearchIterator & sb)
{
    std::ostringstream oss;
    bool first = true;
    for (sb.seek(1u); ! sb.isAtEnd(); sb.seek(sb.getDocId() + 1)) {
        if (!first) oss << ",";
        oss << sb.getDocId();
        first = false;
    }
    return oss.str();
}

SimpleStringTerm
makeTerm(const std::string & term)
{
    return SimpleStringTerm(term, "field", 0, search::query::Weight(0));
}

class Test : public vespalib::TestApp, public TestDiskIndex {
private:
    FakeRequestContext _requestContext;

    void requireThatLookupIsWorking(bool fieldEmpty, bool docEmpty, bool wordEmpty);
    void requireThatWeCanReadPostingList();
    void requireThatWeCanReadBitVector();
    void requireThatBlueprintIsCreated();
    void requireThatBlueprintCanCreateSearchIterators();
    void requireThatSearchIteratorsConforms();
public:
    Test();
    ~Test();
    int Main() override;
};

class Verifier : public SearchIteratorVerifier {
public:
    Verifier(FakePosting::SP fp);
    ~Verifier();
    SearchIterator::UP create(bool strict) const override {
        (void) strict;
        return SearchIterator::UP(_fp->createIterator(_tfmda));
    }
private:
    mutable TermFieldMatchDataArray _tfmda;
    FakePosting::SP _fp;
};

    Verifier::Verifier(FakePosting::SP fp) :
    _fp(std::move(fp))
            { }
    Verifier::~Verifier() {}

void
Test::requireThatSearchIteratorsConforms()
{
    FakePosting::SP tmp;
    Verifier verTmp(tmp);
    Schema schema;
    schema.addIndexField(Schema::IndexField("a", Schema::DataType::STRING));
    bitcompression::PosOccFieldsParams params;
    params.setSchemaParams(schema, 0);
    search::fakedata::FakeWord fw(verTmp.getDocIdLimit(), verTmp.getExpectedDocIds(), "a", params, 0);
    TermFieldMatchData md;
    TermFieldMatchDataArray tfmda;
    tfmda.add(&md);
    std::vector<const FakeWord *> v;
    v.push_back(&fw);
    std::set<std::string> ignored = { "MemTreeOcc", "MemTreeOcc2",
                                      "FilterOcc", "ZcFilterOcc",
                                      "ZcNoSkipFilterOcc", "ZcSkipFilterOcc",
                                      "ZcbFilterOcc",
                                      "EGCompr64FilterOcc", "EGCompr64LEFilterOcc",
                                      "EGCompr64NoSkipFilterOcc", "EGCompr64SkipFilterOcc" };
    for (auto postingType : search::fakedata::getPostingTypes()) {
        if (ignored.find(postingType) == ignored.end()) {
            std::cerr << "Verifying " << postingType << std::endl;
            std::unique_ptr<FPFactory> ff(getFPFactory(postingType, schema));
            ff->setup(v);
            FakePosting::SP f(ff->make(fw));
            Verifier verifier(f);
            TEST_DO(verifier.verify());
        }
    }
}

void
Test::requireThatLookupIsWorking(bool fieldEmpty,
                                 bool docEmpty,
                                 bool wordEmpty)
{
    uint32_t f1(_schema.getIndexFieldId("f1"));
    uint32_t f2(_schema.getIndexFieldId("f2"));
    uint32_t f3(_schema.getIndexFieldId("f3"));
    LookupResult::UP r;
    r = _index->lookup(f1, "not");
    EXPECT_TRUE(!r || r->counts._numDocs == 0);
    r = _index->lookup(f1, "w1not");
    EXPECT_TRUE(!r || r->counts._numDocs == 0);
    r = _index->lookup(f1, "wnot");
    EXPECT_TRUE(!r || r->counts._numDocs == 0);
    { // field 'f1'
        r = _index->lookup(f1, "w1");
        if (wordEmpty || fieldEmpty || docEmpty) {
            EXPECT_TRUE(!r || r->counts._numDocs == 0);
        } else {
            EXPECT_EQUAL(1u, r->wordNum);
            EXPECT_EQUAL(2u, r->counts._numDocs);
        }
        r = _index->lookup(f1, "w2");
        EXPECT_TRUE(!r || r->counts._numDocs == 0);
    }
    { // field 'f2'
        r = _index->lookup(f2, "w1");
        if (wordEmpty || fieldEmpty || docEmpty) {
            EXPECT_TRUE(!r || r->counts._numDocs == 0);
        } else {
            EXPECT_EQUAL(1u, r->wordNum);
            EXPECT_EQUAL(3u, r->counts._numDocs);
        }
        r = _index->lookup(f2, "w2");
        if (wordEmpty || fieldEmpty || docEmpty) {
            EXPECT_TRUE(!r || r->counts._numDocs == 0);
        } else {
            EXPECT_EQUAL(2u, r->wordNum);
            EXPECT_EQUAL(17u, r->counts._numDocs);
        }
    }
    { // field 'f3' doesn't exist
        r = _index->lookup(f3, "w1");
        EXPECT_TRUE(!r || r->counts._numDocs == 0);
        r = _index->lookup(f3, "w2");
        EXPECT_TRUE(!r || r->counts._numDocs == 0);
    }
}

void
Test::requireThatWeCanReadPostingList()
{
    TermFieldMatchDataArray mda;
    { // field 'f1'
        LookupResult::UP r = _index->lookup(0, "w1");
        PostingListHandle::UP h = _index->readPostingList(*r);
        SearchIterator * sb = h->createIterator(r->counts, mda);
        sb->initFullRange();
        EXPECT_EQUAL("1,3", toString(*sb));
        delete sb;
    }
}

void
Test::requireThatWeCanReadBitVector()
{
    { // word 'w1'
        LookupResult::UP r = _index->lookup(1, "w1");
        // not bit vector for 'w1'
        EXPECT_TRUE(_index->readBitVector(*r).get() == NULL);
    }
    { // word 'w2'
        BitVector::UP exp(BitVector::create(32));
        for (uint32_t docId = 1; docId < 18; ++docId) exp->setBit(docId);
        { // field 'f2'
            LookupResult::UP r =
                _index->lookup(1, "w2");
            BitVector::UP bv = _index->readBitVector(*r);
            EXPECT_TRUE(bv.get() != NULL);
            EXPECT_TRUE(*bv == *exp);
        }
    }
}

void
Test::requireThatBlueprintIsCreated()
{
    { // unknown field
        Blueprint::UP b =
            _index->createBlueprint(_requestContext, FieldSpec("none", 0, 0), makeTerm("w1"));
        EXPECT_TRUE(dynamic_cast<EmptyBlueprint *>(b.get()) != NULL);
    }
    { // unknown word
        Blueprint::UP b =
            _index->createBlueprint(_requestContext, FieldSpec("f1", 0, 0), makeTerm("none"));
        EXPECT_TRUE(dynamic_cast<EmptyBlueprint *>(b.get()) != NULL);
    }
    { // known field & word with hits
        Blueprint::UP b =
            _index->createBlueprint(_requestContext, FieldSpec("f1", 0, 0), makeTerm("w1"));
        EXPECT_TRUE(dynamic_cast<DiskTermBlueprint *>(b.get()) != NULL);
        EXPECT_EQUAL(2u, b->getState().estimate().estHits);
        EXPECT_TRUE(!b->getState().estimate().empty);
    }
    { // known field & word without hits
        Blueprint::UP b =
            _index->createBlueprint(_requestContext, FieldSpec("f1", 0, 0), makeTerm("w2"));
//        std::cerr << "BP = " << typeid(*b).name() << std::endl;
        EXPECT_TRUE((dynamic_cast<DiskTermBlueprint *>(b.get()) != NULL) ||
                    (dynamic_cast<EmptyBlueprint *>(b.get()) != NULL));
        EXPECT_EQUAL(0u, b->getState().estimate().estHits);
        EXPECT_TRUE(b->getState().estimate().empty);
    }
}

void
Test::requireThatBlueprintCanCreateSearchIterators()
{
    TermFieldMatchData md;
    TermFieldMatchDataArray mda;
    mda.add(&md);
    Blueprint::UP b;
    SearchIterator::UP s;
    { // bit vector due to isFilter
        b = _index->createBlueprint(_requestContext, FieldSpec("f2", 0, 0, true), makeTerm("w2"));
        b->fetchPostings(true);
        s = (dynamic_cast<LeafBlueprint *>(b.get()))->createLeafSearch(mda, true);
        EXPECT_TRUE(dynamic_cast<BitVectorIterator *>(s.get()) != NULL);
    }
    { // bit vector due to no ranking needed
        b = _index->createBlueprint(_requestContext, FieldSpec("f2", 0, 0, false), makeTerm("w2"));
        b->fetchPostings(true);
        s = (dynamic_cast<LeafBlueprint *>(b.get()))->createLeafSearch(mda, true);
        EXPECT_FALSE(dynamic_cast<BitVectorIterator *>(s.get()) != NULL);
        TermFieldMatchData md2;
        md2.tagAsNotNeeded();
        TermFieldMatchDataArray mda2;
        mda2.add(&md2);
        EXPECT_TRUE(mda2[0]->isNotNeeded());
        s = (dynamic_cast<LeafBlueprint *>(b.get()))->createLeafSearch(mda2, false);
        EXPECT_TRUE(dynamic_cast<BitVectorIterator *>(s.get()) != NULL);
    }
    { // fake bit vector
        b = _index->createBlueprint(_requestContext, FieldSpec("f1", 0, 0, true), makeTerm("w2"));
//        std::cerr << "BP = " << typeid(*b).name() << std::endl;
        b->fetchPostings(true);
        s = (dynamic_cast<LeafBlueprint *>(b.get()))->createLeafSearch(mda, true);
//        std::cerr << "SI = " << typeid(*s).name() << std::endl;
        EXPECT_TRUE((dynamic_cast<BooleanMatchIteratorWrapper *>(s.get()) != NULL) ||
                    dynamic_cast<EmptySearch *>(s.get()));
    }
    { // posting list iterator
        b = _index->createBlueprint(_requestContext, FieldSpec("f1", 0, 0), makeTerm("w1"));
        b->fetchPostings(true);
        s = (dynamic_cast<LeafBlueprint *>(b.get()))->createLeafSearch(mda, true);
        ASSERT_TRUE(dynamic_cast<Zc4RareWordPosOccIterator<true> *>(s.get()) != NULL);
    }
}

Test::Test() :
    TestDiskIndex()
{
}

Test::~Test() {}

int
Test::Main()
{
    TEST_INIT("diskindex_test");

    if (_argc > 0) {
        DummyFileHeaderContext::setCreator(_argv[0]);
    }

    vespalib::mkdir("index", false);
    TEST_DO(openIndex("index/1fedewe", false, false, true, true, true));
    TEST_DO(requireThatLookupIsWorking(true, true, true));
    TEST_DO(openIndex("index/1fede", false, false, true, true, false));
    TEST_DO(requireThatLookupIsWorking(true, true, false));
    TEST_DO(openIndex("index/1fewe", false, false, true, false, true));
    TEST_DO(requireThatLookupIsWorking(true, false, true));
    TEST_DO(openIndex("index/1fe", false, false, true, false, false));
    TEST_DO(requireThatLookupIsWorking(true, false, false));
    buildSchema();
    TEST_DO(openIndex("index/1dewe", false, false, false, true, true));
    TEST_DO(requireThatLookupIsWorking(false, true, true));
    TEST_DO(openIndex("index/1de", false, false, false, true, false));
    TEST_DO(requireThatLookupIsWorking(false, true, false));
    TEST_DO(openIndex("index/1we", false, false, false, false, true));
    TEST_DO(requireThatLookupIsWorking(false, false, true));
    TEST_DO(openIndex("index/1", false, false, false, false, false));
    TEST_DO(requireThatLookupIsWorking(false, false, false));
    TEST_DO(requireThatWeCanReadPostingList());
    TEST_DO(requireThatWeCanReadBitVector());
    TEST_DO(requireThatBlueprintIsCreated());
    TEST_DO(requireThatBlueprintCanCreateSearchIterators());

    TEST_DO(openIndex("index/2", true, false, false, false, false));
    TEST_DO(requireThatLookupIsWorking(false, false, false));
    TEST_DO(requireThatWeCanReadPostingList());
    TEST_DO(requireThatWeCanReadBitVector());
    TEST_DO(requireThatBlueprintIsCreated());
    TEST_DO(requireThatBlueprintCanCreateSearchIterators());

    TEST_DO(openIndex("index/3", false, true, false, false, false));
    TEST_DO(requireThatLookupIsWorking(false, false, false));
    TEST_DO(requireThatWeCanReadPostingList());
    TEST_DO(requireThatWeCanReadBitVector());
    TEST_DO(requireThatBlueprintIsCreated());
    TEST_DO(requireThatBlueprintCanCreateSearchIterators());

    TEST_DO(openIndex("index/4", true, true, false, false, false));
    TEST_DO(requireThatLookupIsWorking(false, false, false));
    TEST_DO(requireThatWeCanReadPostingList());
    TEST_DO(requireThatWeCanReadBitVector());
    TEST_DO(requireThatBlueprintIsCreated());
    TEST_DO(requireThatBlueprintCanCreateSearchIterators());
    TEST_DO(requireThatSearchIteratorsConforms());

    TEST_DONE();
}

}
}

TEST_APPHOOK(search::diskindex::Test);