MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
bplus_tree_log_entry.h
1/* Copyright (c) 2021 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.wyl on 2024/02/20.
13//
14
15#pragma once
16
17#include "common/types.h"
18#include "common/sys/rc.h"
19#include "common/lang/span.h"
20#include "common/lang/string.h"
21#include "storage/index/bplus_tree.h"
22
26
27namespace common {
28class Serializer;
29class Deserializer;
30} // namespace common
31
32namespace bplus_tree {
33
39{
40public:
41 enum class Type
42 {
43 INIT_HEADER_PAGE,
53
55 };
56
57public:
58 LogOperation(Type type) : type_(type) {}
59 explicit LogOperation(int type) : type_(static_cast<Type>(type)) {}
60
61 Type type() const { return type_; }
62 int index() const { return static_cast<int>(type_); }
63
64 string to_string() const;
65
66private:
67 Type type_;
68};
69
76{
77public:
78 LogEntryHandler(LogOperation operation, Frame *frame = nullptr);
79 virtual ~LogEntryHandler() = default;
80
85 Frame *frame() { return frame_; }
86 const Frame *frame() const { return frame_; }
87
88 PageNum page_num() const;
89 void set_page_num(PageNum page_num) { page_num_ = page_num; }
90
92 LogOperation operation_type() const { return operation_type_; }
93
95 RC serialize(common::Serializer &buffer) const;
97 RC serialize_header(common::Serializer &buffer) const;
98
100 virtual RC serialize_body(common::Serializer &buffer) const = 0;
101
106 virtual RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) = 0;
107
112 virtual RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) = 0;
113
114 virtual string to_string() const;
115
128 static RC from_buffer(
129 function<RC(PageNum, Frame *&)> frame_getter, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
130 static RC from_buffer(
131 DiskBufferPool &buffer_pool, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
132 static RC from_buffer(common::Deserializer &deserializer, unique_ptr<LogEntryHandler> &handler);
133
134protected:
135 LogOperation operation_type_;
136 Frame *frame_ = nullptr;
137
140 PageNum page_num_ = BP_INVALID_PAGE_NUM;
141};
142
148{
149public:
151
152 virtual ~NodeLogEntryHandler() = default;
153};
154
160{
161public:
163 virtual ~InitHeaderPageLogEntryHandler() = default;
164
165 RC serialize_body(common::Serializer &buffer) const override;
166 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
167 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
168
169 string to_string() const override;
170
171 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
172
173 const IndexFileHeader &file_header() const { return file_header_; }
174
175private:
176 IndexFileHeader file_header_;
177};
178
184{
185public:
186 UpdateRootPageLogEntryHandler(Frame *frame, PageNum root_page_num, PageNum old_page_num);
187 virtual ~UpdateRootPageLogEntryHandler() = default;
188
189 RC serialize_body(common::Serializer &buffer) const override;
190 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
191 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
192
193 string to_string() const override;
194
195 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
196
197 PageNum root_page_num() const { return root_page_num_; }
198
199private:
200 PageNum root_page_num_ = -1;
201 PageNum old_page_num_ = -1;
202};
203
209{
210public:
211 SetParentPageLogEntryHandler(Frame *frame, PageNum parent_page_num, PageNum old_parent_page_num);
212 virtual ~SetParentPageLogEntryHandler() = default;
213
214 RC serialize_body(common::Serializer &buffer) const override;
215 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
216 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
217
218 string to_string() const override;
219
220 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
221
222 PageNum parent_page_num() const { return parent_page_num_; }
223
224private:
225 PageNum parent_page_num_ = -1;
226 PageNum old_parent_page_num_ = -1;
227};
228
234{
235public:
236 NormalOperationLogEntryHandler(Frame *frame, LogOperation operation, int index, span<const char> items, int item_num);
237 virtual ~NormalOperationLogEntryHandler() = default;
238
239 RC serialize_body(common::Serializer &buffer) const override;
240 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
241 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
242
243 string to_string() const override;
244
245 static RC deserialize(
246 Frame *frame, LogOperation operation, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
247
248 int index() const { return index_; }
249 int item_num() const { return item_num_; }
250 const char *items() const { return items_.data(); }
251 int32_t item_bytes() const { return static_cast<int32_t>(items_.size()); }
252
253private:
254 int index_ = -1;
255 int item_num_ = -1;
256 vector<char> items_;
257};
258
264{
265public:
267 virtual ~LeafInitEmptyLogEntryHandler() = default;
268
269 RC serialize_body(common::Serializer &buffer) const override { return RC::SUCCESS; }
270 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override { return RC::SUCCESS; }
271 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
272
273 // string to_string() const override;
274
275 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
276};
277
283{
284public:
285 LeafSetNextPageLogEntryHandler(Frame *frame, PageNum new_page_num, PageNum old_page_num);
286 virtual ~LeafSetNextPageLogEntryHandler() = default;
287
288 RC serialize_body(common::Serializer &buffer) const override;
289 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
290 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
291
292 string to_string() const override;
293
294 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
295
296 PageNum new_page_num() const { return new_page_num_; }
297
298private:
299 PageNum new_page_num_ = -1;
300 PageNum old_page_num_ = -1;
301};
302
308{
309public:
311 virtual ~InternalInitEmptyLogEntryHandler() = default;
312
313 RC serialize_body(common::Serializer &buffer) const override { return RC::SUCCESS; }
314 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override { return RC::SUCCESS; }
315 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
316
317 // string to_string() const override;
318
319 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
320};
321
327{
328public:
329 InternalCreateNewRootLogEntryHandler(Frame *frame, PageNum first_page_num, span<const char> key, PageNum page_num);
330 virtual ~InternalCreateNewRootLogEntryHandler() = default;
331
332 RC serialize_body(common::Serializer &buffer) const override;
333 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override { return RC::SUCCESS; }
334 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
335
336 string to_string() const override;
337
338 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
339
340 PageNum first_page_num() const { return first_page_num_; }
341 PageNum page_num() const { return page_num_; }
342 const char *key() const { return key_.data(); }
343 int32_t key_bytes() const { return static_cast<int32_t>(key_.size()); }
344
345private:
346 PageNum first_page_num_ = -1;
347 PageNum page_num_ = -1;
348 vector<char> key_;
349};
350
356{
357public:
358 InternalUpdateKeyLogEntryHandler(Frame *frame, int index, span<const char> key, span<const char> old_key);
359 virtual ~InternalUpdateKeyLogEntryHandler() = default;
360
361 RC serialize_body(common::Serializer &buffer) const override;
362 RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
363 RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override;
364
365 string to_string() const override;
366
367 static RC deserialize(Frame *frame, common::Deserializer &buffer, unique_ptr<LogEntryHandler> &handler);
368
369 int index() const { return index_; }
370 const char *key() const { return key_.data(); }
371 int32_t key_bytes() const { return static_cast<int32_t>(key_.size()); }
372
373private:
374 int index_ = -1;
375 vector<char> key_;
376 vector<char> old_key_;
377};
378
379} // namespace bplus_tree
B+树的实现
Definition: bplus_tree.h:450
B+树使用的事务辅助类
Definition: bplus_tree_log.h:170
BufferPool的实现
Definition: disk_buffer_pool.h:189
页帧
Definition: frame.h:66
IndexNode 仅作为数据在内存或磁盘中的表示IndexNodeHandler 负责对IndexNode做各种操作。 作为一个类来说,虚函数会影响“结构体”真实的内存布局,所以将数据存储与操作分开
Definition: bplus_tree.h:267
初始化B+树文件头日志处理类
Definition: bplus_tree_log_entry.h:160
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.cpp:193
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.cpp:218
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:224
创建新的根节点日志处理类
Definition: bplus_tree_log_entry.h:327
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.cpp:456
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.h:333
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:494
初始化内部节点日志处理类
Definition: bplus_tree_log_entry.h:308
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.h:313
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:433
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.h:314
更新内部节点的key日志处理类
Definition: bplus_tree_log_entry.h:356
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.cpp:511
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.cpp:547
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:557
叶子节点初始化日志处理类
Definition: bplus_tree_log_entry.h:264
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.h:269
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:363
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.h:270
设置叶子节点的兄弟节点日志处理类
Definition: bplus_tree_log_entry.h:283
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.cpp:384
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.cpp:409
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:419
B+树日志处理辅助类
Definition: bplus_tree_log_entry.h:76
RC serialize_header(common::Serializer &buffer) const
序列化日志头
Definition: bplus_tree_log_entry.cpp:68
static RC from_buffer(function< RC(PageNum, Frame *&)> frame_getter, common::Deserializer &buffer, unique_ptr< LogEntryHandler > &handler)
从buffer中反序列化出一个LogEntryHandler
Definition: bplus_tree_log_entry.cpp:109
virtual RC serialize_body(common::Serializer &buffer) const =0
序列化日志内容。所有子类应该实现这个函数
PageNum page_num_
page num本来存放在frame中。但是只有在运行时才能拿到frame,为了强制适配 解析文件buffer时不存在运行时的情况,直接记录page num
Definition: bplus_tree_log_entry.h:140
virtual RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler)=0
重做
LogOperation operation_type() const
日志操作类型
Definition: bplus_tree_log_entry.h:92
Frame * frame()
返回日志对应的frame
Definition: bplus_tree_log_entry.h:85
RC serialize(common::Serializer &buffer) const
序列化日志
Definition: bplus_tree_log_entry.cpp:59
virtual RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler)=0
回滚
B+树日志操作类型
Definition: bplus_tree_log_entry.h:39
Type
Definition: bplus_tree_log_entry.h:42
@ LEAF_INIT_EMPTY
设置父节点
@ INTERNAL_INIT_EMPTY
设置叶子节点的兄弟节点
@ NODE_INSERT
更新内部节点的key
@ MAX_TYPE
在节点中间(也可能是末尾)删除一些元素
@ LEAF_SET_NEXT_PAGE
初始化叶子节点
@ SET_PARENT_PAGE
更新根节点
@ UPDATE_ROOT_PAGE
初始化B+树文件头
@ INTERNAL_UPDATE_KEY
创建新的根节点
@ INTERNAL_CREATE_NEW_ROOT
初始化内部节点
@ NODE_REMOVE
在节点中间(也可能是末尾)插入一些元素
节点相关的操作
Definition: bplus_tree_log_entry.h:148
插入或者删除一些节点上的元素
Definition: bplus_tree_log_entry.h:234
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.cpp:285
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.cpp:326
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:339
设置父节点日志处理类
Definition: bplus_tree_log_entry.h:209
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.cpp:238
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.cpp:263
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:272
更新根节点日志处理类
Definition: bplus_tree_log_entry.h:184
RC rollback(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
回滚
Definition: bplus_tree_log_entry.cpp:598
RC redo(BplusTreeMiniTransaction &mtr, BplusTreeHandler &tree_handler) override
重做
Definition: bplus_tree_log_entry.cpp:603
RC serialize_body(common::Serializer &buffer) const override
序列化日志内容。所有子类应该实现这个函数
Definition: bplus_tree_log_entry.cpp:573
反序列化工具
Definition: serializer.h:63
序列化工具
Definition: serializer.h:29
the meta information of bplus tree
Definition: bplus_tree.h:170