aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/attribute/imported_attributes_context.h
blob: aefffd959defa6e705a9882f278a50a0ecc4bdd2 (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
// 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/searchcommon/attribute/i_document_meta_store_context.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 MetaStoreReadGuard = search::IDocumentMetaStoreContext::IReadGuard;

    using AttributeCache = vespalib::hash_map<vespalib::string, std::unique_ptr<AttributeReadGuard>>;
    using MetaStoreCache = std::unordered_map<const void *, std::shared_ptr<MetaStoreReadGuard>>;
    using LockGuard = std::lock_guard<std::mutex>;

    const ImportedAttributesRepo &_repo;
    bool                          _mtSafe;
    mutable AttributeCache        _guardedAttributes;
    mutable AttributeCache        _enumGuardedAttributes;
    mutable MetaStoreCache        _metaStores;
    mutable std::mutex            _cacheMutex;

    const IAttributeVector *getOrCacheAttribute(const vespalib::string &name, AttributeCache &attributes, bool stableEnumGuard) const;
    const IAttributeVector *getOrCacheAttributeMtSafe(const vespalib::string &name, AttributeCache &attributes, bool stableEnumGuard) 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 enableMultiThreadSafe() override { _mtSafe = true; }

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

}