Skip to content

Commit b28883a

Browse files
committed
Added some file
1 parent 1e63c43 commit b28883a

File tree

6 files changed

+211
-6
lines changed

6 files changed

+211
-6
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Makefile
2+
CC = g++
3+
4+
all: write_fixed_len_pages read_fixed_len_page
5+
6+
library.o: library.cc library.h
7+
$(CC) -o $@ -c $<
8+
9+
write_fixed_len_pages: write_fixed_len_pages.cc library.o
10+
$(CC) -o $@ $< library.o
11+
12+
read_fixed_len_page: read_fixed_len_page.cc library.o
13+
$(CC) -o $@ $< library.o
14+
15+
clean :$
16+
\rm -fr write_fixed_len_pages read_fixed_len_page *.o

csv2heapfile.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <sys/timeb.h>
5+
#include <cstring>
6+
#include "library.h"
7+
8+
int main(int argc, const char * argv[]){
9+
if (argc<4){
10+
std::cout << "csv2heapfile <csv_file> <heapfile> <page_size>";
11+
return -1;
12+
}
13+
int size_page,new_page, record_no, page_no;
14+
15+
std::string filename(argv[1]);
16+
std::string heapfile(argv[2]);
17+
size_page = std::stoi(argv[3]);
18+
19+
20+
}

library.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <algorithm>
2+
#include <stdint.h>
3+
4+
#include "library.h"
5+
#include <assert.h>
6+
#include <stdlib.h>
7+
8+
#include <iostream>
9+
10+
#include <vector>
11+
#include "string.h"
12+
13+
/**
14+
* Compute the number of bytes required to serialize record
15+
*/
16+
int fixed_len_sizeof(Record *record){
17+
int length = 0;
18+
for (Record::iterator t = record->begin(); t!=record->end(); ++t){
19+
length += strlen(*t);
20+
}
21+
return length;
22+
}
23+
/**
24+
* Serialize the record to a byte array to be stored in buf.
25+
*/
26+
void fixed_len_write(Record *record, void *buf){
27+
for (int i=0;i<record->size(); i++){
28+
int len_str = std::strlen(record->at(i));
29+
int pos = (len_str * i);
30+
std::memcpy((char *) buf + pos, record->at(i), len_str);
31+
}
32+
}
33+
34+
/**
35+
* Initializes a page using the given slot size
36+
*/
37+
void init_fixed_len_page(Page *page, int page_size, int slot_size){
38+
int slot = page_size/slot_size;
39+
40+
page->slot_size = slot_size;
41+
page->page_size = page_size;
42+
page->slot_used = 0;
43+
std::vector<Record> *data = new std::vector<Record>();
44+
45+
for (int i=0; i<slot; ++i){
46+
Record *new_rec = new Record();
47+
data->push_back(*new_rec);
48+
delete new_rec;
49+
}
50+
page->data = data;
51+
}
52+
53+
/**
54+
* Add a record to the page
55+
* Returns:
56+
* record slot offset if successful,
57+
* -1 if unsuccessful (page full)
58+
*/
59+
int add_fixed_len_page(Page *page, Record *r){
60+
int slot = 0;
61+
std::vector<Record> records = *(page->data);
62+
for (std::vector<Record>::iterator it = records.begin(); it != records.end(); ++it)
63+
{
64+
if (it->empty())
65+
{
66+
return slot;
67+
}
68+
slot++;
69+
}
70+
return -1;
71+
}
72+
73+
/**
74+
* Read a record from the page from a given slot.
75+
*/
76+
void read_fixed_len_page(Page *page, int slot, Record *r){
77+
*r = page->data->at(slot);
78+
}
79+
80+
/**
81+
* Calculates the maximal number of records that fit in a page
82+
*/
83+
int fixed_len_page_capacity(Page *page){
84+
return page->page_size/page->slot_size;
85+
}

library.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ typedef const char* V;
55
typedef std::vector<V> Record;
66

77
typedef struct {
8-
void *data;
8+
std::vector<Record> *data;
99
int page_size;
1010
int slot_size;
11+
int slot_used;
12+
1113
} Page;
1214

1315
//The Heap File
@@ -30,7 +32,7 @@ class RecordIterator {
3032
RecordIterator(Heapfile *heapfile);
3133
Record next();
3234
bool hasNext();
33-
}
35+
};
3436

