This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion.cc
75 lines (60 loc) · 1.92 KB
/
version.cc
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
#include <iostream>
#include "version.h"
version_control::version_control (block_manager *bm): bm_(bm), current_(0) {
versions_.emplace_back();
}
void version_control::commit() {
// print_logs_();
std::cout << "vc: committing version " << current_ << std::endl;
++current_;
if (current_ == versions_.size()) {
versions_.emplace_back();
} else {
versions_[current_].clear();
}
}
void version_control::rollback() {
// print_logs_();
std::cout << "vc: rollback version " << current_ << std::endl;
if (current_ == 0) return;
undo_actions_(current_version_());
--current_;
}
void version_control::redo() {
// print_logs_();
if (current_ == versions_.size() - 1) {
return;
}
++current_;
std::cout << "vc: redo version " << current_ << std::endl;
do_actions_(current_version_());
}
actions &version_control::current_version_() {
return versions_[current_];
}
void version_control::add_entry(const log_entry && entry) {
std::cout << "vc: add entry on version " << current_ << ", block " << entry.id << std::endl;
current_version_().emplace_back(entry);
}
void version_control::do_actions_(const actions &records) {
for (const auto &record: records) {
std::cout << "vc: redo on block " << record.id << std::endl;
bm_->write_block_internal_(record.id, record.new_data);
}
}
void version_control::undo_actions_(const actions &records) {
for (auto it = records.rbegin(); it != records.rend(); it++) {
std::cout << "vc: undo on block " << it->id << std::endl;
bm_->write_block_internal_(it->id, it->old_data);
}
}
void version_control::print_logs_() {
int i = 0;
for (const auto &actions: versions_) {
std::cout << "Version: " << i++ << std::endl;
for (const auto &action: actions) {
std::cout << action.id << std::endl;
}
std::cout << std::endl;
}
}