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 <unordered_set>
19#include <mutex>
20#include <utility>
21
22#include "sql/parser/parse.h"
23#include "storage/record/record_manager.h"
24#include "storage/field/field_meta.h"
25#include "storage/table/table.h"
26#include "common/rc.h"
27
33class Db;
34class CLogManager;
35class CLogRecord;
36class Trx;
37
44{
45public:
50 enum class Type : int
51 {
52 INSERT,
53 UPDATE,
54 DELETE,
55 UNDEFINED,
56 };
57
58public:
59 Operation(Type type, Table *table, const RID &rid)
60 : type_(type),
61 table_(table),
62 page_num_(rid.page_num),
63 slot_num_(rid.slot_num)
64 {}
65
66 Type type() const { return type_; }
67 int32_t table_id() const { return table_->table_id(); }
68 Table * table() const { return table_; }
69 PageNum page_num() const { return page_num_; }
70 SlotNum slot_num() const { return slot_num_; }
71
72private:
75
76 Table * table_ = nullptr;
77 PageNum page_num_; // TODO use RID instead of page num and slot num
78 SlotNum slot_num_;
79};
80
82{
83public:
84 size_t operator()(const Operation &op) const
85 {
86 return (((size_t)op.page_num()) << 32) | (op.slot_num());
87 }
88};
89
91{
92public:
93 bool operator()(const Operation &op1, const Operation &op2) const
94 {
95 return op1.table_id() == op2.table_id() &&
96 op1.page_num() == op2.page_num() && op1.slot_num() == op2.slot_num();
97 }
98};
99
105{
106public:
112 enum Type
113 {
116 };
117
118public:
119 TrxKit() = default;
120 virtual ~TrxKit() = default;
121
122 virtual RC init() = 0;
123 virtual const std::vector<FieldMeta> *trx_fields() const = 0;
124 virtual Trx *create_trx(CLogManager *log_manager) = 0;
125 virtual Trx *create_trx(int32_t trx_id) = 0;
126 virtual Trx *find_trx(int32_t trx_id) = 0;
127 virtual void all_trxes(std::vector<Trx *> &trxes) = 0;
128
129 virtual void destroy_trx(Trx *trx) = 0;
130
131public:
132 static TrxKit *create(const char *name);
133 static RC init_global(const char *name);
134 static TrxKit *instance();
135};
136
141class Trx
142{
143public:
144 Trx() = default;
145 virtual ~Trx() = default;
146
147 virtual RC insert_record(Table *table, Record &record) = 0;
148 virtual RC delete_record(Table *table, Record &record) = 0;
149 virtual RC visit_record(Table *table, Record &record, bool readonly) = 0;
150
151 virtual RC start_if_need() = 0;
152 virtual RC commit() = 0;
153 virtual RC rollback() = 0;
154
155 virtual RC redo(Db *db, const CLogRecord &log_record);
156
157 virtual int32_t id() const = 0;
158};
日志管理器
Definition: clog.h:357
表示一条日志记录
Definition: clog.h:156
一个DB实例负责管理一批表
Definition: db.h:34
Definition: trx.h:91
Definition: trx.h:82
描述一个操作,比如插入、删除行等
Definition: trx.h:44
Type type_
< 操作的哪张表。这里直接使用表其实并不准确,因为表中的索引也可能有日志
Definition: trx.h:74
表示一个记录 当前的记录都是连续存放的空间(内存或磁盘上)。 为了提高访问的效率,record通常直接记录指向页面上的内存,但是需要保证访问这种数据时,拿着锁资源。 为了方便,也提供了复制内存的方法。可...
Definition: record.h:92
Definition: table.h:37
事务管理器
Definition: trx.h:105
事务接口
Definition: trx.h:142
Type
操作的类型
Definition: trx.h:51
Type
事务管理器的类型
Definition: trx.h:113
@ MVCC
支持MVCC的事务管理器
Definition: trx.h:115
@ VACUOUS
空的事务管理器,不做任何事情
Definition: trx.h:114
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35