bplus_tree.h 16.4 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
//
// Created by Xie Meiyi
// Rewritten by Longda & Wangyunlai
//
//
17 18

#pragma once
羽飞's avatar
羽飞 已提交
19 20 21

#include <string.h>
#include <sstream>
羽飞's avatar
羽飞 已提交
22
#include <functional>
23
#include <memory>
羽飞's avatar
羽飞 已提交
24

羽飞's avatar
羽飞 已提交
25
#include "storage/record/record_manager.h"
羽飞's avatar
羽飞 已提交
26
#include "storage/default/disk_buffer_pool.h"
27
#include "storage/trx/latch_memo.h"
羽飞's avatar
羽飞 已提交
28
#include "sql/parser/parse_defs.h"
29
#include "common/lang/comparator.h"
30
#include "common/log/log.h"
羽飞's avatar
羽飞 已提交
31

32
#define EMPTY_RID_PAGE_NUM -1 // TODO remove me
羽飞's avatar
羽飞 已提交
33 34
#define EMPTY_RID_SLOT_NUM -1

35 36 37 38 39 40 41 42 43
enum class BplusTreeOperationType
{
  READ,
  INSERT,
  DELETE,
};

class AttrComparator 
{
羽飞's avatar
羽飞 已提交
44 45 46 47 48 49 50
public:
  void init(AttrType type, int length)
  {
    attr_type_ = type;
    attr_length_ = length;
  }

L
Longda Feng 已提交
51 52
  int attr_length() const
  {
羽飞's avatar
羽飞 已提交
53 54 55
    return attr_length_;
  }

L
Longda Feng 已提交
56 57
  int operator()(const char *v1, const char *v2) const
  {
羽飞's avatar
羽飞 已提交
58
    switch (attr_type_) {
L
Longda Feng 已提交
59
      case INTS: {
60
        return common::compare_int((void *)v1, (void *)v2);
L
Longda Feng 已提交
61 62
      } break;
      case FLOATS: {
63
        return common::compare_float((void *)v1, (void *)v2);
L
Longda Feng 已提交
64 65
      }
      case CHARS: {
66
        return common::compare_string((void *)v1, attr_length_, (void *)v2, attr_length_);
L
Longda Feng 已提交
67 68
      }
      default: {
69
        ASSERT(false, "unknown attr type. %d", attr_type_);
L
Longda Feng 已提交
70
      }
羽飞's avatar
羽飞 已提交
71 72
    }
  }
L
Longda Feng 已提交
73

羽飞's avatar
羽飞 已提交
74 75 76 77 78
private:
  AttrType attr_type_;
  int attr_length_;
};

79 80
class KeyComparator 
{
羽飞's avatar
羽飞 已提交
81 82 83 84 85 86
public:
  void init(AttrType type, int length)
  {
    attr_comparator_.init(type, length);
  }

L
Longda Feng 已提交
87 88
  const AttrComparator &attr_comparator() const
  {
羽飞's avatar
羽飞 已提交
89 90 91
    return attr_comparator_;
  }

L
Longda Feng 已提交
92 93
  int operator()(const char *v1, const char *v2) const
  {
羽飞's avatar
羽飞 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107
    int result = attr_comparator_(v1, v2);
    if (result != 0) {
      return result;
    }

    const RID *rid1 = (const RID *)(v1 + attr_comparator_.attr_length());
    const RID *rid2 = (const RID *)(v2 + attr_comparator_.attr_length());
    return RID::compare(rid1, rid2);
  }

private:
  AttrComparator attr_comparator_;
};

