MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
disk_buffer_pool.h
1/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and 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 Meiyi & Longda on 2021/4/13.
13//
14#pragma once
15
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <time.h>
23#include <string>
24#include <mutex>
25#include <unordered_map>
26#include <functional>
27
28#include "common/rc.h"
29#include "common/types.h"
30#include "common/lang/mutex.h"
31#include "common/mm/mem_pool.h"
32#include "common/lang/lru_cache.h"
33#include "common/lang/lruk_cache.h"
34#include "common/lang/bitmap.h"
35#include "storage/buffer/page.h"
36#include "storage/buffer/frame.h"
37
39class DiskBufferPool;
40
46#define BP_FILE_SUB_HDR_SIZE (sizeof(BPFileSubHeader))
47
60{
61 int32_t page_count;
63 char bitmap[0];
64
68 static const int MAX_PAGE_NUM = (BP_PAGE_DATA_SIZE - sizeof(page_count) - sizeof(allocated_pages)) * 8;
69
70 std::string to_string() const;
71};
72
82{
83public:
84 BPFrameManager(const char *tag);
85
86 RC init(int pool_num);
87 RC cleanup();
88
96 Frame *get(int file_desc, PageNum page_num);
97
104 std::list<Frame *> find_list(int file_desc);
105
113 Frame *alloc(int file_desc, PageNum page_num);
114
119 RC free(int file_desc, PageNum page_num, Frame *frame);
120
128 int purge_frames(int count, std::function<RC(Frame *frame)> purger);
129
130 size_t frame_num() const
131 {
132 return frames_.count();
133 }
134
138 size_t total_frame_num() const
139 {
140 return allocator_.get_size();
141 }
142
143private:
144 Frame *get_internal(const FrameId &frame_id);
145 RC free_internal(const FrameId &frame_id, Frame *frame);
146
147private:
149 public:
150 size_t operator()(const FrameId &frame_id) const
151 {
152 return frame_id.hash();
153 }
154 };
155
156 using FrameLruCache = common::LruCache<FrameId, Frame *, BPFrameIdHasher>;
157 using FrameAllocator = common::MemPoolSimple<Frame>;
158
159 std::mutex lock_;
160 FrameLruCache frames_;
161 FrameAllocator allocator_;
162
163 // npu-exp: you can try this
164 // using FrameLruKCache = common::LruKCache<2, FrameId, Frame *, BPFrameIdHasher>;
165 // FrameKLruCache frames_;
166};
167
173{
174public:
177
178 RC init(DiskBufferPool &bp, PageNum start_page = 0);
179 bool has_next();
180 PageNum next();
181 RC reset();
182
183private:
184 common::Bitmap bitmap_;
185 PageNum current_page_num_ = -1;
186};
187
193{
194public:
195 DiskBufferPool(BufferPoolManager &bp_manager, BPFrameManager &frame_manager);
197
201 RC create_file(const char *file_name);
202
206 RC open_file(const char *file_name);
207
211 RC close_file();
212
216 RC get_this_page(PageNum page_num, Frame **frame);
217
223 RC allocate_page(Frame **frame);
224
230 RC dispose_page(PageNum page_num);
231
236 RC purge_page(PageNum page_num);
237 RC purge_all_pages();
238
246 RC unpin_page(Frame *frame);
247
253
254 int file_desc() const;
255
259 RC flush_page(Frame &frame);
260
264 RC flush_all_pages();
265
269 RC recover_page(PageNum page_num);
270
271protected:
272 RC allocate_frame(PageNum page_num, Frame **buf);
273
277 RC purge_frame(PageNum page_num, Frame *used_frame);
278 RC check_page_num(PageNum page_num);
279
283 RC load_page(PageNum page_num, Frame *frame);
284
288 RC flush_page_internal(Frame &frame);
289
290private:
291 BufferPoolManager & bp_manager_;
292 BPFrameManager & frame_manager_;
293
294 std::string file_name_;
295 int file_desc_ = -1;
296 Frame * hdr_frame_ = nullptr;
297 BPFileHeader * file_header_ = nullptr;
298 std::set<PageNum> disposed_pages_;
299
300 common::Mutex lock_;
301private:
302 friend class BufferPoolIterator;
303};
304
310{
311public:
312 BufferPoolManager(int memory_size = 0);
314
315 RC create_file(const char *file_name);
316 RC open_file(const char *file_name, DiskBufferPool *&bp);
317 RC close_file(const char *file_name);
318
319 RC flush_page(Frame &frame);
320
321public:
322 static void set_instance(BufferPoolManager *bpm); // TODO 优化全局变量的表示方法
323 static BufferPoolManager &instance();
324
325private:
326 BPFrameManager frame_manager_{"BufPool"};
327
328 common::Mutex lock_;
329 std::unordered_map<std::string, DiskBufferPool *> buffer_pools_;
330 std::unordered_map<int, DiskBufferPool *> fd_buffer_pools_;
331};
Definition: disk_buffer_pool.h:148
管理页面Frame
Definition: disk_buffer_pool.h:82
size_t total_frame_num() const
Definition: disk_buffer_pool.h:138
int purge_frames(int count, std::function< RC(Frame *frame)> purger)
Definition: disk_buffer_pool.cpp:62
RC free(int file_desc, PageNum page_num, Frame *frame)
Definition: disk_buffer_pool.cpp:142
Frame * alloc(int file_desc, PageNum page_num)
分配一个新的页面
Definition: disk_buffer_pool.cpp:121
std::list< Frame * > find_list(int file_desc)
列出所有指定文件的页面
Definition: disk_buffer_pool.cpp:164
Frame * get(int file_desc, PageNum page_num)
获取指定的页面
Definition: disk_buffer_pool.cpp:104
用于遍历BufferPool中的所有页面
Definition: disk_buffer_pool.h:173
BufferPool的管理类
Definition: disk_buffer_pool.h:310
RC create_file(const char *file_name)
Definition: disk_buffer_pool.cpp:636
BufferPool的实现
Definition: disk_buffer_pool.h:193
RC purge_frame(PageNum page_num, Frame *used_frame)
Definition: disk_buffer_pool.cpp:424
RC load_page(PageNum page_num, Frame *frame)
Definition: disk_buffer_pool.cpp:591
RC check_all_pages_unpinned()
Definition: disk_buffer_pool.cpp:469
RC close_file()
Definition: disk_buffer_pool.cpp:266
RC flush_page_internal(Frame &frame)
Definition: disk_buffer_pool.cpp:494
RC get_this_page(PageNum page_num, Frame **frame)
Definition: disk_buffer_pool.cpp:295
RC open_file(const char *file_name)
Definition: disk_buffer_pool.cpp:227
RC purge_page(PageNum page_num)
释放指定文件关联的页的内存 如果已经脏, 则刷到磁盘,除了pinned page
Definition: disk_buffer_pool.cpp:445
RC unpin_page(Frame *frame)
用于解除pageHandle对应页面的驻留缓冲区限制
Definition: disk_buffer_pool.cpp:418
RC flush_page(Frame &frame)
Definition: disk_buffer_pool.cpp:488
RC flush_all_pages()
Definition: disk_buffer_pool.cpp:516
RC allocate_page(Frame **frame)
Definition: disk_buffer_pool.cpp:331
RC create_file(const char *file_name)
RC recover_page(PageNum page_num)
Definition: disk_buffer_pool.cpp:529
RC dispose_page(PageNum page_num)
释放某个页面,将此页面设置为未分配状态
Definition: disk_buffer_pool.cpp:399
页帧标识符
Definition: frame.h:34
页帧
Definition: frame.h:62
BufferPool的文件第一个页面,存放一些元数据信息,包括了后面每页的分配信息。
Definition: disk_buffer_pool.h:60
static const int MAX_PAGE_NUM
页面分配位图, 第0个页面(就是当前页面),总是1
Definition: disk_buffer_pool.h:68
int32_t allocated_pages
当前文件一共有多少个页面
Definition: disk_buffer_pool.h:62
char bitmap[0]
已经分配了多少个页面
Definition: disk_buffer_pool.h:63