summaryrefslogtreecommitdiffstats
path: root/slobrok/src/vespa/slobrok/server/visible_map.cpp
blob: a9257dbb391d9074151e9e75b43065fb4e4e0f22 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "visible_map.h"

#include <vespa/log/log.h>
LOG_SETUP(".vismap");

namespace slobrok {

void
VisibleMap::updated()
{
    _genCnt.add();
    WaitList waitList;
    std::swap(waitList, _waitList);
    for (auto & entry : waitList) {
        entry->updated(*this);
    }
}

void
VisibleMap::aborted()
{
    WaitList waitList;
    std::swap(waitList, _waitList);
    for (auto & entry : waitList) {
        entry->aborted(*this);
    }
}

void
VisibleMap::addUpdateListener(IUpdateListener *l)
{
    _waitList.push_back(l);
}


void
VisibleMap::removeUpdateListener(IUpdateListener *l)
{
    uint32_t i = 0;
    uint32_t size = _waitList.size();
    while (i < size) {
        if (_waitList[i] == l) {
            std::swap(_waitList[i], _waitList[size - 1]);
            _waitList.pop_back();
            --size;
        } else {
            ++i;
        }
    }
    LOG_ASSERT(size == _waitList.size());
}

//-----------------------------------------------------------------------------

NamedService *
VisibleMap::lookup(const std::string &name) const {
    auto found = _map.find(name);
    return (found == _map.end()) ? nullptr : found->second;
}

std::vector<const NamedService *>
VisibleMap::allVisible() const
{
    std::vector<const NamedService *> retval;
    // get list of all names in myrpcsrvmap
    for (const auto & entry : _map) {
        retval.push_back(entry.second);
    }
    return retval;
}



void
VisibleMap::addNew(NamedService *rpcsrv)
{
    LOG_ASSERT(rpcsrv != nullptr);
    LOG_ASSERT(_map.find(rpcsrv->getName()) == _map.end());
    _map[rpcsrv->getName()] = rpcsrv;

    _history.add(rpcsrv->getName(), _genCnt);
    updated();
}


NamedService *
VisibleMap::remove(const std::string &name) {

    NamedService *d = _map[name];
    _map.erase(name);
    if (d != nullptr) {
        _history.add(name, _genCnt);
        updated();
    }
    return d;
}


NamedService *
VisibleMap::update(NamedService *rpcsrv) {
    LOG_ASSERT(rpcsrv != nullptr);

    NamedService *d = rpcsrv;
    std::swap(d, _map[rpcsrv->getName()]);
    LOG_ASSERT(d != nullptr);

    _history.add(rpcsrv->getName(), _genCnt);
    updated();

    return d;
}

VisibleMap::MapDiff
VisibleMap::history(const vespalib::GenCnt& gen) const
{
    MapDiff retval;
    std::set<std::string> names = _history.since(gen);
    for (const auto & name : names)
    {
        const NamedService *val = lookup(name.c_str());
        if (val == nullptr) {
            retval.removed.push_back(name);
        } else {
            retval.updated.push_back(val);
        }
    }
    return retval;
}

VisibleMap::MapDiff::MapDiff() = default;
VisibleMap::MapDiff::~MapDiff() = default;

VisibleMap::VisibleMap()
    : _map(),
      _waitList(),
      _genCnt(1)
{
}
VisibleMap::~VisibleMap()
{
    aborted();
}


bool
VisibleMap::match(const char *name, const char *pattern)
{
    LOG_ASSERT(name != nullptr);
    LOG_ASSERT(pattern != nullptr);
    while (*pattern != '\0') {
        if (*name == *pattern) {
            ++name;
            ++pattern;
        } else if (*pattern == '*') {
            ++pattern;
            while (*name != '/' && *name != '\0') {
                ++name;
            }
        } else {
            return false;
        }
    }
    return (*name == *pattern);
}

//-----------------------------------------------------------------------------

} // namespace slobrok