@@ -22,21 +22,6 @@ void fixed_len_write(Record *record, char *buf) {
2222 }
2323}
2424
25- void fixed_len_read (char *buf, int size, Record *record) {
26- for (int i = 0 ; i < size/ATTRIBUTE_SIZE; ++i) {
27- char * attribute = new char [ATTRIBUTE_SIZE + 1 ];
28- int attribute_index = i * ATTRIBUTE_SIZE;
29- strncpy (attribute, buf + attribute_index, ATTRIBUTE_SIZE);
30-
31- // not sure why garbage values are ending up in 'attribute'
32- attribute[ATTRIBUTE_SIZE] = ' \0 ' ;
33-
34- if (strlen (attribute) > 0 ) {
35- record->push_back (attribute);
36- }
37- }
38- }
39-
4025void init_fixed_len_page (Page *page, int page_size, int slot_size) {
4126 page->page_size = page_size;
4227 page->slot_size = slot_size;
@@ -52,6 +37,21 @@ void init_fixed_len_page(Page *page, int page_size, int slot_size) {
5237 page->data = data;
5338}
5439
40+ void read_fixed_len_page (Page *page, int slot, Record *r)
41+ {
42+ *r = page->data ->at (slot);
43+ }
44+
45+ int number_of_pages_per_directory_page (int page_size)
46+ {
47+ return (page_size - sizeof (int )) / sizeof (DirectoryEntry);
48+ }
49+
50+ int get_directory_number (PageID pid, int page_size)
51+ {
52+ return pid / (number_of_pages_per_directory_page (page_size) + 1 );
53+ }
54+
5555int fixed_len_page_capacity (Page *page) {
5656 return page->page_size / page->slot_size ;
5757}
@@ -72,6 +72,24 @@ int add_fixed_len_page(Page *page, Record *r) {
7272 return -1 ;
7373}
7474
75+ void fixed_len_read (char *buf, int size, Record *record)
76+ {
77+ for (int i = 0 ; i < size / ATTRIBUTE_SIZE; ++i)
78+ {
79+ char *attribute = new char [ATTRIBUTE_SIZE + 1 ];
80+ int attribute_index = i * ATTRIBUTE_SIZE;
81+ strncpy (attribute, buf + attribute_index, ATTRIBUTE_SIZE);
82+
83+ // not sure why garbage values are ending up in 'attribute'
84+ attribute[ATTRIBUTE_SIZE] = ' \0 ' ;
85+
86+ if (strlen (attribute) > 0 )
87+ {
88+ record->push_back (attribute);
89+ }
90+ }
91+ }
92+
7593void write_fixed_len_page (Page *page, int slot, Record *r) {
7694 // only increase count if prev is empty and new is not empty
7795 if (page->data ->at (slot).empty () && !r->empty ()) {
@@ -80,16 +98,24 @@ void write_fixed_len_page(Page *page, int slot, Record *r) {
8098 page->data ->at (slot) = *r;
8199}
82100
83- void read_fixed_len_page (Page *page, int slot, Record *r) {
84- *r = page->data ->at (slot);
85- }
101+ int get_offset_to_last_directory_page (FILE *file)
102+ {
103+ fseek (file, 0 , SEEK_SET);
104+ int offset_to_next_directory_page = 0 ;
105+ int total_offset_to_last_directory_page = 0 ;
86106
87- int number_of_pages_per_directory_page (int page_size) {
88- return (page_size - sizeof (int )) / sizeof (DirectoryEntry);
89- }
107+ while (offset_to_next_directory_page != 0 )
108+ {
109+ // read offset, will be 0 if current direcory is the last in the file
110+ fread (&offset_to_next_directory_page, sizeof (int ), 1 , file);
90111
91- int get_directory_number (PageID pid, int page_size) {
92- return pid / (number_of_pages_per_directory_page (page_size) + 1 );
112+ total_offset_to_last_directory_page += offset_to_next_directory_page;
113+
114+ // jump to next directory
115+ fseek (file, total_offset_to_last_directory_page, SEEK_SET);
116+ }
117+
118+ return total_offset_to_last_directory_page;
93119}
94120
95121/*
@@ -144,23 +170,6 @@ void init_heapfile(Heapfile *heapfile, int page_size, FILE *file) {
144170 fflush (file);
145171}
146172
147- int get_offset_to_last_directory_page (FILE *file) {
148- fseek (file, 0 , SEEK_SET);
149- int offset_to_next_directory_page = 0 ;
150- int total_offset_to_last_directory_page = 0 ;
151-
152- while (offset_to_next_directory_page != 0 ) {
153- // read offset, will be 0 if current direcory is the last in the file
154- fread (&offset_to_next_directory_page, sizeof (int ), 1 , file);
155-
156- total_offset_to_last_directory_page += offset_to_next_directory_page;
157-
158- // jump to next directory
159- fseek (file, total_offset_to_last_directory_page, SEEK_SET);
160- }
161-
162- return total_offset_to_last_directory_page;
163- }
164173
165174PageID alloc_page (Heapfile *heapfile) {
166175 int offset_to_last_directory = get_offset_to_last_directory_page (heapfile->file_ptr ) * heapfile->page_size ;
0 commit comments