Skip to content

Commit 20bfa61

Browse files
committed
Thisis final i hope
1 parent 9158412 commit 20bfa61

File tree

9 files changed

+139
-120
lines changed

9 files changed

+139
-120
lines changed

csv2colstore.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
#include <algorithm>
21
#include <fstream>
32
#include <iostream>
43
#include <sstream>
5-
#include <stdlib.h>
6-
#include <string.h>
74
#include <sys/timeb.h>
85
#include <sys/stat.h>
96
#include "library.h"
7+
#include <algorithm>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
1011

1112
int main(int argc, char** argv) {
1213
if (argc < 4) {

csv2heapfile.cc

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
2+
#include <string.h>
3+
#include <sys/timeb.h>
4+
#include "library.h"
15
#include <algorithm>
26
#include <stdlib.h>
37
#include <iostream>
48
#include <fstream>
5-
#include <string.h>
6-
#include <sys/timeb.h>
7-
#include "library.h"
9+
810

911
int main(int argc, char** argv) {
1012
if (argc < 4) {
@@ -13,46 +15,47 @@ int main(int argc, char** argv) {
1315
return 1;
1416
}
1517

16-
bool show_output = true;
17-
if (argc == 5 && strcmp(argv[4], "--no-output") == 0) {
18-
show_output = false;
19-
}
18+
std::ifstream fileCsv;
19+
fileCsv.open(argv[1]);
2020

21-
// open csv file
22-
std::ifstream csv_file;
23-
csv_file.open(argv[1]);
24-
if (!csv_file) {
21+
if (!fileCsv)
22+
{
2523
std::cout << "Error, could not find file " << argv[1] << "\n";
2624
return 1;
2725
}
2826

27+
bool show_output = true;
28+
if (argc == 5 && strcmp(argv[4], "--no-output") == 0) {
29+
show_output = false;
30+
}
31+
2932
// initialize page
3033
int page_size = atoi(argv[3]);
31-
int record_size = NUM_ATTRIBUTES * ATTRIBUTE_SIZE;
34+
int record_size = 100 * 10;
3235

3336
Page page;
3437
init_fixed_len_page(&page, page_size, record_size);
3538

3639
// initialize heapfile
3740
Heapfile *heap = new Heapfile();
38-
FILE* heap_file = fopen(argv[2], "w+b");
39-
if (!heap_file) {
41+
FILE* fileHeap = fopen(argv[2], "w+b");
42+
if (!fileHeap) {
4043
std::cout << "Error, could not find file " << argv[2] << "\n";
4144
return 1;
4245
}
4346

44-
init_heapfile(heap, page_size, heap_file);
47+
init_heapfile(heap, page_size, fileHeap);
4548

4649
// timing and counting stuff
4750
struct timeb t;
4851
ftime(&t);
4952
long start_time_in_ms = (t.time * 1000) + t.millitm;
5053

51-
int total_records = 0;
52-
while (csv_file) {
54+
int Tots = 0;
55+
while (fileCsv) {
5356

5457
std::string line;
55-
csv_file >> line;
58+
fileCsv >> line;
5659

5760
if (line.size() == 0) {
5861
// ignore empty lines
@@ -83,7 +86,7 @@ int main(int argc, char** argv) {
8386

8487
write_fixed_len_page(&page, slot_index, r);
8588

86-
total_records++;
89+
Tots++;
8790
}
8891

8992
// write last page to file if it has records
@@ -96,11 +99,11 @@ int main(int argc, char** argv) {
9699
ftime(&t);
97100
long total_run_time = ((t.time * 1000) + t.millitm) - start_time_in_ms;
98101

99-
csv_file.close();
102+
fileCsv.close();
100103

101104
if (show_output) {
102105
std::cout << "RUN TIME: " << total_run_time << " milliseconds\n";
103-
std::cout << "TOTAL RECORDS: " << total_records << "\n";
106+
std::cout << "TOTAL RECORDS: " << Tots << "\n";
104107
}
105108
return 0;
106109
}

delete.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ int main(int argc, char** argv) {
1010
return 1;
1111
}
1212

13-
FILE *heapfile = fopen(argv[1], "r+");
14-
if (!heapfile) {
15-
std::cout << "Error, could not find file " << argv[1] << "\n";
16-
return 1;
17-
}
18-
19-
std::string serialized_record_id = argv[2];
20-
size_t delimiter_position = serialized_record_id.find(RID_DELIMITER);
21-
if (delimiter_position == 0 || delimiter_position == serialized_record_id.length() - 1) {
13+
std::string serial_record = argv[2];
14+
size_t pos_del = serial_record.find(RID_DELIMITER);
15+
if (pos_del == 0 || pos_del == serial_record.length() - 1) {
2216
std::cout << "Error, record_id must be:\n";
2317
std::cout << "<page_id>%<slot_number>\n";
2418
return 1;
2519
}
2620

2721
RecordID *rid = new RecordID();
28-
rid->page_id = atoi(serialized_record_id.substr(0, delimiter_position).c_str());
29-
rid->slot = atoi(serialized_record_id.substr(delimiter_position + 1).c_str());
22+
rid->page_id = atoi(serial_record.substr(0, pos_del).c_str());
23+
rid->slot = atoi(serial_record.substr(pos_del + 1).c_str());
24+
25+
FILE *heapfile = fopen(argv[1], "r+");
26+
if (!heapfile)
27+
{
28+
std::cout << "Error, could not find file " << argv[1] << "\n";
29+
return 1;
30+
}
3031

3132
int page_size = atoi(argv[3]);
3233

insert.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ int main(int argc, char** argv) {
1111
return 1;
1212
}
1313

14-
FILE *heapfile = fopen(argv[1], "r+");
15-
if (!heapfile) {
16-
std::cout << "Error, could not find file " << argv[1] << "\n";
17-
return 1;
18-
}
1914

2015
std::ifstream csv_file;
2116
csv_file.open(argv[2]);
@@ -24,15 +19,21 @@ int main(int argc, char** argv) {
2419
return 1;
2520
}
2621

22+
FILE *heapfile = fopen(argv[1], "r+");
23+
if (!heapfile)
24+
{
25+
std::cout << "Error, could not find file " << argv[1] << "\n";
26+
return 1;
27+
}
2728
int page_size = atoi(argv[3]);
2829

2930
Heapfile *heap = new Heapfile();
3031
heap->page_size = page_size;
3132
heap->file_ptr = heapfile;
3233

33-
int record_size = NUM_ATTRIBUTES * ATTRIBUTE_SIZE;
34+
int record_size = 100 * 10;
3435

35-
char *buf;
36+
char *buffer;
3637
Page page;
3738
init_fixed_len_page(&page, page_size, record_size);
3839

@@ -57,11 +58,11 @@ int main(int argc, char** argv) {
5758

5859
if (slot_index == -1) { // page is full
5960
int buf_size = page.page_size * record_size;
60-
buf = new char[buf_size];
61+
buffer = new char[buf_size];
6162

6263
std::vector<Record> *page_data = page.data;
6364
for (int i = 0; i < fixed_len_page_capacity(&page); i++) {
64-
fixed_len_write(&page_data->at(i), buf);
65+
fixed_len_write(&page_data->at(i), buffer);
6566
}
6667

6768
PageID new_pid = alloc_page(heap);
@@ -79,11 +80,11 @@ int main(int argc, char** argv) {
7980
// write last page to file if it has records
8081
if (page.used_slots > 0) {
8182
int buf_size = page.page_size * record_size;
82-
buf = new char[buf_size];
83+
buffer = new char[buf_size];
8384

8485
std::vector<Record> *page_data = page.data;
8586
for (int i = 0; i < fixed_len_page_capacity(&page); i++) {
86-
fixed_len_write(&page_data->at(i), buf);
87+
fixed_len_write(&page_data->at(i), buffer);
8788
}
8889

8990
PageID new_pid = alloc_page(heap);

library.cc

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
4025
void 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+
5555
int 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+
7593
void 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

165174
PageID alloc_page(Heapfile *heapfile) {
166175
int offset_to_last_directory = get_offset_to_last_directory_page(heapfile->file_ptr) * heapfile->page_size;

library.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ typedef struct {
3333
int page_size;
3434
} Heapfile;
3535

36+
class RecordIterator
37+
{
38+
public:
39+
RecordIterator(Heapfile *heapfile);
40+
Record next();
41+
bool hasNext();
42+
};
43+
3644
const int NUM_ATTRIBUTES = 100;
3745
const int ATTRIBUTE_SIZE = 10;
3846

@@ -68,12 +76,6 @@ void write_fixed_len_page(Page *page, int slot, Record *r);
6876
// Read a record from the page from a given slot.
6977
void read_fixed_len_page(Page *page, int slot, Record *r);
7078

71-
class RecordIterator {
72-
public:
73-
RecordIterator(Heapfile *heapfile);
74-
Record next();
75-
bool hasNext();
76-
};
7779

7880
// Returns max # of directory entries that can fit into one page
7981
int number_of_pages_per_directory_page(int page_size);

0 commit comments

Comments
 (0)