bplus_tree_test.cpp 19.8 KB
Newer Older
L
Longda 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 longda on 2022
//

#include <list>
#include <iostream>

羽飞's avatar
羽飞 已提交
18
#include "storage/index/bplus_tree.h"
L
Longda 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "storage/default/disk_buffer_pool.h"
#include "rc.h"
#include "common/log/log.h"
#include "sql/parser/parse_defs.h"
#include "gtest/gtest.h"

using namespace common;

#define TIMES 3
// order must small real order
// if page is 8k, it is 400
#define ORDER 4
#define INSERT_NUM (TIMES * ORDER * ORDER * ORDER * ORDER)
#define POOL_NUM 2

羽飞's avatar
羽飞 已提交
34
BufferPoolManager bpm;
L
Longda 已提交
35 36 37 38 39 40 41
BplusTreeHandler *handler = nullptr;
const char *index_name = "test.btree";
int insert_num = INSERT_NUM;
const int page_size = 1024;
RID rid, check_rid;
int k = 0;

羽飞's avatar
羽飞 已提交
42 43
void init_bpm()
{
L
Longda Feng 已提交
44
  BufferPoolManager::set_instance(&bpm);
羽飞's avatar
羽飞 已提交
45
}
L
Longda 已提交
46 47
void test_insert()
{
羽飞's avatar
羽飞 已提交
48
  RC rc = RC::SUCCESS;
L
Longda 已提交
49 50 51 52 53 54 55 56 57 58 59
  for (int i = 0; i < insert_num; i++) {

    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    if (i % TIMES == 1) {
      if (insert_num > page_size) {
        if (k++ % 100 == 0) {
          LOG_INFO("Begin to insert the page's num %s", rid.to_string().c_str());
        }
      } else {
羽飞's avatar
羽飞 已提交
60
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
61
      }
羽飞's avatar
羽飞 已提交
62
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
63
      ASSERT_EQ(RC::SUCCESS, rc);
羽飞's avatar
羽飞 已提交
64
      handler->print_tree();
L
Longda 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      ASSERT_EQ(true, handler->validate_tree());
    }
  }
  handler->print_tree();

  for (int i = 0; i < insert_num; i++) {

    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    if (i % TIMES == 2) {
      if (insert_num > page_size) {
        if (k++ % 100 == 0) {
          LOG_INFO("Begin to insert the page's num %s", rid.to_string().c_str());
        }
      } else {
羽飞's avatar
羽飞 已提交
81
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
82
      }
羽飞's avatar
羽飞 已提交
83
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
84
      ASSERT_EQ(RC::SUCCESS, rc);
羽飞's avatar
羽飞 已提交
85
      handler->print_tree();
L
Longda 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      ASSERT_EQ(true, handler->validate_tree());
    }
  }

  handler->print_tree();
  for (int i = 0; i < insert_num; i++) {

    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    if (i % TIMES == 0) {
      if (insert_num > page_size) {
        if (k++ % 100 == 0) {
          LOG_INFO("Begin to insert the page's num %s", rid.to_string().c_str());
        }
      } else {
羽飞's avatar
羽飞 已提交
102
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
103
      }
羽飞's avatar
羽飞 已提交
104
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      ASSERT_EQ(RC::SUCCESS, rc);
      ASSERT_EQ(true, handler->validate_tree());
    }
  }

  LOG_INFO("@@@@ finish first step insert");
  handler->print_tree();
  handler->print_leafs();

  int start = insert_num / TIMES > page_size ? page_size : insert_num / TIMES;
  int end = insert_num / TIMES > page_size ? (2 * page_size) : (2 * insert_num / TIMES);
  for (int i = start; i < end; i++) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    if (insert_num > page_size) {
      if (k++ % 100 == 0) {
        LOG_INFO("Begin to check duplicated insert the page's num %s", rid.to_string().c_str());
      }
    } else {
羽飞's avatar
羽飞 已提交
125
      LOG_INFO("check duplicate Insert %d. rid=%s. i%TIMES=%d", i, rid.to_string().c_str(), i%TIMES);
L
Longda 已提交
126
    }
羽飞's avatar
羽飞 已提交
127
    rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
128 129
    int t = i % TIMES;
    if (t == 0 || t == 1 || t == 2) {
羽飞's avatar
羽飞 已提交
130
      if (rc != RC::RECORD_DUPLICATE_KEY) {
131
	      LOG_WARN("insert duplicate key success");
羽飞's avatar
羽飞 已提交
132
      }
L
Longda 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      ASSERT_EQ(RC::RECORD_DUPLICATE_KEY, rc);
    } else {
      ASSERT_EQ(RC::SUCCESS, rc);
      ASSERT_EQ(true, handler->validate_tree());
    }
  }
}