3537
/**
3638
* Initalize a heapfile to use the file and page size given.

read_fixed_len_page.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <sstream>
4+
#include <istream>
5+
#include <sys/timeb.h>
6+
#include "library.h"
7+
#include <string>
8+
9+
int main(int argc, const char * argv[]){
10+
if (argc<3){
11+
std::cout << "USAGE: read_fixed_len_page <page_file> <page_size>";
12+
return 1;
13+
}
14+
int record_no, size_page, page_no;
15+
16+
std::string filename(argv[1]);
17+
size_page = std::stoi(argv[2]);
18+
19+
// start timer
20+
struct timeb t;
21+
ftime(&t);
22+
unsigned long start_ms = t.time * 1000 + t.millitm;
23+
24+
std::ifstream filePage;
25+
filePage.open(filename, std::ios::in|std::ios::binary);
26+
27+
FILE * file = fopen("tuples2.csv", "w");
28+
29+
while(!filePage.eof()){
30+
Page page;
31+
32+
init_fixed_len_page(&page, size_page, 1000);
33+
34+
filePage.read((char *) page.data, size_page);
35+
36+
int i=0;
37+
while(i!=fixed_len_page_capacity(&page)){
38+
Record record;
39+
read_fixed_len_page(&page, i, &record);
40+
unsigned int j=0;
41+
while(j != record.size()){
42+
std::cout << record.at(j);
43+
if (j!=record.size() - 1){
44+
std::cout << ",";
45+
}
46+
j++;
47+
}
48+
fputs("\n", file);
49+
record_no++;
50+
}
51+
page_no++;
52+
}
53+
fclose(file);
54+
filePage.close();
55+
ftime(&t);
56+
unsigned long stop_ms = t.time * 1000 + t.millitm;
57+
58+
std::cout << "Number of Records: " << record_no << "\n";
59+
std::cout << "Number of Pages: " << page_no << "\n";
60+
std::cout << "Time: " << stop_ms - start_ms << " milliseconds\n";
61+
62+
return 0;
63+
}

write_fixed_len_pages.cc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
#include <fstream>
33
#include <sys/timeb.h>
44
#include <iostream>
5+
#include <cstring>
56
#include "library.h"
7+
#include <sstream>
68

79
int main(int argc, const char *argv[]) {
810
if (argc < 4){
9-
std::cout << "Error Usage. USAGE: ./write_fixed_len_pages <csv_file> <page_file> <page_size>"
11+
std::cout << "Error Usage. USAGE: ./write_fixed_len_pages <csv_file> <page_file> <page_size>";
1012
return 1;
1113
}
1214
// Initializing variables
@@ -22,7 +24,7 @@ int main(int argc, const char *argv[]) {
2224
int pageSize = std::stoi(argv[3]);
2325

2426

25-
FilePage.open(pageFilename, std::ios::binary | std::ios:out);
27+
FilePage.open(pageFilename, std::ios::binary | std::ios::out);
2628
std::ifstream csv_file(file_csv);
2729

2830
int new_page_required = 1;
@@ -35,8 +37,8 @@ int main(int argc, const char *argv[]) {
3537
Record record;
3638
std::stringstream linestr(l);
3739
std::string cell;
38-
while(std::getline(linestr, cell, ','){
39-
char *temp = (cahr *)malloc(11);
40+
while(std::getline(linestr, cell, ',')){
41+
char *temp = (char *)malloc(11);
4042
std::strncpy(temp, cell.c_str(), 11);
4143
record.push_back(temp);
4244
}
@@ -51,8 +53,25 @@ int main(int argc, const char *argv[]) {
5153
FilePage.write((const char *) page.data, page.page_size);
5254
init_fixed_len_page(&page, pageSize, fixed_len_sizeof(&record));
5355
add_fixed_len_page(&page,&record);
56+
new_page_required = 0;
57+
Page_no ++;
5458
}
5559
}
5660

61+
if (!new_page_required){
62+
FilePage.write((const char *) page.data, page.page_size);
63+
}
64+
65+
FilePage.close();
66+
67+
ftime(&t);
68+
unsigned long stop_ms = t.time * 1000 + t.millitm;
69+
70+
std::cout << "Number of record are : " << record_no << "\n";
71+
std::cout << " Number of page are : " << Page_no << "\n";
72+
std::cout << "Time: " << stop_ms - start_ms << " milliseconds\n";
73+
74+
return 0;
75+
5776

5877
}

0 commit comments

Comments
 (0)