summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/index/index_writer/index_writer_test.cpp
blob: 9776923abb05395a8f3c2e008fce3ecaf1d68582 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("index_writer_test");
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/searchcore/proton/index/index_writer.h>
#include <vespa/searchcore/proton/test/mock_index_manager.h>
#include <vespa/searchlib/index/docbuilder.h>

using namespace proton;
using namespace search;
using namespace search::index;
using namespace searchcorespi;

using document::Document;

std::string
toString(const std::vector<SerialNum> &vec)
{
    std::ostringstream oss;
    for (size_t i = 0; i < vec.size(); ++i) {
        if (i > 0) oss << ",";
        oss << vec[i];
    }
    return oss.str();
}

struct MyIndexManager : public test::MockIndexManager
{
    typedef std::map<uint32_t, std::vector<SerialNum> > LidMap;
    LidMap puts;
    LidMap removes;
    SerialNum current;
    SerialNum flushed;
    SerialNum commitSerial;
    MyIndexManager() : puts(), removes(), current(0), flushed(0),
                       commitSerial(0)
    {
    }
    std::string getPut(uint32_t lid) {
        return toString(puts[lid]);
    }
    std::string getRemove(uint32_t lid) {
        return toString(removes[lid]);
    }
    // Implements IIndexManager
    virtual void putDocument(uint32_t lid, const Document &,
                             SerialNum serialNum) override {
        puts[lid].push_back(serialNum);
    }
    virtual void removeDocument(uint32_t lid,
                                SerialNum serialNum) override {
        removes[lid].push_back(serialNum);
    }
    virtual void commit(SerialNum serialNum,
                        OnWriteDoneType) override {
        commitSerial = serialNum;
    }
    virtual SerialNum getCurrentSerialNum() const override {
        return current;
    }
    virtual SerialNum getFlushedSerialNum() const override {
        return flushed;
    }
};

struct Fixture
{
    IIndexManager::SP iim;
    MyIndexManager   &mim;
    IndexWriter      iw;
    Schema            schema;
    DocBuilder        builder;
    Document::UP      dummyDoc;
    Fixture()
        : iim(new MyIndexManager()),
          mim(static_cast<MyIndexManager &>(*iim)),
          iw(iim),
          schema(),
          builder(schema),
          dummyDoc(createDoc(1234)) // This content of this is not used
    {
    }
    Document::UP createDoc(uint32_t lid) {
        builder.startDocument(vespalib::make_string("doc:test:%u", lid));
        return builder.endDocument();
    }
    void put(SerialNum serialNum, const search::DocumentIdT lid) {
        iw.put(serialNum, *dummyDoc, lid);
        iw.commit(serialNum, std::shared_ptr<IDestructorCallback>());
    }
    void remove(SerialNum serialNum, const search::DocumentIdT lid) {
        iw.remove(serialNum, lid);
        iw.commit(serialNum, std::shared_ptr<IDestructorCallback>());
    }
};

TEST_F("require that index adapter ignores old operations", Fixture)
{
    f.mim.flushed = 10;
    f.put(8, 1);
    f.remove(9, 2);
    EXPECT_EQUAL("", f.mim.getPut(1));
    EXPECT_EQUAL("", f.mim.getRemove(2));
}

TEST_F("require that commit is forwarded to index manager", Fixture)
{
    f.iw.commit(10, std::shared_ptr<IDestructorCallback>());
    EXPECT_EQUAL(10u, f.mim.commitSerial);
}

TEST_MAIN()
{
    TEST_RUN_ALL();
}