-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.h
87 lines (71 loc) · 2.85 KB
/
page.h
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
#ifndef PAGE_H
#define PAGE_H
#include "error.h"
// Record ID - used to identify tuples based on page and slot number
struct RID{
int pageNo;
int slotNo;
bool operator == (const RID & other) const
{
return ((other.pageNo == pageNo) && (other.slotNo == slotNo));
}
RID() {reset();}
void reset() {pageNo = -1; slotNo = -1;}
};
// Record - pointer to start of tuple and length of the record
struct Record
{
void* data;
int length;
};
// slot structure
struct slot_t {
short offset;
short length; // equals -1 if slot is not in use
};
const unsigned PAGESIZE = 1024;
// DPFIXED - amount of space reserved for book-keeping. Please
// note that the Page class has 4 shorts and 3 ints which must be
// subtracted from space availale for data.
const unsigned DPFIXED= sizeof(slot_t)+4*sizeof(short)+3*sizeof(int);
// Class definition for a minirel data page.
// The design assumes that records are kept compacted when
// deletions are performed. Notice, however, that the slot
// array cannot be compacted.
//
// Notice, this class does not delineate records by column,
// instead it relies on other parts of the database to ensure
// correct byte offset of the various columns
class Page {
private:
char data[PAGESIZE - DPFIXED]; // all record data + slot array stored here
slot_t slot[1]; // first element of slot array - grows backwards!
short slotCnt; // number of slots in use;
short freePtr; // offset of first free byte in data[]
short freeSpace; // number of bytes free in data[]
short dummy; // for alignment purposes
int prevPage; // backwards pointer
int nextPage; // forwards pointer
int curPage; // page number of current pointer
public:
void init(const int pageNo); // initialize a new page
void dumpPage() const; // dump contents of a page
const int getNextPage() const; // returns value of nextPage
const int getPrevPage() const; // returns value of prevPage
void setNextPage(const int pageNo); // sets value of nextPage to pageNo
void setPrevPage(const int pageNo); // sets value of prevPage to pageNo
const short getFreeSpace() const; // returns amount of free space
// inserts a new record (rec) into the page, returns RID of record
const Status insertRecord(const Record & rec, RID& rid);
// delete the record with the specified rid
const Status deleteRecord(const RID & rid);
// returns RID of first record on page
// returns NORECORDS if page contains no records. Otherwise, returns OK
const Status firstRecord(RID& firstRid) const;
// returns RID of next record on the page
// returns ENDOFPAGE if no more records exist on the page
const Status nextRecord (const RID & curRid, RID& nextRid) const;
// returns reference to record with RID rid
const Status getRecord(const RID & rid, Record & rec);
};
#endif