· 8 years ago · Apr 16, 2018, 01:16 AM
1///////////////////////////////////////////////////////////////////////////////
2///////////// The Header File for the Buffer Manager /////////////////////////
3///////////////////////////////////////////////////////////////////////////////
4
5
6#ifndef BUF_H
7#define BUF_H
8
9#include <vector>
10#include "db.h"
11#include "page.h"
12
13#define NUMBUF 20
14// Default number of frames, artifically small number for ease of debugging.
15
16#define HTSIZE 7
17// Hash Table size
18
19
20/*******************ALL BELOW are purely local to buffer Manager********/
21
22// You should create enums for internal errors in the buffer manager.
23enum bufErrCodes {
24 H_OK, H_ERR, ADD_ERR, RM_ERR
25};
26
27
28
29class Replacer;
30
31enum MODE {
32 READ_MODE, READ_WRITE_MODE
33};
34
35class BufMgr {
36
37 public:
38
39 typedef struct des{
40 PageId p_num;
41 MODE mode;
42 int pin_count;
43 bool dirtybit;
44 } descriptor;
45
46 struct HT{
47 PageId p_num;
48 int f_num;
49 };
50 Page* bufPool;
51 // The physical buffer pool of pages.
52 descriptor *bufDescr;
53 int *LH;
54 int num_buf;
55 int hashlen;
56
57 int hashtable(PageId pageId);
58 vector<HT> htable[HTSIZE];
59 bufErrCodes add(PageId pid, int fid);
60 bufErrCodes rm(PageId pid);
61
62 BufMgr (int numbuf, Replacer *replacer = 0);
63 // Initializes a buffer manager managing "numbuf" buffers.
64 // Disregard the "replacer" parameter for now. In the full
65 // implementation of minibase, it is a pointer to an object
66 // representing one of several buffer pool replacement schemes.
67
68 ~BufMgr();
69 // Should flush all dirty pages in the pool
70 // to disk before shutting down
71 // and deallocate the buffer pool in main memory.
72
73 Status pinPage(PageId PageId_in_a_DB, Page*& page, MODE mode);
74 // Check if this page is in buffer pool. If it is, increment the
75 // pin_count and let page be a pointer to this page.
76 // If the pin_count was 0 before the call, the page was
77 // a replacement candidate, but is no longer a candidate.
78 // If the page is not in the pool,
79 // choose a frame (from the set of replacement candidates)
80 // to hold this page, read the page (using the appropriate DB
81 // class method) and pin it.
82 // (If there is no candidate, return error.)
83 // Also, you must write out the old page in chosen
84 // frame if it is dirty before reading new page.
85 // you must check on read/write mode, if the pinned page
86 // has been pinned before, make sure that write
87 // exclusive condition is hold, else return error
88
89 Status unpinPage(PageId globalPageId_in_a_DB, int dirty, int hate);
90 // hate should be TRUE if type page is “hated†and
91 // FALSE if the is loved.
92 // User should call this with dirty = TRUE if the page
93 // has be modified.
94 // If so, this call should set the dirty bit for this frame.
95 // Further, if pin_count > 0, should decrement it.
96 // If pin_count = 0 before this call, return error.
97
98 Status newPage(PageId& firstPageId, Page*& firstpage, int howmany=1);
99 // Find a frame in the buffer pool for the first page.
100 // If a frame exists, call DB object to allocate a run
101 // of new pages and pin it in read_write_mode.
102 // (This call allows a client of the Buffer Manager
103 // to allocate pages on disk.)
104 // If buffer is full, i.e., you can’t find a frame
105 // for the first page, return error.
106
107
108 Status freePage(PageId globalPageId);
109 // user should call this method if it needs to delete a page
110 // this routine will call DB to deallocate the page
111 // (When the page is be pinned you should return error.)
112
113 Status flushPage(PageId pageid);
114 // Used to flush a particular page of the buffer pool to disk
115 // (without modify love/hate list)
116 // Should call the write_page method of the DB class
117
118
119 Status flushAllPages();
120 // Flush all pages of the buffer pool to disk, as per flushPage.
121
122 // DO NOT REMOVE THESE METHODS ================================================
123 // For backward compatibility with lib
124 Status pinPage(PageId PageId_in_a_DB, Page*& page, int emptyPage=0);
125
126 Status unpinPage(PageId globalPageId_in_a_DB, int dirty=FALSE)
127 {
128 return unpinPage(globalPageId_in_a_DB, dirty, FALSE);
129 }
130 // ===========================================================================
131};
132
133#endif