108 109
class AttrPrinter 
{
羽飞's avatar
羽飞 已提交
110 111 112 113 114 115 116
public:
  void init(AttrType type, int length)
  {
    attr_type_ = type;
    attr_length_ = length;
  }

L
Longda Feng 已提交
117 118
  int attr_length() const
  {
羽飞's avatar
羽飞 已提交
119 120 121
    return attr_length_;
  }

L
Longda Feng 已提交
122 123
  std::string operator()(const char *v) const
  {
羽飞's avatar
羽飞 已提交
124
    switch (attr_type_) {
L
Longda Feng 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      case INTS: {
        return std::to_string(*(int *)v);
      } break;
      case FLOATS: {
        return std::to_string(*(float *)v);
      }
      case CHARS: {
        std::string str;
        for (int i = 0; i < attr_length_; i++) {
          if (v[i] == 0) {
            break;
          }
          str.push_back(v[i]);
        }
        return str;
      }
      default: {
142
        ASSERT(false, "unknown attr type. %d", attr_type_);
羽飞's avatar
羽飞 已提交
143
      }
羽飞's avatar
羽飞 已提交
144 145
    }
  }
L
Longda Feng 已提交
146

羽飞's avatar
羽飞 已提交
147 148 149 150 151
private:
  AttrType attr_type_;
  int attr_length_;
};

152 153
class KeyPrinter 
{
羽飞's avatar
羽飞 已提交
154 155 156 157 158 159
public:
  void init(AttrType type, int length)
  {
    attr_printer_.init(type, length);
  }

L
Longda Feng 已提交
160 161
  const AttrPrinter &attr_printer() const
  {
羽飞's avatar
羽飞 已提交
162 163 164
    return attr_printer_;
  }

L
Longda Feng 已提交
165 166
  std::string operator()(const char *v) const
  {
羽飞's avatar
羽飞 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    std::stringstream ss;
    ss << "{key:" << attr_printer_(v) << ",";

    const RID *rid = (const RID *)(v + attr_printer_.attr_length());
    ss << "rid:{" << rid->to_string() << "}}";
    return ss.str();
  }

private:
  AttrPrinter attr_printer_;
};

/**
 * the meta information of bplus tree
 * this is the first page of bplus tree.
 * only one field can be supported, can you extend it to multi-fields?
 */
184 185
struct IndexFileHeader 
{
羽飞's avatar
羽飞 已提交
186 187 188 189 190
  IndexFileHeader()
  {
    memset(this, 0, sizeof(IndexFileHeader));
    root_page = BP_INVALID_PAGE_NUM;
  }
L
Longda Feng 已提交
191 192 193 194 195
  PageNum root_page;
  int32_t internal_max_size;
  int32_t leaf_max_size;
  int32_t attr_length;
  int32_t key_length;  // attr length + sizeof(RID)
羽飞's avatar
羽飞 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  AttrType attr_type;

  const std::string to_string()
  {
    std::stringstream ss;

    ss << "attr_length:" << attr_length << ","
       << "key_length:" << key_length << ","
       << "attr_type:" << attr_type << ","
       << "root_page:" << root_page << ","
       << "internal_max_size:" << internal_max_size << ","
       << "leaf_max_size:" << leaf_max_size << ";";

    return ss.str();
  }
};

/**
 * the common part of page describtion of bplus tree
 * storage format:
 * | page type | item number | parent page id |
 */
218 219
struct IndexNode 
{
羽飞's avatar
羽飞 已提交
220 221
  static constexpr int HEADER_SIZE = 12;

222 223
  bool    is_leaf;
  int     key_num;
羽飞's avatar
羽飞 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237
  PageNum parent;
};

/**
 * leaf page of bplus tree
 * storage format:
 * | common header | prev page id | next page id |
 * | key0, rid0 | key1, rid1 | ... | keyn, ridn |
 *
 * the key is in format: the key value of record and rid.
 * so the key in leaf page must be unique.
 * the value is rid.
 * can you implenment a cluster index ?
 */
238 239 240
struct LeafIndexNode : public IndexNode 
{
  static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE + 4;
L
Longda Feng 已提交
241

羽飞's avatar
羽飞 已提交
242 243 244 245 246 247 248 249 250 251
  PageNum next_brother;
  /**
   * leaf can store order keys and rids at most
   */
  char array[0];
};

/**
 * internal page of bplus tree
 * storage format:
L
Longda Feng 已提交
252
 * | common header |
羽飞's avatar
羽飞 已提交
253 254 255 256 257
 * | key(0),page_id(0) | key(1), page_id(1) | ... | key(n), page_id(n) |
 *
 * the first key is ignored(key0).
 * so it will waste space, can you fix this?
 */
