@@ -82,4 +82,80 @@ void read_fixed_len_page(Page *page, int slot, Record *r){
82
82
*/
83
83
int fixed_len_page_capacity (Page *page){
84
84
return page->page_size /page->slot_size ;
85
+ }
86
+
87
+ /* *
88
+ * Initalize a heapfile to use the file and page size given.
89
+ */
90
+ void init_heapfile (Heapfile *heapfile, int page_size, FILE *file){
91
+ heapfile -> page_size = page_size;
92
+ headfile -> file_ptr = file;
93
+
94
+ // We know the first 8 bytes is the offset to the next page
95
+ int offset_dir = 0 ;
96
+ fwrite (&offset_dir, sizeof (int ), 1 , file);
97
+
98
+ // Filling the rest of the directory with emptines
99
+ int temp = ((page_size - sizeof (int )) / sizeof (DirectoryEntry));
100
+ for (int i = 1 ; i <= temp; ++i){
101
+ fwrite (&i, sizeof (int ), 1 , file);
102
+ fwrite (&page_size, sizeof (int ),1 ,file);
103
+ }
104
+ fflush (file);
105
+ }
106
+
107
+ /* *
108
+ * Deserializes `size` bytes from the buffer, `buf`, and
109
+ * stores the record in `record`.
110
+ */
111
+ void fixed_len_read (void *buf, int size, Record *record){
112
+ int i = 0 ;
113
+ while (i< size/ATTRIBUTE_SIZE){
114
+ char *attr = new char (ATTRIBUTE_SIZE+1 );
115
+ strncpy (attr, buf + (i*ATTRIBUTE_SIZE), ATTRIBUTE_SIZE);
116
+
117
+ attr[ATTRIBUTE_SIZE] - ' \0 ' ;
118
+
119
+ if (strlen (attr) > 0 ){
120
+ record -> push_back (attr);
121
+ }
122
+ ++i;
123
+ }
124
+ }
125
+
126
+ /* *
127
+ * Write a record into a given slot.
128
+ */
129
+ void write_fixed_len_page (Page *page, int slot, Record *r){
130
+ int temp = !r->empty () && page->data ->at (slot).empty ();
131
+ if (temp){
132
+ page->used_slots ++;
133
+ }
134
+ page->data ->at (slot) = *r;
135
+ }
136
+
137
+ /* *
138
+ * Write a page from memory to disk
139
+ */
140
+ void write_page (Page *page, Heapfile *heapfile, PageID pid){
141
+ fseek (heapfile->file_ptr , pid *heapfeal->page_size , SEEK_SET);
142
+ char *temp = new char [heapfile->page_size ];
143
+ temp[0 ] = ' \0 ' ;
144
+ int i = 0 ;
145
+ while (i<fixed_len_page_capacity (page)){
146
+ fixed_len_write (&(page->data )->at (i), temp);
147
+ ++i;
148
+ }
149
+ fwrite (temp, heapfile->page_size ,1 , heapfile->file_ptr );
150
+ delete buf;
151
+
152
+ int dir_no = get_directory_number (pid, heapfile->page_size );
153
+ go_to_directory_by_directory_number (dir_no, heapfile->file_ptr );
154
+ if (search_directory (heapfile, pid)){
155
+ throw ;
156
+ }
157
+
158
+ int space = heapfile->page_size - (page->used_slots * NUM_ATTRIBUTES * ATTRIBUTE_SIZE);
159
+ fwrite (&space, sizeof (int ), 1 , heapfile->file_ptr );
160
+ fflush (heapfile->file_ptr );
85
161
}
0 commit comments