forked from bianhq/tinydata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.h
125 lines (109 loc) · 2.73 KB
/
storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
存储管理
段页式文件块管理
基于Clock算法的请求分页式缓冲区管理
段页表用了定长的数组,之后要改成动态数组或者链表
采用64位逻辑地址:段表号32位,页表号20位,页内偏移12位
*/
#ifndef STORAGE_H_INCLUDED
#define STORAGE_H_INCLUDED
#include "util.h"
#include <stdio.h>
#define BLOCK_SIZE 3692u
#define META_HEAD_SIZE 1024u
#define SEG_NUM 4
/**
file_manager
*/
#define MAX_PAGE_TAB_LEN 1048576
#define INIT_PAGE_TAB_LEN 51200
#define MAX_US_NUM 100
#define PAGE_SIZE 4096u
#define PAGE_HEAD_SIZE 404u
/**
buf_manager
*/
#define BUFFED_FRAMES 100u
struct PageTabItem
{
bool Used;
Addr PageEnt;
PageTabItem();
};
struct SegTabItem
{
bool Used;
uint PageTableLength;
PageTabItem *PageTable;
SegTabItem();
};
struct UsedSpace
{
PageAddr Pos;
ushort Length;
};
struct Frame
{
bool Valid;
ushort UsedBytes;
ushort USNum;
UsedSpace USTable[MAX_US_NUM];
byte FilePage[PAGE_SIZE];
};
struct FastTabItem
{
Addr PageEnt;
uint FrameId;
bool A;//访问位,在Clock置换算法中使用
bool M;//修改位,在换出是,决定是否需要写回文件
bool P;//Pin位
bool SLock;//共享锁
bool Xlock;//排它锁
};
struct FastTable
{
uint length;
FastTabItem Items[BUFFED_FRAMES];
};
class Storage
{
private:
Addr p;
uint index[SEG_NUM];//标记上一次查询到的、快表中的下标
Frame Buffer[BUFFED_FRAMES];
FastTable FastTables[SEG_NUM];
SegTabItem SegPage[SEG_NUM];
FILE *Data, *Meta;// = NULL = NULL
char MetaPath[MAX_PATH_LEN];
char DataPath[MAX_PATH_LEN];
uint64 DataPageNum;
Addr FM_AllocPage(uint segId);
int FM_FreePage(Addr pageEnt);
int FM_ReadPage(Addr pageEnt, void *buf);
int FM_WritePage(Addr pageEnt, const void *buf);
void FM_LoadSegPage();
void FM_FlushSegPage();
inline uint FM_GetSegId(Addr addr);
inline uint FM_GetPageId(Addr addr);
FastTabItem* ClockAlg(uint segId);
void CloseFileStream();
int HitBuffer(Addr addr);
void OpenFileStream();
int ReadFrmFile(char f, uint offset, void *buf, size_t nbytes);
int WriteToFile(char f, uint offset, const void *buf, size_t nbytes);
public:
Storage(char *dataPath, char *metaPath);
~Storage();
Addr Alloc(uint segId, uint length);
int Free(Addr pageEnt, uint length);
int Read(Addr pageEnt, void *buf, uint length);
int ReadDataPage(uint pageId, void *page);
int Write(Addr pageEnt, const void *buf, uint length);
void PageToFrame(byte *filePage, Frame *frame);
void FrameToPage(Frame *frame, byte *filePage);
uint64 GetDataPageNum();
void Create();
void Init();
void Flush();
};
#endif // STORAGE_H_INCLUDED