void test_get()
{
  std::list<RID> rids;
  for (int i = 0; i < insert_num; i++) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;
    if (insert_num > page_size) {
      if (k++ % 100 == 0) {
        LOG_INFO("Begin to get every entry of index,  rid: %s", rid.to_string().c_str());
      }
    } else {
      LOG_INFO("Begin to get every entry of index,  rid: %s", rid.to_string().c_str());
    }

    rids.clear();
羽飞's avatar
羽飞 已提交
156
    RC rc = handler->get_entry((const char *)&i, 4, rids);
L
Longda 已提交
157 158 159 160 161 162 163 164 165 166 167

    ASSERT_EQ(RC::SUCCESS, rc);
    ASSERT_EQ(1, rids.size());
    check_rid = rids.front();
    ASSERT_EQ(rid.page_num, check_rid.page_num);
    ASSERT_EQ(rid.slot_num, check_rid.slot_num);
  }
}

void test_delete()
{
羽飞's avatar
羽飞 已提交
168
  RC rc = RC::SUCCESS;
L
Longda 已提交
169 170 171 172 173 174 175 176 177 178
  std::list<RID> rids;

  for (int i = 0; i < insert_num / 2; i++) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    int t = i % TIMES;
    if (t == 0 || t == 1) {
      if (insert_num > page_size) {
        if (k++ % 100 == 0) {
羽飞's avatar
羽飞 已提交
179
          LOG_INFO("Begin to delete entry of index, i=%d rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
180 181
        }
      } else {
羽飞's avatar
羽飞 已提交
182
        LOG_INFO("Begin to delete entry of index,  i=%d, rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
183 184
      }

羽飞's avatar
羽飞 已提交
185 186
      rc = handler->delete_entry((const char *)&i, &rid);
      if (rc != RC::SUCCESS) {
187
	      LOG_WARN("failed to delete entry. i=%d, rid=%s", i, rid.to_string().c_str());
羽飞's avatar
羽飞 已提交
188 189
      }
      ASSERT_EQ(RC::SUCCESS, rc);
L
Longda 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

      ASSERT_EQ(true, handler->validate_tree());
    }
  }

  handler->print_tree();

  for (int i = insert_num - 1; i >= insert_num / 2; i--) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    int t = i % TIMES;
    if (t == 0 || t == 1) {
      if (insert_num > page_size) {
        if (k++ % 100 == 0) {
          LOG_INFO("Begin to delete entry of index,  rid: %s", rid.to_string().c_str());
        }
      } else {
        LOG_INFO("Begin to delete entry of index,  rid: %s", rid.to_string().c_str());
      }
羽飞's avatar
羽飞 已提交
210
      rc = handler->delete_entry((const char *)&i, &rid);
L
Longda 已提交
211 212 213 214 215 216 217 218 219 220 221 222

      ASSERT_EQ(true, handler->validate_tree());
      ASSERT_EQ(RC::SUCCESS, rc);
    }
  }
  handler->print_tree();

  for (int i = 0; i < insert_num; i++) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;
    if (insert_num > page_size) {
      if (k++ % 100 == 0) {
羽飞's avatar
羽飞 已提交
223
        LOG_INFO("Begin to get entry of index, i=%d,rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
224 225
      }
    } else {
羽飞's avatar
羽飞 已提交
226
      LOG_INFO("Begin to get entry of index, i=%d, rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
227 228
    }
    rids.clear();
羽飞's avatar
羽飞 已提交
229
    rc = handler->get_entry((const char *)&i, 4, rids);
L
Longda 已提交
230 231 232 233 234
    ASSERT_EQ(RC::SUCCESS, rc);
    int t = i % TIMES;
    if (t == 0 || t == 1) {
      ASSERT_EQ(0, rids.size());
    } else {
羽飞's avatar
羽飞 已提交
235
      if (rids.size() != 1) {
236
	      LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str());
羽飞's avatar
羽飞 已提交
237
      }
L
Longda 已提交
238 239
      ASSERT_EQ(1, rids.size());
      check_rid = rids.front();
羽飞's avatar
羽飞 已提交
240
      if (rid != check_rid) {
241
	      LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str());
羽飞's avatar
羽飞 已提交
242
      }
L
Longda 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
      ASSERT_EQ(rid.page_num, check_rid.page_num);
      ASSERT_EQ(rid.slot_num, check_rid.slot_num);
      ASSERT_EQ(true, handler->validate_tree());
    }
  }

  handler->print_tree();
  for (int i = 0; i < insert_num / 2; i++) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    int t = i % TIMES;
    if (t == 2) {
      if (insert_num > page_size) {
        if (k++ % 100 == 0) {
          LOG_INFO("Begin to delete entry of index,  rid: %s", rid.to_string().c_str());
        }
      } else {
        LOG_INFO("Begin to delete entry of index,  rid: %s", rid.to_string().c_str());
      }
羽飞's avatar
羽飞 已提交
263
      rc = handler->delete_entry((const char *)&i, &rid);
L
Longda 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

      ASSERT_EQ(true, handler->validate_tree());
      ASSERT_EQ(RC::SUCCESS, rc);
    }
  }

  handler->print_tree();

  for (int i = insert_num - 1; i >= insert_num / 2; i--) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;

    int t = i % TIMES;
    if (t == 2) {
      if (insert_num > page_size) {
        if (k++ % 100 == 0) {
          LOG_INFO("Begin to delete entry of index,  rid: %s", rid.to_string().c_str());
        }
      } else {
        LOG_INFO("Begin to delete entry of index,  rid: %s", rid.to_string().c_str());
      }
羽飞's avatar
羽飞 已提交
285
      rc = handler->delete_entry((const char *)&i, &rid);
L
Longda 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

      ASSERT_EQ(true, handler->validate_tree());
      ASSERT_EQ(RC::SUCCESS, rc);
    }
  }

  handler->print_tree();

  for (int i = 0; i < insert_num; i++) {
    rid.page_num = i / page_size;
    rid.slot_num = i % page_size;
    if (insert_num > page_size) {
      if (k++ % 100 == 0) {
        LOG_INFO("Begin to insert entry of index,  rid: %s", rid.to_string().c_str());
      }
    } else {
      LOG_INFO("Begin to insert entry of index,  rid: %s", rid.to_string().c_str());
    }
羽飞's avatar
羽飞 已提交
304
    rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
305 306 307 308 309 310 311 312 313 314 315
    int t = i % TIMES;
    if (t == 0 || t == 1 || t == 2) {
      ASSERT_EQ(RC::SUCCESS, rc);
      ASSERT_EQ(true, handler->validate_tree());
    } else {
      ASSERT_EQ(RC::RECORD_DUPLICATE_KEY, rc);
    }
  }
  handler->print_tree();
}