258 259
struct InternalIndexNode : public IndexNode 
{
羽飞's avatar
羽飞 已提交
260 261 262 263 264 265 266 267
  static constexpr int HEADER_SIZE = IndexNode::HEADER_SIZE;

  /**
   * internal node just store order -1 keys and order rids, the last rid is last rght child.
   */
  char array[0];
};

268 269 270 271 272 273 274
/**
 * IndexNode 仅作为数据在内存或磁盘中的表示
 * IndexNodeHandler 负责对IndexNode做各种操作。
 * 作为一个类来说,虚函数会影响“结构体”真实的内存布局,所以将数据存储与操作分开
 */
class IndexNodeHandler 
{
羽飞's avatar
羽飞 已提交
275
public:
羽飞's avatar
羽飞 已提交
276
  IndexNodeHandler(const IndexFileHeader &header, Frame *frame);
277
  virtual ~IndexNodeHandler() = default;
羽飞's avatar
羽飞 已提交
278 279 280 281

  void init_empty(bool leaf);

  bool is_leaf() const;
282 283 284
  int  key_size() const;
  int  value_size() const;
  int  item_size() const;
羽飞's avatar
羽飞 已提交
285 286

  void increase_size(int n);
287 288 289
  int  size() const;
  int  max_size() const;
  int  min_size() const;
羽飞's avatar
羽飞 已提交
290 291 292 293
  void set_parent_page_num(PageNum page_num);
  PageNum parent_page_num() const;
  PageNum page_num() const;

294 295
  bool is_safe(BplusTreeOperationType op, bool is_root_node);

羽飞's avatar
羽飞 已提交
296 297 298 299 300 301 302 303 304 305
  bool validate() const;

  friend std::string to_string(const IndexNodeHandler &handler);

protected:
  const IndexFileHeader &header_;
  PageNum page_num_;
  IndexNode *node_;
};

306 307
class LeafIndexNodeHandler : public IndexNodeHandler 
{
L
Longda Feng 已提交
308
public:
羽飞's avatar
羽飞 已提交
309
  LeafIndexNodeHandler(const IndexFileHeader &header, Frame *frame);
310
  virtual ~LeafIndexNodeHandler() = default;
羽飞's avatar
羽飞 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

  void init_empty();
  void set_next_page(PageNum page_num);
  PageNum next_page() const;

  char *key_at(int index);
  char *value_at(int index);

  /**
   * 查找指定key的插入位置(注意不是key本身)
   * 如果key已经存在,会设置found的值
   * NOTE: 当前lookup的实现效率非常低,你是否可以优化它?
   */
  int lookup(const KeyComparator &comparator, const char *key, bool *found = nullptr) const;

  void insert(int index, const char *key, const char *value);
  void remove(int index);
328
  int  remove(const char *key, const KeyComparator &comparator);
L
Longda Feng 已提交
329 330 331
  RC move_half_to(LeafIndexNodeHandler &other, DiskBufferPool *bp);
  RC move_first_to_end(LeafIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool);
  RC move_last_to_front(LeafIndexNodeHandler &other, DiskBufferPool *bp);
羽飞's avatar
羽飞 已提交
332 333 334
  /**
   * move all items to left page
   */
羽飞's avatar
羽飞 已提交
335
  RC move_to(LeafIndexNodeHandler &other, DiskBufferPool *bp);
羽飞's avatar
羽飞 已提交
336

羽飞's avatar
羽飞 已提交
337
  bool validate(const KeyComparator &comparator, DiskBufferPool *bp) const;
羽飞's avatar
羽飞 已提交
338 339

  friend std::string to_string(const LeafIndexNodeHandler &handler, const KeyPrinter &printer);
L
Longda Feng 已提交
340

羽飞's avatar
羽飞 已提交
341 342 343 344 345 346 347 348 349 350 351 352
private:
  char *__item_at(int index) const;
  char *__key_at(int index) const;
  char *__value_at(int index) const;

  void append(const char *item);
  void preappend(const char *item);

private:
  LeafIndexNode *leaf_node_;
};

353 354
class InternalIndexNodeHandler : public IndexNodeHandler 
{
羽飞's avatar
羽飞 已提交
355
public:
羽飞's avatar
羽飞 已提交
356
  InternalIndexNodeHandler(const IndexFileHeader &header, Frame *frame);
357
  virtual ~InternalIndexNodeHandler() = default;
羽飞's avatar
羽飞 已提交
358 359 360 361 362

