summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/attribute/imported_attributes_context.cpp
blob: 63ae28444d3719bf68a2c3efea17604747ea9ab9 (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
// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include "imported_attributes_context.h"
#include "imported_attributes_repo.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 {

ImportedAttributesContext::GuardedAttribute::GuardedAttribute(ImportedAttributeVector::SP attr,
                                                              bool stableEnumGuard)
    : _attr(std::move(attr)),
      _guard(stableEnumGuard ? _attr->acquireEnumGuard() : _attr->acquireGuard())
{
}

ImportedAttributesContext::GuardedAttribute::~GuardedAttribute()
{
}

const IAttributeVector *
ImportedAttributesContext::GuardedAttribute::get() const
{
    return _attr.get();
}

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.get();
    }
    ImportedAttributeVector::SP result = _repo.get(name);
    if (result.get() != nullptr) {
        attributes.emplace(name, GuardedAttribute(result, stableEnumGuard));
        return result.get();
    } else {
        return nullptr;
    }
}

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

ImportedAttributesContext::~ImportedAttributesContext()
{
}

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();
}
    
}