羽飞's avatar
羽飞 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
TEST(test_bplus_tree, test_leaf_index_node_handle)
{
  IndexFileHeader index_file_header;
  index_file_header.root_page = BP_INVALID_PAGE_NUM;
  index_file_header.internal_max_size = 5;
  index_file_header.leaf_max_size = 5;
  index_file_header.attr_length = 4;
  index_file_header.key_length = 4 + sizeof(RID);
  index_file_header.attr_type = INTS;

  Frame frame;

  KeyComparator key_comparator;
  key_comparator.init(INTS, 4);

羽飞's avatar
羽飞 已提交
331
  LeafIndexNodeHandler leaf_node(index_file_header, &frame);
羽飞's avatar
羽飞 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  leaf_node.init_empty();
  ASSERT_EQ(0, leaf_node.size());

  bool found;
  int index;
  char key_mem[4 + sizeof(RID)];
  int &key = *(int *)key_mem;
  RID &rid = *(RID *)(key_mem + 4);
  rid.page_num = 0;
  rid.slot_num = 0;
  for (int i = 0; i < 5; i++) {
    key = i * 2 + 1;
    index = leaf_node.lookup(key_comparator, key_mem, &found);
    ASSERT_EQ(false, found);
    leaf_node.insert(index, (const char *)&key, (const char *)&rid);
  }

  ASSERT_EQ(5, leaf_node.size());

  for (int i = 0; i < 5; i++) {
    key = i * 2;
    index = leaf_node.lookup(key_comparator, key_mem, &found);
    ASSERT_EQ(false, found);
    ASSERT_EQ(i, index);
  }

  key = 12;
  index = leaf_node.lookup(key_comparator, key_mem, &found);
  ASSERT_EQ(false, found);
  ASSERT_EQ(5, index);

  for (int i = 0; i < 5; i++) {
    key = i * 2 + 1;
    index = leaf_node.lookup(key_comparator, key_mem, &found);
    if (!found || i != index) {
      printf("found=%d, index=%d, key=%d", found, index, key);
    }
    ASSERT_EQ(true, found);
    ASSERT_EQ(i, index);
  }
}
TEST(test_bplus_tree, test_internal_index_node_handle)
{
  IndexFileHeader index_file_header;
  index_file_header.root_page = BP_INVALID_PAGE_NUM;
  index_file_header.internal_max_size = 5;
  index_file_header.leaf_max_size = 5;
  index_file_header.attr_length = 4;
  index_file_header.key_length = 4 + sizeof(RID);
  index_file_header.attr_type = INTS;

  Frame frame;

  KeyComparator key_comparator;
  key_comparator.init(INTS, 4);

羽飞's avatar
羽飞 已提交
388
  InternalIndexNodeHandler internal_node(index_file_header, &frame);
羽飞's avatar
羽飞 已提交
389 390 391 392 393 394 395 396 397 398 399 400
  internal_node.init_empty();
  ASSERT_EQ(0, internal_node.size());

  bool found;
  int index;
  int insert_position;
  char key_mem[4 + sizeof(RID)];
  int &key = *(int *)key_mem;
  RID &rid = *(RID *)(key_mem + 4);
  rid.page_num = 0;
  rid.slot_num = 0;

羽飞's avatar
羽飞 已提交
401 402 403 404 405 406
  key = 0;
  index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position);
  ASSERT_EQ(false, found);
  ASSERT_EQ(0, index);
  ASSERT_EQ(1, insert_position);

