trx.h 3.3 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
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 2021/5/24.
//

15
#pragma once
羽飞's avatar
羽飞 已提交
16 17 18 19

#include <stddef.h>
#include <unordered_set>
#include <mutex>
羽飞's avatar
羽飞 已提交
20
#include <utility>
羽飞's avatar
羽飞 已提交
21 22

#include "sql/parser/parse.h"
羽飞's avatar
羽飞 已提交
23
#include "storage/record/record_manager.h"
羽飞's avatar
羽飞 已提交
24 25
#include "storage/common/field_meta.h"
#include "storage/common/table.h"
羽飞's avatar
羽飞 已提交
26 27
#include "rc.h"

28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * @defgroup Transaction 事务模块
 * @brief 描述事务相关的代码
 */

class Db;
class CLogManager;
class CLogRecord;
class Trx;

/**
 * @brief 描述一个操作,比如插入、删除行等
 * @details 通常包含一个操作的类型,以及操作的对象和具体的数据
 */
42 43
class Operation 
{
羽飞's avatar
羽飞 已提交
44
public:
羽飞's avatar
羽飞 已提交
45 46
  enum class Type : int 
  {
羽飞's avatar
羽飞 已提交
47 48 49 50 51 52 53
    INSERT,
    UPDATE,
    DELETE,
    UNDEFINED,
  };

public:
羽飞's avatar
羽飞 已提交
54 55 56 57 58
  Operation(Type type, Table *table, const RID &rid) 
      : type_(type), 
        table_(table),
        page_num_(rid.page_num), 
        slot_num_(rid.slot_num)
59
  {}
羽飞's avatar
羽飞 已提交
60

羽飞's avatar
羽飞 已提交
61 62 63 64 65
  Type    type() const { return type_; }
  int32_t table_id() const { return table_->table_id(); }
  Table * table() const { return table_; }
  PageNum page_num() const { return page_num_; }
  SlotNum slot_num() const { return slot_num_; }
羽飞's avatar
羽飞 已提交
66 67 68

private:
  Type type_;
69
  /// 操作的哪张表。这里直接使用表其实并不准确,因为表中的索引也可能有日志
羽飞's avatar
羽飞 已提交
70 71
  Table * table_ = nullptr;
  PageNum page_num_; // TODO use RID instead of page num and slot num
72
  SlotNum slot_num_;
羽飞's avatar
羽飞 已提交
73
};
74 75 76

class OperationHasher 
{
羽飞's avatar
羽飞 已提交
77
public:
78 79
  size_t operator()(const Operation &op) const
  {
羽飞's avatar
羽飞 已提交
80 81 82 83
    return (((size_t)op.page_num()) << 32) | (op.slot_num());
  }
};

84 85
class OperationEqualer 
{
羽飞's avatar
羽飞 已提交
86
public:
87 88
  bool operator()(const Operation &op1, const Operation &op2) const
  {
羽飞's avatar
羽飞 已提交
89 90
    return op1.table_id() == op2.table_id() &&
        op1.page_num() == op2.page_num() && op1.slot_num() == op2.slot_num();
羽飞's avatar
羽飞 已提交
91 92 93
  }
};

羽飞's avatar
羽飞 已提交
94
class TrxKit
95
{
羽飞's avatar
羽飞 已提交
96
public:
羽飞's avatar
羽飞 已提交
97 98 99 100 101
  enum Type
  {
    VACUOUS,
    MVCC,
  };
羽飞's avatar
羽飞 已提交
102

羽飞's avatar
羽飞 已提交
103 104 105
public:
  TrxKit() = default;
  virtual ~TrxKit() = default;
羽飞's avatar
羽飞 已提交
106

羽飞's avatar
羽飞 已提交
107 108
  virtual RC init() = 0;
  virtual const std::vector<FieldMeta> *trx_fields() const = 0;
109 110 111 112 113 114
  virtual Trx *create_trx(CLogManager *log_manager) = 0;
  virtual Trx *create_trx(int32_t trx_id) = 0;
  virtual Trx *find_trx(int32_t trx_id) = 0;
  virtual void all_trxes(std::vector<Trx *> &trxes) = 0;

  virtual void destroy_trx(Trx *trx) = 0;
羽飞's avatar
羽飞 已提交
115 116

public:
羽飞's avatar
羽飞 已提交
117 118 119 120
  static TrxKit *create(const char *name);
  static RC init_global(const char *name);
  static TrxKit *instance();
};
羽飞's avatar
羽飞 已提交
121

羽飞's avatar
羽飞 已提交
122 123
class Trx
{
羽飞's avatar
羽飞 已提交
124
public:
羽飞's avatar
羽飞 已提交
125 126
  Trx() = default;
  virtual ~Trx() = default;
羽飞's avatar
羽飞 已提交
127

羽飞's avatar
羽飞 已提交
128 129 130
  virtual RC insert_record(Table *table, Record &record) = 0;
  virtual RC delete_record(Table *table, Record &record) = 0;
  virtual RC visit_record(Table *table, Record &record, bool readonly) = 0;
羽飞's avatar
羽飞 已提交
131

羽飞's avatar
羽飞 已提交
132 133 134
  virtual RC start_if_need() = 0;
  virtual RC commit() = 0;
  virtual RC rollback() = 0;
135 136 137 138

  virtual RC redo(Db *db, const CLogRecord &log_record);

  virtual int32_t id() const = 0;
羽飞's avatar
羽飞 已提交
139
};