trx.h 2.6 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
class Operation 
{
羽飞's avatar
羽飞 已提交
30
public:
羽飞's avatar
羽飞 已提交
31 32
  enum class Type : int 
  {
羽飞's avatar
羽飞 已提交
33 34 35 36 37 38 39
    INSERT,
    UPDATE,
    DELETE,
    UNDEFINED,
  };

public:
羽飞's avatar
羽飞 已提交
40 41 42 43 44
  Operation(Type type, Table *table, const RID &rid) 
      : type_(type), 
        table_(table),
        page_num_(rid.page_num), 
        slot_num_(rid.slot_num)
45
  {}
羽飞's avatar
羽飞 已提交
46

羽飞's avatar
羽飞 已提交
47 48 49 50 51
  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
羽飞 已提交
52 53 54

private:
  Type type_;
羽飞's avatar
羽飞 已提交
55 56
  Table * table_ = nullptr;
  PageNum page_num_; // TODO use RID instead of page num and slot num
57
  SlotNum slot_num_;
羽飞's avatar
羽飞 已提交
58
};
59 60 61

class OperationHasher 
{
羽飞's avatar
羽飞 已提交
62
public:
63 64
  size_t operator()(const Operation &op) const
  {
羽飞's avatar
羽飞 已提交
65 66 67 68
    return (((size_t)op.page_num()) << 32) | (op.slot_num());
  }
};

69 70
class OperationEqualer 
{
羽飞's avatar
羽飞 已提交
71
public:
72 73
  bool operator()(const Operation &op1, const Operation &op2) const
  {
羽飞's avatar
羽飞 已提交
74 75
    return op1.table_id() == op2.table_id() &&
        op1.page_num() == op2.page_num() && op1.slot_num() == op2.slot_num();
羽飞's avatar
羽飞 已提交
76 77 78
  }
};

羽飞's avatar
羽飞 已提交
79
class TrxKit
80
{
羽飞's avatar
羽飞 已提交
81
public:
羽飞's avatar
羽飞 已提交
82 83 84 85 86
  enum Type
  {
    VACUOUS,
    MVCC,
  };
羽飞's avatar
羽飞 已提交
87

羽飞's avatar
羽飞 已提交
88 89 90
public:
  TrxKit() = default;
  virtual ~TrxKit() = default;
羽飞's avatar
羽飞 已提交
91

羽飞's avatar
羽飞 已提交
92 93 94
  virtual RC init() = 0;
  virtual const std::vector<FieldMeta> *trx_fields() const = 0;
  virtual Trx *create_trx() = 0;
羽飞's avatar
羽飞 已提交
95 96

public:
羽飞's avatar
羽飞 已提交
97 98 99 100
  static TrxKit *create(const char *name);
  static RC init_global(const char *name);
  static TrxKit *instance();
};
羽飞's avatar
羽飞 已提交
101

羽飞's avatar
羽飞 已提交
102 103
class Trx
{
羽飞's avatar
羽飞 已提交
104
public:
羽飞's avatar
羽飞 已提交
105 106
  Trx() = default;
  virtual ~Trx() = default;
羽飞's avatar
羽飞 已提交
107

羽飞's avatar
羽飞 已提交
108 109 110
  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
羽飞 已提交
111

羽飞's avatar
羽飞 已提交
112 113 114
  virtual RC start_if_need() = 0;
  virtual RC commit() = 0;
  virtual RC rollback() = 0;
羽飞's avatar
羽飞 已提交
115
};