-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternalsort.cpp
176 lines (160 loc) · 4.98 KB
/
externalsort.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<stdio.h>
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
using namespace std;
int num_blocks;
vector<ifstream* > fPointers;
string output_file;
int memory_size;
int save_buffer = 1024;
class CompareDist {
public:
bool operator()(pair<vector<string>, int>& p1, pair<vector<string>, int>& p2) {
vector<string> a = p1.first;
vector<string> b = p2.first;
if(a > b) {
return true;
}
else {
return false;
}
}
};
priority_queue<pair<vector<string>, int>, vector<pair<vector<string>, int> >, CompareDist> myheap;
void mergeFiles() {
int noSplits = num_blocks;
int OB = 0.2 * (double)memory_size;
// cout << "memory_size: " << memory_size << endl;
// cout << "OB: " << OB << endl;
int nOB = OB*1024*1024;
// cout << "nOB: " << nOB << endl;
int nF = ((double)((memory_size-OB)/noSplits))*1024*1024;
// cout << "nF: " << nF << endl;
// Store all file pointers in an array
for(int i=0; i<noSplits; i++) {
ifstream *file = new ifstream(("index/file" + to_string(i)).c_str());
fPointers.push_back(file);
}
ifstream *f;
string line;
vector<string> record;
int cnt;
for(int i=0; i<noSplits; i++) {
f = fPointers[i];
cnt = 0;
cout << "Reading from intermediate file" + to_string(i) << endl;
while(getline(*f, line)) {
cnt += line.size();
vector<string> tokens;
stringstream ss(line);
string item;
int cnt_item = 0;
while (getline(ss, item, '|')) {
if(cnt_item == 0) {
int i;
for(i=0; i<item.size(); i++) {
if(item[i] == ':') {
break;
}
}
tokens.push_back(item.substr(0, i));
tokens.push_back(item.substr(i+1, item.size()));
}
else {
if(item.size() != 0) {
tokens.push_back(item);
}
}
cnt_item++;
}
myheap.push(make_pair(tokens, i));
if (cnt >= nF-save_buffer) {
break;
}
}
}
int cntOB = 0;
ofstream outFile;
outFile.open(output_file);
string str_rec;
pair<vector<string>, int> ele;
vector<vector<string> > record_buff;
while(!myheap.empty()) {
ele = myheap.top();
vector<string> tep = ele.first;
//cout << tep[0] << " " << tep[1] << " " << tep[2] << endl;
myheap.pop();
// push record to output buffer
record_buff.push_back(ele.first);
int cnt_item = 0;
if (getline(*fPointers[ele.second], line)) {
vector<string> tokens;
stringstream ss(line);
string item;
int cnt_item = 0;
while (getline(ss, item, '|')) {
if(cnt_item == 0) {
int i;
for(i=0; i<item.size(); i++) {
if(item[i] == ':') {
break;
}
}
tokens.push_back(item.substr(0, i+1));
tokens.push_back(item.substr(i+1, item.size()));
}
else {
if(item.size() != 0) {
tokens.push_back(item);
}
}
cnt_item++;
}
myheap.push(make_pair(tokens, ele.second));
}
cntOB += line.size();
if (cntOB >= nOB-save_buffer) {
// output buffer is full
// put records to file
for(int i=0; i<record_buff.size(); i++) {;
str_rec = record_buff[i][0];
for(int j=1; j<record_buff[i].size(); j++) {
str_rec += " " + record_buff[i][j];
}
str_rec += "\n";
outFile << str_rec;
}
// clear Buffer
cntOB = 0;
record_buff.erase(record_buff.begin(), record_buff.end());
}
}
if (cntOB < nOB) {
for(int i=0; i<record_buff.size(); i++) {
str_rec = record_buff[i][0];
for(int j=1; j<record_buff[i].size(); j++) {
str_rec += " " + record_buff[i][j];
}
str_rec += "\n";
outFile << str_rec;
}
// clear Buffer
cntOB = 0;
record_buff.erase(record_buff.begin(), record_buff.end());
}
for(int i=0; i<noSplits; i++) {
delete fPointers[i];
}
fPointers.erase(fPointers.begin(), fPointers.end());
outFile.close();
}
int main(int argc, char* argv[]) {
output_file = "finalindex.txt";
memory_size = 64;
num_blocks = stoi(argv[1]);
mergeFiles();
return 0;
}