aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/blueprint.cpp
blob: 6589cb9446ab2763f363a796196cc1538061e52e (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "blueprint.h"
#include "parametervalidator.h"
#include <cassert>
#include <vespa/vespalib/util/stringfmt.h>

namespace search::fef {

std::optional<FeatureType>
Blueprint::defineInput(vespalib::stringref inName, AcceptInput accept)
{
    assert(_dependency_handler != nullptr);
    return _dependency_handler->resolve_input(inName, accept);
}

void
Blueprint::describeOutput(vespalib::stringref outName,
                          vespalib::stringref desc,
                          FeatureType type)
{
    (void) desc;
    assert(_dependency_handler != nullptr);
    _dependency_handler->define_output(outName, std::move(type));
}

bool
Blueprint::fail(const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    vespalib::string msg = vespalib::make_string_va(format, ap);
    va_end(ap);
    assert(_dependency_handler != nullptr);
    _dependency_handler->fail(msg);
    return false;
}

Blueprint::Blueprint(vespalib::stringref baseName)
    : _baseName(baseName),
      _name(),
      _dependency_handler(nullptr)
{
}

Blueprint::~Blueprint() = default;

ParameterDescriptions
Blueprint::getDescriptions() const
{
    // desc: 0-n parameters
    return ParameterDescriptions().desc().string().repeat();
}

bool
Blueprint::setup(const IIndexEnvironment &indexEnv,
                 const StringVector &params)
{
    ParameterDescriptions descs = getDescriptions();
    ParameterValidator validator(indexEnv, params, descs);
    ParameterValidator::Result result = validator.validate();
    if (result.valid()) {
        return setup(indexEnv, result.getParameters());
    } else {
        return fail("The parameter list used for setting up rank feature %s is not valid: %s",
                    getBaseName().c_str(), result.getError().c_str());
    }
}

bool
Blueprint::setup(const IIndexEnvironment &indexEnv, const ParameterList &params)
{
    (void) indexEnv; (void) params;
    return fail("The setup function using a typed parameter list does not have a default implementation. "
                "Make sure the setup function is implemented in the rank feature %s.", getBaseName().c_str());
}

void
Blueprint::prepareSharedState(const IQueryEnvironment & queryEnv, IObjectStore & objectStore) const {
    (void) queryEnv; (void) objectStore;
}

using IAttributeVectorWrapper = AnyWrapper<const attribute::IAttributeVector *>;

const attribute::IAttributeVector *
Blueprint::lookupAndStoreAttribute(const vespalib::string & key, vespalib::stringref attrName,
                                   const IQueryEnvironment & env, IObjectStore & store)
{
    const Anything * obj = store.get(key);
    if (obj == nullptr) {
        const IAttributeVector * attribute = env.getAttributeContext().getAttribute(attrName);
        store.add(key, std::make_unique<IAttributeVectorWrapper>(attribute));
        return attribute;
    }
    return IAttributeVectorWrapper::getValue(*obj);
}

const attribute::IAttributeVector *
Blueprint::lookupAttribute(const vespalib::string & key, vespalib::stringref attrName, const IQueryEnvironment & env)
{
    const Anything * attributeArg = env.getObjectStore().get(key);
    const IAttributeVector * attribute = (attributeArg != nullptr)
                                       ? IAttributeVectorWrapper::getValue(*attributeArg)
                                       : nullptr;
    if (attribute == nullptr) {
        attribute = env.getAttributeContext().getAttribute(attrName);
    }
    return attribute;;
}

vespalib::string
Blueprint::createAttributeKey(vespalib::stringref attrName) {
    return "fef.attribute.key." + attrName;
}

}