-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operation.h
260 lines (183 loc) · 8.55 KB
/
Operation.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// Created by 唐仁初 on 2022/10/11.
//
#ifndef ALGYOLO_OPERATION_H
#define ALGYOLO_OPERATION_H
#include "Value.h"
#include "Version.h"
#include <utility>
namespace mvcc::op {
class Transaction;
/// @brief Abstract Base Class. The parent class of all derived operations
/// @details Operation is an abstract class of sequenced operation. Each operation class will put revised ValueNode in
/// assigned Version and commit or undo when operation finishes.
class Operation {
protected:
friend class Transaction;
/// Construct an Operation impl with assigned Version
/// \param version Assigned version
explicit Operation(const Version &version) : version_(version) {}
/// Transaction interface. Do operation without commit automatically.
/// \return Is operation succeeded
virtual bool doWithoutCommit() = 0;
/// Transaction interface. Commit operation.
/// \return Is committed
virtual bool commit() = 0;
/// Transaction interface. Undo operation.
virtual void undo() = 0;
virtual ~Operation() = default;
protected:
Version version_;
};
/// @brief WriteOperation derived from Operation. Generated by OpCoordinator.
/// @details Class WriteOperation is an abstract of write process. User can use write() function to start write process.
/// Transaction and use derived interface to control transaction process.
class WriteOperation : public Operation {
public:
friend class Transaction;
/// Construct a WriteOperation impl.
/// \param node Node to write
/// \param value Value to write
/// \param version Assigned Version
explicit WriteOperation(Value *node, std::string value, const Version &version);
/// Default Destructor
~WriteOperation() override = default;
/// Start write process.
/// \return Is operation succeeded
bool write();
/// Comparison operator compares the ptr of operated Value node.
/// \param other Another WriteOperation
/// \return this.node_ptr is less than other._node_ptr
bool operator<(const WriteOperation &other);
/// Comparison operator compares the ptr of operated Value node.
/// \param other Another WriteOperation
/// \return this.node_ptr is equal to other._node_ptr
bool operator==(const WriteOperation &other);
/// Comparison operator compares the ptr of operated Value node.
/// \param other Another WriteOperation
/// \return this.node_ptr is greater than other._node_ptr
bool operator>(const WriteOperation &other);
private:
/// Transaction interface. Do operation without commit automatically.
/// \return Is operation succeeded
bool doWithoutCommit() override;
/// Commit changes.
/// \return Is committed
bool commit() override;
/// Undo changes.
void undo() override;
private:
Value *node_ = nullptr;
std::string value_;
bool acquire_newest = false; //FIXME : 如果用户要求插入的是最新值
};
/// @brief DeleteOperation derived from WriteOperation. Generated by OpCoordinator.
/// @details Class DeleteOperation is an abstract of write process. It will write an empty value to assigned Value node.
/// User can use write() function to start write process. Transaction and use derived interface to control transaction process.
class DeleteOperation final : public WriteOperation {
public:
/// Construct a DeleteOperation impl.
/// \param node Node to write
/// \param version Assigned Version
explicit DeleteOperation(Value *node, const Version &version);
~DeleteOperation() override = default;
};
/// @brief ReadOperation derived from Operation. Generated by OpCoordinator.
/// @details Class ReadOperation is an abstract of read process. User can use read() function to start write process.
/// ValueNode is visible in this operation only if it has been committed and the version is less than this operation.
class ReadOperation final : public Operation {
public:
/// Construct a ReadOperation impl.
/// \param node Node to read
/// \param version Assigned Version
explicit ReadOperation(Value *node, const Version &version);
~ReadOperation() override = default;
/// Start read process and get value.
/// \return Expected value
std::string read();
private:
/// Transaction interface. No use.
/// \return Is operation succeeded
bool doWithoutCommit() override;
bool commit() override;
void undo() override;
private:
Value *node_ = nullptr;
};
/// @brief StreamReadOperation derived from Operation. Generated by OpCoordinator.
/// @details Class StreamReadOperation is an abstract of stream read process. User can use read() function to start
/// write process. ValueNode is visible in this operation only if it has been committed and the version is less than
/// this operation.
/// @note Every single read op shares the same version. So it can be seen as a snapshot read.
class StreamReadOperation final : public Operation {
public:
explicit StreamReadOperation(Value *node, const Version &version);
~StreamReadOperation() override = default;
/// Start read process and get value.
/// \return Expected value
std::string read();
/// Change read node to next.
/// \param node Node to move to
void next(Value *node);
private:
/// Transaction interface. No use.
/// \return Is operation succeeded
bool doWithoutCommit() override;
bool commit() override;
void undo() override;
private:
Value *node_ = nullptr;
};
/// @brief UpdateOperation is an alias of WriteOperation. Generated by OpCoordinator.
using UpdateOperation = WriteOperation;
/// @brief BulkWriteOperation derived from Operation. Generated by OpCoordinator.
/// @details Class BulkWriteOperation is an abstract of bulk write process. User should firstly use function appendOperation
/// to append write operation to this impl and then use function run to start a bulk write. Every write operation will
/// shares the same version.
/// @note Bulk write process will be shutdown when facing an error without rollback.
class BulkWriteOperation final : public Operation {
public:
/// Constructs a BulkWriteOperation impl
/// \param version Assigned version
explicit BulkWriteOperation(const Version &version);
~BulkWriteOperation() override = default;
/// Append write operation to this operation stream.
/// \param node Node to write
/// \param value Value to write
void appendOperation(Value *node, const std::string &value);
/// Execute bulk write operation. If one write operation is failed, bulk write operation will stop without undo.
/// \return Is all write operation succeeded
bool run();
private:
/// Transaction interface. No use.
/// \return Is operation succeeded
bool doWithoutCommit() override;;
bool commit() override;;
void undo() override;
private:
std::list<WriteOperation> ops_;
};
/// @brief Transaction describes a read-committed isolation transaction. Generated by OpCoordinator.
/// @details Class BulkWriteOperation is an abstract of transaction process. User should firstly use function appendOperation
/// to append write operation to this impl and then use function tryCommit to end transaction. Every write operation will
/// shares the same version.
/// @note Transaction process will be shutdown when facing an error and rollback.
class Transaction {
public:
/// Construct a Transaction impl.
/// \param version Assigned version
explicit Transaction(const Version &version);
~Transaction() = default;
/// Append write operation to this operation stream.
/// \param node Node to write
/// \param value Value to write
void appendOperation(Value *node, const std::string &value);
/// Try do and commit this transaction. If an error occurs, all transaction will rollback.
/// \return Is committed
bool tryCommit() ;
private:
Version version_;
std::list<WriteOperation> ops_;
};
}
#endif //ALGYOLO_OPERATION_H