-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.cc
312 lines (230 loc) · 6.58 KB
/
File.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "File.h"
#include "TwoWayList.cc"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <signal.h>
Page::Page() {
curSizeInBytes = sizeof(int);
numRecs = 0;
myRecs = new(std::nothrow) TwoWayList<Record>;
if (myRecs == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
pageToDisk = true;
}
Page::~Page() {
delete myRecs;
}
void Page::EmptyItOut() {
// get rid of all of the records
while (1) {
Record temp;
if (!GetFirst(&temp))
break;
}
// reset the page size
curSizeInBytes = sizeof(int);
numRecs = 0;
}
int Page::GetFirst(Record *firstOne) {
// move to the first record
myRecs->MoveToStart();
// make sure there is data
if (!myRecs->RightLength()) {
// cout << "List Empty" << endl;
return 0;
}
// and remove it
myRecs->Remove(firstOne);
numRecs--;
char *b = firstOne->GetBits();
curSizeInBytes -= ((int *) b)[0];
return 1;
}
int Page::GetRecord(Record *firstOne, off_t offset) {
// move to the first record
myRecs->MoveToStart();
// make sure there is data
if (!myRecs->RightLength() || offset > myRecs->RightLength()) {
return 0;
}
for(int i = 0; i < offset; i++){
myRecs->Remove(firstOne);
}
return 1;
}
int Page::Append(Record *addMe) {
char *b = addMe->GetBits();
// first see if we can fit the record
if (curSizeInBytes + ((int *) b)[0] > PAGE_SIZE) {
// cout << "page overflow: " << endl;
// cout << "Page Size in Append: " << curSizeInBytes << " Rec Size: " << ((int *) b)[0] << endl;
return 0;
}
// move to the last record
myRecs->MoveToFinish();
// and add it
curSizeInBytes += ((int *) b)[0];
myRecs->Insert(addMe);
numRecs++;
pageToDisk = false;
return 1;
}
void Page::ToBinary(char *bits) {
// first write the number of records on the page
((int *) bits)[0] = numRecs;
char *curPos = bits + sizeof(int);
// and copy the records one-by-one
myRecs->MoveToStart();
for (int i = 0; i < numRecs; i++) {
char *b = myRecs->Current(0)->GetBits();
// copy over the bits of the current record
memcpy(curPos, b, ((int *) b)[0]);
curPos += ((int *) b)[0];
// and traverse the list
myRecs->Advance();
}
}
void Page::FromBinary(char *bits) {
// first read the number of records on the page
numRecs = ((int *) bits)[0];
// sanity check
if (numRecs > 1000000 || numRecs < 0) {
cerr << "This is probably an error. Found " << numRecs << " records on a page.\n";
exit(1);
}
// and now get the binary representations of each
char *curPos = bits + sizeof(int);
// first, empty out the list of current records
myRecs->MoveToStart();
while (myRecs->RightLength()) {
Record temp;
myRecs->Remove(&temp);
}
// now loop through and re-populate it
Record *temp = new(std::nothrow) Record();
if (temp == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
curSizeInBytes = sizeof(int);
for (int i = 0; i < numRecs; i++) {
// get the length of the current record
int len = ((int *) curPos)[0];
curSizeInBytes += len;
// create the record
temp->CopyBits(curPos, len);
// add it
myRecs->Insert(temp);
// and move along
myRecs->Advance();
curPos += len;
}
delete temp;
}
int Page::numRecords(){
return numRecs;
}
void Page::MoveToStartPage(){
myRecs->MoveToStart();
}
File::File() {
}
File::~File() {
}
void File::GetPage(Page *putItHere, off_t whichPage) {
// this is because the first page has no data
whichPage++;
if (whichPage >= curLength) {
cerr << "curLength of the file is : " << curLength << endl;
cerr << "whichPage " << whichPage << " length " << curLength << endl;
cerr << "BAD: you tried to read past the end of the file\n";
// raise(SIGSEGV);
exit(1);
}
// read in the specified page
char *bits = new(std::nothrow) char[PAGE_SIZE];
if (bits == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
lseek(myFilDes, PAGE_SIZE * whichPage, SEEK_SET);
read(myFilDes, bits, PAGE_SIZE);
putItHere->FromBinary(bits);
delete[] bits;
}
void File::AddPage(Page *addMe, off_t whichPage) {
// this is because the first page has no data
whichPage++;
// if we are trying to add past the end of the file, then
// zero all of the pages out
if (whichPage >= curLength) {
// do the zeroing
for (off_t i = curLength; i < whichPage; i++) {
int foo = 0;
lseek(myFilDes, PAGE_SIZE * i, SEEK_SET);
write(myFilDes, &foo, sizeof(int));
}
// set the size
curLength = whichPage + 1;
}
// now write the page
char *bits = new(std::nothrow) char[PAGE_SIZE];
if (bits == NULL) {
cout << "ERROR : Not enough memory. EXIT !!!\n";
exit(1);
}
addMe->ToBinary(bits);
lseek(myFilDes, PAGE_SIZE * whichPage, SEEK_SET);
write(myFilDes, bits, PAGE_SIZE);
delete[] bits;
#ifdef F_DEBUG
cerr << " File: curLength " << curLength << " whichPage " << whichPage << endl;
#endif
}
void File::Open(int fileLen, const char *fName) {
// figure out the flags for the system open call
int mode;
if (fileLen == 0)
mode = O_TRUNC | O_RDWR | O_CREAT;
else
mode = O_RDWR;
// actually do the open
myFilDes = open(fName, mode, S_IRUSR | S_IWUSR);
#ifdef verbose
cout << "Opening file " << fName << " with "<< curLength << " pages.\n";
#endif
// see if there was an error
if (myFilDes < 0) {
string temp(fName);
cerr << "BAD! Open did not work for " << temp << "\n";
raise(SIGSEGV);
exit(1);
}
// read in the buffer if needed
if (fileLen != 0) {
// read in the first few bits, which is the page size
lseek(myFilDes, 0, SEEK_SET);
read(myFilDes, &curLength, sizeof(off_t));
} else {
curLength = 0;
}
}
off_t File::GetLength() {
return curLength;
}
int File::Close() {
// write out the current length in pages
lseek(myFilDes, 0, SEEK_SET);
write(myFilDes, &curLength, sizeof(off_t));
// close the file
return close(myFilDes);
// and return the size
// return curLength;
}