25#include "storage/record/record_manager.h"
26#include "storage/buffer/disk_buffer_pool.h"
27#include "storage/trx/latch_memo.h"
28#include "sql/parser/parse_defs.h"
29#include "common/lang/comparator.h"
30#include "common/log/log.h"
55 void init(AttrType type,
int length)
58 attr_length_ = length;
61 int attr_length()
const
66 int operator()(
const char *v1,
const char *v2)
const
70 return common::compare_int((
void *)v1, (
void *)v2);
73 return common::compare_float((
void *)v1, (
void *)v2);
76 return common::compare_string((
void *)v1, attr_length_, (
void *)v2, attr_length_);
79 ASSERT(
false,
"unknown attr type. %d", attr_type_);
98 void init(AttrType type,
int length)
100 attr_comparator_.init(type, length);
105 return attr_comparator_;
108 int operator()(
const char *v1,
const char *v2)
const
110 int result = attr_comparator_(v1, v2);
115 const RID *rid1 = (
const RID *)(v1 + attr_comparator_.attr_length());
116 const RID *rid2 = (
const RID *)(v2 + attr_comparator_.attr_length());
117 return RID::compare(rid1, rid2);
131 void init(AttrType type,
int length)
134 attr_length_ = length;
137 int attr_length()
const
142 std::string operator()(
const char *v)
const
144 switch (attr_type_) {
146 return std::to_string(*(
int *)v);
149 return std::to_string(*(
float *)v);
153 for (
int i = 0; i < attr_length_; i++) {
162 ASSERT(
false,
"unknown attr type. %d", attr_type_);
165 return std::string();
180 void init(AttrType type,
int length)
182 attr_printer_.init(type, length);
187 return attr_printer_;
190 std::string operator()(
const char *v)
const
192 std::stringstream ss;
193 ss <<
"{key:" << attr_printer_(v) <<
",";
195 const RID *rid = (
const RID *)(v + attr_printer_.attr_length());
196 ss <<
"rid:{" << rid->to_string() <<
"}}";
224 const std::string to_string()
226 std::stringstream ss;
249 static constexpr int HEADER_SIZE = 12;
271 static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE + 4;
273 PageNum next_brother;
293 static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE;
313 void init_empty(
bool leaf);
315 bool is_leaf()
const;
316 int key_size()
const;
317 int value_size()
const;
318 int item_size()
const;
320 void increase_size(
int n);
322 int max_size()
const;
323 int min_size()
const;
324 void set_parent_page_num(PageNum page_num);
325 PageNum parent_page_num()
const;
326 PageNum page_num()
const;
330 bool validate()
const;
351 void set_next_page(PageNum page_num);
352 PageNum next_page()
const;
354 char *key_at(
int index);
355 char *value_at(
int index);
363 void insert(
int index,
const char *key,
const char *value);
364 void remove(
int index);
365 int remove(
const char *key,
const KeyComparator &comparator);
379 char *__item_at(
int index)
const;
380 char *__key_at(
int index)
const;
381 char *__value_at(
int index)
const;
383 void append(
const char *item);
384 void preappend(
const char *item);
401 void create_new_root(PageNum first_page_num,
const char *key, PageNum page_num);
405 char *key_at(
int index);
406 PageNum value_at(
int index);
412 void set_key_at(
int index,
const char *key);
413 void remove(
int index);
425 bool *found =
nullptr,
426 int *insert_position =
nullptr)
const;
443 char *__item_at(
int index)
const;
444 char *__key_at(
int index)
const;
445 char *__value_at(
int index)
const;
447 int value_size()
const;
448 int item_size()
const;
465 RC
create(
const char *file_name,
468 int internal_max_size = -1,
469 int leaf_max_size = -1);
476 RC
open(
const char *file_name);
498 bool is_empty()
const;
505 RC
get_entry(
const char *user_key,
int key_len, std::list<RID> &rids);
528 RC print_internal_node_recursive(
Frame *frame);
530 bool validate_leaf_link(
LatchMemo &latch_memo);
531 bool validate_node_recursive(
LatchMemo &latch_memo,
Frame *frame);
542 RC insert_into_parent(
LatchMemo &latch_memo, PageNum parent_page,
Frame *left_frame,
const char *pkey,
545 RC delete_entry_internal(
LatchMemo &latch_memo,
Frame *leaf_frame,
const char *key);
547 template <
typename IndexNodeHandlerType>
549 template <
typename IndexNodeHandlerType>
551 template <
typename IndexNodeHandlerType>
553 template <
typename IndexNodeHandlerType>
554 RC redistribute(
Frame *neighbor_frame,
Frame *frame,
Frame *parent_frame,
int index);
557 RC insert_entry_into_leaf_node(
LatchMemo &latch_memo,
Frame *frame,
const char *pkey,
const RID *rid);
558 RC create_new_tree(
const char *key,
const RID *rid);
560 void update_root_page_num(PageNum root_page_num);
561 void update_root_page_num_locked(PageNum root_page_num);
566 common::MemPoolItem::unique_ptr make_key(
const char *user_key,
const RID &rid);
567 void free_key(
char *key);
571 bool header_dirty_ =
false;
576 common::SharedMutex root_lock_;
581 std::unique_ptr<common::MemPoolItem> mem_pool_item_;
585 friend class BplusTreeTester;
607 RC
open(
const char *left_user_key,
int left_len,
bool left_inclusive,
608 const char *right_user_key,
int right_len,
bool right_inclusive);
618 RC
fix_user_key(
const char *user_key,
int key_len,
bool want_greater,
char **fixed_key,
bool *should_inclusive);
620 void fetch_item(
RID &rid);
624 bool inited_ =
false;
633 common::MemPoolItem::unique_ptr right_key_;
634 int iter_index_ = -1;
635 bool first_emitted_ =
false;
属性比较(BplusTree)
Definition: bplus_tree.h:53
属性打印,调试使用(BplusTree)
Definition: bplus_tree.h:129
B+树的实现
Definition: bplus_tree.h:459
RC close()
Definition: bplus_tree.cpp:866
RC delete_entry(const char *user_key, const RID *rid)
Definition: bplus_tree.cpp:1621
RC insert_entry_into_parent(LatchMemo &latch_memo, Frame *frame, Frame *new_frame, const char *key)
Definition: bplus_tree.cpp:1193
RC create(const char *file_name, AttrType attr_type, int attr_length, int internal_max_size=-1, int leaf_max_size=-1)
Definition: bplus_tree.cpp:748
bool validate_tree()
Definition: bplus_tree.cpp:1051
RC print_leaf(Frame *frame)
Definition: bplus_tree.cpp:876
RC insert_entry(const char *user_key, const RID *rid)
Definition: bplus_tree.cpp:1363
RC get_entry(const char *user_key, int key_len, std::list< RID > &rids)
Definition: bplus_tree.cpp:1407
RC open(const char *file_name)
Definition: bplus_tree.cpp:822
RC print_tree()
Definition: bplus_tree.cpp:919
RC split(LatchMemo &latch_memo, Frame *frame, Frame *&new_frame)
Definition: bplus_tree.cpp:1293
B+树的扫描器
Definition: bplus_tree.h:593
Frame * current_frame_
使用左右叶子节点和位置来表示扫描的起始位置和终止位置 起始位置和终止位置都是有效的数据
Definition: bplus_tree.h:631
RC fix_user_key(const char *user_key, int key_len, bool want_greater, char **fixed_key, bool *should_inclusive)
Definition: bplus_tree.cpp:1872
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:1663
RC next_entry(RID &rid)
Definition: bplus_tree.cpp:1813
BufferPool的实现
Definition: disk_buffer_pool.h:193
IndexNode 仅作为数据在内存或磁盘中的表示IndexNodeHandler 负责对IndexNode做各种操作。 作为一个类来说,虚函数会影响“结构体”真实的内存布局,所以将数据存储与操作分开
Definition: bplus_tree.h:308
bool is_safe(BplusTreeOperationType op, bool is_root_node)
Definition: bplus_tree.cpp:113
内部节点的操作
Definition: bplus_tree.h:395
int lookup(const KeyComparator &comparator, const char *key, bool *found=nullptr, int *insert_position=nullptr) const
Definition: bplus_tree.cpp:453
int value_index(PageNum page_num)
Definition: bplus_tree.cpp:499
void insert(const char *key, PageNum page_num, const KeyComparator &comparator)
Definition: bplus_tree.cpp:422
RC copy_from(const char *items, int num, DiskBufferPool *disk_buffer_pool)
Definition: bplus_tree.cpp:559
键值比较(BplusTree)
Definition: bplus_tree.h:96
键值打印,调试使用(BplusTree)
Definition: bplus_tree.h:178
Definition: latch_memo.h:50
叶子节点的操作
Definition: bplus_tree.h:345
int lookup(const KeyComparator &comparator, const char *key, bool *found=nullptr) const
Definition: bplus_tree.cpp:202
RC move_to(LeafIndexNodeHandler &other, DiskBufferPool *bp)
Definition: bplus_tree.cpp:271
BplusTreeOperationType
B+树的操作类型
Definition: bplus_tree.h:42
the common part of page describtion of bplus tree
Definition: bplus_tree.h:248
internal page of bplus tree
Definition: bplus_tree.h:292
char array[0]
Definition: bplus_tree.h:298
leaf page of bplus tree
Definition: bplus_tree.h:270
char array[0]
Definition: bplus_tree.h:277
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35