MiniOB
1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
src
observer
storage
trx
trx.h
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2
miniob is licensed under Mulan PSL v2.
3
You can use this software according to the terms and conditions of the Mulan PSL v2.
4
You may obtain a copy of Mulan PSL v2 at:
5
http://license.coscl.org.cn/MulanPSL2
6
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9
See 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
32
class
Db
;
33
class
LogHandler
;
34
class
LogEntry
;
35
class
Trx
;
36
class
LogReplayer
;
37
44
class
Operation
45
{
46
public
:
51
enum class
Type
:
int
52
{
53
INSERT,
54
UPDATE,
55
DELETE,
56
UNDEFINED,
57
};
58
59
public
:
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
70
private
:
72
Type
type_
;
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
79
class
OperationHasher
80
{
81
public
:
82
size_t
operator()(
const
Operation
&op)
const
{
return
(((
size_t
)op.page_num()) << 32) | (op.slot_num()); }
83
};
84
85
class
OperationEqualer
86
{
87
public
:
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
98
class
TrxKit
99
{
100
public
:
106
enum
Type
107
{
108
VACUOUS
,
109
MVCC
,
110
};
111
112
public
:
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
132
public
:
133
static
TrxKit
*create(
const
char
*name);
134
};
135
140
class
Trx
141
{
142
public
:
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
一个DB实例负责管理一批表
Definition:
db.h:46
LogEntry
描述一条日志
Definition:
log_entry.h:44
LogHandler
对外提供服务的CLog模块
Definition:
log_handler.h:40
LogReplayer
日志回放接口类
Definition:
log_replayer.h:26
OperationEqualer
Definition:
trx.h:86
OperationHasher
Definition:
trx.h:80
Operation
描述一个操作,比如插入、删除行等
Definition:
trx.h:45
Operation::type_
Type type_
< 操作的哪张表。这里直接使用表其实并不准确,因为表中的索引也可能有日志
Definition:
trx.h:72
Record
表示一个记录
Definition:
record.h:101
Table
表
Definition:
table.h:42
TrxKit
事务管理器
Definition:
trx.h:99
TrxKit::create_trx
virtual Trx * create_trx(LogHandler &log_handler, int32_t trx_id)=0
创建一个事务,日志回放时使用
Trx
事务接口
Definition:
trx.h:141
Operation::Type
Type
操作的类型
Definition:
trx.h:52
TrxKit::Type
Type
事务管理器的类型
Definition:
trx.h:107
TrxKit::MVCC
@ MVCC
支持MVCC的事务管理器
Definition:
trx.h:109
TrxKit::VACUOUS
@ VACUOUS
空的事务管理器,不做任何事情
Definition:
trx.h:108
RID
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition:
record.h:35
制作者
1.9.5