MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
frame.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 lianyu on 2022/10/29.
13//
14
15#pragma once
16
17#include <pthread.h>
18#include <string.h>
19
20#include "common/lang/mutex.h"
21#include "common/lang/string.h"
22#include "common/lang/atomic.h"
23#include "common/lang/unordered_map.h"
24#include "common/log/log.h"
25#include "common/types.h"
26#include "storage/buffer/page.h"
27
33{
34public:
35 FrameId() = default;
36 FrameId(int buffer_pool_id, PageNum page_num);
37 bool equal_to(const FrameId &other) const;
38 bool operator==(const FrameId &other) const;
39 size_t hash() const;
40 int buffer_pool_id() const;
41 PageNum page_num() const;
42
43 void set_buffer_pool_id(int buffer_pool_id) { buffer_pool_id_ = buffer_pool_id; }
44 void set_page_num(PageNum page_num) { page_num_ = page_num; }
45
46 string to_string() const;
47
48private:
49 int buffer_pool_id_ = -1;
50 PageNum page_num_ = -1;
51};
52
65class Frame
66{
67public:
68 ~Frame()
69 {
70 // LOG_DEBUG("deallocate frame. this=%p, lbt=%s", this, common::lbt());
71 }
72
78 void reinit() {}
79 void reset() {}
80
81 void clear_page() { memset(&page_, 0, sizeof(page_)); }
82
83 int buffer_pool_id() const { return frame_id_.buffer_pool_id(); }
84 void set_buffer_pool_id(int id) { frame_id_.set_buffer_pool_id(id); }
85
91 Page &page() { return page_; }
92
97 PageNum page_num() const { return frame_id_.page_num(); }
98 void set_page_num(PageNum page_num) { frame_id_.set_page_num(page_num); }
99 FrameId frame_id() const { return frame_id_; }
100
106 LSN lsn() const { return page_.lsn; }
107 void set_lsn(LSN lsn) { page_.lsn = lsn; }
108
113 CheckSum check_sum() const { return page_.check_sum; }
114 void set_check_sum(CheckSum check_sum) { page_.check_sum = check_sum; }
115
122 void access();
123
129 void mark_dirty() { dirty_ = true; }
130
135 void clear_dirty() { dirty_ = false; }
136 bool dirty() const { return dirty_; }
137
138 char *data() { return page_.data; }
139
140 bool can_purge() { return pin_count_.load() == 0; }
141
147 void pin();
148
153 int unpin();
154 int pin_count() const { return pin_count_.load(); }
155
156 void write_latch();
157 void write_latch(intptr_t xid);
158
159 void write_unlatch();
160 void write_unlatch(intptr_t xid);
161
162 void read_latch();
163 void read_latch(intptr_t xid);
164 bool try_read_latch();
165
166 void read_unlatch();
167 void read_unlatch(intptr_t xid);
168
169 string to_string() const;
170
171private:
172 friend class BufferPool;
173
174 bool dirty_ = false;
175 atomic<int> pin_count_{0};
176 unsigned long acc_time_ = 0;
177 FrameId frame_id_;
178 Page page_;
179
182
186 intptr_t write_locker_ = 0;
187 int write_recursive_count_ = 0;
188 unordered_map<intptr_t, int> read_lockers_;
189};
页帧标识符
Definition: frame.h:33
页帧
Definition: frame.h:66
CheckSum check_sum() const
页面校验和
Definition: frame.h:113
common::RecursiveSharedMutex lock_
在非并发编译时,加锁解锁动作将什么都不做
Definition: frame.h:181
void reinit()
reinit 和 reset 在 MemPoolSimple 中使用
Definition: frame.h:78
void access()
刷新当前内存页面的访问时间
Definition: frame.cpp:259
void pin()
给当前页帧增加引用计数 pin通常都会加着frame manager锁来访问。 当我们访问某个页面时,我们不期望此页面被淘汰,所以我们会增加引用计数。
Definition: frame.cpp:211
void mark_dirty()
标记指定页面为“脏”页。
Definition: frame.h:129
Page & page()
在磁盘和内存中内容完全一致的数据页
Definition: frame.h:91
PageNum page_num() const
每个页面都有一个编号
Definition: frame.h:97
int unpin()
释放一个当前页帧的引用计数 与pin对应,但是通常不会加着frame manager的锁来访问
Definition: frame.cpp:224
LSN lsn() const
为了实现持久化,需要将页面的修改记录记录到日志中,这里记录了日志序列号
Definition: frame.h:106
void clear_dirty()
重置“脏”标记
Definition: frame.h:135
common::DebugMutex debug_lock_
使用一些手段来做测试,提前检测出头疼的死锁问题 如果编译时没有增加调试选项,这些代码什么都不做
Definition: frame.h:185
Definition: mutex.h:253
Definition: mutex.h:310
表示一个页面,可能放在内存或磁盘上
Definition: page.h:34