MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
log_file.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 wangyunlai on 2024/01/30
13//
14
15#pragma once
16
17#include "common/sys/rc.h"
18#include "common/types.h"
19#include "common/lang/map.h"
20#include "common/lang/functional.h"
21#include "common/lang/filesystem.h"
22#include "common/lang/fstream.h"
23#include "common/lang/string.h"
24
25class LogEntry;
26
33{
34public:
35 LogFileReader() = default;
36 ~LogFileReader() = default;
37
38 RC open(const char *filename);
39 RC close();
40
41 RC iterate(function<RC(LogEntry &)> callback, LSN start_lsn = 0);
42
43private:
49 RC skip_to(LSN start_lsn);
50
51private:
52 int fd_ = -1;
53 string filename_;
54};
55
61{
62public:
63 LogFileWriter() = default;
65
71 RC open(const char *filename, int end_lsn);
72
74 RC close();
75
77 RC write(LogEntry &entry);
78
82 bool valid() const;
83
87 bool full() const;
88
89 string to_string() const;
90
91 const char *filename() const { return filename_.c_str(); }
92
93private:
94 string filename_;
95 int fd_ = -1;
96 int last_lsn_ = 0;
97 int end_lsn_ = 0;
98};
99
107{
108public:
109 LogFileManager() = default;
110 ~LogFileManager() = default;
111
118 RC init(const char *directory, int max_entry_number_per_file);
119
126 RC list_files(vector<string> &files, LSN start_lsn);
127
132 RC last_file(LogFileWriter &file_writer);
133
138 RC next_file(LogFileWriter &file_writer);
139
140private:
145 static RC get_lsn_from_filename(const string &filename, LSN &lsn);
146
147private:
148 static constexpr const char *file_prefix_ = "clog_";
149 static constexpr const char *file_suffix_ = ".log";
150
151 filesystem::path directory_;
153
154 map<LSN, filesystem::path> log_files_;
155};
描述一条日志
Definition: log_entry.h:44
管理所有的日志文件
Definition: log_file.h:107
RC list_files(vector< string > &files, LSN start_lsn)
列出所有的日志文件,第一个日志文件包含大于等于start_lsn最小的日志
Definition: log_file.cpp:297
map< LSN, filesystem::path > log_files_
一个文件最大允许存放多少条日志
Definition: log_file.h:154
int max_entry_number_per_file_
日志文件存放的目录
Definition: log_file.h:152
RC init(const char *directory, int max_entry_number_per_file)
初始化
Definition: log_file.cpp:235
RC next_file(LogFileWriter &file_writer)
获取一个新的日志文件名
Definition: log_file.cpp:325
static RC get_lsn_from_filename(const string &filename, LSN &lsn)
从文件名称中获取LSN
Definition: log_file.cpp:280
RC last_file(LogFileWriter &file_writer)
获取最新的一个日志文件名
Definition: log_file.cpp:312
负责处理一个日志文件,包括读取和写入
Definition: log_file.h:33
RC skip_to(LSN start_lsn)
跳到第一条不小于start_lsn的日志
Definition: log_file.cpp:95
负责写入一个日志文件
Definition: log_file.h:61
int end_lsn_
写入的最后一条日志LSN
Definition: log_file.h:97
RC close()
关闭当前文件
Definition: log_file.cpp:168
int fd_
日志文件名
Definition: log_file.h:95
bool full() const
文件是否已经写满。当前是按照日志条数来判断的
Definition: log_file.cpp:222
int last_lsn_
日志文件描述符
Definition: log_file.h:96
RC write(LogEntry &entry)
写入一条日志
Definition: log_file.cpp:179
bool valid() const
当前文件是否已经打开
Definition: log_file.cpp:217
RC open(const char *filename, int end_lsn)
打开一个日志文件
Definition: log_file.cpp:149