forked from vmware/concord-bft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_table.cpp
72 lines (59 loc) · 1.97 KB
/
cron_table.cpp
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
// Concord
//
// Copyright (c) 2021 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache 2.0 license (the "License"). You may not use this product except in
// compliance with the Apache 2.0 License.
//
// This product may include a number of subcomponents with separate copyright notices and license terms. Your use of
// these subcomponents is subject to the terms and conditions of the sub-component's license, as noted in the LICENSE
// file.
#include "ccron/cron_table.hpp"
#include <utility>
namespace concord::cron {
CronTable::CronTable(std::uint32_t component_id) : component_id_{component_id} {}
void CronTable::addEntry(CronEntry entry) { entries_.emplace(entry.position, std::move(entry)); }
bool CronTable::removeEntry(std::uint32_t position) {
auto it = entries_.find(position);
if (it == entries_.cend()) {
return false;
}
auto& entry = it->second;
if (entry.on_remove) {
(*entry.on_remove)(component_id_, position);
}
entries_.erase(it);
return true;
}
std::uint32_t CronTable::removeAllEntries() {
for (const auto& [position, entry] : entries_) {
if (entry.on_remove) {
(*entry.on_remove)(component_id_, position);
}
}
const auto count = entries_.size();
entries_.clear();
return count;
}
const CronEntry* CronTable::at(std::uint32_t position) const {
auto it = entries_.find(position);
if (it == entries_.cend()) {
return nullptr;
}
return &it->second;
}
std::uint32_t CronTable::numberOfEntries() const { return entries_.size(); }
bool CronTable::empty() const { return (numberOfEntries() == 0); }
std::uint32_t CronTable::componentId() const { return component_id_; }
void CronTable::evaluate(const Tick& tick) const {
for (const auto& [_, entry] : entries_) {
(void)_;
if (tick.component_id == component_id_ && entry.rule(tick)) {
if (entry.schedule_next) {
(*entry.schedule_next)(tick);
}
entry.action(tick);
}
}
}
} // namespace concord::cron