Repository navigation
Expand file tree
/
Copy pathTransLogToChar.cpp
More file actions
103 lines (87 loc) · 2.78 KB
/
Copy pathTransLogToChar.cpp
File metadata and controls
103 lines (87 loc) · 2.78 KB
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
enum LogRecType { LRT_INVALID = 0, LRT_INSERT, LRT_UPDATE, LRT_DELETE, LRT_TRUNCATE };
enum LogIUD { L_INSERT = 0, L_UPDATE, L_DELETE, L_NOTIFY };
struct AriesLogRecord {
uint32_t checksum;
uint64_t lsn;
LogRecType type;
LogIUD iud;
uint64_t txn_id; // transaction id
// uint32_t table_id; // table being updated
uint64_t key; // primary key (determines the partition ID)
uint32_t state; // run_txn_state
uint32_t part_id; // partition id
uint32_t n_cols;
uint32_t id_cols[17];
char before_image[1024];
char after_image[1024];
};
std::string AriesLogRecordToString(const AriesLogRecord& record) {
std::ostringstream oss;
oss << "Checksum: " << record.checksum << '\n'
<< "LSN: " << record.lsn << '\n'
<< "Type: " << record.type << '\n'
<< "IUD: " << record.iud << '\n'
<< "Transaction ID: " << record.txn_id << '\n'
// << "Table ID: " << record.table_id << '\n'
<< "Key: " << record.key << '\n'
<< "State: " << record.state << '\n'
<< "Partition ID: " << record.part_id << '\n'
<< "Number of Columns: " << record.n_cols << '\n'
<< "ID of Modified Columns: ";
for (int i = 0; i < 17; ++i) {
oss << record.id_cols[i] << ' ';
}
oss << '\n';
oss << "before_image: " << std::string(record.before_image, 1024) << "\n"
<< "after_image: " << std::string(record.after_image, 1024) << "\n"
<< "-----------------------------\n";
return oss.str();
}
#define READ_VAL(f,v) \
f.read((char*)&v, sizeof(v));
#define READ_VAL_SIZE(f,v,s) \
f.read(v, sizeof(char) * s);
int main() {
// 创建一个输入文件流对象
std::ifstream infile("../logfile1.log", std::ios::binary);
if (!infile) {
std::cerr << "无法打开文件用于读取!" << std::endl;
return 1;
}
// 创建一个输出文件流对象
std::ofstream outfile("logfile20240706.txt");
if (!outfile) {
std::cerr << "无法打开文件用于写入!" << std::endl;
return 1;
}
while (infile) {
AriesLogRecord record;
READ_VAL(infile, record.checksum);
READ_VAL(infile, record.lsn);
READ_VAL(infile, record.type);
READ_VAL(infile, record.iud);
READ_VAL(infile, record.txn_id);
// READ_VAL(infile, record.table_id);
READ_VAL(infile, record.key);
READ_VAL(infile, record.state);
READ_VAL(infile, record.part_id);
READ_VAL(infile, record.n_cols);
for (int i = 0; i < 17; i++) {
READ_VAL(infile, record.id_cols[i]);
}
READ_VAL_SIZE(infile, record.before_image, 1024);
READ_VAL_SIZE(infile, record.after_image, 1024);
if (infile) {
outfile << AriesLogRecordToString(record);
}
}
// 关闭文件
infile.close();
outfile.close();
std::cout << "数据已读取并保存到文本文件" << std::endl;
return 0;
}