aboutsummaryrefslogtreecommitdiffstats
path: root/streamingvisitors/src/tests/searchvisitor/searchvisitor.cpp
blob: 5d8e1e15ef932d1a448154941459bd62a6ebc3d3 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/query/tree/querybuilder.h>
#include <vespa/searchlib/query/tree/simplequery.h>
#include <vespa/searchlib/query/tree/stackdumpcreator.h>
#include <vespa/searchvisitor/searchenvironment.h>
#include <vespa/searchvisitor/searchvisitor.h>
#include <vespa/storage/frameworkimpl/component/storagecomponentregisterimpl.h>
#include <vespa/storageframework/defaultimplementation/clock/fakeclock.h>

using namespace search;
using namespace search::query;
using namespace document;

namespace storage {

class SearchVisitorTest : public vespalib::TestApp
{
private:
    framework::defaultimplementation::FakeClock _clock;
    StorageComponentRegisterImpl      _componentRegister;
    std::unique_ptr<StorageComponent> _component;
    SearchEnvironment                 _env;
    void testSearchVisitor();
    void testSearchEnvironment();
    void testCreateSearchVisitor(const vespalib::string & dir, const vdslib::Parameters & parameters);
    void testOnlyRequireWeakReadConsistency();

public:
    SearchVisitorTest();
    int Main();
};

SearchVisitorTest::SearchVisitorTest() :
    vespalib::TestApp(),
    _componentRegister(),
    _env("dir:" + TEST_PATH("cfg"))
{
    _componentRegister.setNodeInfo("mycluster", lib::NodeType::STORAGE, 1);
    _componentRegister.setClock(_clock);
    StorageComponent::DocumentTypeRepoSP repo(new DocumentTypeRepo(readDocumenttypesConfig(TEST_PATH("cfg/documenttypes.cfg"))));
    _componentRegister.setDocumentTypeRepo(repo);
    _component.reset(new StorageComponent(_componentRegister, "storage"));
};

std::vector<spi::DocEntry::LP>
createDocuments(const vespalib::string & dir)
{
    (void) dir;
    std::vector<spi::DocEntry::LP> documents;
    spi::Timestamp ts;
    document::Document::UP doc(new document::Document());
    spi::DocEntry::LP e(new spi::DocEntry(ts, 0, std::move(doc)));
    documents.push_back(e);
    return documents;
}

void
SearchVisitorTest::testCreateSearchVisitor(const vespalib::string & dir, const vdslib::Parameters & params)
{
    SearchVisitorFactory sFactory(dir);
    VisitorFactory & factory(sFactory);
    std::unique_ptr<Visitor> sv(static_cast<SearchVisitor *>(factory.makeVisitor(*_component, _env, params)));
    document::OrderingSpecification orderSpec;
    document::BucketId bucketId;
    std::vector<spi::DocEntry::LP> documents(createDocuments(dir));
    Visitor::HitCounter hitCounter(&orderSpec);
    sv->handleDocuments(bucketId, documents, hitCounter);
}

void
SearchVisitorTest::testSearchEnvironment()
{
    EXPECT_TRUE(_env.getVSMAdapter("simple") != NULL);
    EXPECT_TRUE(_env.getRankManager("simple") != NULL);
}

void
SearchVisitorTest::testSearchVisitor()
{
    vdslib::Parameters params;
    params.set("searchcluster", "aaa");
    params.set("queryflags", "0x40000");
    params.set("summarycount", "3");
    params.set("summaryclass", "petra");
    params.set("rankprofile", "default");

    QueryBuilder<SimpleQueryNodeTypes> builder;
    builder.addStringTerm("maptest", "sddocname", 0, Weight(0));
    Node::UP node = builder.build();
    vespalib::string stackDump = StackDumpCreator::create(*node);

    params.set("query", stackDump);
    testCreateSearchVisitor("dir:" + TEST_PATH("cfg"), params);
}

void
SearchVisitorTest::testOnlyRequireWeakReadConsistency()
{
    SearchVisitorFactory factory("dir:" + TEST_PATH("cfg"));
    VisitorFactory& factoryBase(factory);
    vdslib::Parameters params;
    std::unique_ptr<Visitor> sv(
            factoryBase.makeVisitor(*_component, _env, params));
    EXPECT_TRUE(sv->getRequiredReadConsistency() == spi::ReadConsistency::WEAK);
}

int
SearchVisitorTest::Main()
{
    TEST_INIT("searchvisitor_test");

    testSearchVisitor(); TEST_FLUSH();
    testSearchEnvironment(); TEST_FLUSH();
    testOnlyRequireWeakReadConsistency(); TEST_FLUSH();

    TEST_DONE();
}

} // namespace storage

TEST_APPHOOK(storage::SearchVisitorTest)