-
Notifications
You must be signed in to change notification settings - Fork 8
/
raft_logger.cpp
62 lines (50 loc) · 1.07 KB
/
raft_logger.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
#include "raft_logger.h"
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#include <vector>
using namespace std;
RaftLogger::RaftLogger() : entries() {
}
int RaftLogger::log_append_entry(const raft_entry_t& c) {
if (0 == c.d_id)
return 0;
entries.push_back(c);
entries.back().d_num_nodes = 0;
return 1;
}
raft_entry_t& RaftLogger::log_get_from_idx(int idx) {
if (entries.empty() || idx < 0 || static_cast<size_t>(idx) > entries.size()) {
throw std::runtime_error("No Such Log");
} else {
return entries[idx-1];
}
}
int RaftLogger::log_count()
{
return entries.size();
}
void RaftLogger::log_delete(int idx)
{
entries.erase(entries.begin() + idx - 1, entries.end());
}
raft_entry_t& RaftLogger::log_peektail()
{
if (!entries.empty()) {
return entries.back();
} else {
throw std::runtime_error("No Such Log");
}
}
void RaftLogger::log_empty()
{
entries.clear();
}
RaftLogger::~RaftLogger()
{
}
void RaftLogger::log_mark_node_has_committed(int idx)
{
raft_entry_t& e = log_get_from_idx(idx);
e.d_num_nodes += 1;
}