summaryrefslogtreecommitdiffstats
path: root/searchcorespi/src/vespa/searchcorespi/plugin/factoryregistry.cpp
blob: 683b1e420b549c73d7f016b51beac78400fb81eb (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 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/searchcorespi/plugin/factoryregistry.h>
#include <vespa/vespalib/util/exceptions.h>

using vespalib::LockGuard;
using vespalib::IllegalArgumentException;
using vespalib::stringref;
using vespalib::string;

namespace searchcorespi {

FactoryRegistry::FactoryRegistry()
{
}

FactoryRegistry::~FactoryRegistry()
{
}

void FactoryRegistry::add(const stringref & uniqueName, const IIndexManagerFactory::SP & factory)
{
    LockGuard guard(_lock);
    if (_registry.find(uniqueName) == _registry.end()) {
        _registry[uniqueName] = factory;
    } else {
        throw IllegalArgumentException("A factory is already registered with the same name as '" + uniqueName + "'.", VESPA_STRLOC);
    }
}

void FactoryRegistry::remove(const stringref & uniqueName)
{
    LockGuard guard(_lock);
    if (_registry.find(uniqueName) == _registry.end()) {
        throw IllegalArgumentException("No factory is registered with the name of '" + uniqueName + "'.", VESPA_STRLOC);
    }
    _registry.erase(uniqueName);
}

const IIndexManagerFactory::SP &
FactoryRegistry::get(const stringref & uniqueName) const
{
    LockGuard guard(_lock);
    Registry::const_iterator found = _registry.find(uniqueName);
    if (found == _registry.end()) {
        throw IllegalArgumentException("No factory is registered with the name of '" + uniqueName + "'.", VESPA_STRLOC);
    }
    return found->second;
}

bool
FactoryRegistry::isRegistered(const vespalib::stringref & uniqueName) const
{
    LockGuard guard(_lock);
    Registry::const_iterator found = _registry.find(uniqueName);
    return found != _registry.end();
}

}