bplus_tree_test.cpp 19.7 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 44 45 46 47
void init_bpm()
{
  if (&BufferPoolManager::instance() == nullptr) {
    BufferPoolManager::set_instance(&bpm);
  }
}
L
Longda 已提交
48 49
void test_insert()
{
羽飞's avatar
羽飞 已提交
50
  RC rc = RC::SUCCESS;
L
Longda 已提交
51 52 53 54 55 56 57 58 59 60 61
  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
羽飞 已提交
62
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
63
      }
羽飞's avatar
羽飞 已提交
64
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
65
      ASSERT_EQ(RC::SUCCESS, rc);
羽飞's avatar
羽飞 已提交
66
      handler->print_tree();
L
Longda 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      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
羽飞 已提交
83
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
84
      }
羽飞's avatar
羽飞 已提交
85
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
86
      ASSERT_EQ(RC::SUCCESS, rc);
羽飞's avatar
羽飞 已提交
87
      handler->print_tree();
L
Longda 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      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
羽飞 已提交
104
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
105
      }
羽飞's avatar
羽飞 已提交
106
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
      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
羽飞 已提交
127
      LOG_INFO("check duplicate Insert %d. rid=%s. i%TIMES=%d", i, rid.to_string().c_str(), i%TIMES);
L
Longda 已提交
128
    }
羽飞's avatar
羽飞 已提交
129
    rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
130 131
    int t = i % TIMES;
    if (t == 0 || t == 1 || t == 2) {
羽飞's avatar
羽飞 已提交
132 133 134
      if (rc != RC::RECORD_DUPLICATE_KEY) {
	LOG_WARN("insert duplicate key success");
      }
L
Longda 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      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
羽飞 已提交
158
    RC rc = handler->get_entry((const char *)&i, 4, rids);
L
Longda 已提交
159 160 161 162 163 164 165 166 167 168 169

    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
羽飞 已提交
170
  RC rc = RC::SUCCESS;
L
Longda 已提交
171 172 173 174 175 176 177 178 179 180
  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
羽飞 已提交
181
          LOG_INFO("Begin to delete entry of index, i=%d rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
182 183
        }
      } else {
羽飞's avatar
羽飞 已提交
184
        LOG_INFO("Begin to delete entry of index,  i=%d, rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
185 186
      }

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

      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
羽飞 已提交
212
      rc = handler->delete_entry((const char *)&i, &rid);
L
Longda 已提交
213 214 215 216 217 218 219 220 221 222 223 224

      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
羽飞 已提交
225
        LOG_INFO("Begin to get entry of index, i=%d,rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
226 227
      }
    } else {
羽飞's avatar
羽飞 已提交
228
      LOG_INFO("Begin to get entry of index, i=%d, rid: %s", i, rid.to_string().c_str());
L
Longda 已提交
229 230
    }
    rids.clear();
羽飞's avatar
羽飞 已提交
231
    rc = handler->get_entry((const char *)&i, 4, rids);
L
Longda 已提交
232 233 234 235 236
    ASSERT_EQ(RC::SUCCESS, rc);
    int t = i % TIMES;
    if (t == 0 || t == 1) {
      ASSERT_EQ(0, rids.size());
    } else {
羽飞's avatar
羽飞 已提交
237 238 239
      if (rids.size() != 1) {
	LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str());
      }
L
Longda 已提交
240 241
      ASSERT_EQ(1, rids.size());
      check_rid = rids.front();
羽飞's avatar
羽飞 已提交
242 243 244
      if (rid != check_rid) {
	LOG_WARN("invalid. i=%d, rid=%s, check rid=%s", i, rid.to_string().c_str(), check_rid.to_string().c_str());
      }
L
Longda 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
      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
羽飞 已提交
265
      rc = handler->delete_entry((const char *)&i, &rid);
L
Longda 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

      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
羽飞 已提交
287
      rc = handler->delete_entry((const char *)&i, &rid);
L
Longda 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

      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
羽飞 已提交
306
    rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
307 308 309 310 311 312 313 314 315 316 317
    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
羽飞 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
TEST(test_bplus_tree, test_leaf_index_node_handle)
{
  LoggerFactory::init_default("test.log");

  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
羽飞 已提交
335
  LeafIndexNodeHandler leaf_node(index_file_header, &frame);
羽飞's avatar
羽飞 已提交
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 388 389 390 391 392 393
  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)
{
  LoggerFactory::init_default("test.log");

  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
羽飞 已提交
394
  InternalIndexNodeHandler internal_node(index_file_header, &frame);
羽飞's avatar
羽飞 已提交
395 396 397 398 399 400 401 402 403 404 405 406 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
  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;

  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 505 506 507 508 509 510 511

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;
  while (RC::SUCCESS == (rc = scanner.next_entry(&rid))) {
    count++;
  }
  scanner.close();
  ASSERT_EQ(2, count);
}

羽飞's avatar
羽飞 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
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;
  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
羽飞 已提交
538
  rc = scanner.open((const char *)&begin, 4, false, (const char *)&end, 4, false);
羽飞's avatar
羽飞 已提交
539 540 541 542 543 544 545 546 547
  ASSERT_EQ(RC::SUCCESS, rc);

  rc = scanner.next_entry(&rid);
  ASSERT_EQ(RC::RECORD_EOF, rc);

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();

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

  scanner.close();
}

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

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

  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()的运行结果

羽飞's avatar
羽飞 已提交
749
  init_bpm();
L
Longda 已提交
750 751 752
  int rc = RUN_ALL_TESTS();

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