|
1 |
| - /** |
| 1 | +/** |
2 | 2 | * Copyright (c) 2023 Peking University and Peking University
|
3 | 3 | * Changsha Institute for Computing and Digital Economy
|
4 | 4 | *
|
|
14 | 14 | * See the Mulan PSL v2 for more details.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| - #include "LicensesManager.h" |
18 |
| - |
| 17 | +#include "LicensesManager.h" |
19 | 18 |
|
20 | 19 | namespace Ctld {
|
21 | 20 |
|
22 | 21 | LicensesManager::LicensesManager() {}
|
23 | 22 |
|
24 |
| -int LicensesManager::Init(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) { |
25 |
| - util::read_lock_guard readLock(rw_mutex_); |
26 |
| - for (auto& [lic_id, count] : lic_id_to_count_map) { |
27 |
| - lic_id_to_lic_map_.insert({lic_id, std::make_unique<License>(License(lic_id, count, 0, count))}); |
28 |
| - } |
29 |
| - return 0; |
| 23 | +int LicensesManager::Init( |
| 24 | + const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) { |
| 25 | + util::read_lock_guard readLock(rw_mutex_); |
| 26 | + for (auto& [lic_id, count] : lic_id_to_count_map) { |
| 27 | + lic_id_to_lic_map_.insert({lic_id, License(lic_id, count, 0, count)}); |
| 28 | + } |
| 29 | + return 0; |
30 | 30 | }
|
31 | 31 |
|
32 |
| -void LicensesManager::GetLicensesInfo(const crane::grpc::QueryLicensesInfoRequest* request, crane::grpc::QueryLicensesInfoReply* response) { |
33 |
| - auto* list = response->mutable_license_info_list(); |
| 32 | +void LicensesManager::GetLicensesInfo( |
| 33 | + const crane::grpc::QueryLicensesInfoRequest* request, |
| 34 | + crane::grpc::QueryLicensesInfoReply* response) { |
| 35 | + auto* list = response->mutable_license_info_list(); |
34 | 36 |
|
35 |
| - if (request->license_name().empty()) { |
36 |
| - util::read_lock_guard readLock(rw_mutex_); |
37 |
| - for(auto& [lic_index, lic_ptr] : lic_id_to_lic_map_) { |
38 |
| - auto *lic_info = list->Add(); |
39 |
| - lic_info->set_name(lic_ptr->license_id); |
40 |
| - lic_info->set_total(lic_ptr->total); |
41 |
| - lic_info->set_used(lic_ptr->used); |
42 |
| - lic_info->set_free(lic_ptr->free); |
43 |
| - } |
44 |
| - } else { |
45 |
| - util::read_lock_guard readLock(rw_mutex_); |
46 |
| - auto it = lic_id_to_lic_map_.find(request->license_name()); |
47 |
| - if (it != lic_id_to_lic_map_.end()) { |
48 |
| - auto *lic_info = list->Add(); |
49 |
| - lic_info->set_name(it->second->license_id); |
50 |
| - lic_info->set_total(it->second->total); |
51 |
| - lic_info->set_used(it->second->used); |
52 |
| - lic_info->set_free(it->second->free); |
53 |
| - } |
| 37 | + if (request->license_name().empty()) { |
| 38 | + util::read_lock_guard readLock(rw_mutex_); |
| 39 | + for (auto& [lic_index, lic] : lic_id_to_lic_map_) { |
| 40 | + auto* lic_info = list->Add(); |
| 41 | + lic_info->set_name(lic.license_id); |
| 42 | + lic_info->set_total(lic.total); |
| 43 | + lic_info->set_used(lic.used); |
| 44 | + lic_info->set_free(lic.free); |
| 45 | + } |
| 46 | + } else { |
| 47 | + util::read_lock_guard readLock(rw_mutex_); |
| 48 | + auto it = lic_id_to_lic_map_.find(request->license_name()); |
| 49 | + if (it != lic_id_to_lic_map_.end()) { |
| 50 | + auto* lic_info = list->Add(); |
| 51 | + lic_info->set_name(it->second.license_id); |
| 52 | + lic_info->set_total(it->second.total); |
| 53 | + lic_info->set_used(it->second.used); |
| 54 | + lic_info->set_free(it->second.free); |
54 | 55 | }
|
| 56 | + } |
55 | 57 | }
|
56 | 58 |
|
57 |
| -bool LicensesManager::CheckLicenseCountSufficient(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) { |
58 |
| - |
59 |
| - util::read_lock_guard readLock(rw_mutex_); |
60 |
| - for(auto& [lic_id, count] : lic_id_to_count_map) { |
61 |
| - auto it = lic_id_to_lic_map_.find(lic_id); |
62 |
| - if (it == lic_id_to_lic_map_.end() || it->second->free < count) { |
63 |
| - return false; |
64 |
| - } |
| 59 | +bool LicensesManager::CheckLicenseCountSufficient( |
| 60 | + const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) { |
| 61 | + util::read_lock_guard readLock(rw_mutex_); |
| 62 | + for (auto& [lic_id, count] : lic_id_to_count_map) { |
| 63 | + auto it = lic_id_to_lic_map_.find(lic_id); |
| 64 | + if (it == lic_id_to_lic_map_.end() || it->second.free < count) { |
| 65 | + return false; |
65 | 66 | }
|
| 67 | + } |
66 | 68 |
|
67 |
| - return true; |
| 69 | + return true; |
68 | 70 | }
|
69 | 71 |
|
70 |
| -result::result<void, std::string> LicensesManager::CheckLicensesLegal(const ::google::protobuf::Map<std::string, uint32_t> &lic_id_to_count_map) { |
71 |
| - |
72 |
| - util::read_lock_guard readLock(rw_mutex_); |
73 |
| - for(auto& [lic_id, count] : lic_id_to_count_map) { |
74 |
| - auto it = lic_id_to_lic_map_.find(lic_id); |
75 |
| - if (it == lic_id_to_lic_map_.end() || count > it->second->total) { |
76 |
| - return result::fail("Invalid license specification"); |
77 |
| - } |
| 72 | +result::result<void, std::string> LicensesManager::CheckLicensesLegal( |
| 73 | + const ::google::protobuf::Map<std::string, uint32_t>& lic_id_to_count_map) { |
| 74 | + util::read_lock_guard readLock(rw_mutex_); |
| 75 | + for (auto& [lic_id, count] : lic_id_to_count_map) { |
| 76 | + auto it = lic_id_to_lic_map_.find(lic_id); |
| 77 | + if (it == lic_id_to_lic_map_.end() || count > it->second.total) { |
| 78 | + return result::fail("Invalid license specification"); |
78 | 79 | }
|
| 80 | + } |
79 | 81 |
|
80 |
| - return {}; |
| 82 | + return {}; |
81 | 83 | }
|
82 | 84 |
|
83 |
| -void LicensesManager::MallocLicenseResource(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) { |
84 |
| - |
85 |
| - util::write_lock_guard writeLock(rw_mutex_); |
86 |
| - for(auto& [lic_id, count] : lic_id_to_count_map) { |
87 |
| - auto it = lic_id_to_lic_map_.find(lic_id); |
88 |
| - it->second->used += count; |
89 |
| - it->second->free -= count; |
90 |
| - } |
| 85 | +void LicensesManager::MallocLicenseResource( |
| 86 | + const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) { |
| 87 | + util::write_lock_guard writeLock(rw_mutex_); |
| 88 | + for (auto& [lic_id, count] : lic_id_to_count_map) { |
| 89 | + auto it = lic_id_to_lic_map_.find(lic_id); |
| 90 | + it->second.used += count; |
| 91 | + it->second.free -= count; |
| 92 | + } |
91 | 93 | }
|
92 | 94 |
|
93 |
| -void LicensesManager::FreeLicenseResource(const std::unordered_map<LicenseId, uint32_t> &lic_id_to_count_map) { |
94 |
| - |
95 |
| - util::write_lock_guard writeLock(rw_mutex_); |
96 |
| - for(auto& [lic_id, count] : lic_id_to_count_map) { |
97 |
| - auto it = lic_id_to_lic_map_.find(lic_id); |
98 |
| - it->second->used -= count; |
99 |
| - it->second->free += count; |
100 |
| - } |
101 |
| -} |
| 95 | +void LicensesManager::FreeLicenseResource( |
| 96 | + const std::unordered_map<LicenseId, uint32_t>& lic_id_to_count_map) { |
| 97 | + util::write_lock_guard writeLock(rw_mutex_); |
| 98 | + for (auto& [lic_id, count] : lic_id_to_count_map) { |
| 99 | + auto it = lic_id_to_lic_map_.find(lic_id); |
| 100 | + it->second.used -= count; |
| 101 | + it->second.free += count; |
| 102 | + } |
| 103 | +} |
102 | 104 |
|
103 |
| -} // namespace Craned |
| 105 | +} // namespace Ctld |
0 commit comments