record_manager_test.cpp 5.0 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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 wangyunlai.wyl on 2022
//

#include <string.h>
#include <sstream>

#include "gtest/gtest.h"
#include "storage/default/disk_buffer_pool.h"
羽飞's avatar
羽飞 已提交
20
#include "storage/record/record_manager.h"
羽飞's avatar
羽飞 已提交
21
#include "storage/trx/vacuous_trx.h"
羽飞's avatar
羽飞 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

using namespace common;

TEST(test_record_page_handler, test_record_page_handler)
{
  const char *record_manager_file = "record_manager.bp";
  ::remove(record_manager_file);

  BufferPoolManager *bpm = new BufferPoolManager();
  DiskBufferPool *bp = nullptr;
  RC rc = bpm->create_file(record_manager_file);
  ASSERT_EQ(rc, RC::SUCCESS);
  
  rc = bpm->open_file(record_manager_file, bp);
  ASSERT_EQ(rc, RC::SUCCESS);

  Frame *frame = nullptr;
  rc = bp->allocate_page(&frame);
  ASSERT_EQ(rc, RC::SUCCESS);

  const int record_size = 8;
  RecordPageHandler record_page_handle;
  rc = record_page_handle.init_empty_page(*bp, frame->page_num(), record_size);
  ASSERT_EQ(rc, RC::SUCCESS);

  RecordPageIterator iterator;
  iterator.init(record_page_handle);

  int count = 0;
  Record record;
  while (iterator.has_next()) {
    count++;
    rc = iterator.next(record);
    ASSERT_EQ(rc, RC::SUCCESS);
  }
  ASSERT_EQ(count, 0);

  char buf[record_size];
  RID rid;
  rid.page_num = 100;
  rid.slot_num = 100;
  rc = record_page_handle.insert_record(buf, &rid);
  ASSERT_EQ(rc, RC::SUCCESS);

  count = 0;
  iterator.init(record_page_handle);
  while (iterator.has_next()) {
    count++;
    rc = iterator.next(record);
    ASSERT_EQ(rc, RC::SUCCESS);
  }
  ASSERT_EQ(count, 1);

  for (int i = 0; i < 10; i++) {
    rid.page_num = i;
    rid.slot_num = i;
    rc = record_page_handle.insert_record(buf, &rid);
    ASSERT_EQ(rc, RC::SUCCESS);
  }

  count = 0;
  iterator.init(record_page_handle);
  while (iterator.has_next()) {
    count++;
    rc = iterator.next(record);
    ASSERT_EQ(rc, RC::SUCCESS);
  }
  ASSERT_EQ(count, 11);

  for (int i = 0; i < 5; i++) {
    rid.page_num = i * 2;
    rid.slot_num = i * 2;
    rc = record_page_handle.delete_record(&rid);
    ASSERT_EQ(rc, RC::SUCCESS);
  }

  count = 0;
  iterator.init(record_page_handle);
  while (iterator.has_next()) {
    count++;
    rc = iterator.next(record);
    ASSERT_EQ(rc, RC::SUCCESS);
  }
  ASSERT_EQ(count, 6);
羽飞's avatar
羽飞 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

  bpm->close_file(record_manager_file);
}

TEST(test_record_page_handler, test_record_file_iterator)
{
  const char *record_manager_file = "record_manager.bp";
  ::remove(record_manager_file);

  BufferPoolManager *bpm = new BufferPoolManager();
  DiskBufferPool *bp = nullptr;
  RC rc = bpm->create_file(record_manager_file);
  ASSERT_EQ(rc, RC::SUCCESS);
  
  rc = bpm->open_file(record_manager_file, bp);
  ASSERT_EQ(rc, RC::SUCCESS);

  RecordFileHandler file_handler;
  rc = file_handler.init(bp);
  ASSERT_EQ(rc, RC::SUCCESS);

羽飞's avatar
羽飞 已提交
127
  VacuousTrx trx;
羽飞's avatar
羽飞 已提交
128
  RecordFileScanner file_scanner;
羽飞's avatar
羽飞 已提交
129
  rc = file_scanner.open_scan(nullptr/*table*/, *bp, &trx, true/*readonly*/, nullptr/*condition_filter*/);
羽飞's avatar
羽飞 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  ASSERT_EQ(rc, RC::SUCCESS);

  int count = 0;
  Record record;
  while (file_scanner.has_next()) {
    rc = file_scanner.next(record);
    ASSERT_EQ(rc, RC::SUCCESS);
    count++;
  }
  file_scanner.close_scan();
  ASSERT_EQ(count, 0);
  
  const int record_insert_num = 1000;
  char record_data[20];
  std::vector<RID> rids;
  for (int i = 0; i < record_insert_num; i++) {
    RID rid;
    rc = file_handler.insert_record(record_data, sizeof(record_data), &rid);
    ASSERT_EQ(rc, RC::SUCCESS);
    rids.push_back(rid);
  }

羽飞's avatar
羽飞 已提交
152
  rc = file_scanner.open_scan(nullptr/*table*/, *bp, &trx, true/*readonly*/, nullptr/*condition_filter*/);
羽飞's avatar
羽飞 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  ASSERT_EQ(rc, RC::SUCCESS);

  count = 0;
  while (file_scanner.has_next()) {
    rc = file_scanner.next(record);
    ASSERT_EQ(rc, RC::SUCCESS);
    count++;
  }
  file_scanner.close_scan();
  ASSERT_EQ(count, rids.size());
  
  for (int i = 0; i < record_insert_num; i += 2) {
    rc = file_handler.delete_record(&rids[i]);
    ASSERT_EQ(rc, RC::SUCCESS);
  }

羽飞's avatar
羽飞 已提交
169
  rc = file_scanner.open_scan(nullptr/*table*/, *bp, &trx, true/*readonly*/, nullptr/*condition_filter*/);
羽飞's avatar
羽飞 已提交
170 171 172 173 174 175 176 177 178 179 180 181
  ASSERT_EQ(rc, RC::SUCCESS);

  count = 0;
  while (file_scanner.has_next()) {
    rc = file_scanner.next(record);
    ASSERT_EQ(rc, RC::SUCCESS);
    count++;
  }
  file_scanner.close_scan();
  ASSERT_EQ(count, rids.size() / 2);
  
  bpm->close_file(record_manager_file);
羽飞's avatar
羽飞 已提交
182 183 184 185 186 187 188 189 190 191 192
}

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

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