  void init_empty();
  void create_new_root(PageNum first_page_num, const char *key, PageNum page_num);

  void insert(const char *key, PageNum page_num, const KeyComparator &comparator);
L
Longda Feng 已提交
363
  RC move_half_to(LeafIndexNodeHandler &other, DiskBufferPool *bp);
羽飞's avatar
羽飞 已提交
364 365 366 367 368 369
  char *key_at(int index);
  PageNum value_at(int index);

  /**
   * 返回指定子节点在当前节点中的索引
   */
L
Longda Feng 已提交
370
  int value_index(PageNum page_num);
羽飞's avatar
羽飞 已提交
371 372 373 374 375 376
  void set_key_at(int index, const char *key);
  void remove(int index);

  /**
   * 与Leaf节点不同,lookup返回指定key应该属于哪个子节点,返回这个子节点在当前节点中的索引
   * 如果想要返回插入位置,就提供 `insert_position` 参数
377 378 379 380
   * @param comparator 用于键值比较的函数
   * @param key 查找的键值
   * @param found 如果是有效指针,将会返回当前是否存在指定的键值
   * @param insert_position 如果是有效指针,将会返回可以插入指定键值的位置
羽飞's avatar
羽飞 已提交
381 382
   * NOTE: 查找效率不高,你可以优化它吗?
   */
383 384 385 386
  int lookup(const KeyComparator &comparator, 
             const char *key, 
             bool *found = nullptr, 
             int *insert_position = nullptr) const;
羽飞's avatar
羽飞 已提交
387

羽飞's avatar
羽飞 已提交
388 389 390 391
  RC move_to(InternalIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool);
  RC move_first_to_end(InternalIndexNodeHandler &other, DiskBufferPool *disk_buffer_pool);
  RC move_last_to_front(InternalIndexNodeHandler &other, DiskBufferPool *bp);
  RC move_half_to(InternalIndexNodeHandler &other, DiskBufferPool *bp);
羽飞's avatar
羽飞 已提交
392

羽飞's avatar
羽飞 已提交
393
  bool validate(const KeyComparator &comparator, DiskBufferPool *bp) const;
羽飞's avatar
羽飞 已提交
394 395

  friend std::string to_string(const InternalIndexNodeHandler &handler, const KeyPrinter &printer);
L
Longda Feng 已提交
396

羽飞's avatar
羽飞 已提交
397
private:
羽飞's avatar
羽飞 已提交
398 399 400
  RC copy_from(const char *items, int num, DiskBufferPool *disk_buffer_pool);
  RC append(const char *item, DiskBufferPool *bp);
  RC preappend(const char *item, DiskBufferPool *bp);
羽飞's avatar
羽飞 已提交
401 402 403 404 405 406 407 408 409 410

private:
  char *__item_at(int index) const;
  char *__key_at(int index) const;
  char *__value_at(int index) const;

  int value_size() const;
  int item_size() const;

private:
411
  InternalIndexNode *internal_node_ = nullptr;
羽飞's avatar
羽飞 已提交
412 413
};

414 415
class BplusTreeHandler 
{
羽飞's avatar
羽飞 已提交
416 417 418 419 420
public:
  /**
   * 此函数创建一个名为fileName的索引。
   * attrType描述被索引属性的类型,attrLength描述被索引属性的长度
   */
421 422 423 424 425
  RC create(const char *file_name, 
            AttrType attr_type, 
            int attr_length, 
            int internal_max_size = -1, 
            int leaf_max_size = -1);
羽飞's avatar
羽飞 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

  /**
   * 打开名为fileName的索引文件。
   * 如果方法调用成功,则indexHandle为指向被打开的索引句柄的指针。
   * 索引句柄用于在索引中插入或删除索引项,也可用于索引的扫描
   */
  RC open(const char *file_name);

  /**
   * 关闭句柄indexHandle对应的索引文件
   */
  RC close();

