bplus_tree_test.cpp 19.8 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
L
Longda 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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
#include "storage/default/disk_buffer_pool.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
羽飞 已提交
33
BufferPoolManager bpm;
L
Longda 已提交
34 35 36 37 38 39 40
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
羽飞 已提交
41 42
void init_bpm()
{
L
Longda Feng 已提交
43
  BufferPoolManager::set_instance(&bpm);
羽飞's avatar
羽飞 已提交
44
}
L
Longda 已提交
45 46
void test_insert()
{
羽飞's avatar
羽飞 已提交
47
  RC rc = RC::SUCCESS;
L
Longda 已提交
48 49 50 51 52 53 54 55 56 57 58
  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
羽飞 已提交
59
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
60
      }
羽飞's avatar
羽飞 已提交
61
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
62
      ASSERT_EQ(RC::SUCCESS, rc);
羽飞's avatar
羽飞 已提交
63
      handler->print_tree();
L
Longda 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      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
羽飞 已提交
80
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
81
      }
羽飞's avatar
羽飞 已提交
82
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
83
      ASSERT_EQ(RC::SUCCESS, rc);
羽飞's avatar
羽飞 已提交
84
      handler->print_tree();
L
Longda 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      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
羽飞 已提交
101
        LOG_INFO("Insert %d. rid=%s", i, rid.to_string().c_str());
L
Longda 已提交
102
      }
羽飞's avatar
羽飞 已提交
103
      rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      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
羽飞 已提交
124
      LOG_INFO("check duplicate Insert %d. rid=%s. i%TIMES=%d", i, rid.to_string().c_str(), i%TIMES);
L
Longda 已提交
125
    }
羽飞's avatar
羽飞 已提交
126
    rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
127 128
    int t = i % TIMES;
    if (t == 0 || t == 1 || t == 2) {
羽飞's avatar
羽飞 已提交
129
      if (rc != RC::RECORD_DUPLICATE_KEY) {
130
	      LOG_WARN("insert duplicate key success");
羽飞's avatar
羽飞 已提交
131
      }
L
Longda 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      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
羽飞 已提交
155
    RC rc = handler->get_entry((const char *)&i, 4, rids);
L
Longda 已提交
156 157 158 159 160 161 162 163 164 165 166

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

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

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

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

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

      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
羽飞 已提交
303
    rc = handler->insert_entry((const char *)&i, &rid);
L
Longda 已提交
304 305 306 307 308 309 310 311 312 313 314
    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
羽飞 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
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
羽飞 已提交
330
  LeafIndexNodeHandler leaf_node(index_file_header, &frame);
羽飞's avatar
羽飞 已提交
331 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
  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
羽飞 已提交
387
  InternalIndexNodeHandler internal_node(index_file_header, &frame);
羽飞's avatar
羽飞 已提交
388 389 390 391 392 393 394 395 396 397 398 399
  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
羽飞 已提交
400 401 402 403 404 405
  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
羽飞 已提交
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
  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
羽飞 已提交
467 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

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;
504
  while (RC::SUCCESS == (rc = scanner.next_entry(rid))) {
羽飞's avatar
羽飞 已提交
505 506 507 508 509 510
    count++;
  }
  scanner.close();
  ASSERT_EQ(2, count);
}

羽飞's avatar
羽飞 已提交
511 512 513 514 515 516 517 518 519 520 521 522
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;
523
  // 初始化B树,插入数据[1 - 199] 所有奇数
羽飞's avatar
羽飞 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537
  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
  ASSERT_EQ(RC::SUCCESS, rc);

541
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
542 543 544 545 546 547
  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
  ASSERT_EQ(RC::SUCCESS, rc);
550
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
551 552 553 554 555 556
  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
  ASSERT_EQ(RC::SUCCESS, rc);
559
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
560
  ASSERT_EQ(RC::SUCCESS, rc);
561
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
562 563 564 565 566 567
  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
  ASSERT_EQ(RC::SUCCESS, rc);
570
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
571 572 573 574 575 576
  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
  ASSERT_EQ(RC::SUCCESS, rc);
579
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
580 581 582 583 584 585 586 587 588
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
592
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
593 594 595 596 597 598 599 600 601
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
605
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
606 607 608 609 610 611 612 613 614
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
618
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
619 620 621 622 623 624 625 626 627
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
631
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
632 633 634 635 636 637 638 639 640
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
644
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
645 646 647 648 649 650 651 652 653
    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
  ASSERT_EQ(RC::SUCCESS, rc);
656
  rc = scanner.next_entry(rid);
羽飞's avatar
羽飞 已提交
657 658 659 660 661 662
  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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
673
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
674 675 676 677 678 679 680 681 682
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
686
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
687 688 689 690 691 692 693 694 695
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
699
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
700 701 702 703 704 705 706 707 708
    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
  ASSERT_EQ(RC::SUCCESS, rc);
  count = 0;
712
  while ((rc = scanner.next_entry(rid)) == RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
713 714 715 716 717 718 719 720
    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()的运行结果

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

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