-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2heapfile.cpp
More file actions
73 lines (60 loc) · 2.38 KB
/
csv2heapfile.cpp
File metadata and controls
73 lines (60 loc) · 2.38 KB
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
//
// Created by Yuchen Zhang on 2/17/23.
//
#include "library.h"
int main(int argc, char* argv[]){
auto startTime = system_clock::now();
// Receive parameters and open files
FILE* csvFile = fopen(argv[1], "r");
FILE* heapFile = fopen(argv[2], "w+");
int page_size = atoi(argv[3]);
// Initialize heapfile
auto* heapfile = new Heapfile;
init_heapfile(heapfile, page_size, heapFile);
// Initialize page
Page* page = new Page;
Record record;
init_fixed_len_page(page, page_size, SLOT_SIZE);
// line size of csv file
const int oneLineSize = SLOT_SIZE + (SLOT_SIZE / ATTRIBUTE_SIZE);
char data[oneLineSize];
// Record the number of records
int numOfRecord = 0;
// Read csv file by line
while(fread(data, 1, oneLineSize, csvFile)){
numOfRecord++;
// Remove unless characters in one line
string line(data);
line = line.substr(0, SLOT_SIZE + SLOT_SIZE / ATTRIBUTE_SIZE - 1);
// // For Linux
// if(line.length() == SLOT_SIZE + SLOT_SIZE / ATTRIBUTE_SIZE - 1 + 2)
// line = line.substr(0, line.size() - 2);
// // For mac
// else if(line.length() == SLOT_SIZE + SLOT_SIZE / ATTRIBUTE_SIZE - 1 + 3)
// line = line.substr(0, line.size() - 3);
stringstream ss(line);
string attr;
while(getline(ss, attr, ',')){
char* temp = new char[ATTRIBUTE_SIZE+1];
strcpy(temp, attr.c_str());
record.push_back(temp);
}
// When the page is not full, Add record into page
// When the page is full, alloc a page in heapfile and then clear page for the next writing
while (add_fixed_len_page(page, &record) == -1){
PageID pid = alloc_page(heapfile);
write_page(page, heapfile, pid);
init_fixed_len_page(page, atoi(argv[3]), fixed_len_sizeof(&record));
}
record.clear();
}
PageID pid = alloc_page(heapfile);
// Write the rest of records into the heapfile
write_page(page, heapfile, pid);
::fclose(heapFile);
cout << pid << endl;
auto endTime = system_clock::now();
cout << "NUMBER OF RECORDS: " << numOfRecord << endl;
auto duration = duration_cast<microseconds>(endTime - startTime);
std::cout << "TIME: " << double(duration.count()) * microseconds::period::num / microseconds::period::den << "s" << endl;
}