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

#include <vespa/searchcommon/attribute/iattributecontext.h>
#include <vespa/vespalib/stllike/hash_fun.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include <mutex>
#include <unordered_map>

namespace search { class AttributeGuard; }
namespace search::attribute {
    class AttributeReadGuard;
    class IAttributeVector;
    class ImportedAttributeVector;
}

namespace proton {

class ImportedAttributesRepo;

/**
 * Short lived context class that gives access to all imported attributes in a given repo.
 *
 * Attribute guards and enum guards are cached in this class and released upon destruction.
 */
class ImportedAttributesContext : public search::attribute::IAttributeContext {
private:
    using AttributeGuard = search::AttributeGuard;
    using AttributeReadGuard = search::attribute::AttributeReadGuard;
    using IAttributeVector = search::attribute::IAttributeVector;
    using ImportedAttributeVector = search::attribute::ImportedAttributeVector;
    using IAttributeFunctor = search::attribute::IAttributeFunctor;

    using AttributeCache = std::unordered_map<vespalib::string, std::unique_ptr<AttributeReadGuard>, vespalib::hash<vespalib::string>>;
    using LockGuard = std::lock_guard<std::mutex>;

    const ImportedAttributesRepo &_repo;
    mutable AttributeCache _guardedAttributes;
    mutable AttributeCache _enumGuardedAttributes;
    mutable std::mutex _cacheMutex;

    const IAttributeVector *getOrCacheAttribute(const vespalib::string &name, AttributeCache &attributes,
                                                bool stableEnumGuard, const LockGuard &) const;

public:
    ImportedAttributesContext(const ImportedAttributesRepo &repo);
    ~ImportedAttributesContext() override;

    // Implements search::attribute::IAttributeContext
    const IAttributeVector *getAttribute(const vespalib::string &name) const override;
    const IAttributeVector *getAttributeStableEnum(const vespalib::string &name) const override;
    void getAttributeList(std::vector<const IAttributeVector *> &list) const override;
    void releaseEnumGuards() override;

    void asyncForAttribute(const vespalib::string &name, std::unique_ptr<IAttributeFunctor> func) const override;
};

}