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#include <string>
20#include <mutex>
21#include <set>
22#include <atomic>
23
24#include "storage/buffer/page.h"
25#include "common/log/log.h"
26#include "common/lang/mutex.h"
27#include "common/types.h"
28
33class FrameId
34{
35public:
36 FrameId(int file_desc, 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 file_desc() const;
41 PageNum page_num() const;
42
43 friend std::string to_string(const FrameId &frame_id);
44private:
45 int file_desc_;
46 PageNum page_num_;
47};
48
61class Frame
62{
63public:
64 ~Frame()
65 {
66 // LOG_DEBUG("deallocate frame. this=%p, lbt=%s", this, common::lbt());
67 }
68
74 void reinit()
75 {}
76 void reset()
77 {}
78
79 void clear_page()
80 {
81 memset(&page_, 0, sizeof(page_));
82 }
83
84 int file_desc() const { return file_desc_; }
85 void set_file_desc(int fd) { file_desc_ = fd; }
86 Page & page() { return page_; }
87 PageNum page_num() const { return page_.page_num; }
88 void set_page_num(PageNum page_num) { page_.page_num = page_num; }
89 FrameId frame_id() const { return FrameId(file_desc_, page_.page_num); }
90 LSN lsn() const { return page_.lsn; }
91 void set_lsn(LSN lsn) { page_.lsn = lsn; }
92
94 void access();
95
100 void mark_dirty() { dirty_ = true; }
101 void clear_dirty() { dirty_ = false; }
102 bool dirty() const { return dirty_; }
103
104 char *data() { return page_.data; }
105
106 bool can_purge() { return pin_count_.load() == 0; }
107
112 void pin();
113
118 int unpin();
119 int pin_count() const { return pin_count_.load(); }
120
121 void write_latch();
122 void write_latch(intptr_t xid);
123
124 void write_unlatch();
125 void write_unlatch(intptr_t xid);
126
127 void read_latch();
128 void read_latch(intptr_t xid);
129 bool try_read_latch();
130
131 void read_unlatch();
132 void read_unlatch(intptr_t xid);
133
134 friend std::string to_string(const Frame &frame);
135
136private:
137 friend class BufferPool;
138
139 bool dirty_ = false;
140 std::atomic<int> pin_count_{0};
141 unsigned long acc_time_ = 0;
142 int file_desc_ = -1;
143 Page page_;
144
146 common::RecursiveSharedMutex lock_;
147
150 common::DebugMutex debug_lock_;
151 intptr_t write_locker_ = 0;
152 int write_recursive_count_ = 0;
153 std::unordered_map<intptr_t, int> read_lockers_;
154};
155
页帧标识符
Definition: frame.h:34
页帧
Definition: frame.h:62
common::RecursiveSharedMutex lock_
在非并发编译时,加锁解锁动作将什么都不做
Definition: frame.h:146
void reinit()
reinit 和 reset 在 MemPoolSimple 中使用
Definition: frame.h:74
void access()
刷新访问时间 TODO touch is better?
Definition: frame.cpp:280
void pin()
给当前页帧增加引用计数 pin通常都会加着frame manager锁来访问
Definition: frame.cpp:230
void mark_dirty()
标记指定页面为“脏”页。如果修改了页面的内容,则应调用此函数, 以便该页面被淘汰出缓冲区时系统将新的页面数据写入磁盘文件
Definition: frame.h:100
int unpin()
释放一个当前页帧的引用计数 与pin对应,但是通常不会加着frame manager的锁来访问
Definition: frame.cpp:243
common::DebugMutex debug_lock_
使用一些手段来做测试,提前检测出头疼的死锁问题 如果编译时没有增加调试选项,这些代码什么都不做
Definition: frame.h:150
表示一个页面,可能放在内存或磁盘上
Definition: page.h:34