羽飞's avatar
羽飞 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
  key = 3;
  internal_node.create_new_root(1, key_mem, key);
  for (int i = 2; i < 5; i++) {
    key = i * 2 + 1;
    internal_node.insert((const char *)&key, (PageNum)key, key_comparator);
  }

  ASSERT_EQ(5, internal_node.size());

  for (int i = 1; i < 5; i++) {
    key = i * 2 + 1;
    int real_key = *(int*)internal_node.key_at(i);
    ASSERT_EQ(key, real_key);
  }

  key = 0;
  index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position);
  ASSERT_EQ(false, found);
  ASSERT_EQ(0, index);
  ASSERT_EQ(1, insert_position);

  key = 2;
  index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position);
  ASSERT_EQ(false, found);
  ASSERT_EQ(0, index);
  ASSERT_EQ(1, insert_position);

  key = 4;
  index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position);
  ASSERT_EQ(false, found);
  ASSERT_EQ(1, index);
  ASSERT_EQ(2, insert_position);

  key = 8;
  index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position);
  ASSERT_EQ(false, found);
  ASSERT_EQ(3, index);
  ASSERT_EQ(4, insert_position);

  key = 10;
  index = internal_node.lookup(key_comparator, key_mem, &found, &insert_position);
  ASSERT_EQ(false, found);
  ASSERT_EQ(4, index);
  ASSERT_EQ(5, insert_position);

  key = 12;
  index = internal_node.lookup(key_comparator, key_mem, &found);
  ASSERT_EQ(false, found);
  ASSERT_EQ(4, index);
  ASSERT_EQ(5, insert_position);

  for (int i = 1; i < 5; i++) {
    key = i * 2 + 1;
    index = internal_node.lookup(key_comparator, key_mem, &found);
    if (!found || i != index) {
      printf("found=%d, index=%d, key=%d", found, index, key);
    }
    ASSERT_EQ(true, found);
    ASSERT_EQ(i, index);
  }
}
羽飞's avatar
羽飞 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