  /**
   * 此函数向IndexHandle对应的索引中插入一个索引项。
   * 参数user_key指向要插入的属性值,参数rid标识该索引项对应的元组,
   * 即向索引中插入一个值为(user_key,rid)的键值对
羽飞's avatar
羽飞 已提交
443
   * @note 这里假设user_key的内存大小与attr_length 一致
羽飞's avatar
羽飞 已提交
444 445 446 447 448 449
   */
  RC insert_entry(const char *user_key, const RID *rid);

  /**
   * 从IndexHandle句柄对应的索引中删除一个值为(*pData,rid)的索引项
   * @return RECORD_INVALID_KEY 指定值不存在
羽飞's avatar
羽飞 已提交
450
   * @note 这里假设user_key的内存大小与attr_length 一致
羽飞's avatar
羽飞 已提交
451 452 453 454 455 456 457
   */
  RC delete_entry(const char *user_key, const RID *rid);

  bool is_empty() const;

  /**
   * 获取指定值的record
羽飞's avatar
羽飞 已提交
458
   * @param key_len user_key的长度
羽飞's avatar
羽飞 已提交
459 460
   * @param rid  返回值,记录记录所在的页面号和slot
   */
羽飞's avatar
羽飞 已提交
461
  RC get_entry(const char *user_key, int key_len, std::list<RID> &rids);
羽飞's avatar
羽飞 已提交
462 463 464 465 466

  RC sync();

  /**
   * Check whether current B+ tree is invalid or not.
467 468
   * @return true means current tree is valid, return false means current tree is invalid.
   * @note thread unsafe
羽飞's avatar
羽飞 已提交
469 470 471 472
   */
  bool validate_tree();

public:
473 474 475
  /**
   * 这些函数都是线程不安全的,不要在多线程的环境下调用
   */
羽飞's avatar
羽飞 已提交
476 477 478 479
  RC print_tree();
  RC print_leafs();

private:
480 481 482
  /**
   * 这些函数都是线程不安全的,不要在多线程的环境下调用
   */
羽飞's avatar
羽飞 已提交
483 484
  RC print_leaf(Frame *frame);
  RC print_internal_node_recursive(Frame *frame);
羽飞's avatar
羽飞 已提交
485

486 487
  bool validate_leaf_link(LatchMemo &latch_memo);
  bool validate_node_recursive(LatchMemo &latch_memo, Frame *frame);
羽飞's avatar
羽飞 已提交
488 489

protected:
490 491 492 493 494 495 496
  RC find_leaf(LatchMemo &latch_memo, BplusTreeOperationType op, const char *key, Frame *&frame);
  RC left_most_page(LatchMemo &latch_memo, Frame *&frame);
  RC find_leaf_internal(LatchMemo &latch_memo, BplusTreeOperationType op, 
                        const std::function<PageNum(InternalIndexNodeHandler &)> &child_page_getter, 
                        Frame *&frame);
  RC crabing_protocal_fetch_page(LatchMemo &latch_memo, BplusTreeOperationType op, PageNum page_num, bool is_root_page,
                                 Frame *&frame);
羽飞's avatar
羽飞 已提交
497

498 499
  RC insert_into_parent(LatchMemo &latch_memo, PageNum parent_page, Frame *left_frame, const char *pkey, 
                        Frame &right_frame);
羽飞's avatar
羽飞 已提交
500

501
  RC delete_entry_internal(LatchMemo &latch_memo, Frame *leaf_frame, const char *key);
羽飞's avatar
羽飞 已提交
502 503

  template <typename IndexNodeHandlerType>
504
  RC split(LatchMemo &latch_memo, Frame *frame, Frame *&new_frame);
羽飞's avatar
羽飞 已提交
505
  template <typename IndexNodeHandlerType>
506
  RC coalesce_or_redistribute(LatchMemo &latch_memo, Frame *frame);
羽飞's avatar
羽飞 已提交
507
  template <typename IndexNodeHandlerType>
508
  RC coalesce(LatchMemo &latch_memo, Frame *neighbor_frame, Frame *frame, Frame *parent_frame, int index);
羽飞's avatar
羽飞 已提交
509
  template <typename IndexNodeHandlerType>
羽飞's avatar
羽飞 已提交
510
  RC redistribute(Frame *neighbor_frame, Frame *frame, Frame *parent_frame, int index);
羽飞's avatar
羽飞 已提交
511

512 513
  RC insert_entry_into_parent(LatchMemo &latch_memo, Frame *frame, Frame *new_frame, const char *key);
  RC insert_entry_into_leaf_node(LatchMemo &latch_memo, Frame *frame, const char *pkey, const RID *rid);
羽飞's avatar
羽飞 已提交
514 515
  RC create_new_tree(const char *key, const RID *rid);

516 517 518 519
  void update_root_page_num(PageNum root_page_num);
  void update_root_page_num_locked(PageNum root_page_num);

