1+ #include < vector>
2+
3+ // Vector meta data
4+ typedef const char * V;
5+ typedef std::vector<V> Record;
6+
7+ typedef struct {
8+ void *data;
9+ int page_size;
10+ int slot_size;
11+ } Page;
12+
13+ // The Heap File
14+ typedef struct {
15+ FILE *file_ptr;
16+ int page_size;
17+ } Heapfile;
18+
19+ // Structure to abstract page ID and record ID
20+ typedef int PageID;
21+
22+ typedef struct {
23+ int page_id;
24+ int slot;
25+ } RecordID;
26+
27+ // Iterator function
28+ class RecordIterator {
29+ public:
30+ RecordIterator (Heapfile *heapfile);
31+ Record next ();
32+ bool hasNext ();
33+ }
34+
35+ /* *
36+ * Initalize a heapfile to use the file and page size given.
37+ */
38+ void init_heapfile (Heapfile *heapfile, int page_size, FILE *file);
39+ /* *
40+ * Allocate another page in the heapfile. This grows the file by a page.
41+ */
42+ PageID alloc_page (Heapfile *heapfile);
43+ /* *
44+ * Read a page into memory
45+ */
46+ void read_page (Heapfile *heapfile, PageID pid, Page *page);
47+ /* *
48+ * Write a page from memory to disk
49+ */
50+ void write_page (Page *page, Heapfile *heapfile, PageID pid);
51+ /* *
52+ * Initializes a page using the given slot size
53+ */
54+ void init_fixed_len_page (Page *page, int page_size, int slot_size);
55+
56+ /* *
57+ * Calculates the maximal number of records that fit in a page
58+ */
59+ int fixed_len_page_capacity (Page *page);
60+
61+ /* *
62+ * Calculate the free space (number of free slots) in the page
63+ */
64+ int fixed_len_page_freeslots (Page *page);
65+
66+ /* *
67+ * Add a record to the page
68+ * Returns:
69+ * record slot offset if successful,
70+ * -1 if unsuccessful (page full)
71+ */
72+ int add_fixed_len_page (Page *page, Record *r);
73+
74+ /* *
75+ * Write a record into a given slot.
76+ */
77+ void write_fixed_len_page (Page *page, int slot, Record *r);
78+
79+ /* *
80+ * Read a record from the page from a given slot.
81+ */
82+ void read_fixed_len_page (Page *page, int slot, Record *r);
83+ /* *
84+ * Compute the number of bytes required to serialize record
85+ */
86+ int fixed_len_sizeof (Record *record);
87+
88+ /* *
89+ * Serialize the record to a byte array to be stored in buf.
90+ */
91+ void fixed_len_write (Record *record, void *buf);
92+ /* *
93+ * Deserializes `size` bytes from the buffer, `buf`, and
94+ * stores the record in `record`.
95+ */
96+ void fixed_len_read (void *buf, int size, Record *record);
0 commit comments