-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageFile.cc
180 lines (148 loc) · 3.94 KB
/
PageFile.cc
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
/**
* Copyright (C) 2008 by The Regents of the University of California
* Redistribution of this file is permitted under the terms of the GNU
* Public License (GPL).
*
* @author Junghoo "John" Cho <cho AT cs.ucla.edu>
* @date 3/24/2008
*/
#include "Bruinbase.h"
#include "PageFile.h"
#include <cstring>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
using std::string;
int PageFile::readCount = 0;
int PageFile::writeCount = 0;
int PageFile::cacheClock = 1;
struct PageFile::cacheStruct PageFile::readCache[PageFile::CACHE_COUNT];
PageFile::PageFile()
{
fd = -1;
epid = 0;
}
PageFile::PageFile(const string& filename, char mode)
{
fd = -1;
epid = 0;
open(filename.c_str(), mode);
}
RC PageFile::open(const string& filename, char mode)
{
RC rc;
int oflag;
struct stat statbuf;
if (fd > 0) return RC_FILE_OPEN_FAILED;
// set the unix file flag depending on the file mode
switch (mode) {
case 'r':
case 'R':
oflag = O_RDONLY;
break;
case 'w':
case 'W':
oflag = (O_RDWR|O_CREAT);
break;
default:
return RC_INVALID_FILE_MODE;
}
// open the file
fd = ::open(filename.c_str(), oflag, 0644);
if (fd < 0) { fd = -1; return RC_FILE_OPEN_FAILED; }
// get the size of the file to set the end pid
rc = ::fstat(fd, &statbuf);
if (rc < 0) { ::close(fd); fd = -1; return RC_FILE_OPEN_FAILED; }
epid = statbuf.st_size / PAGE_SIZE;
return 0;
}
RC PageFile::close()
{
if (fd <= 0) return RC_FILE_CLOSE_FAILED;
// close the file
if (::close(fd) < 0) return RC_FILE_CLOSE_FAILED;
// evict all cached pages for this file
for (int i = 0; i < CACHE_COUNT; i++) {
if (readCache[i].fd == fd && readCache[i].lastAccessed != 0) {
readCache[i].fd = 0;
readCache[i].pid = 0;
readCache[i].lastAccessed = 0;
}
}
// set the fd and epid to the initial state
fd = -1;
epid = 0;
return 0;
}
PageId PageFile::endPid() const
{
return epid;
}
RC PageFile::seek(PageId pid) const
{
return (::lseek(fd, pid * PAGE_SIZE, SEEK_SET) < 0) ? RC_FILE_SEEK_FAILED : 0;
}
RC PageFile::write(PageId pid, const void* buffer)
{
RC rc;
if (pid < 0) return RC_INVALID_PID;
// seek to the location of the page
if ((rc = seek(pid)) < 0) return rc;
// write the buffer to the disk page
if (::write(fd, buffer, PAGE_SIZE) < 0) return RC_FILE_WRITE_FAILED;
// if the page is in read cache, invalidate it
for (int i = 0; i < CACHE_COUNT; i++) {
if (readCache[i].fd == fd && readCache[i].pid == pid &&
readCache[i].lastAccessed != 0) {
readCache[i].fd = 0;
readCache[i].pid = 0;
readCache[i].lastAccessed = 0;
break;
}
}
// if the written pid >= end pid, update the end pid
if (pid >= epid) epid = pid + 1;
// increase page write count
writeCount++;
return 0;
}
RC PageFile::read(PageId pid, void* buffer) const
{
RC rc;
if (pid < 0 || pid >= epid) return RC_INVALID_PID;
//
// if the page is in cache, read it from there
//
for (int i = 0; i < CACHE_COUNT; i++) {
if (readCache[i].fd == fd && readCache[i].pid == pid &&
readCache[i].lastAccessed != 0) {
memcpy(buffer, readCache[i].buffer, PAGE_SIZE);
readCache[i].lastAccessed = ++cacheClock;
return 0;
}
}
// seek to the page
if ((rc = seek(pid)) < 0) return rc;
// find the cache slot to evict
int toEvict = 0;
for (int i = 0; i < CACHE_COUNT; i++) {
if (readCache[i].lastAccessed == 0) {
toEvict = i;
break;
}
if (readCache[i].lastAccessed < readCache[toEvict].lastAccessed) {
toEvict = i;
}
}
readCache[toEvict].fd = fd;
readCache[toEvict].pid = pid;
readCache[toEvict].lastAccessed = ++cacheClock;
// read the page to cache first and copy it to the buffer
if (::read(fd, readCache[toEvict].buffer, PAGE_SIZE) < 0) {
return RC_FILE_READ_FAILED;
}
memcpy(buffer, readCache[toEvict].buffer, PAGE_SIZE);
// increase the page read count
readCount++;
return 0;
}