blob: 2ad62f6bbca8f06d0cc78c3477e06a61f2b1d63f (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "document_type_repo_factory.h"
#include "documenttyperepo.h"
#include <vespa/document/config/config-documenttypes.h>
#include <iostream>
#include <vespa/log/log.h>
LOG_SETUP(".document.repo.document_type_repo_factory");
namespace document {
std::mutex DocumentTypeRepoFactory::_mutex;
DocumentTypeRepoFactory::DocumentTypeRepoMap DocumentTypeRepoFactory::_repos;
namespace {
class EmptyFactoryCheck
{
public:
~EmptyFactoryCheck();
};
EmptyFactoryCheck::~EmptyFactoryCheck()
{
if (!DocumentTypeRepoFactory::empty()) {
LOG_ABORT("DocumentTypeRepoFactory not empty at shutdown");
}
}
EmptyFactoryCheck emptyFactoryCheck;
}
DocumentTypeRepoFactory::DocumentTypeRepoEntry::~DocumentTypeRepoEntry() = default;
/*
* Class handling deletion of document type repo after last reference is gone.
*/
class DocumentTypeRepoFactory::Deleter
{
public:
void operator()(DocumentTypeRepo *repoRawPtr) const noexcept {
deleteRepo(repoRawPtr);
}
};
void
DocumentTypeRepoFactory::deleteRepo(DocumentTypeRepo *repoRawPtr) noexcept
{
std::unique_ptr<const DocumentTypeRepo> repo(repoRawPtr);
std::lock_guard guard(_mutex);
_repos.erase(repo.get());
}
std::shared_ptr<const DocumentTypeRepo>
DocumentTypeRepoFactory::make(const DocumenttypesConfig &config)
{
std::lock_guard guard(_mutex);
// Return existing instance if config matches
for (const auto &entry : _repos) {
const auto repo = entry.second.repo.lock();
const auto &repoConfig = *entry.second.config;
if (repo && repoConfig == config) {
return repo;
}
}
auto repoConfig = std::make_unique<const DocumenttypesConfig>(config);
auto repoup = std::make_unique<DocumentTypeRepo>(*repoConfig);
auto repo = std::shared_ptr<const DocumentTypeRepo>(repoup.release(), Deleter());
_repos.emplace(repo.get(), DocumentTypeRepoEntry(repo, std::move(repoConfig)));
return repo;
}
bool
DocumentTypeRepoFactory::empty()
{
std::lock_guard guard(_mutex);
return _repos.empty();
}
}
|