-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbufferman.hpp
182 lines (149 loc) · 5.07 KB
/
bufferman.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef bufferman_hpp
#define bufferman_hpp
#include <map>
#include <vector>
#include <cassert>
#include <hash_map>
using namespace std;
//! Holds information about the position of a file on disk
struct File
{
//! starts on this sector
int startingBlock;
//! number of records in the file
int numRecords;
//! number of blocks in the file
int numBlocks;
//! constructor
File(int startingBlock_, int numRecords_, int numBlocks_) : startingBlock(
startingBlock_), numRecords(numRecords_), numBlocks(numBlocks_) {}
//! default constructor
File() : startingBlock(-1) {}
};
//! Holds information about one page (block) in the buffer
struct BufferPage
{
//! the sector number stored in this buffer page
int sector;
//! last time this page was read
int lastRead;
//! priority level of this page
int priority;
//! default constructor
BufferPage() : sector(-1), priority(0) {}
};
typedef vector<BufferPage> Buffer;
typedef Buffer::iterator BufferIterator;
typedef hash_map<int, BufferIterator> SectorMap;
typedef SectorMap::iterator SectorIterator;
typedef SectorMap::value_type SectorPair;
//! Implements buffer management algorithm and measures disk accesses
class BufferManager
{
public:
//! how many reads so far (number of times BufferManager::read() called)
int readNumber;
//! number of blocks read from disk so far
int fetchNumber;
//! sector number which was read from the disk most recently, used to detect consecutive reads
int lastSector;
//! total time in milliseconds of disk accesses so far
double time;
//! next page of buffer to try to read into
int nextPage;
//! vector of pages
Buffer buffer;
//! map of sector addresses that are currently loaded in the buffer
SectorMap mapped;
//! System object
System & system;
//! Databse object
Database & database;
//! list of files in the database indexed by file number
map<int, File> files;
//! constructor
BufferManager(System & system_, Database & database_)
: system(system_), database(database_), readNumber(0), fetchNumber(0),
lastSector(-1), time(0), nextPage(0)
{
buffer.resize(system.numBlocks);
mapped.resize(system.numBlocks*2);
int lastBlock(0);
for(int a = 0; a < database.relations.size(); ++a)
{
Relation & r = database.relations[a];
files[r.fileNum] = File(lastBlock, r.numRecords, r.blockSize);
lastBlock +=r.blockSize;
}
for(int b = 0; b < database.indices.size(); ++b)
{
Index & i = database.indices[b];
files[i.fileNum] = File(lastBlock, i.blockSize, i.blockSize);
lastBlock += i.blockSize;
}
}
//! read from a disk sector
void fetchSector(int sector)
{
++fetchNumber;
if (sector != lastSector + 1)
time += system.rotationalLatency + system.seekTime;
time += system.transferTime;
lastSector = sector;
}
//! find the next low priority page to replace
BufferIterator getFreePage(int sector, int priority)
{
for(;;)
{
BufferIterator page = buffer.begin() + nextPage;
nextPage = (nextPage + 1) % buffer.size();
if (page->lastRead != readNumber) page->priority -= 10;
if (page->priority < priority)
{
page->lastRead = readNumber;
return page;
}
}
};
/*!
\brief Read a range of records from a file into the buffer
Returns the number of disk accesses needed to perform the read
\param fileNum file number to read from
\param startRecord first record to read
\param endRecord last record to read
\param priority priority value of this read operation
\param startSector output parameter that holds the sector number corresponding to startRecord
\param endSector output parameter that holds the sector number corresponding to endRecord
*/
int read(int fileNum, int startRecord, int endRecord, int priority, int & startSector, int & endSector)
{
++readNumber;
int oldFetchNumber = fetchNumber;
File & f = files[fileNum];
if (f.startingBlock < 0) THROW_STREAM("Invalid File number " << fileNum << endl);
startSector = f.startingBlock + (startRecord-1) * f.numBlocks / f.numRecords;
endSector = f.startingBlock + (endRecord-1) * f.numBlocks / f.numRecords;
if (endSector - startSector > buffer.size()) THROW_STREAM("Buffer is not big "
"enough to hold records " << startRecord << "-" << endRecord << " from "
"file " << fileNum);
for(int r = startSector; r <= endSector; ++r)
{
BufferIterator page;
pair<SectorIterator, bool> f = mapped.insert(SectorPair(r, page));
if (!f.second) // no need to insert page, it's already in buffer
page = f.first->second;
else
{
fetchSector(r); // simulate disk read
page = f.first->second = getFreePage(r, priority); // find a spot in memory
if (page->sector >= 0) mapped.erase(page->sector); // erase old mapping
page->sector = r;
}
page->lastRead = readNumber;
if (page->priority < priority) page->priority = priority;
}
return fetchNumber - oldFetchNumber;
}
};
#endif