22#include "common/lang/comparator.h"
23#include "common/lang/memory.h"
24#include "common/lang/sstream.h"
25#include "common/lang/functional.h"
26#include "common/log/log.h"
27#include "sql/parser/parse_defs.h"
28#include "storage/buffer/disk_buffer_pool.h"
29#include "storage/record/record_manager.h"
30#include "storage/index/latch_memo.h"
31#include "storage/index/bplus_tree_log.h"
59 void init(AttrType type,
int length)
62 attr_length_ = length;
65 int attr_length()
const {
return attr_length_; }
67 int operator()(
const char *v1,
const char *v2)
const
71 left.set_type(attr_type_);
72 left.set_data(v1, attr_length_);
74 right.set_type(attr_type_);
75 right.set_data(v2, attr_length_);
76 return DataType::type_instance(attr_type_)->
compare(left, right);
92 void init(AttrType type,
int length) { attr_comparator_.init(type, length); }
94 const AttrComparator &attr_comparator()
const {
return attr_comparator_; }
96 int operator()(
const char *v1,
const char *v2)
const
98 int result = attr_comparator_(v1, v2);
103 const RID *rid1 = (
const RID *)(v1 + attr_comparator_.attr_length());
104 const RID *rid2 = (
const RID *)(v2 + attr_comparator_.attr_length());
105 return RID::compare(rid1, rid2);
119 void init(AttrType type,
int length)
122 attr_length_ = length;
125 int attr_length()
const {
return attr_length_; }
127 string operator()(
const char *v)
const
129 Value value(attr_type_,
const_cast<char *
>(v), attr_length_);
130 return value.to_string();
145 void init(AttrType type,
int length) { attr_printer_.init(type, length); }
147 const AttrPrinter &attr_printer()
const {
return attr_printer_; }
149 string operator()(
const char *v)
const
152 ss <<
"{key:" << attr_printer_(v) <<
",";
154 const RID *rid = (
const RID *)(v + attr_printer_.attr_length());
155 ss <<
"rid:{" << rid->to_string() <<
"}}";
183 const string to_string()
const
189 <<
"attr_type:" << attr_type_to_string(
attr_type) <<
","
208 static constexpr int HEADER_SIZE = 12;
230 static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE + 4;
232 PageNum next_brother;
252 static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE;
286 void increase_size(
int n);
288 int max_size()
const;
289 int min_size()
const;
290 RC set_parent_page_num(PageNum page_num);
291 PageNum parent_page_num()
const;
292 PageNum page_num()
const;
307 Frame *frame()
const {
return frame_; }
311 RC recover_insert_items(
int index,
const char *items,
int num);
312 RC recover_remove_items(
int index,
int num);
320 virtual char *
__item_at(
int index)
const {
return nullptr; }
321 char *__key_at(
int index)
const {
return __item_at(index); }
327 Frame *frame_ =
nullptr;
342 RC set_next_page(PageNum page_num);
343 PageNum next_page()
const;
345 char *key_at(
int index);
346 char *value_at(
int index);
354 RC insert(
int index,
const char *key,
const char *value);
355 RC remove(
int index);
356 int remove(
const char *key,
const KeyComparator &comparator);
370 char *
__item_at(
int index)
const override;
372 RC append(
const char *items,
int num);
373 RC append(
const char *item);
374 RC preappend(
const char *item);
391 RC create_new_root(PageNum first_page_num,
const char *key, PageNum page_num);
394 char *key_at(
int index);
395 PageNum value_at(
int index);
401 void set_key_at(
int index,
const char *key);
402 void remove(
int index);
413 const KeyComparator &comparator,
const char *key,
bool *found =
nullptr,
int *insert_position =
nullptr)
const;
430 RC insert_items(
int index,
const char *items,
int num);
431 RC
append(
const char *items,
int num);
432 RC
append(
const char *item);
433 RC preappend(
const char *item);
436 char *
__item_at(
int index)
const override;
463 int internal_max_size = -1,
int leaf_max_size = -1);
465 int internal_max_size = -1,
int leaf_max_size = -1);
496 bool is_empty()
const;
503 RC
get_entry(
const char *user_key,
int key_len, list<RID> &rids);
517 LogHandler &log_handler()
const {
return *log_handler_; }
543 RC print_internal_node_recursive(
Frame *frame);
586 template <
typename IndexNodeHandlerType>
593 template <
typename IndexNodeHandlerType>
600 template <
typename IndexNodeHandlerType>
607 template <
typename IndexNodeHandlerType>
636 common::MemPoolItem::item_unique_ptr make_key(
const char *user_key,
const RID &rid);
651 unique_ptr<common::MemPoolItem> mem_pool_item_;
655 friend class BplusTreeTester;
678 RC
open(
const char *left_user_key,
int left_len,
bool left_inclusive,
const char *right_user_key,
int right_len,
679 bool right_inclusive);
702 RC
fix_user_key(
const char *user_key,
int key_len,
bool want_greater,
char **fixed_key,
bool *should_inclusive);
704 void fetch_item(
RID &rid);
712 bool inited_ =
false;
720 common::MemPoolItem::item_unique_ptr right_key_;
721 int iter_index_ = -1;
722 bool first_emitted_ =
false;
属性比较(BplusTree)
Definition: bplus_tree.h:57
属性打印,调试使用(BplusTree)
Definition: bplus_tree.h:117
B+树的实现
Definition: bplus_tree.h:450
RC close()
Definition: bplus_tree.cpp:966
RC get_entry(const char *user_key, int key_len, list< RID > &rids)
获取指定值的record
Definition: bplus_tree.cpp:1545
RC crabing_protocal_fetch_page(BplusTreeMiniTransaction &mtr, BplusTreeOperationType op, PageNum page_num, bool is_root_page, Frame *&frame)
使用crabing protocol 获取页面
Definition: bplus_tree.cpp:1239
RC delete_entry(const char *user_key, const RID *rid)
从IndexHandle句柄对应的索引中删除一个值为(user_key,rid)的索引项
Definition: bplus_tree.cpp:1785
IndexFileHeader file_header_
是否需要更新头页面
Definition: bplus_tree.h:642
void update_root_page_num_locked(BplusTreeMiniTransaction &mtr, PageNum root_page_num)
更新根节点的页号
Definition: bplus_tree.cpp:1445
RC recover_init_header_page(BplusTreeMiniTransaction &mtr, Frame *frame, const IndexFileHeader &header)
恢复初始化头页面
Definition: bplus_tree.cpp:1431
RC left_most_page(BplusTreeMiniTransaction &mtr, Frame *&frame)
找到最左边的叶子节点
Definition: bplus_tree.cpp:1195
RC recover_update_root_page(BplusTreeMiniTransaction &mtr, PageNum root_page_num)
恢复更新ROOT页面
Definition: bplus_tree.cpp:1425
RC adjust_root(BplusTreeMiniTransaction &mtr, Frame *root_frame)
调整根节点
Definition: bplus_tree.cpp:1568
bool header_dirty_
磁盘缓冲池
Definition: bplus_tree.h:641
RC open(LogHandler &log_handler, BufferPoolManager &bpm, const char *file_name)
打开一个B+树
Definition: bplus_tree.cpp:906
RC create(LogHandler &log_handler, BufferPoolManager &bpm, const char *file_name, AttrType attr_type, int attr_length, int internal_max_size=-1, int leaf_max_size=-1)
创建一个B+树
Definition: bplus_tree.cpp:795
RC find_leaf(BplusTreeMiniTransaction &mtr, BplusTreeOperationType op, const char *key, Frame *&frame)
查找叶子节点
Definition: bplus_tree.cpp:1187
RC redistribute(BplusTreeMiniTransaction &mtr, Frame *neighbor_frame, Frame *frame, Frame *parent_frame, int index)
重新分配两个相邻节点
Definition: bplus_tree.cpp:1733
bool validate_tree()
Definition: bplus_tree.cpp:1159
RC coalesce(BplusTreeMiniTransaction &mtr, Frame *neighbor_frame, Frame *frame, Frame *parent_frame, int index)
合并两个相邻节点
Definition: bplus_tree.cpp:1686
DiskBufferPool * disk_buffer_pool_
日志处理器
Definition: bplus_tree.h:640
RC print_leaf(Frame *frame)
Definition: bplus_tree.cpp:976
RC insert_entry_into_parent(BplusTreeMiniTransaction &mtr, Frame *frame, Frame *new_frame, const char *key)
在父节点插入一个元素
Definition: bplus_tree.cpp:1299
RC find_leaf_internal(BplusTreeMiniTransaction &mtr, BplusTreeOperationType op, const function< PageNum(InternalIndexNodeHandler &)> &child_page_getter, Frame *&frame)
查找指定的叶子节点
Definition: bplus_tree.cpp:1201
RC delete_entry_internal(BplusTreeMiniTransaction &mtr, Frame *leaf_frame, const char *key)
从叶子节点中删除指定的键值对
Definition: bplus_tree.cpp:1764
RC coalesce_or_redistribute(BplusTreeMiniTransaction &mtr, Frame *frame)
合并或重新分配
Definition: bplus_tree.cpp:1610
RC insert_entry(const char *user_key, const RID *rid)
此函数向IndexHandle对应的索引中插入一个索引项。
Definition: bplus_tree.cpp:1498
RC insert_entry_into_leaf_node(BplusTreeMiniTransaction &mtr, Frame *frame, const char *pkey, const RID *rid)
在叶子节点插入一个元素
Definition: bplus_tree.cpp:1261
RC split(BplusTreeMiniTransaction &mtr, Frame *frame, Frame *&new_frame)
拆分节点
Definition: bplus_tree.cpp:1401
RC print_tree()
Definition: bplus_tree.cpp:1022
RC create_new_tree(BplusTreeMiniTransaction &mtr, const char *key, const RID *rid)
创建一个新的B+树
Definition: bplus_tree.cpp:1459
B+树使用的事务辅助类
Definition: bplus_tree_log.h:170
B+树的扫描器
Definition: bplus_tree.h:663
Frame * current_frame_
使用左右叶子节点和位置来表示扫描的起始位置和终止位置 起始位置和终止位置都是有效的数据
Definition: bplus_tree.h:718
RC fix_user_key(const char *user_key, int key_len, bool want_greater, char **fixed_key, bool *should_inclusive)
Definition: bplus_tree.cpp:2046
RC open(const char *left_user_key, int left_len, bool left_inclusive, const char *right_user_key, int right_len, bool right_inclusive)
扫描指定范围的数据
Definition: bplus_tree.cpp:1828
bool touch_end()
判断是否到了扫描的结束位置
Definition: bplus_tree.cpp:1972
RC close()
关闭当前扫描器
Definition: bplus_tree.cpp:2039
RC next_entry(RID &rid)
获取下一条记录
Definition: bplus_tree.cpp:1985
BufferPool的管理类
Definition: disk_buffer_pool.h:322
virtual int compare(const Value &left, const Value &right) const
Definition: data_type.h:48
BufferPool的实现
Definition: disk_buffer_pool.h:189
IndexNode 仅作为数据在内存或磁盘中的表示IndexNodeHandler 负责对IndexNode做各种操作。 作为一个类来说,虚函数会影响“结构体”真实的内存布局,所以将数据存储与操作分开
Definition: bplus_tree.h:267
virtual char * __item_at(int index) const
获取指定元素的开始内存位置
Definition: bplus_tree.h:320
virtual int item_size() const
存储的键值对的大小。值是指叶子节点中存放的数据
Definition: bplus_tree.cpp:68
void init_empty(bool leaf)
初始化一个新的页面
Definition: bplus_tree.cpp:52
virtual int value_size() const
存储的值的大小。内部节点和叶子节点是不一样的,但是这里返回的是叶子节点存储的大小
Definition: bplus_tree.cpp:62
bool validate() const
验证当前节点是否有问题
Definition: bplus_tree.cpp:143
bool is_leaf() const
是否叶子节点
Definition: bplus_tree.cpp:51
bool is_safe(BplusTreeOperationType op, bool is_root_node)
判断对指定的操作,是否安全的
Definition: bplus_tree.cpp:103
virtual int key_size() const
存储的键值大小
Definition: bplus_tree.cpp:60
内部节点的操作
Definition: bplus_tree.h:385
int lookup(const KeyComparator &comparator, const char *key, bool *found=nullptr, int *insert_position=nullptr) const
Definition: bplus_tree.cpp:513
RC move_half_to(InternalIndexNodeHandler &other)
move half of the items to the other node ends
Definition: bplus_tree.cpp:492
RC move_to(InternalIndexNodeHandler &other)
把当前节点的所有数据都迁移到另一个节点上
Definition: bplus_tree.cpp:584
char * __item_at(int index) const override
获取指定元素的开始内存位置
Definition: bplus_tree.cpp:677
int value_index(PageNum page_num)
Definition: bplus_tree.cpp:561
RC insert(const char *key, PageNum page_num, const KeyComparator &comparator)
insert one entry
Definition: bplus_tree.cpp:473
int item_size() const override
存储的键值对的大小。值是指叶子节点中存放的数据
Definition: bplus_tree.cpp:681
RC append(const char *items, int num)
Definition: bplus_tree.cpp:665
int value_size() const override
存储的值的大小。内部节点和叶子节点是不一样的,但是这里返回的是叶子节点存储的大小
Definition: bplus_tree.cpp:679
键值比较(BplusTree)
Definition: bplus_tree.h:90
键值打印,调试使用(BplusTree)
Definition: bplus_tree.h:143
叶子节点的操作
Definition: bplus_tree.h:336
int lookup(const KeyComparator &comparator, const char *key, bool *found=nullptr) const
Definition: bplus_tree.cpp:226
char * __item_at(int index) const override
获取指定元素的开始内存位置
Definition: bplus_tree.cpp:346
RC move_to(LeafIndexNodeHandler &other)
Definition: bplus_tree.cpp:310
对外提供服务的CLog模块
Definition: log_handler.h:40
属性的值
Definition: value.h:30
BplusTreeOperationType
B+树的操作类型
Definition: bplus_tree.h:46
the common part of page describtion of bplus tree
Definition: bplus_tree.h:207
int key_num
当前是叶子节点还是内部节点
Definition: bplus_tree.h:211
PageNum parent
当前页面上一共有多少个键值对
Definition: bplus_tree.h:212
internal page of bplus tree
Definition: bplus_tree.h:251
char array[0]
Definition: bplus_tree.h:257
leaf page of bplus tree
Definition: bplus_tree.h:229
char array[0]
Definition: bplus_tree.h:236
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35