clog_test.cpp 3.6 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021-2022 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 20 21 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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 huhaosheng.hhs on 2022
//

#include <string.h>

#include "storage/clog/clog.h"
#include "gtest/gtest.h"

Record *gen_ins_record(int32_t page_num, int32_t slot_num, int data_len)
{
  Record *rec = new Record();
  char *data = new char[data_len];
  rec->set_rid(page_num, slot_num);
  memset(data, data_len, data_len);
  rec->set_data(data);
  return rec;
}

Record *gen_del_record(int32_t page_num, int32_t slot_num)
{
  Record *rec = new Record();
  rec->set_rid(page_num, slot_num);
  return rec;
}

TEST(test_clog, test_clog)
{
  CLogManager *log_mgr = new CLogManager("/home/huhaosheng.hhs/Alibaba/miniob");

  CLogRecord *log_rec[6];
  CLogRecord *log_mtr_rec = nullptr;
  Record *rec = nullptr;
  //
  log_mgr->clog_gen_record(REDO_MTR_BEGIN, 1, log_mtr_rec);
  log_mgr->clog_append_record(log_mtr_rec);  // NOTE: 需要保留log_rec
  delete log_mtr_rec;

  rec = gen_ins_record(1, 1, 100);
  log_mgr->clog_gen_record(REDO_INSERT, 1, log_rec[0], "table1", 100, rec);
  log_mgr->clog_append_record(log_rec[0]);
  delete[] rec->data();
  delete rec;

  rec = gen_ins_record(1, 1, 120);
  log_mgr->clog_gen_record(REDO_INSERT, 1, log_rec[1], "table2", 120, rec);
  log_mgr->clog_append_record(log_rec[1]);
  delete[] rec->data();
  delete rec;

  log_mgr->clog_gen_record(REDO_MTR_BEGIN, 2, log_mtr_rec);
  log_mgr->clog_append_record(log_mtr_rec);
  delete log_mtr_rec;

  rec = gen_ins_record(1, 1, 200);
  log_mgr->clog_gen_record(REDO_INSERT, 1, log_rec[2], "table3", 200, rec);
  log_mgr->clog_append_record(log_rec[2]);
  delete[] rec->data();
  delete rec;

  rec = gen_ins_record(1, 2, 120);
  log_mgr->clog_gen_record(REDO_INSERT, 2, log_rec[3], "table2", 120, rec);
  log_mgr->clog_append_record(log_rec[3]);
  delete[] rec->data();
  delete rec;

  rec = gen_ins_record(1, 2, 100);
  log_mgr->clog_gen_record(REDO_INSERT, 2, log_rec[4], "table1", 100, rec);
  log_mgr->clog_append_record(log_rec[4]);
  delete[] rec->data();
  delete rec;

  log_mgr->clog_gen_record(REDO_MTR_COMMIT, 2, log_mtr_rec);
  log_mgr->clog_append_record(log_mtr_rec);
  delete log_mtr_rec;

  rec = gen_del_record(1, 1);
  log_mgr->clog_gen_record(REDO_DELETE, 1, log_rec[5], "table1", 0, rec);
  log_mgr->clog_append_record(log_rec[5]);
  delete rec;

  log_mgr->clog_gen_record(REDO_MTR_COMMIT, 1, log_mtr_rec);
  log_mgr->clog_append_record(log_mtr_rec);
  delete log_mtr_rec;

  log_mgr->recover();

  CLogMTRManager *log_mtr_mgr = log_mgr->get_mtr_manager();

  ASSERT_EQ(true, log_mtr_mgr->trx_commited[1]);
  ASSERT_EQ(true, log_mtr_mgr->trx_commited[2]);

  ASSERT_EQ(6, log_mtr_mgr->log_redo_list.size());

  int i = 0;
  for (auto iter = log_mtr_mgr->log_redo_list.begin(); iter != log_mtr_mgr->log_redo_list.end();
       iter++) {
    CLogRecord *tmp = *iter;
    ASSERT_EQ(1, log_rec[i]->cmp_eq(tmp));
    delete tmp;
    i++;
  }

  for (i = 0; i < 6; i++) {
    delete log_rec[i];
  }
}

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

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