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 "common/lang/vector.h"
18#include "storage/trx/trx.h"
19#include "storage/trx/mvcc_trx_log.h"
20
21class CLogManager;
22class LogHandler;
24
25class MvccTrxKit : public TrxKit
26{
27public:
28 MvccTrxKit() = default;
29 virtual ~MvccTrxKit();
30
31 RC init() override;
32 const vector<FieldMeta> *trx_fields() const override;
33
34 Trx *create_trx(LogHandler &log_handler) override;
35 Trx *create_trx(LogHandler &log_handler, int32_t trx_id) override;
36 void destroy_trx(Trx *trx) override;
37
38 void all_trxes(vector<Trx *> &trxes) override;
39
40 LogReplayer *create_log_replayer(Db &db, LogHandler &log_handler) override;
41
42public:
43 int32_t next_trx_id();
44
45public:
46 int32_t max_trx_id() const;
47
48private:
49 vector<FieldMeta> fields_; // 存储事务数据需要用到的字段元数据,所有表结构都需要带的
50
51 atomic<int32_t> current_trx_id_{0};
52
53 common::Mutex lock_;
54 vector<Trx *> trxes_;
55};
56
62class MvccTrx : public Trx
63{
64public:
70 MvccTrx(MvccTrxKit &trx_kit, LogHandler &log_handler);
71 MvccTrx(MvccTrxKit &trx_kit, LogHandler &log_handler, int32_t trx_id); // used for recover
72 virtual ~MvccTrx();
73
74 RC insert_record(Table *table, Record &record) override;
75 RC delete_record(Table *table, Record &record) override;
76 RC update_record(Table *table, Record &old_record, Record &new_record) override { return RC::UNIMPLEMENTED; };
77
88 RC visit_record(Table *table, Record &record, ReadWriteMode mode) override;
89
90 RC start_if_need() override;
91 RC commit() override;
92 RC rollback() override;
93
94 RC redo(Db *db, const LogEntry &log_entry) override;
95
96 int32_t id() const override { return trx_id_; }
97
98private:
99 RC commit_with_trx_id(int32_t commit_id);
100 void trx_fields(Table *table, Field &begin_xid_field, Field &end_xid_field) const;
101
102private:
103 static const int32_t MAX_TRX_ID = numeric_limits<int32_t>::max();
104
105private:
106 // using OperationSet = unordered_set<Operation, OperationHasher, OperationEqualer>;
107 using OperationSet = vector<Operation>;
108
109 MvccTrxKit &trx_kit_;
110 MvccTrxLogHandler log_handler_;
111 int32_t trx_id_ = -1;
112 bool started_ = false;
113 bool recovering_ = false;
114 OperationSet operations_;
115};
一个DB实例负责管理一批表
Definition: db.h:46
字段
Definition: field.h:25
描述一条日志
Definition: log_entry.h:44
对外提供服务的CLog模块
Definition: log_handler.h:40
日志回放接口类
Definition: log_replayer.h:26
Definition: mvcc_trx.h:26
处理事务日志的辅助类
Definition: mvcc_trx_log.h:110
多版本并发事务TODO 没有垃圾回收
Definition: mvcc_trx.h:63
void trx_fields(Table *table, Field &begin_xid_field, Field &end_xid_field) const
获取指定表上的事务使用的字段
Definition: mvcc_trx.cpp:237
RC visit_record(Table *table, Record &record, ReadWriteMode mode) override
当访问到某条数据时,使用此函数来判断是否可见,或者是否有访问冲突
Definition: mvcc_trx.cpp:175
表示一个记录
Definition: record.h:101
Definition: table.h:42
事务管理器
Definition: trx.h:99
事务接口
Definition: trx.h:141
Definition: mutex.h:268