-
Notifications
You must be signed in to change notification settings - Fork 0
/
Version.cpp
83 lines (63 loc) · 1.77 KB
/
Version.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
73
74
75
76
77
78
79
80
81
82
83
//
// Created by 唐仁初 on 2022/10/10.
//
#include "Version.h"
#include "Value.h"
#include "OpCoordinator.h"
namespace mvcc {
Version::Version(long version, bool refer) : version_(version), use_count_(new std::atomic<int>(1)),
refer_(refer) {}
Version::Version(const Version &other) : version_(other.version_.load()), use_count_(other.use_count_), refer_(other.refer_) {
use_count_->fetch_add(1);
}
Version::~Version() {
if (running_default) {
for (auto &op: operations_) {
if (op != nullptr)
op->undo();
}
}
if (use_count_->fetch_sub(1) == 1) {
Coordinator.versionReleaseNotify(version_);
}
}
Version &Version::operator=(const Version &other) {
if (this == &other) {
return *this;
}
version_ = other.version_.load();
use_count_ = other.use_count_;
operations_ = {};
use_count_->fetch_add(1);
return *this;
}
long Version::version() const {
return version_.load();
}
void Version::undo() {
if (refer_)
return;
running_default = false;
for (auto &op: operations_) {
op->undo();
}
}
bool Version::commit() {
if (refer_)
return false;
running_default = false;
for (auto &op: operations_) {
op->commit();
}
return true;
}
void Version::recordOperation(ValueNode *operated) {
operations_.emplace_back(operated);
}
int Version::count() const {
return use_count_->load();
}
void Version::versionUpdate(int n) {
version_.fetch_add(n);
}
}