MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
clog.h
浏览该文件的文档.
1/* Copyright (c) 2021-2022 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by huhaosheng.hhs on 2022
13//
14
15#pragma once
16
17#include <stddef.h>
18#include <stdint.h>
19#include <list>
20#include <atomic>
21#include <unordered_map>
22#include <deque>
23#include <memory>
24#include <string>
25
26#include "storage/record/record.h"
27#include "storage/persist/persist.h"
28#include "common/lang/mutex.h"
29
30class CLogManager;
31class CLogBuffer;
32class CLogFile;
33class Db;
34
52#define DEFINE_CLOG_TYPE_ENUM \
53 DEFINE_CLOG_TYPE(ERROR) \
54 DEFINE_CLOG_TYPE(MTR_BEGIN) \
55 DEFINE_CLOG_TYPE(MTR_COMMIT) \
56 DEFINE_CLOG_TYPE(MTR_ROLLBACK) \
57 DEFINE_CLOG_TYPE(INSERT) \
58 DEFINE_CLOG_TYPE(DELETE)
59
60enum class CLogType
61{
62#define DEFINE_CLOG_TYPE(name) name,
63 DEFINE_CLOG_TYPE_ENUM
64#undef DEFINE_CLOG_TYPE
65};
66
71const char *clog_type_name(CLogType type);
72
76int32_t clog_type_to_integer(CLogType type);
77
82CLogType clog_type_from_integer(int32_t value);
83
89{
90 int32_t lsn_ = -1;
91 int32_t trx_id_ = -1;
92 int32_t type_ = clog_type_to_integer(CLogType::ERROR);
93 int32_t logrec_len_ = 0;
94
95 bool operator==(const CLogRecordHeader &other) const
96 {
97 return lsn_ == other.lsn_ && trx_id_ == other.trx_id_ && type_ == other.type_ && logrec_len_ == other.logrec_len_;
98 }
99
100 std::string to_string() const;
101};
102
109{
110 int32_t commit_xid_ = -1;
111
112 bool operator == (const CLogRecordCommitData &other) const
113 {
114 return this->commit_xid_ == other.commit_xid_;
115 }
116
117 std::string to_string() const;
118};
119
126{
127 int32_t table_id_ = -1;
129 int32_t data_len_ = 0;
130 int32_t data_offset_ = 0;
131 char * data_ = nullptr;
132
134
135 bool operator==(const CLogRecordData &other) const
136 {
137 return table_id_ == other.table_id_ &&
138 rid_ == other.rid_ &&
139 data_len_ == other.data_len_ &&
140 data_offset_ == other.data_offset_ &&
141 0 == memcmp(data_, other.data_, data_len_);
142 }
143
144 std::string to_string() const;
145
146 const static int32_t HEADER_SIZE;
147};
148
156{
157public:
162 CLogRecord() = default;
163
164 ~CLogRecord();
165
172 static CLogRecord *build_mtr_record(CLogType type, int32_t trx_id);
173
180 static CLogRecord *build_commit_record(int32_t trx_id, int32_t commit_xid);
181
194 int32_t trx_id,
195 int32_t table_id,
196 const RID &rid,
197 int32_t data_len,
198 int32_t data_offset,
199 const char *data);
200
207 static CLogRecord *build(const CLogRecordHeader &header, char *data);
208
209 CLogType log_type() const { return clog_type_from_integer(header_.type_); }
210 int32_t trx_id() const { return header_.trx_id_; }
211 int32_t logrec_len() const { return header_.logrec_len_; }
212
213 CLogRecordHeader &header() { return header_; }
214 CLogRecordCommitData &commit_record() { return commit_record_; }
215 CLogRecordData &data_record() { return data_record_; }
216
217 const CLogRecordHeader &header() const { return header_; }
218 const CLogRecordCommitData &commit_record() const { return commit_record_; }
219 const CLogRecordData &data_record() const { return data_record_; }
220
221 std::string to_string() const;
222
223protected:
225
228};
229
238{
239public:
240 CLogBuffer();
241 ~CLogBuffer();
242
247 RC append_log_record(CLogRecord *log_record);
248
254 RC flush_buffer(CLogFile &log_file);
255
256private:
263 RC write_log_record(CLogFile &log_file, CLogRecord *log_record);
264
265private:
266 common::Mutex lock_;
267 std::deque<std::unique_ptr<CLogRecord>> log_records_;
268 std::atomic_int32_t total_size_;
269};
270
278{
279public:
280 CLogFile() = default;
281 ~CLogFile();
282
288 RC init(const char *path);
289
297 RC write(const char *data, int len);
298
305 RC read(char *data, int len);
306
310 RC sync();
311
315 RC offset(int64_t &off) const;
316
320 bool eof() const { return eof_; }
321
322protected:
323 std::string filename_;
324 int fd_ = -1;
325 bool eof_ = false;
326};
327
334{
335public:
336 CLogRecordIterator() = default;
337 ~CLogRecordIterator() = default;
338
339 RC init(CLogFile &log_file);
340
341 bool valid() const;
342 RC next();
343 const CLogRecord &log_record();
344
345private:
346 CLogFile *log_file_ = nullptr;
347 CLogRecord *log_record_ = nullptr;
348};
349
357{
358public:
359 CLogManager() = default;
360 ~CLogManager();
361
367 RC init(const char *path);
368
372 RC append_log(CLogType type,
373 int32_t trx_id,
374 int32_t table_id,
375 const RID &rid,
376 int32_t data_len,
377 int32_t data_offset,
378 const char *data);
379
385 RC begin_trx(int32_t trx_id);
386
393 RC commit_trx(int32_t trx_id, int32_t commit_xid);
394
400 RC rollback_trx(int32_t trx_id);
401
405 RC append_log(CLogRecord *log_record);
406
410 RC sync();
411
417 RC recover(Db *db);
418
419private:
421 CLogFile * log_file_ = nullptr;
422};
缓存运行时产生的日志对象
Definition: clog.h:238
std::deque< std::unique_ptr< CLogRecord > > log_records_
当前等待刷数据的日志记录
Definition: clog.h:267
std::atomic_int32_t total_size_
当前缓存中的日志记录的总大小
Definition: clog.h:268
RC write_log_record(CLogFile &log_file, CLogRecord *log_record)
将日志记录写入到日志文件中
Definition: clog.cpp:255
RC flush_buffer(CLogFile &log_file)
将当前的日志都刷新到日志文件中
Definition: clog.cpp:225
RC append_log_record(CLogRecord *log_record)
增加一条日志
Definition: clog.cpp:207
common::Mutex lock_
加锁支持多线程并发写入
Definition: clog.h:266
读写日志文件
Definition: clog.h:278
RC read(char *data, int len)
读取指定长度的数据。全部读取成功返回成功,否则返回失败
Definition: clog.cpp:333
RC init(const char *path)
初始化
Definition: clog.cpp:296
RC write(const char *data, int len)
写入指定数据,全部写入成功返回成功,否则返回失败
Definition: clog.cpp:323
RC sync()
将当前写的文件执行sync同步数据到磁盘
Definition: clog.cpp:348
bool eof_
是否已经读取到文件尾
Definition: clog.h:325
std::string filename_
日志文件名。总是init函数参数path路径下的clog文件
Definition: clog.h:323
RC offset(int64_t &off) const
获取当前读取的文件位置
Definition: clog.cpp:358
bool eof() const
当前是否已经读取到文件尾
Definition: clog.h:320
int fd_
操作的文件描述符
Definition: clog.h:324
日志管理器
Definition: clog.h:357
RC rollback_trx(int32_t trx_id)
回滚一个事务
Definition: clog.cpp:480
RC init(const char *path)
初始化日志管理器
Definition: clog.cpp:427
RC begin_trx(int32_t trx_id)
开启一个事务
Definition: clog.cpp:463
CLogBuffer * log_buffer_
日志缓存。新增日志时先放到内存,也就是这个buffer中
Definition: clog.h:420
RC commit_trx(int32_t trx_id, int32_t commit_xid)
提交一个事务
Definition: clog.cpp:468
RC sync()
刷新日志到磁盘
Definition: clog.cpp:493
RC recover(Db *db)
重做
Definition: clog.cpp:498
RC append_log(CLogType type, int32_t trx_id, int32_t table_id, const RID &rid, int32_t data_len, int32_t data_offset, const char *data)
新增一条数据更新的日志
Definition: clog.cpp:447
CLogFile * log_file_
管理日志,比如读写日志
Definition: clog.h:421
日志记录遍历器
Definition: clog.h:334
表示一条日志记录
Definition: clog.h:156
CLogRecordData data_record_
如果日志操作的是数据,此结构生效
Definition: clog.h:226
static CLogRecord * build_commit_record(int32_t trx_id, int32_t commit_xid)
创建一个表示提交事务的日志对象
Definition: clog.cpp:106
CLogRecordCommitData commit_record_
如果是事务提交日志,此结构生效
Definition: clog.h:227
CLogRecord()=default
默认构造函数。
CLogRecordHeader header_
日志头信息
Definition: clog.h:224
static CLogRecord * build_mtr_record(CLogType type, int32_t trx_id)
创建一个事务相关的日志对象
Definition: clog.cpp:97
static CLogRecord * build_data_record(CLogType type, int32_t trx_id, int32_t table_id, const RID &rid, int32_t data_len, int32_t data_offset, const char *data)
创建一个表示数据操作的日志对象
Definition: clog.cpp:119
static CLogRecord * build(const CLogRecordHeader &header, char *data)
根据二进制数据创建日志对象
Definition: clog.cpp:152
一个DB实例负责管理一批表
Definition: db.h:34
int32_t clog_type_to_integer(CLogType type)
clog type 转换成数字
Definition: clog.cpp:42
CLogType
定义clog的几种类型
Definition: clog.h:61
const char * clog_type_name(CLogType type)
clog type 转换成字符串
Definition: clog.cpp:32
CLogType clog_type_from_integer(int32_t value)
数字转换成clog type
Definition: clog.cpp:46
MTR_COMMIT 日志的数据
Definition: clog.h:109
int32_t commit_xid_
事务提交的事务号
Definition: clog.h:110
有具体数据修改的事务日志数据
Definition: clog.h:126
int32_t table_id_
操作的表
Definition: clog.h:127
char * data_
具体的数据,可能没有任何数据
Definition: clog.h:131
int32_t data_len_
记录的数据长度(因为header中也包含长度信息,这个长度可以不要)
Definition: clog.h:129
static const int32_t HEADER_SIZE
指RecordData的头长度,即不包含data_的长度
Definition: clog.h:146
RID rid_
操作的哪条记录
Definition: clog.h:128
int32_t data_offset_
操作的数据在完整记录中的偏移量
Definition: clog.h:130
CLog的记录头。每个日志都带有这个信息
Definition: clog.h:89
int32_t type_
日志类型
Definition: clog.h:92
int32_t trx_id_
日志所属事务的编号
Definition: clog.h:91
int32_t lsn_
log sequence number。当前没有使用
Definition: clog.h:90
int32_t logrec_len_
record的长度,不包含header长度
Definition: clog.h:93
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35