TEST(test_bplus_tree, test_chars)
{
  LoggerFactory::init_default("test_chars.log");

  const char *index_name = "chars.btree";
  ::remove(index_name);
  handler = new BplusTreeHandler();
  handler->create(index_name, CHARS, 8, ORDER, ORDER);

  char keys[][9] = {
    "abcdefg",
    "12345678",
    "12345678",
    "abcdefg",
    "abcdefga"
  };

  RID rid;
  RC rc = RC::SUCCESS;
  for (size_t i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
    rid.page_num = 0;
    rid.slot_num = i;
    rc = handler->insert_entry(keys[i], &rid);
    ASSERT_EQ(RC::SUCCESS, rc);
  }

  LOG_INFO("begin to print bplus tree of chars");
  handler->print_tree();
  LOG_INFO("end to print bplus tree of chars");

  BplusTreeScanner scanner(*handler);
  const char *key = "abcdefg";
  rc = scanner.open(key, strlen(key), true, key, strlen(key), true);
  ASSERT_EQ(rc, RC::SUCCESS);

  int count = 0;
505
  while (RC::SUCCESS == (rc = scanner.next_entry(rid))) {
羽飞's avatar
羽飞 已提交
506 507 508 509 510 511
    count++;
  }
  scanner.close();
  ASSERT_EQ(2, count);
}

羽飞's avatar
羽飞 已提交
512 513 514 515 516 517 518 519 520 521 522 523
TEST(test_bplus_tree, test_scanner)
{
  LoggerFactory::init_default("test.log");

  const char *index_name = "scanner.btree";
  ::remove(index_name);
  handler = new BplusTreeHandler();
  handler->create(index_name, INTS, sizeof(int), ORDER, ORDER);

  int count = 0;
  RC rc = RC::SUCCESS;
  RID rid;
524
  // 初始化B树,插入数据[1 - 199] 所有奇数
羽飞's avatar
羽飞 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538
  for (int i = 0; i < 100; i++) {
    int key = i * 2 + 1;
    rid.page_num = 0;
    rid.slot_num = key;
    rc = handler->insert_entry((const char *)&key, &rid);
    ASSERT_EQ(RC::SUCCESS, rc);
  }

  handler->print_tree();

  BplusTreeScanner scanner(*handler);

  int begin = -100;
  int end = -20;
羽飞's avatar
羽飞 已提交
539
  rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false);
羽飞's avatar
羽飞 已提交
540 541
  ASSERT_EQ(RC::SUCCESS, rc);

542
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
543 544 545 546 547 548
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = -100;
  end = 1;
羽飞's avatar
羽飞 已提交
549
  rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false);
羽飞's avatar
羽飞 已提交
550
  ASSERT_EQ(RC::SUCCESS, rc);
551
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
552 553 554 555 556 557
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = -100;
  end = 1;
羽飞's avatar
羽飞 已提交
558
  rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
559
  ASSERT_EQ(RC::SUCCESS, rc);
560
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
561
  ASSERT_EQ(RC::SUCCESS, rc);
562
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
563 564 565 566 567 568
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 1;
  end = 3;
羽飞's avatar
羽飞 已提交
569
  rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false/*inclusive*/);
羽飞's avatar
羽飞 已提交
570
  ASSERT_EQ(RC::SUCCESS, rc);
571
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
572 573 574 575 576 577
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 1;
  end = 3;