  RC adjust_root(LatchMemo &latch_memo, Frame *root_frame);
羽飞's avatar
羽飞 已提交
520 521

private:
522
  common::MemPoolItem::unique_ptr make_key(const char *user_key, const RID &rid);
L
Longda Feng 已提交
523 524
  void free_key(char *key);

羽飞's avatar
羽飞 已提交
525 526
protected:
  DiskBufferPool *disk_buffer_pool_ = nullptr;
527
  bool            header_dirty_ = false; // 
羽飞's avatar
羽飞 已提交
528 529
  IndexFileHeader file_header_;

530 531 532
  // 在调整根节点时,需要加上这个锁。
  // 这个锁可以使用递归读写锁,但是这里偷懒先不改
  common::SharedMutex   root_lock_;
羽飞's avatar
羽飞 已提交
533

534 535 536 537
  KeyComparator   key_comparator_;
  KeyPrinter      key_printer_;

  std::unique_ptr<common::MemPoolItem> mem_pool_item_;
羽飞's avatar
羽飞 已提交
538 539 540 541 542 543

private:
  friend class BplusTreeScanner;
  friend class BplusTreeTester;
};

544 545
class BplusTreeScanner 
{
羽飞's avatar
羽飞 已提交
546 547 548 549 550 551
public:
  BplusTreeScanner(BplusTreeHandler &tree_handler);
  ~BplusTreeScanner();

  /**
   * 扫描指定范围的数据
羽飞's avatar
羽飞 已提交
552 553
   * @param left_user_key 扫描范围的左边界,如果是null,则没有左边界
   * @param left_len left_user_key 的内存大小(只有在变长字段中才会关注)
羽飞's avatar
羽飞 已提交
554
   * @param left_inclusive 左边界的值是否包含在内
羽飞's avatar
羽飞 已提交
555 556
   * @param right_user_key 扫描范围的右边界。如果是null,则没有右边界
   * @param right_len right_user_key 的内存大小(只有在变长字段中才会关注)
羽飞's avatar
羽飞 已提交
557 558
   * @param right_inclusive 右边界的值是否包含在内
   */
559 560
  RC open(const char *left_user_key, int left_len, bool left_inclusive, 
          const char *right_user_key, int right_len, bool right_inclusive);
羽飞's avatar
羽飞 已提交
561

562
  RC next_entry(RID &rid);
羽飞's avatar
羽飞 已提交
563 564 565

  RC close();

羽飞's avatar
羽飞 已提交
566 567 568 569
private:
  /**
   * 如果key的类型是CHARS, 扩展或缩减user_key的大小刚好是schema中定义的大小
   */
L
Longda Feng 已提交
570 571
  RC fix_user_key(const char *user_key, int key_len, bool want_greater, char **fixed_key, bool *should_inclusive);

572 573 574
  void fetch_item(RID &rid);
  bool touch_end();

羽飞's avatar
羽飞 已提交
575 576 577 578
private:
  bool inited_ = false;
  BplusTreeHandler &tree_handler_;

579 580
  LatchMemo latch_memo_;

羽飞's avatar
羽飞 已提交
581 582
  /// 使用左右叶子节点和位置来表示扫描的起始位置和终止位置
  /// 起始位置和终止位置都是有效的数据
583 584 585
  Frame *current_frame_ = nullptr;

  common::MemPoolItem::unique_ptr right_key_;
L
Longda Feng 已提交
586
  int iter_index_ = -1;
587
  bool first_emitted_ = false;
羽飞's avatar
羽飞 已提交
588
};