summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/metrics/name_collection.cpp
blob: 697d41c4c6baa61fd6b6e487446fe980467d08d4 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "name_collection.h"
#include <assert.h>

namespace vespalib {
namespace metrics {

using Guard = std::lock_guard<std::mutex>;

const vespalib::string &
NameCollection::lookup(size_t id) const
{
    Guard guard(_lock);
    assert(id < _names_by_id.size());
    return _names_by_id[id]->first;
}

size_t
NameCollection::resolve(const vespalib::string& name)
{
    Guard guard(_lock);
    size_t nextId = _names_by_id.size();
    auto iter_check = _names.emplace(name, nextId);
    if (iter_check.second) {
        _names_by_id.push_back(iter_check.first);
    }
    return iter_check.first->second;
}

size_t
NameCollection::size() const
{
    Guard guard(_lock);
    return _names_by_id.size();
}

} // namespace vespalib::metrics
} // namespace vespalib