summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/memoryindex/document_remover/document_remover_test.cpp
blob: 4938d40a21fdcad6b861c2dca82142659ab12c94 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/log/log.h>
LOG_SETUP("document_remover_test");
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/searchlib/memoryindex/document_remover.h>
#include <vespa/searchlib/memoryindex/wordstore.h>
#include <vespa/searchlib/memoryindex/i_document_remove_listener.h>
#include <vespa/vespalib/test/insertion_operators.h>
#include <map>

using namespace search;
using namespace search::memoryindex;

struct WordFieldPair
{
    vespalib::string _word;
    uint32_t _fieldId;
    WordFieldPair(const vespalib::stringref &word, uint32_t fieldId)
        : _word(word), _fieldId(fieldId)
    {}
    bool operator<(const WordFieldPair &rhs) {
        if (_word != rhs._word) {
            return _word < rhs._word;
        }
        return _fieldId < rhs._fieldId;
    }
};

typedef std::vector<WordFieldPair> WordFieldVector;

std::ostream &
operator<<(std::ostream &os, const WordFieldPair &val)
{
    os << "{" << val._word << "," << val._fieldId << "}";
    return os;
}

struct MockRemoveListener : public IDocumentRemoveListener
{
    WordFieldVector _words;
    uint32_t _expDocId;
    uint32_t _fieldId;
    virtual void remove(const vespalib::stringref word, uint32_t docId) override {
        EXPECT_EQUAL(_expDocId, docId);
        _words.emplace_back(word, _fieldId);
    }
    void reset(uint32_t expDocId) {
        _words.clear();
        _expDocId = expDocId;
    }
    vespalib::string getWords() {
        std::sort(_words.begin(), _words.end());
        std::ostringstream oss;
        oss << _words;
        return oss.str();
    }
    void setFieldId(uint32_t fieldId) { _fieldId = fieldId; }
};

struct Fixture
{
    MockRemoveListener _listener;
    std::vector<std::unique_ptr<WordStore>> _wordStores;
    std::vector<std::map<vespalib::string, datastore::EntryRef>> _wordToRefMaps;
    std::vector<std::unique_ptr<DocumentRemover>> _removers;
    Fixture()
        : _listener(),
          _wordStores(),
          _wordToRefMaps(),
          _removers()
    {
        uint32_t numFields = 4;
        for (uint32_t fieldId = 0; fieldId < numFields; ++fieldId) {
            _wordStores.push_back(std::make_unique<WordStore>());
            _removers.push_back(std::make_unique<DocumentRemover>
                                (*_wordStores.back()));
        }
        _wordToRefMaps.resize(numFields);
    }
    datastore::EntryRef getWordRef(const vespalib::string &word, uint32_t fieldId) {
        auto &wordToRefMap = _wordToRefMaps[fieldId];
        WordStore &wordStore = *_wordStores[fieldId];
        auto itr = wordToRefMap.find(word);
        if (itr == wordToRefMap.end()) {
            datastore::EntryRef ref = wordStore.addWord(word);
            wordToRefMap[word] = ref;
            return ref;
        }
        return itr->second;
    }
    Fixture &insert(const vespalib::string &word, uint32_t fieldId, uint32_t docId) {
        assert(fieldId < _wordStores.size());
        _removers[fieldId]->insert(getWordRef(word, fieldId), docId);
        return *this;
    }
    void flush() {
        for (auto &remover : _removers) {
            remover->flush();
        }
    }
    vespalib::string remove(uint32_t docId) {
        _listener.reset(docId);
        uint32_t fieldId = 0;
        for (auto &remover : _removers) {
            _listener.setFieldId(fieldId);
            remover->remove(docId, _listener);
            ++fieldId;
        }
        return _listener.getWords();
    }
};

TEST_F("require that {word,fieldId} pairs for multiple doc ids can be inserted", Fixture)
{
    f.insert("a", 1, 10).insert("a", 1, 20).insert("a", 1, 30);
    f.insert("a", 2, 10).insert("a", 2, 20);
    f.insert("b", 1, 20).insert("b", 1, 30);
    f.insert("b", 2, 10).insert("b", 2, 30);
    f.insert("c", 1, 10);
    f.insert("c", 2, 20);
    f.insert("c", 3, 30);
    f.flush();

    EXPECT_EQUAL("[{a,1},{a,2},{b,2},{c,1}]", f.remove(10));
    EXPECT_EQUAL("[{a,1},{a,2},{b,1},{c,2}]", f.remove(20));
    EXPECT_EQUAL("[{a,1},{b,1},{b,2},{c,3}]", f.remove(30));
}

TEST_F("require that we can insert after flush", Fixture)
{
    f.insert("a", 1, 10).insert("b", 1, 10);
    f.flush();
    f.insert("b", 1, 20).insert("b", 2, 20);
    f.flush();

    EXPECT_EQUAL("[{a,1},{b,1}]", f.remove(10));
    EXPECT_EQUAL("[{b,1},{b,2}]", f.remove(20));
}


TEST_MAIN() { TEST_RUN_ALL(); }