aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/test/indexenvironment.h
blob: 35b5b607ca33a55e02a7ef6e47f8bb37eff855f4 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "attribute_map.h"
#include <vespa/searchlib/attribute/attributemanager.h>
#include <vespa/searchlib/fef/iindexenvironment.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/searchlib/fef/onnx_model.h>
#include <vespa/searchlib/fef/fieldinfo.h>
#include <vespa/searchlib/fef/tablemanager.h>
#include <vespa/eval/eval/value_cache/constant_value.h>
#include <string>
#include <vector>

namespace search::fef::test {

/**
 * Implementation of the IIndexEnvironment interface used for testing.
 */
class IndexEnvironment : public IIndexEnvironment
{
public:
    struct Constant : vespalib::eval::ConstantValue {
        vespalib::eval::ValueType _type;
        std::unique_ptr<vespalib::eval::Value> _value;
        Constant(vespalib::eval::ValueType type,
                 std::unique_ptr<vespalib::eval::Value> value)
            : _type(std::move(type)), _value(std::move(value))
        { }
        Constant(Constant &&rhs) noexcept
            : _type(std::move(rhs._type)),
              _value(std::move(rhs._value))
        {
        }
        const vespalib::eval::ValueType &type() const override { return _type; }
        const vespalib::eval::Value &value() const override { return *_value; }
        ~Constant() override;
    };

    struct ConstantRef : vespalib::eval::ConstantValue {
        const Constant &_value;
        explicit ConstantRef(const Constant &value)
            : _value(value)
        { }
        const vespalib::eval::ValueType &type() const override { return _value.type(); }
        const vespalib::eval::Value &value() const override { return _value.value(); }
        ~ConstantRef() override = default;
    };

    using ConstantsMap = std::map<vespalib::string, Constant>;
    using ExprMap = std::map<vespalib::string, vespalib::string>;
    using ModelMap = std::map<vespalib::string, OnnxModel>;

    IndexEnvironment();
    IndexEnvironment(const IndexEnvironment &) = delete;
    IndexEnvironment & operator=(const IndexEnvironment &) = delete;
    ~IndexEnvironment() override;

    const Properties &getProperties() const override { return _properties; }
    uint32_t getNumFields() const override { return _fields.size(); }
    const FieldInfo *getField(uint32_t id) const override;
    const FieldInfo *getFieldByName(const string &name) const override;
    const ITableManager &getTableManager() const override { return _tableMan; }
    FeatureMotivation getFeatureMotivation() const override { return UNKNOWN; }
    void hintFeatureMotivation(FeatureMotivation) const override {}
    uint32_t getDistributionKey() const override { return 3; }

    /** Returns a reference to the properties map of this. */
    Properties &getProperties() { return _properties; }

    /** Returns a reference to the list of fields of this. */
    std::vector<FieldInfo> &getFields() { return _fields; }

    /** Returns a const reference to the list of fields of this. */
    const std::vector<FieldInfo> &getFields() const { return _fields; }

    /** Returns a reference to the attribute map of this. */
    AttributeMap &getAttributeMap() { return _attrMap; }

    /** Returns a reference to the table manager of this. */
    TableManager &getTableManager() { return _tableMan; }

    vespalib::eval::ConstantValue::UP getConstantValue(const vespalib::string &name) const override;

    void addConstantValue(const vespalib::string &name,
                          vespalib::eval::ValueType type,
                          std::unique_ptr<vespalib::eval::Value> value);

    vespalib::string getRankingExpression(const vespalib::string &name) const override;
    void addRankingExpression(const vespalib::string &name, const vespalib::string &value);

    const OnnxModel *getOnnxModel(const vespalib::string &name) const override;
    void addOnnxModel(OnnxModel model);

private:
    Properties             _properties;
    std::vector<FieldInfo> _fields;
    AttributeMap           _attrMap;
    TableManager           _tableMan;
    ConstantsMap           _constants;
    ExprMap                _expressions;
    ModelMap               _models;
};

}