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

#include "mock_attribute_context.h"
#include <vespa/searchcommon/attribute/iattributecontext.h>
#include <vespa/searchlib/attribute/attribute_read_guard.h>
#include <memory>
#include <map>

#pragma once

namespace search::fef::test {

/**
 * Simple mapping from attribute name to IAttributeVector which can be used for tests
 * that do do not want the complexity of instantiating a full AttributeManager, or
 * for tests that need to work with IAttributeVectors rather than AttributeVectors.
 *
 * Allows for creating AttributeContext instances which transparently access the
 * attribute map for their lookups.
 */
class AttributeMap {
    std::map<vespalib::string, std::shared_ptr<attribute::IAttributeVector>> _attributes;
    std::map<vespalib::string, std::unique_ptr<attribute::AttributeReadGuard>> _guards;
public:
    using IAttributeVector = attribute::IAttributeVector;
    ~AttributeMap();

    void add(std::shared_ptr<attribute::IAttributeVector> attr) {
        _attributes.emplace(attr->getName(), std::move(attr));
    }

    void add(std::unique_ptr<attribute::AttributeReadGuard> guard) {
        _guards.emplace(guard->attribute()->getName(), std::move(guard));
    }

    const IAttributeVector * getAttribute(const vespalib::string & name) const {
        auto attrItr = _attributes.find(name);
        if (attrItr != _attributes.end()) {
            return attrItr->second.get();
        }
        auto guardItr = _guards.find(name);
        if (guardItr != _guards.end()) {
            return guardItr->second->attribute();
        }
        return nullptr;
    }

    void getAttributeList(std::vector<const IAttributeVector *> & list) const {
        for (const auto& attr : _attributes) {
            list.emplace_back(attr.second.get());
        }
        for (const auto &guard : _guards) {
            list.emplace_back(guard.second->attribute());
        }
    }

    attribute::IAttributeContext::UP createContext() const {
        return std::make_unique<MockAttributeContext>(*this);
    }
};

}