aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/index/indexcollection_test.cpp
blob: 3653ea32bae1a407f8d3a0afda88675a1272d04e (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchcore/proton/matching/fakesearchcontext.h>
#include <vespa/searchcorespi/index/warmupindexcollection.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/threadstackexecutor.h>

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

using search::queryeval::ISourceSelector;
using search::queryeval::FakeSearchable;
using search::FixedSourceSelector;
using namespace proton;
using namespace searchcorespi;
using searchcorespi::index::WarmupConfig;

class IndexCollectionTest : public ::testing::Test,
                            public IWarmupDone
{
public:
    std::shared_ptr<ISourceSelector> _selector;
    std::shared_ptr<IndexSearchable> _source1;
    std::shared_ptr<IndexSearchable> _source2;
    std::shared_ptr<IndexSearchable> _fusion_source;
    vespalib::ThreadStackExecutor    _executor;
    std::shared_ptr<IndexSearchable> _warmup;

    void expect_searchable_can_be_appended(IndexCollection::UP collection) {
        const uint32_t id = 42;

        collection->append(id, _source1);
        EXPECT_EQ(1u, collection->getSourceCount());
        EXPECT_EQ(id, collection->getSourceId(0));
    }

    void expect_searchable_can_be_replaced(IndexCollection::UP collection) {
        const uint32_t id = 42;

        collection->append(id, _source1);
        EXPECT_EQ(1u, collection->getSourceCount());
        EXPECT_EQ(id, collection->getSourceId(0));
        EXPECT_EQ(_source1.get(), &collection->getSearchable(0));

        collection->replace(id, _source2);
        EXPECT_EQ(1u, collection->getSourceCount());
        EXPECT_EQ(id, collection->getSourceId(0));
        EXPECT_EQ(_source2.get(), &collection->getSearchable(0));
    }

    IndexCollection::UP create_warmup(const IndexCollection::SP& prev, const IndexCollection::SP& next) {
        return std::make_unique<WarmupIndexCollection>(WarmupConfig(1.0, false), prev, next, *_warmup, _executor, *this);
    }

    virtual void warmupDone(ISearchableIndexCollection::SP current) override {
        (void) current;
    }

    IndexCollectionTest()
        : _selector(new FixedSourceSelector(0, "fs1")),
          _source1(new FakeIndexSearchable),
          _source2(new FakeIndexSearchable),
          _fusion_source(new FakeIndexSearchable),
          _executor(1, 128*1024),
          _warmup(new FakeIndexSearchable)
    {}
    ~IndexCollectionTest() {}
};


TEST_F(IndexCollectionTest, searchable_can_be_appended_to_normal_collection)
{
    expect_searchable_can_be_appended(std::make_unique<IndexCollection>(_selector));
}

TEST_F(IndexCollectionTest, searchable_can_be_replaced_in_normal_collection)
{
    expect_searchable_can_be_replaced(std::make_unique<IndexCollection>(_selector));
}

TEST_F(IndexCollectionTest, searchable_can_be_appended_to_warmup_collection)
{
    auto prev = std::make_shared<IndexCollection>(_selector);
    auto next = std::make_shared<IndexCollection>(_selector);
    expect_searchable_can_be_appended(create_warmup(prev, next));
    EXPECT_EQ(0u, prev->getSourceCount());
    EXPECT_EQ(1u, next->getSourceCount());
}

TEST_F(IndexCollectionTest, searchable_can_be_replaced_in_warmup_collection)
{
    auto prev = std::make_shared<IndexCollection>(_selector);
    auto next = std::make_shared<IndexCollection>(_selector);
    expect_searchable_can_be_replaced(create_warmup(prev, next));
    EXPECT_EQ(0u, prev->getSourceCount());
    EXPECT_EQ(1u, next->getSourceCount());
}

TEST_F(IndexCollectionTest, replace_and_renumber_updates_collection_after_fusion)
{
    IndexCollection fsc(_selector);

    fsc.append(0, _source1);
    fsc.append(1, _source1);
    fsc.append(2, _source1);
    fsc.append(3, _source2);
    EXPECT_EQ(4u, fsc.getSourceCount());

    const uint32_t id_diff = 2;
    auto new_fsc = IndexCollection::replaceAndRenumber(_selector, fsc, id_diff, _fusion_source);
    EXPECT_EQ(2u, new_fsc->getSourceCount());
    EXPECT_EQ(0u, new_fsc->getSourceId(0));
    EXPECT_EQ(_fusion_source.get(), &new_fsc->getSearchable(0));
    EXPECT_EQ(1u, new_fsc->getSourceId(1));
    EXPECT_EQ(_source2.get(), &new_fsc->getSearchable(1));
}

GTEST_MAIN_RUN_ALL_TESTS()