mvcc_trx.h 2.3 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 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
/* Copyright (c) 2021 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 Wangyunlai on 2023/04/24.
//

#pragma once

#include "storage/trx/trx.h"

class MvccTrxKit : public TrxKit
{
public:
  MvccTrxKit() = default;
  virtual ~MvccTrxKit() = default;

  RC init() override;
  const std::vector<FieldMeta> *trx_fields() const override;
  Trx *create_trx() override;

  int32_t next_trx_id();

public:
  int32_t max_trx_id() const;

private:
  std::vector<FieldMeta> fields_; // 存储事务数据需要用到的字段元数据,所有表结构都需要带的

  std::atomic<int32_t> current_trx_id_{0};
};

/**
 * TODO 没有垃圾回收
 * 
 */
class MvccTrx : public Trx
{
public:
  MvccTrx(MvccTrxKit &trx_kit);
  virtual ~MvccTrx() = default;

  RC insert_record(Table *table, Record &record) override;
  RC delete_record(Table *table, Record &record) override;

  /**
   * @brief 当访问到某条数据时,使用此函数来判断是否可见,或者是否有访问冲突
   * 
   * @param table    要访问的数据属于哪张表
   * @param record   要访问哪条数据
   * @param readonly 是否只读访问
   * @return RC      - SUCCESS 成功
   *                 - RECORD_INVISIBLE 此数据对当前事务不可见,应该跳过
   *                 - LOCKED_CONCURRENCY_CONFLICT 与其它事务有冲突
   */
  RC visit_record(Table *table, Record &record, bool readonly) override;

  RC start_if_need() override;
  RC commit() override;
  RC rollback() override;

private:
  void trx_fields(Table *table, Field &begin_xid_field, Field &end_xid_field) const;

private:
  static const int32_t MAX_TRX_ID = std::numeric_limits<int32_t>::max();

private:
  using OperationSet = std::unordered_set<Operation, OperationHasher, OperationEqualer>;
  MvccTrxKit &trx_kit_;
  int32_t trx_id_;
  bool started_ = false;
  OperationSet operations_;
};