-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_backup.hpp
111 lines (78 loc) · 3.6 KB
/
buffer_backup.hpp
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
#ifndef __BUFFER_BACKUP_H__
#define __BUFFER_BACKUP_H__
#include "diskmanage.hpp"
typedef struct TableInfo_t{
int num_table;
bool in_use[MAX_TABLE_NUMBER + 1];
char pathname[MAX_TABLE_NUMBER + 1][512];
int fd[MAX_TABLE_NUMBER + 1];
} TableInfo_t;
struct BufferBlock_t{
// Physical frame which contains up-to-date contents of target page
Page_t frame;
// The unique ID of table(file) containing target page
int table_id;
// The target page number within a table
Pagenum_t page_num;
// Indicates whether this block is dirty or not
bool is_dirty;
// Indicates wheter this block is pinned or not
int pin_count;
// Points previous and next buffer blocks
// Buffer blocks are managed by the LRU(least recently used) list
struct BufferBlock_t * prev, * next;
};
extern struct BufferBlock_t * buffer;
extern struct TableInfo_t tables;
void clear(BufferBlock_t * frame);
/*
Functions for operating database in top layer
*/
// Allocate the buffer pool (array) with the given number of entries.
// Initialize other fields such as state info, LRU info
// If success, return 0. Otherwise, return non zero value.
int init_db(int num_buf);
// Open existing data file using ‘pathname’ or create one if not existed.
// If success, return the unique table id, which represents the own table in this database. Otherwise,
// return negative value.
// You have to maintain a table id once open_table () is called, which is matching file descriptor
// or file pointer depending on your previous implementation. (table id ≥1 and maximum allocated id is set to 10)
int open_table (char * pathname);
// Write the pages relating to this table to disk and close the table
int close_table(int table_id);
// Destroy buffer manager
int shutdown_db(void);
// Functions for read/write pages in buffer.
// If the page is not in buffer pool (cache miss), read page from disk and maintain that page in buffer block.
// Page modification only occurs in memory buffer. If the page frame in buffer is updated,
// mark the buffer block as dirty.
// According to LRU policy, least recently used buffer is the victim for page eviction.
// Writing page to disk occurs during LRU page eviction.
// Read page from buffer and increase pin count by 1
// This may cause page enviction
BufferBlock_t * buffer_read_page(int table_id, Pagenum_t page_num);
// Write page to buffer
// This makes frame dirty and increases pin count by 1
void buffer_write_page(BufferBlock_t * frame, Page_t page);
// Flush all data of frame into coresponding disk page
void buffer_flush_page(BufferBlock_t * frame);
// Allocate new page from disk and stage it on a buffer frame
// This increases pin count by 1
BufferBlock_t * buffer_allocate_page(int table_id);
// Free an allocated page from the buffer
// Flush the changes into the disk and remove frame from the buffer
void buffer_free_page(BufferBlock_t * frame);
// Decrease the pin count of frame in buffer by count
void buffer_unpin_page(BufferBlock_t * frame, int count);
// Decrease the pin count of frame in buffer by 1
void buffer_unpin_page(BufferBlock_t * frame);
// Check if the parent and it child is connected well or not
void buffer_check_relationship();
// Print some information about the frame in buffer
void buffer_print_page(BufferBlock_t * frame);
// Print information of all of the frames
// Currently exist in buffer
void buffer_print_all();
// Print information of a currently opened table
void print_all_tables();
#endif /* __BUFFER_BACKUP_H__ */