-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrm_filehandle.cc
531 lines (445 loc) · 13 KB
/
rm_filehandle.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// File: rm_filehandle.cc
// Description: RM_FileHandle class implementation
// Author: Hyunjung Park ([email protected])
//
#include "rm_internal.h"
//
// RM_FileHandle
//
// Desc: Default constructor
//
RM_FileHandle::RM_FileHandle()
{
// Initialize member variables
bHdrChanged = FALSE;
memset(&fileHdr, 0, sizeof(fileHdr));
fileHdr.firstFree = RM_PAGE_LIST_END;
}
//
// ~RM_FileHandle
//
// Desc: Destructor
//
RM_FileHandle::~RM_FileHandle()
{
// Don't need to do anything
}
//
// GetRec
//
// Desc: Given a RID, return the record
// In: rid -
// Out: rec -
// Ret: RM return code
//
RC RM_FileHandle::GetRec(const RID &rid, RM_Record &rec) const
{
RC rc;
PageNum pageNum;
SlotNum slotNum;
PF_PageHandle pageHandle;
char *pData;
// Extract page number from rid
if (rc = rid.GetPageNum(pageNum))
// Test: inviable rid
goto err_return;
// Extract slot number from rid
if (rc = rid.GetSlotNum(slotNum))
// Should not happen
goto err_return;
// Sanity Check: slotNum bound check
// Note that PF_FileHandle.GetThisPage() will take care of pageNum
if (slotNum >= fileHdr.numRecordsPerPage || slotNum < 0)
// Test: rid with invalid slotNum
return (RM_INVALIDSLOTNUM);
// Get the page where rid points
if (rc = pfFileHandle.GetThisPage(pageNum, pageHandle))
// Test: rid with invalid pageNum
goto err_return;
// Get the data
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Sanity Check: a record corresponding to rid should exist
if (!GetBitmap(pData + sizeof(RM_PageHdr), slotNum)) {
// Test: non-existing rid
pfFileHandle.UnpinPage(pageNum);
return (RM_RECORDNOTFOUND);
}
// Copy the record to RM_Record
rec.rid = rid;
if (rec.pData)
delete [] rec.pData;
rec.recordSize = fileHdr.recordSize;
rec.pData = new char[rec.recordSize];
memcpy(rec.pData,
pData + fileHdr.pageHeaderSize + slotNum * fileHdr.recordSize,
fileHdr.recordSize);
// Unpin the page
if (rc = pfFileHandle.UnpinPage(pageNum))
// Should not happen
goto err_return;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
pfFileHandle.UnpinPage(pageNum);
err_return:
// Return error
return (rc);
}
//
// InsertRec
//
// Desc: Insert a new record
// In: pData -
// Out: rid -
// Ret: RM return code
//
RC RM_FileHandle::InsertRec(const char *pRecordData, RID &rid)
{
RC rc;
PageNum pageNum;
SlotNum slotNum;
PF_PageHandle pageHandle;
char *pData;
RID *pRid;
// Sanity Check: pRecordData must not be NULL
if (pRecordData == NULL)
return (RM_NULLPOINTER);
// Allocate a new page if free page list is empty
if (fileHdr.firstFree == RM_PAGE_LIST_END) {
// Call PF_FileHandle::AllocatePage()
if (rc = pfFileHandle.AllocatePage(pageHandle))
// Unopened file handle
goto err_return;
// Get page number
if (rc = pageHandle.GetPageNum(pageNum))
// Should not happen
goto err_unpin;
// Get data pointer
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Set page header
// Note that allocated page was already zeroed out
// (We don't have to initialize the bitmap)
((RM_PageHdr *)pData)->nextFree = RM_PAGE_LIST_END;
// Mark the page dirty since we changed the next pointer
if (rc = pfFileHandle.MarkDirty(pageNum))
// Should not happen
goto err_unpin;
// Unpin the page
if (rc = pfFileHandle.UnpinPage(pageNum))
// Should not happen
goto err_return;
// Place into the free page list
fileHdr.firstFree = pageNum;
bHdrChanged = TRUE;
}
// Pick the first page on the list
else {
pageNum = fileHdr.firstFree;
}
// Pin the page where new record will be inserted
if (rc = pfFileHandle.GetThisPage(pageNum, pageHandle))
goto err_return;
// Get data pointer
if (rc = pageHandle.GetData(pData))
goto err_unpin;
// Find an empty slot
for (slotNum = 0; slotNum < fileHdr.numRecordsPerPage; slotNum++)
if (!GetBitmap(pData + sizeof(RM_PageHdr), slotNum))
break;
// There should be a free slot
assert(slotNum < fileHdr.numRecordsPerPage);
// Assign rid
pRid = new RID(pageNum, slotNum);
rid = *pRid;
delete pRid;
// Copy the given record data to the buffer pool
memcpy(pData + fileHdr.pageHeaderSize + slotNum * fileHdr.recordSize,
pRecordData, fileHdr.recordSize);
// Set bit
SetBitmap(pData + sizeof(RM_PageHdr), slotNum);
// Find an empty slot
for (slotNum = 0; slotNum < fileHdr.numRecordsPerPage; slotNum++)
if (!GetBitmap(pData + sizeof(RM_PageHdr), slotNum))
break;
// Remove the page from the free page list if necessary
if (slotNum == fileHdr.numRecordsPerPage) {
fileHdr.firstFree = ((RM_PageHdr *)pData)->nextFree;
bHdrChanged = TRUE;
((RM_PageHdr *)pData)->nextFree = RM_PAGE_FULL;
}
// Mark the header page as dirty because we changed bitmap at least
if (rc = pfFileHandle.MarkDirty(pageNum))
// Should not happen
goto err_unpin;
// Unpin the page
if (rc = pfFileHandle.UnpinPage(pageNum))
// Should not happen
goto err_return;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
pfFileHandle.UnpinPage(pageNum);
err_return:
// Return error
return (rc);
}
//
// DeleteRec
//
// Desc: Delete a record
// In: rid -
// Ret: RM return code
//
RC RM_FileHandle::DeleteRec(const RID &rid)
{
RC rc;
PageNum pageNum;
SlotNum slotNum;
PF_PageHandle pageHandle;
char *pData;
// Extract page number from rid
if (rc = rid.GetPageNum(pageNum))
// Test: inviable rid
goto err_return;
// Extract slot number from rid
if (rc = rid.GetSlotNum(slotNum))
// Should not happen
goto err_return;
// Sanity Check: slotNum bound check
// Note that PF_FileHandle.GetThisPage() will take care of pageNum
if (slotNum >= fileHdr.numRecordsPerPage || slotNum < 0)
// Test: rid with invalid slotNum
return (RM_INVALIDSLOTNUM);
// Get the page where rid points
if (rc = pfFileHandle.GetThisPage(pageNum, pageHandle))
// Test: rid with invalid pageNum
goto err_return;
// Get the data
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Sanity Check: a record corresponding to rid should exist
if (!GetBitmap(pData + sizeof(RM_PageHdr), slotNum)) {
// Test: non-existing rid
pfFileHandle.UnpinPage(pageNum);
return (RM_RECORDNOTFOUND);
}
// Clear bit
ClrBitmap(pData + sizeof(RM_PageHdr), slotNum);
// Not necessary
memset(pData + fileHdr.pageHeaderSize + slotNum * fileHdr.recordSize,
'\0', fileHdr.recordSize);
// Find an empty slot
for (slotNum = 0; slotNum < fileHdr.numRecordsPerPage; slotNum++)
if (GetBitmap(pData + sizeof(RM_PageHdr), slotNum))
break;
// Dispose the page if empty (the deleted record was the last one)
// This will help the total number of occupied pages to be remained
// as small as possible
if (slotNum == fileHdr.numRecordsPerPage) {
fileHdr.firstFree = ((RM_PageHdr *)pData)->nextFree;
bHdrChanged = TRUE;
// Mark the header page as dirty
if (rc = pfFileHandle.MarkDirty(pageNum))
// Should not happen
goto err_return;
// Unpin the page
if (rc = pfFileHandle.UnpinPage(pageNum))
// Should not happen
goto err_return;
// Call PF_FileHandle.DisposePage()
return pfFileHandle.DisposePage(pageNum);
}
// Insert the page into the free page list if necessary
if (((RM_PageHdr *)pData)->nextFree == RM_PAGE_FULL) {
((RM_PageHdr *)pData)->nextFree = fileHdr.firstFree;
fileHdr.firstFree = pageNum;
bHdrChanged = TRUE;
}
// Mark the header page as dirty because we changed bitmap at least
if (rc = pfFileHandle.MarkDirty(pageNum))
// Should not happen
goto err_unpin;
// Unpin the page
if (rc = pfFileHandle.UnpinPage(pageNum))
// Should not happen
goto err_return;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
pfFileHandle.UnpinPage(pageNum);
err_return:
// Return error
return (rc);
}
//
// UpdateRec
//
// Desc: Update a record
// In: rec -
// Ret: RM return code
//
RC RM_FileHandle::UpdateRec(const RM_Record &rec)
{
RC rc;
RID rid;
PageNum pageNum;
SlotNum slotNum;
PF_PageHandle pageHandle;
char *pData;
char *pRecordData;
// Get rid
if (rc = rec.GetRid(rid))
// Test: unread record
goto err_return;
// Get record data
if (rc = rec.GetData(pRecordData))
// Should not happen
goto err_return;
// Extract page number from rid
if (rc = rid.GetPageNum(pageNum))
// Should not happen
goto err_return;
// Extract slot number from rid
if (rc = rid.GetSlotNum(slotNum))
// Should not happen
goto err_return;
// Sanity Check: slotNum bound check
// Note that PF_FileHandle.GetThisPage() will take care of pageNum
if (slotNum >= fileHdr.numRecordsPerPage || slotNum < 0)
// Test: apply R1's record to R2
return (RM_INVALIDSLOTNUM);
// Sanity Check: recordSize of updating record and file handle
// must match
if (rec.recordSize != fileHdr.recordSize)
// Test: apply R1's record to R2
return (RM_INVALIDRECSIZE);
// Get the page where rid points
if (rc = pfFileHandle.GetThisPage(pageNum, pageHandle))
// Test: rid with invalid pageNum
goto err_return;
// Get the data
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Sanity Check: a record corresponding to rid should exist
if (!GetBitmap(pData + sizeof(RM_PageHdr), slotNum)) {
// Test: non-existing rid
pfFileHandle.UnpinPage(pageNum);
return (RM_RECORDNOTFOUND);
}
// Update
memcpy(pData + fileHdr.pageHeaderSize + slotNum * fileHdr.recordSize,
pRecordData,
fileHdr.recordSize);
// Mark the header page as dirty
if (rc = pfFileHandle.MarkDirty(pageNum))
// Should not happen
goto err_unpin;
// Unpin the page
if (rc = pfFileHandle.UnpinPage(pageNum))
// Should not happen
goto err_return;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
pfFileHandle.UnpinPage(pageNum);
err_return:
// Return error
return (rc);
}
//
// ForcePages
//
// Desc: Forces a page (along with any contents stored in this class)
// from the buffer pool to disk. Default value forces all pages.
// In: pageNum -
// Ret: RM return code
//
RC RM_FileHandle::ForcePages(PageNum pageNum)
{
RC rc;
// Write back the file header if any changes made to the header
// while the file is open
if (bHdrChanged) {
PF_PageHandle pageHandle;
char* pData;
// Get the header page
if (rc = pfFileHandle.GetFirstPage(pageHandle))
// Test: unopened(closed) fileHandle, invalid file
goto err_return;
// Get a pointer where header information will be written
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Write the file header (to the buffer pool)
memcpy(pData, &fileHdr, sizeof(fileHdr));
// Mark the header page as dirty
if (rc = pfFileHandle.MarkDirty(RM_HEADER_PAGE_NUM))
// Should not happen
goto err_unpin;
// Unpin the header page
if (rc = pfFileHandle.UnpinPage(RM_HEADER_PAGE_NUM))
// Should not happen
goto err_return;
if (rc = pfFileHandle.ForcePages(RM_HEADER_PAGE_NUM))
// Should not happen
goto err_return;
// Set file header to be not changed
bHdrChanged = FALSE;
}
// Call PF_FileHandle::ForcePages()
if (rc = pfFileHandle.ForcePages(pageNum))
goto err_return;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
pfFileHandle.UnpinPage(RM_HEADER_PAGE_NUM);
err_return:
// Return error
return (rc);
}
//
// GetBitmap
//
// Desc: Return a bit corresponding to the given index
// In: map - address of bitmap
// idx - bit index
// Ret: TRUE or FALSE
//
int RM_FileHandle::GetBitmap(char *map, int idx) const
{
return (map[idx / 8] & (1 << (idx % 8))) != 0;
}
//
// SetBitmap
//
// Desc: Set a bit corresponding to the given index
// In: map - address of bitmap
// idx - bit index
//
void RM_FileHandle::SetBitmap(char *map, int idx) const
{
map[idx / 8] |= (1 << (idx % 8));
}
//
// ClrBitmap
//
// Desc: Clear a bit corresponding to the given index
// In: map - address of bitmap
// idx - bit index
//
void RM_FileHandle::ClrBitmap(char *map, int idx) const
{
map[idx / 8] &= ~(1 << (idx % 8));
}