tinySQL
0.1
A self-contained database management system
src
include
buffer
buffer_manager.h
1
11
#ifndef _BUFFER_MANAGER_H_
12
#define _BUFFER_MANAGER_H_ 1
13
14
#include <cstdio>
15
#include <iostream>
16
#include <vector>
17
#include <string>
18
#include <sys/timeb.h>
19
#include <sys/stat.h>
20
#include <filesystem>
21
#include <map>
22
23
#include "share/config.h"
24
#include "share/err_type.h"
25
30
class
Page
{
31
public
:
32
Page
();
33
void
initialize
();
34
void
setFileName(std::string file_name);
35
std::string getFileName()
const
;
36
void
setBlockId(
int
block_id);
37
int
getBlockId()
const
;
38
void
setPinCount(
int
pin_count);
39
int
getPinCount()
const
;
40
void
setDirty(
bool
dirty);
41
bool
isDirty()
const
;
42
void
setAvaliable(
bool
avaliable);
43
bool
getAvaliable()
const
;
44
void
setTime();
45
long
long
getTime();
46
char
* getBuffer();
47
// private:
48
char
buffer_
[PAGESIZE];
49
std::string
file_name_
;
50
int
block_id_
;
51
int
pin_count_
;
52
bool
dirty_
;
53
bool
avaliable_
;
54
long
long
last_access_time;
55
56
std::string getPageStrId(){
57
return
getFileName() + std::to_string(getBlockId());
58
};
59
60
static
std::string generatePageStrId(
const
std::string &file_path,
int
b_id){
61
return
file_path + std::to_string(b_id);
62
}
63
};
64
69
class
BufferManager
{
70
public
:
71
BufferManager
();
72
BufferManager
(
int
frame_size);
73
~BufferManager
();
74
char
*
getPage
(
const
std::string& file_name ,
int
block_id);
75
char
*
getPage
(
const
std::string& file_name,
int
block_id, pageId_t& pageId);
76
void
modifyPage
(
int
page_id);
77
void
pinPage
(
int
page_id);
78
int
unpinPage
(
int
page_id);
79
int
flushPage
(pageId_t page_id);
80
int
getPageId
(
const
std::string& file_name ,
int
block_id);
82
static
size_t
getFileSize(std::string file_name){
83
struct
stat file_state{};
84
const
char
*path = file_name.c_str();
85
if
(stat(path, &file_state) == -1){
86
std::cout <<
"File path error"
<< std::endl;
87
throw
DB_FILE_NOT_FOUND;
88
}
89
size_t
size = file_state.st_size;
90
return
size;
91
}
92
93
static
bool
FileExists(
const
std::string& path){
94
return
std::filesystem::exists(path);
95
}
96
97
static
void
createEmptyFile(
const
std::string& path){
98
FILE* f;
99
f = fopen(path.c_str(),
"w"
);
100
if
(f ==
nullptr
)
throw
DB_FAILED;
101
fclose(f);
102
}
103
104
void
removeFile(
const
std::string& path){
105
remove(path.c_str());
106
auto
iter = fname_page_map.find(path);
107
if
(iter != fname_page_map.end()){
// Bfm still has a block of the file. Clear.
108
Frames[iter->second].
initialize
();
109
}
110
}
111
112
int
getBlockNum
(
const
std::string& fileName);
113
114
private
:
115
Page
* Frames;
116
int
frame_size_;
117
int
current_position_;
118
void
initialize(
int
frame_size);
119
int
getEmptyPageId();
120
int
loadDiskBlock(
int
page_id ,
const
std::string& file_name ,
int
block_id);
121
std::map<std::string, pageId_t> fname_page_map;
122
};
123
124
#endif
BufferManager::getBlockNum
int getBlockNum(const std::string &fileName)
获取文件块数
Definition:
buffer_manager.cpp:270
BufferManager::unpinPage
int unpinPage(int page_id)
解钉一次
Definition:
buffer_manager.cpp:173
BufferManager::getPageId
int getPageId(const std::string &file_name, int block_id)
简单遍历获取页号
Definition:
buffer_manager.cpp:258
BufferManager
Buffer manager is an abstraction of memory on computer for modules at higher level.
Definition:
buffer_manager.h:69
Page::dirty_
bool dirty_
Definition:
buffer_manager.h:52
Page::initialize
void initialize()
初始化
Definition:
buffer_manager.cpp:26
Page
The storage details of each page in the memory.
Definition:
buffer_manager.h:30
BufferManager::~BufferManager
~BufferManager()
Destroy the Buffer Manager:: Buffer Manager object.
Definition:
buffer_manager.cpp:108
Page::file_name_
std::string file_name_
Definition:
buffer_manager.h:49
BufferManager::flushPage
int flushPage(pageId_t page_id)
核心函数之一。内存和磁盘交互的接口。
Definition:
buffer_manager.cpp:227
Page::pin_count_
int pin_count_
Definition:
buffer_manager.h:51
BufferManager::modifyPage
void modifyPage(int page_id)
标记页是否被修改
Definition:
buffer_manager.cpp:155
BufferManager::BufferManager
BufferManager()
Construct a new Buffer Manager:: Buffer Manager object.
Definition:
buffer_manager.cpp:96
Page::block_id_
int block_id_
Definition:
buffer_manager.h:50
Page::avaliable_
bool avaliable_
Definition:
buffer_manager.h:53
BufferManager::pinPage
void pinPage(int page_id)
钉住一页
Definition:
buffer_manager.cpp:163
BufferManager::getPage
char * getPage(const std::string &file_name, int block_id)
获取一页
Definition:
buffer_manager.cpp:130
Page::Page
Page()
Construct a new Page:: Page object.
Definition:
buffer_manager.cpp:19
Page::buffer_
char buffer_[PAGESIZE]
Definition:
buffer_manager.h:48
Generated by
1.8.17