MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
mvcc_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 2023/04/24.
13//
14
15#pragma once
16
17#include <vector>
18
19#include "storage/trx/trx.h"
20
21class CLogManager;
22
23class MvccTrxKit : public TrxKit
24{
25public:
26 MvccTrxKit() = default;
27 virtual ~MvccTrxKit();
28
29 RC init() override;
30 const std::vector<FieldMeta> *trx_fields() const override;
31 Trx *create_trx(CLogManager *log_manager) override;
32 Trx *create_trx(int32_t trx_id) override;
33 void destroy_trx(Trx *trx) override;
34
39 Trx *find_trx(int32_t trx_id) override;
40 void all_trxes(std::vector<Trx *> &trxes) override;
41
42public:
43 int32_t next_trx_id();
44
45public:
46 int32_t max_trx_id() const;
47
48private:
49 std::vector<FieldMeta> fields_; // 存储事务数据需要用到的字段元数据,所有表结构都需要带的
50
51 std::atomic<int32_t> current_trx_id_{0};
52
53 common::Mutex lock_;
54 std::vector<Trx *> trxes_;
55};
56
62class MvccTrx : public Trx
63{
64public:
65 MvccTrx(MvccTrxKit &trx_kit, CLogManager *log_manager);
66 MvccTrx(MvccTrxKit &trx_kit, int32_t trx_id); // used for recover
67 virtual ~MvccTrx();
68
69 RC insert_record(Table *table, Record &record) override;
70 RC delete_record(Table *table, Record &record) override;
71
82 RC visit_record(Table *table, Record &record, bool readonly) override;
83
84 RC start_if_need() override;
85 RC commit() override;
86 RC rollback() override;
87
88 RC redo(Db *db, const CLogRecord &log_record) override;
89
90 int32_t id() const override { return trx_id_; }
91
92private:
93 RC commit_with_trx_id(int32_t commit_id);
94 void trx_fields(Table *table, Field &begin_xid_field, Field &end_xid_field) const;
95
96private:
97 static const int32_t MAX_TRX_ID = std::numeric_limits<int32_t>::max();
98
99private:
100 using OperationSet = std::unordered_set<Operation, OperationHasher, OperationEqualer>;
101 MvccTrxKit & trx_kit_;
102 CLogManager *log_manager_ = nullptr;
103 int32_t trx_id_ = -1;
104 bool started_ = false;
105 bool recovering_ = false;
106 OperationSet operations_;
107};
日志管理器
Definition: clog.h:357
表示一条日志记录
Definition: clog.h:156
一个DB实例负责管理一批表
Definition: db.h:34
字段
Definition: field.h:25
Definition: mvcc_trx.h:24
Trx * find_trx(int32_t trx_id) override
找到对应事务号的事务
Definition: mvcc_trx.cpp:99
多版本并发事务TODO 没有垃圾回收
Definition: mvcc_trx.h:63
RC visit_record(Table *table, Record &record, bool readonly) override
当访问到某条数据时,使用此函数来判断是否可见,或者是否有访问冲突
Definition: mvcc_trx.cpp:188
void trx_fields(Table *table, Field &begin_xid_field, Field &end_xid_field) const
获取指定表上的事务使用的字段
Definition: mvcc_trx.cpp:229
RC delete_record(Table *table, Record &record) override
Definition: mvcc_trx.cpp:163
表示一个记录 当前的记录都是连续存放的空间(内存或磁盘上)。 为了提高访问的效率,record通常直接记录指向页面上的内存,但是需要保证访问这种数据时,拿着锁资源。 为了方便,也提供了复制内存的方法。可...
Definition: record.h:92
Definition: table.h:37
事务管理器
Definition: trx.h:105
事务接口
Definition: trx.h:142