summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/attribute/imported_attributes_context.cpp
blob: 0aaa88db859c69e31be5c0952ee90ada98383b14 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "imported_attributes_context.h"
#include "imported_attributes_repo.h"
#include <vespa/searchlib/attribute/attribute_read_guard.h>
#include <vespa/searchlib/attribute/attributeguard.h>
#include <vespa/searchlib/attribute/imported_attribute_vector.h>
#include <vespa/vespalib/stllike/hash_map.hpp>

using search::attribute::IAttributeVector;
using search::attribute::ImportedAttributeVector;
using LockGuard = std::lock_guard<std::mutex>;

namespace proton {

const IAttributeVector *
ImportedAttributesContext::getOrCacheAttribute(const vespalib::string &name, AttributeCache &attributes,
                                               bool stableEnumGuard, const LockGuard &) const
{
    auto itr = attributes.find(name);
    if (itr != attributes.end()) {
        return itr->second->attribute();
    }
    const ImportedAttributeVector::SP & result = _repo.get(name);
    if (result) {
        auto insRes = attributes.emplace(name, result->makeReadGuard(stableEnumGuard));
        return insRes.first->second->attribute();
    } else {
        return nullptr;
    }
}

ImportedAttributesContext::ImportedAttributesContext(const ImportedAttributesRepo &repo)
    : _repo(repo),
      _guardedAttributes(),
      _enumGuardedAttributes(),
      _cacheMutex()
{
}

ImportedAttributesContext::~ImportedAttributesContext() = default;

const IAttributeVector *
ImportedAttributesContext::getAttribute(const vespalib::string &name) const
{
    LockGuard guard(_cacheMutex);
    return getOrCacheAttribute(name, _guardedAttributes, false, guard);
}

const IAttributeVector *
ImportedAttributesContext::getAttributeStableEnum(const vespalib::string &name) const
{
    LockGuard guard(_cacheMutex);
    return getOrCacheAttribute(name, _enumGuardedAttributes, true, guard);
}

void
ImportedAttributesContext::getAttributeList(std::vector<const IAttributeVector *> &list) const
{
    std::vector<ImportedAttributeVector::SP> attributes;
    _repo.getAll(attributes);
    for (const auto &attr : attributes) {
        list.push_back(getAttribute(attr->getName()));
    }
}

void
ImportedAttributesContext::releaseEnumGuards()
{
    LockGuard guard(_cacheMutex);
    _enumGuardedAttributes.clear();
}

void
ImportedAttributesContext::asyncForAttribute(const vespalib::string &, std::unique_ptr<IAttributeFunctor> ) const {
    throw std::runtime_error("proton::ImportedAttributesContext::asyncForAttribute should never be called.");
}

}