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

#include "document_db_reference_registry.h"

namespace proton {

DocumentDBReferenceRegistry::DocumentDBReferenceRegistry()
    : _lock(),
      _cv(),
      _handlers()
{
}

DocumentDBReferenceRegistry::~DocumentDBReferenceRegistry()
{
}

std::shared_ptr<IDocumentDBReference>
DocumentDBReferenceRegistry::get(vespalib::stringref name) const
{
    std::unique_lock<std::mutex> guard(_lock);
    auto itr = _handlers.find(name);
    while (itr == _handlers.end()) {
        _cv.wait(guard);
        itr = _handlers.find(name);
    }
    return itr->second;
}

std::shared_ptr<IDocumentDBReference>
DocumentDBReferenceRegistry::tryGet(vespalib::stringref name) const
{
    std::lock_guard<std::mutex> guard(_lock);
    auto itr = _handlers.find(name);
    if (itr == _handlers.end()) {
        return std::shared_ptr<IDocumentDBReference>();
    } else {
        return itr->second;
    }
}

void
DocumentDBReferenceRegistry::add(vespalib::stringref name, std::shared_ptr<IDocumentDBReference> referee)
{
    std::lock_guard<std::mutex> guard(_lock);
    _handlers[name] = referee;
    _cv.notify_all();
}

void
DocumentDBReferenceRegistry::remove(vespalib::stringref name)
{
    std::lock_guard<std::mutex> guard(_lock);
    _handlers.erase(name);
}

} // namespace proton