summaryrefslogtreecommitdiffstats
path: root/searchcorespi/src/vespa/searchcorespi/index/indexcollection.cpp
blob: 5de0a8cb69dcf5a5809d94c3e92d8acb7f2bb7f8 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "indexcollection.h"
#include "indexsearchablevisitor.h"
#include <vespa/searchlib/queryeval/isourceselector.h>
#include <vespa/searchlib/queryeval/create_blueprint_visitor_helper.h>
#include <vespa/searchlib/queryeval/intermediate_blueprints.h>
#include <vespa/searchlib/queryeval/leaf_blueprints.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchcorespi.index.indexcollection");

using namespace search::queryeval;
using namespace search::query;
using search::attribute::IAttributeContext;
using search::index::FieldLengthInfo;

namespace searchcorespi {

IndexCollection::IndexCollection(const ISourceSelector::SP & selector)
    : _source_selector(selector),
      _sources()
{
}

IndexCollection::IndexCollection(const ISourceSelector::SP & selector,
                                 const ISearchableIndexCollection &sources)
    : _source_selector(selector),
      _sources()
{
    for (size_t i(0), m(sources.getSourceCount()); i < m; i++) {
        append(sources.getSourceId(i), sources.getSearchableSP(i));
    }
    setCurrentIndex(sources.getCurrentIndex());
}

IndexCollection::~IndexCollection() = default;

void
IndexCollection::setSource(uint32_t docId)
{
    assert( valid() );
    _source_selector->setSource(docId, getCurrentIndex());
}

ISearchableIndexCollection::UP
IndexCollection::replaceAndRenumber(const ISourceSelector::SP & selector,
                                    const ISearchableIndexCollection &fsc,
                                    uint32_t id_diff,
                                    const IndexSearchable::SP &new_source)
{
    auto new_fsc = std::make_unique<IndexCollection>(selector);
    new_fsc->append(0, new_source);
    for (size_t i = 0; i < fsc.getSourceCount(); ++i) {
        if (fsc.getSourceId(i) > id_diff) {
            new_fsc->append(fsc.getSourceId(i) - id_diff, fsc.getSearchableSP(i));
        }
    }
    return new_fsc;
}

void
IndexCollection::append(uint32_t id, const IndexSearchable::SP &fs)
{
    _sources.push_back(SourceWithId(id, fs));
}

IndexSearchable::SP
IndexCollection::getSearchableSP(uint32_t i) const
{
    return _sources[i].source_wrapper;
}

void
IndexCollection::replace(uint32_t id, const IndexSearchable::SP &fs)
{
    for (size_t i = 0; i < _sources.size(); ++i) {
        if (_sources[i].id == id) {
            _sources[i].source_wrapper = fs;
            return;
        }
    }
    LOG(warning, "Tried to replace Searchable %d, but it wasn't there.", id);
    append(id, fs);
}

const ISourceSelector &
IndexCollection::getSourceSelector() const
{
    return *_source_selector;
}

size_t
IndexCollection::getSourceCount() const
{
    return _sources.size();
}

IndexSearchable &
IndexCollection::getSearchable(uint32_t i) const
{
    return *_sources[i].source_wrapper;
}

uint32_t
IndexCollection::getSourceId(uint32_t i) const
{
    return _sources[i].id;
}

search::SearchableStats
IndexCollection::getSearchableStats() const
{
    search::SearchableStats stats;
    for (size_t i = 0; i < _sources.size(); ++i) {
        stats.merge(_sources[i].source_wrapper->getSearchableStats());
    }
    return stats;
}

search::SerialNum
IndexCollection::getSerialNum() const
{
    search::SerialNum serialNum = 0;
    for (auto &source : _sources) {
        serialNum = std::max(serialNum, source.source_wrapper->getSerialNum());
    }
    return serialNum;
}


void
IndexCollection::accept(IndexSearchableVisitor &visitor) const
{
    for (auto &source : _sources) {
        source.source_wrapper->accept(visitor);
    }
}

namespace {

struct Mixer {
    const ISourceSelector                &_selector;
    std::unique_ptr<SourceBlenderBlueprint> _blender;