羽飞's avatar
羽飞 已提交
578
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
579
  ASSERT_EQ(RC::SUCCESS, rc);
580
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
581 582 583 584 585 586 587 588 589
    count++;
  }
  ASSERT_EQ(2, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 0;
  end = 3;
羽飞's avatar
羽飞 已提交
590
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
591 592
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
593
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
594 595 596 597 598 599 600 601 602
    count++;
  }
  ASSERT_EQ(2, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 11;
  end = 21;
羽飞's avatar
羽飞 已提交
603
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
604 605
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
606
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
607 608 609 610 611 612 613 614 615
    count++;
  }
  ASSERT_EQ((end - begin) / 2 + 1, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 11;
  end = 91;
羽飞's avatar
羽飞 已提交
616
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
617 618
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
619
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
620 621 622 623 624 625 626 627 628
    count++;
  }
  ASSERT_EQ((end - begin) / 2 + 1, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 191;
  end = 199;
羽飞's avatar
羽飞 已提交
629
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
630 631
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
632
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
633 634 635 636 637 638 639 640 641
    count++;
  }
  ASSERT_EQ((end - begin) / 2 + 1, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 191;
  end = 201;
羽飞's avatar
羽飞 已提交
642
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
643 644
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
645
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
646 647 648 649 650 651 652 653 654
    count++;
  }
  ASSERT_EQ(5, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 200;
  end = 301;
羽飞's avatar
羽飞 已提交
655
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
656
  ASSERT_EQ(RC::SUCCESS, rc);
657
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
658 659 660 661 662 663
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 300;
  end = 201;
羽飞's avatar
羽飞 已提交
664
  rc = scanner.open((const char *)&begin, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
665 666 667 668 669 670
  ASSERT_EQ(RC::INVALID_ARGUMENT, rc);

  scanner.close();

  begin = 300;
  end = 201;
羽飞's avatar
羽飞 已提交
671
  rc = scanner.open(nullptr, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
672 673
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
674
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
675 676 677 678 679 680 681 682 683
    count++;
  }
  ASSERT_EQ(100, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 300;
  end = 10;
羽飞's avatar
羽飞 已提交
684
  rc = scanner.open(nullptr, 4, true, (const char *)&end, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
685 686
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
687
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
688 689 690 691 692 693 694 695 696
    count++;
  }
  ASSERT_EQ(5, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 190;
  end = 10;
羽飞's avatar
羽飞 已提交
697
  rc = scanner.open((const char *)&begin, 4, true, nullptr, 4, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
698 699
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
700
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
701 702 703 704 705 706 707 708 709
    count++;
  }
  ASSERT_EQ(5, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

  begin = 190;
  end = 10;
羽飞's avatar
羽飞 已提交
710
  rc = scanner.open(nullptr, 0, true, nullptr, 0, true/*inclusive*/);
羽飞's avatar
羽飞 已提交
711 712
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
713
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
714 715 716 717 718 719 720 721
    count++;
  }
  ASSERT_EQ(100, count);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();
}

L
Longda 已提交
722 723 724 725 726 727
TEST(test_bplus_tree, test_bplus_tree_insert)
{
  LoggerFactory::init_default("test.log");

  ::remove(index_name);
  handler = new BplusTreeHandler();
羽飞's avatar
羽飞 已提交
728
  handler->create(index_name, INTS, sizeof(int), ORDER, ORDER);
L
Longda 已提交
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

  test_insert();

  test_get();

  test_delete();

  handler->close();
  delete handler;
  handler = nullptr;
}

int main(int argc, char **argv)
{

  // 分析gtest程序的命令行参数
  testing::InitGoogleTest(&argc, argv);

  // 调用RUN_ALL_TESTS()运行所有测试用例
  // main函数返回RUN_ALL_TESTS()的运行结果

750
    LoggerFactory::init_default("test.log", LOG_LEVEL_TRACE);
羽飞's avatar
羽飞 已提交
751
  init_bpm();
L
Longda 已提交
752 753 754
  int rc = RUN_ALL_TESTS();

  return rc;
羽飞's avatar
羽飞 已提交
755
}