MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
trx.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by Wangyunlai on 2021/5/24.
13//
14
15#pragma once
16
17#include <stddef.h>
18#include <utility>
19
20#include "common/sys/rc.h"
21#include "common/lang/mutex.h"
22#include "sql/parser/parse.h"
23#include "storage/field/field_meta.h"
24#include "storage/record/record_manager.h"
25#include "storage/table/table.h"
26
32class Db;
33class LogHandler;
34class LogEntry;
35class Trx;
36class LogReplayer;
37
45{
46public:
51 enum class Type : int
52 {
53 INSERT,
54 UPDATE,
55 DELETE,
56 UNDEFINED,
57 };
58
59public:
60 Operation(Type type, Table *table, const RID &rid)
61 : type_(type), table_(table), page_num_(rid.page_num), slot_num_(rid.slot_num)
62 {}
63
64 Type type() const { return type_; }
65 int32_t table_id() const { return table_->table_id(); }
66 Table *table() const { return table_; }
67 PageNum page_num() const { return page_num_; }
68 SlotNum slot_num() const { return slot_num_; }
69
70private:
73
74 Table *table_ = nullptr;
75 PageNum page_num_; // TODO use RID instead of page num and slot num
76 SlotNum slot_num_;
77};
78
80{
81public:
82 size_t operator()(const Operation &op) const { return (((size_t)op.page_num()) << 32) | (op.slot_num()); }
83};
84
86{
87public:
88 bool operator()(const Operation &op1, const Operation &op2) const
89 {
90 return op1.table_id() == op2.table_id() && op1.page_num() == op2.page_num() && op1.slot_num() == op2.slot_num();
91 }
92};
93
98class TrxKit
99{
100public:
106 enum Type
107 {
110 };
111
112public:
113 TrxKit() = default;
114 virtual ~TrxKit() = default;
115
116 virtual RC init() = 0;
117 virtual const vector<FieldMeta> *trx_fields() const = 0;
118
119 virtual Trx *create_trx(LogHandler &log_handler) = 0;
120
124 virtual Trx *create_trx(LogHandler &log_handler, int32_t trx_id) = 0;
125 virtual Trx *find_trx(int32_t trx_id) = 0;
126 virtual void all_trxes(vector<Trx *> &trxes) = 0;
127
128 virtual void destroy_trx(Trx *trx) = 0;
129
130 virtual LogReplayer *create_log_replayer(Db &db, LogHandler &log_handler) = 0;
131
132public:
133 static TrxKit *create(const char *name);
134};
135
140class Trx
141{
142public:
143 Trx() = default;
144 virtual ~Trx() = default;
145
146 virtual RC insert_record(Table *table, Record &record) = 0;
147 virtual RC delete_record(Table *table, Record &record) = 0;
148 virtual RC visit_record(Table *table, Record &record, ReadWriteMode mode) = 0;
149
150 virtual RC start_if_need() = 0;
151 virtual RC commit() = 0;
152 virtual RC rollback() = 0;
153
154 virtual RC redo(Db *db, const LogEntry &log_entry) = 0;
155
156 virtual int32_t id() const = 0;
157};
一个DB实例负责管理一批表
Definition: db.h:46
描述一条日志
Definition: log_entry.h:44
对外提供服务的CLog模块
Definition: log_handler.h:40
日志回放接口类
Definition: log_replayer.h:26
Definition: trx.h:86
Definition: trx.h:80
描述一个操作,比如插入、删除行等
Definition: trx.h:45
Type type_
< 操作的哪张表。这里直接使用表其实并不准确,因为表中的索引也可能有日志
Definition: trx.h:72
表示一个记录
Definition: record.h:101
Definition: table.h:42
事务管理器
Definition: trx.h:99
virtual Trx * create_trx(LogHandler &log_handler, int32_t trx_id)=0
创建一个事务,日志回放时使用
事务接口
Definition: trx.h:141
Type
操作的类型
Definition: trx.h:52
Type
事务管理器的类型
Definition: trx.h:107
@ MVCC
支持MVCC的事务管理器
Definition: trx.h:109
@ VACUOUS
空的事务管理器,不做任何事情
Definition: trx.h:108
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35