    Mixer(const ISourceSelector &selector)
        : _selector(selector), _blender() {}

    void addIndex(Blueprint::UP index) {
        if ( ! _blender) {
            _blender = std::make_unique<SourceBlenderBlueprint>(_selector);
        }
        _blender->addChild(std::move(index));
    }

    Blueprint::UP mix() {
        if (_blender) {
            return std::move(_blender);
        }
        return std::make_unique<EmptyBlueprint>();
    }
};

class CreateBlueprintVisitor : public search::query::QueryVisitor {
private:
    const IIndexCollection  &_indexes;
    const FieldSpecList     &_fields;
    const IRequestContext   &_requestContext;
    Blueprint::UP            _result;

    template <typename NodeType>
    void visitTerm(NodeType &n) {
        Mixer mixer(_indexes.getSourceSelector());
        for (size_t i = 0; i < _indexes.getSourceCount(); ++i) {
            Blueprint::UP blueprint = _indexes.getSearchable(i).createBlueprint(_requestContext, _fields, n);
            blueprint->setSourceId(_indexes.getSourceId(i));
            mixer.addIndex(std::move(blueprint));
        }
        _result = mixer.mix();
    }

    void visit(And &)         override { }
    void visit(AndNot &)      override { }
    void visit(Or &)          override { }
    void visit(WeakAnd &)     override { }
    void visit(Equiv &)       override { }
    void visit(Rank &)        override { }
    void visit(Near &)        override { }
    void visit(ONear &)       override { }
    void visit(SameElement &) override { }

    void visit(WeightedSetTerm &n) override { visitTerm(n); }
    void visit(DotProduct &n)      override { visitTerm(n); }
    void visit(WandTerm &n)        override { visitTerm(n); }
    void visit(Phrase &n)          override { visitTerm(n); }
    void visit(NumberTerm &n)      override { visitTerm(n); }
    void visit(LocationTerm &n)    override { visitTerm(n); }
    void visit(PrefixTerm &n)      override { visitTerm(n); }
    void visit(RangeTerm &n)       override { visitTerm(n); }
    void visit(StringTerm &n)      override { visitTerm(n); }
    void visit(SubstringTerm &n)   override { visitTerm(n); }
    void visit(SuffixTerm &n)      override { visitTerm(n); }
    void visit(PredicateQuery &n)  override { visitTerm(n); }
    void visit(RegExpTerm &n)      override { visitTerm(n); }
    void visit(NearestNeighborTerm &n) override { visitTerm(n); }

public:
    CreateBlueprintVisitor(const IIndexCollection &indexes,
                           const FieldSpecList &fields,
                           const IRequestContext & requestContext)
        : _indexes(indexes),
          _fields(fields),
          _requestContext(requestContext),
          _result() {}

    Blueprint::UP getResult() { return std::move(_result); }
};

}

Blueprint::UP
IndexCollection::createBlueprint(const IRequestContext & requestContext,
                                 const FieldSpec &field,
                                 const Node &term)
{
    FieldSpecList fields;
    fields.add(field);
    return createBlueprint(requestContext, fields, term);
}

Blueprint::UP
IndexCollection::createBlueprint(const IRequestContext & requestContext,
                                 const FieldSpecList &fields,
                                 const Node &term)
{
    CreateBlueprintVisitor visitor(*this, fields, requestContext);
    const_cast<Node &>(term).accept(visitor);
    return visitor.getResult();
}

FieldLengthInfo
IndexCollection::get_field_length_info(const vespalib::string& field_name) const
{
    if (_sources.empty()) {
        return FieldLengthInfo();
    }
    return _sources.back().source_wrapper->get_field_length_info(field_name);
}

}  // namespace searchcorespi