Apollo 10.0
自动驾驶开放平台
multi_value_warehouse.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <algorithm>
20#include <utility>
21
22#include "cyber/common/log.h"
23
24namespace apollo {
25namespace cyber {
26namespace service_discovery {
27
28using base::AtomicRWLock;
29using base::ReadLockGuard;
30using base::WriteLockGuard;
31using proto::RoleAttributes;
32
33bool MultiValueWarehouse::Add(uint64_t key, const RolePtr& role,
34 bool ignore_if_exist) {
35 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
36 if (!ignore_if_exist) {
37 if (roles_.find(key) != roles_.end()) {
38 return false;
39 }
40 }
41 std::pair<uint64_t, RolePtr> role_pair(key, role);
42 roles_.insert(role_pair);
43 return true;
44}
45
47 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
48 roles_.clear();
49}
50
52 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
53 return roles_.size();
54}
55
56void MultiValueWarehouse::Remove(uint64_t key) {
57 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
58 roles_.erase(key);
59}
60
61void MultiValueWarehouse::Remove(uint64_t key, const RolePtr& role) {
62 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
63 auto range = roles_.equal_range(key);
64 for (auto it = range.first; it != range.second;) {
65 if (it->second->Match(role->attributes())) {
66 it = roles_.erase(it);
67 } else {
68 ++it;
69 }
70 }
71}
72
74 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
75 for (auto it = roles_.begin(); it != roles_.end();) {
76 auto curr_role = it->second;
77 if (curr_role->Match(target_attr)) {
78 it = roles_.erase(it);
79 } else {
80 ++it;
81 }
82 }
83}
84
85bool MultiValueWarehouse::Search(uint64_t key) {
86 RolePtr role;
87 return Search(key, &role);
88}
89
90bool MultiValueWarehouse::Search(uint64_t key, RolePtr* first_matched_role) {
91 RETURN_VAL_IF_NULL(first_matched_role, false);
92 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
93 auto search = roles_.find(key);
94 if (search == roles_.end()) {
95 return false;
96 }
97 *first_matched_role = search->second;
98 return true;
99}
100
102 RoleAttributes* first_matched_role_attr) {
103 RETURN_VAL_IF_NULL(first_matched_role_attr, false);
104 RolePtr role;
105 if (!Search(key, &role)) {
106 return false;
107 }
108 first_matched_role_attr->CopyFrom(role->attributes());
109 return true;
110}
111
113 std::vector<RolePtr>* matched_roles) {
114 RETURN_VAL_IF_NULL(matched_roles, false);
115 bool find = false;
116 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
117 auto range = roles_.equal_range(key);
118 for_each(range.first, range.second,
119 [&matched_roles, &find](RoleMap::value_type& item) {
120 matched_roles->emplace_back(item.second);
121 find = true;
122 });
123 return find;
124}
125
127 uint64_t key, std::vector<RoleAttributes>* matched_roles_attr) {
128 RETURN_VAL_IF_NULL(matched_roles_attr, false);
129 bool find = false;
130 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
131 auto range = roles_.equal_range(key);
132 for_each(range.first, range.second,
133 [&matched_roles_attr, &find](RoleMap::value_type& item) {
134 matched_roles_attr->emplace_back(item.second->attributes());
135 find = true;
136 });
137 return find;
138}
139
141 RolePtr role;
142 return Search(target_attr, &role);
143}
144
146 RolePtr* first_matched_role) {
147 RETURN_VAL_IF_NULL(first_matched_role, false);
148 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
149 for (auto& item : roles_) {
150 if (item.second->Match(target_attr)) {
151 *first_matched_role = item.second;
152 return true;
153 }
154 }
155 return false;
156}
157
159 RoleAttributes* first_matched_role_attr) {
160 RETURN_VAL_IF_NULL(first_matched_role_attr, false);
161 RolePtr role;
162 if (!Search(target_attr, &role)) {
163 return false;
164 }
165 first_matched_role_attr->CopyFrom(role->attributes());
166 return true;
167}
168
170 std::vector<RolePtr>* matched_roles) {
171 RETURN_VAL_IF_NULL(matched_roles, false);
172 bool find = false;
173 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
174 for (auto& item : roles_) {
175 if (item.second->Match(target_attr)) {
176 matched_roles->emplace_back(item.second);
177 find = true;
178 }
179 }
180 return find;
181}
182
184 const RoleAttributes& target_attr,
185 std::vector<RoleAttributes>* matched_roles_attr) {
186 RETURN_VAL_IF_NULL(matched_roles_attr, false);
187 bool find = false;
188 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
189 for (auto& item : roles_) {
190 if (item.second->Match(target_attr)) {
191 matched_roles_attr->emplace_back(item.second->attributes());
192 find = true;
193 }
194 }
195 return find;
196}
197
198void MultiValueWarehouse::GetAllRoles(std::vector<RolePtr>* roles) {
199 RETURN_IF_NULL(roles);
200 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
201 for (auto& item : roles_) {
202 roles->emplace_back(item.second);
203 }
204}
205
206void MultiValueWarehouse::GetAllRoles(std::vector<RoleAttributes>* roles_attr) {
207 RETURN_IF_NULL(roles_attr);
208 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
209 for (auto& item : roles_) {
210 roles_attr->emplace_back(item.second->attributes());
211 }
212}
213
214} // namespace service_discovery
215} // namespace cyber
216} // namespace apollo
void GetAllRoles(std::vector< RolePtr > *roles) override
bool Add(uint64_t key, const RolePtr &role, bool ignore_if_exist=true) override
#define RETURN_VAL_IF_NULL(ptr, val)
Definition log.h:98
#define RETURN_IF_NULL(ptr)
Definition log.h:90
std::shared_ptr< RoleBase > RolePtr
Definition role.h:31
class register implement
Definition arena_queue.h:37