aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/memoryindex/invert_context.cpp
blob: 262994222a3c13bf35f1794efdca201b859c99f6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "invert_context.h"
#include "document_inverter_context.h"
#include <vespa/document/datatype/documenttype.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/searchlib/index/schema_index_fields.h>

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

namespace search::memoryindex {

using document::Document;
using document::DocumentType;
using document::Field;

namespace {

std::unique_ptr<document::Field>
get_field(const DocumentType& doc_type, const vespalib::string& name)
{
    std::unique_ptr<Field> fp;
    if ( ! doc_type.hasField(name)) {
        LOG(error,
            "Mismatch between documentdefinition and schema. "
            "No field named '%s' from schema in document type '%s'",
            name.c_str(),
            doc_type.getName().c_str());
    } else {
        fp = std::make_unique<Field>(doc_type.getField(name));
    }
    return fp;
}

}


InvertContext::InvertContext(vespalib::ISequencedTaskExecutor::ExecutorId id)
    : BundledFieldsContext(id),
      _pushers(),
      _document_fields(),
      _document_uri_fields(),
      _data_type(nullptr)
{
}

InvertContext::~InvertContext() = default;

InvertContext::InvertContext(InvertContext&&) = default;

void
InvertContext::add_pusher(uint32_t pusher_id)
{
    _pushers.emplace_back(pusher_id);
}

void
InvertContext::set_data_type(const DocumentInverterContext &doc_inv_context, const Document& doc) const
{
    auto data_type(doc.getDataType());
    if (_data_type == data_type) {
        return;
    }
    auto& doc_type(doc.getType());
    _document_fields.clear();
    auto& schema = doc_inv_context.get_schema();
    for (auto field_id : get_fields()) {
        auto& name = schema.getIndexField(field_id).getName();
        _document_fields.emplace_back(get_field(doc_type, name));
    }
    _document_uri_fields.clear();
    auto& schema_index_fields = doc_inv_context.get_schema_index_fields();
    for (auto uri_field_id : get_uri_fields()) {
        auto& name = schema.getIndexField(schema_index_fields._uriFields[uri_field_id]._all).getName();
        _document_uri_fields.emplace_back(get_field(doc_type, name));
    }
    _data_type = data_type;
}

}