MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
log_buffer.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/31
13//
14
15#pragma once
16
17#include "common/sys/rc.h"
18#include "common/types.h"
19#include "common/lang/mutex.h"
20#include "common/lang/vector.h"
21#include "common/lang/deque.h"
22#include "common/lang/atomic.h"
23#include "storage/clog/log_module.h"
24#include "storage/clog/log_entry.h"
25
26class LogFileWriter;
27
36{
37public:
38 LogEntryBuffer() = default;
39 ~LogEntryBuffer() = default;
40
41 RC init(LSN lsn, int32_t max_bytes = 0);
42
46 RC append(LSN &lsn, LogModule::Id module_id, vector<char> &&data);
47 RC append(LSN &lsn, LogModule module, vector<char> &&data);
48
55 RC flush(LogFileWriter &file_writer, int &count);
56
60 int64_t bytes() const;
61
65 int32_t entry_number() const;
66
67 LSN current_lsn() const { return current_lsn_.load(); }
68 LSN flushed_lsn() const { return flushed_lsn_.load(); }
69
70private:
71 mutex mutex_;
72 deque<LogEntry> entries_;
73 atomic<int64_t> bytes_;
74
75 atomic<LSN> current_lsn_{0};
76 atomic<LSN> flushed_lsn_{0};
77
78 int32_t max_bytes_ = 4 * 1024 * 1024;
79};
日志数据缓冲区
Definition: log_buffer.h:36
RC append(LSN &lsn, LogModule::Id module_id, vector< char > &&data)
在缓冲区中追加一条日志
Definition: log_buffer.cpp:32
deque< LogEntry > entries_
当前数据结构一定会在多线程中访问,所以强制使用有效的锁,而不是有条件生效的common::Mutex
Definition: log_buffer.h:72
int64_t bytes() const
当前缓冲区中有多少字节的日志
Definition: log_buffer.cpp:98
atomic< LSN > current_lsn_
当前缓冲区中的日志数据大小
Definition: log_buffer.h:75
RC flush(LogFileWriter &file_writer, int &count)
刷新缓冲区中的日志到磁盘
Definition: log_buffer.cpp:62
int32_t entry_number() const
当前缓冲区中有多少条日志
Definition: log_buffer.cpp:103
atomic< int64_t > bytes_
日志缓冲区
Definition: log_buffer.h:73
负责写入一个日志文件
Definition: log_file.h:61
日志模块
Definition: log_module.h:24
Id
Definition: log_module.h:27