-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapreduce.cpp
More file actions
327 lines (266 loc) · 9.29 KB
/
Copy pathmapreduce.cpp
File metadata and controls
327 lines (266 loc) · 9.29 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <exception>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <boost/process.hpp>
#include <boost/filesystem.hpp>
const size_t SIZE = 10000;
const size_t MAX_COUNT_OF_PROCESS = 10;
const long double EPS = 1e-9;
namespace bp = boost::process;
namespace bf = boost::filesystem;
void CreateProcess(std::vector<bp::child> &all_process,
const std::string &script_path,
std::ofstream &file_for_close,
const std::string &input_path,
const std::string &output_path,
size_t &count_of_file) {
file_for_close.close();
all_process.emplace_back(bp::child(script_path,
bp::std_out > output_path +
std::to_string(count_of_file) +
".txt",
bp::std_err > stderr,
bp::std_in < input_path +
std::to_string(count_of_file) +
".txt"));
++count_of_file;
}
void WaitingCompletionOfAllProcess(std::vector<bp::child> &all_process) {
for (auto &process : all_process) {
std::error_code error_code;
process.wait(error_code);
if (error_code.value() != 0) {
throw std::system_error(error_code);
}
}
}
size_t RunMappers(std::ifstream &file_input,
std::ofstream &temp_file_input,
const std::string &script_path,
const std::string &temp_input_file_path,
const std::string &temp_output_file_path,
std::vector<bp::child> &all_process) {
size_t cur_count_of_line = 0, count_of_file = 0;
std::string line;
while (getline(file_input, line)) {
if (cur_count_of_line == 0) {
temp_file_input.open(temp_input_file_path +
std::to_string(count_of_file) + ".txt");
if (!temp_file_input.is_open()) {
throw std::logic_error("Something went wrong while creating the file");
}
}
temp_file_input << line << '\n';
++cur_count_of_line;
if (cur_count_of_line == SIZE) {
CreateProcess(all_process,
script_path,
temp_file_input,
temp_input_file_path,
temp_output_file_path,
count_of_file);
cur_count_of_line = 0;
}
if (all_process.size() == MAX_COUNT_OF_PROCESS) {
WaitingCompletionOfAllProcess(all_process);
all_process.clear();
}
}
if (temp_file_input.is_open()) {
CreateProcess(all_process,
script_path,
temp_file_input,
temp_input_file_path,
temp_output_file_path,
count_of_file);
}
return count_of_file;
}
size_t RunReducers(std::ifstream &file_input,
std::ofstream &temp_file_input,
const std::string &script_path,
const std::string &temp_input_file_path,
const std::string &temp_output_file_path,
std::vector<bp::child> &all_process,
size_t count_of_range) {
size_t count_of_file = 0;
long double step = 1.0 / count_of_range, cur_range = 0;
std::string line;
while (std::getline(file_input, line)) {
std::stringstream stream(line);
std::string string_key;
std::getline(stream, string_key, '\t');
long double cur_value;
std::stringstream stream_key(string_key);
stream_key >> cur_value;
if (cur_value > cur_range + EPS) {
if (temp_file_input.is_open()) {
CreateProcess(all_process,
script_path,
temp_file_input,
temp_input_file_path,
temp_output_file_path,
count_of_file);
}
if (all_process.size() == MAX_COUNT_OF_PROCESS) {
WaitingCompletionOfAllProcess(all_process);
all_process.clear();
}
while (cur_value > cur_range + EPS) {
cur_range += step;
}
temp_file_input.open(temp_input_file_path +
std::to_string(count_of_file) + ".txt");
}
temp_file_input << line << '\n';
}
if (temp_file_input.is_open()) {
CreateProcess(all_process,
script_path,
temp_file_input,
temp_input_file_path,
temp_output_file_path,
count_of_file);
}
return count_of_file;
}
void WriteSortData(std::vector<std::string> &data,
const std::string &temp_file_path,
size_t &count_of_file) {
std::ofstream chunk_output;
sort(data.begin(), data.end());
chunk_output.open(temp_file_path + std::to_string(count_of_file) + ".txt");
if (!chunk_output.is_open()) {
throw std::logic_error("Something went wrong while creating the file");
}
for (const auto &el : data) {
chunk_output << el << '\n';
}
chunk_output.close();
data.clear();
++count_of_file;
}
size_t SplitFile(const std::string &input_file,
const std::string &temp_file_path) {
std::ifstream file_input(input_file);
size_t cur_count_of_line = 0, count_of_file = 0;
std::string line;
std::vector<std::string> data;
while (getline(file_input, line)) {
data.push_back(line);
++cur_count_of_line;
if (cur_count_of_line == SIZE) {
WriteSortData(data, temp_file_path, count_of_file);
cur_count_of_line = 0;
}
}
if (cur_count_of_line != 0) {
WriteSortData(data, temp_file_path, count_of_file);
}
return count_of_file;
}
void MergeFile(const std::string &output_file,
const std::string &temp_file_path,
size_t count_of_file) {
std::vector<std::ifstream> flow_of_file(count_of_file);
std::priority_queue<std::pair<std::string, size_t>,
std::vector<std::pair<std::string, size_t >>,
std::greater<> > heap;
std::string line;
for (size_t i = 0; i < count_of_file; ++i) {
flow_of_file[i] = std::ifstream(temp_file_path +
std::to_string(i) + ".txt");
getline(flow_of_file[i], line);
heap.push({line, i});
}
std::ofstream out(output_file);
while (!heap.empty()) {
std::pair<std::string, size_t> top = heap.top();
heap.pop();
if (flow_of_file[top.second].peek() != EOF) {
getline(flow_of_file[top.second], line);
heap.push({line, top.second});
}
out << top.first << '\n';
}
for (auto &el : flow_of_file) {
el.close();
}
out.close();
}
void ExternalSort(const std::string &input_file,
const std::string &temp_file_path) {
size_t count_of_file = SplitFile(input_file, temp_file_path);
MergeFile(input_file, temp_file_path, count_of_file);
}
void MergeTempFiles(const std::string &output_path,
const std::string &temp_output_file_path,
size_t count_of_file) {
std::ofstream file_output(output_path);
std::string line;
for (size_t i = 0; i < count_of_file; ++i) {
std::ifstream temp_file_input(temp_output_file_path +
std::to_string(i) + ".txt");
if (!temp_file_input.is_open()) {
throw std::logic_error("Something went wrong while opening the file");
}
while (getline(temp_file_input, line)) {
file_output << line << '\n';
}
temp_file_input.close();
}
file_output.close();
}
int main(int argc, char *argv[]) {
if (argc != 6) {
throw std::logic_error("Invalid count of arguments");
}
std::string mode = argv[1],
script_path = argv[2],
input_path = argv[3],
output_path = argv[4];
size_t count_of_range = std::stoi(argv[5]);
if (mode != "map" && mode != "reduce") {
throw std::logic_error("Invalid mode");
}
const bf::path temp_directory = bf::unique_path();
bf::path temp_input_file_path = temp_directory,
temp_output_file_path = temp_directory;
temp_input_file_path.append("input_"),
temp_output_file_path.append("output_");
bf::create_directories(temp_directory);
std::ofstream temp_file_input;
std::vector<bp::child> all_process;
std::string line;
size_t count_of_file;
if (mode == "map") {
std::ifstream file_input(input_path);
count_of_file = RunMappers(file_input,
temp_file_input,
script_path,
temp_input_file_path.string(),
temp_output_file_path.string(),
all_process);
file_input.close();
} else {
bf::path temp_sort_file_path = temp_directory;
temp_sort_file_path.append("sort_");
ExternalSort(input_path, temp_sort_file_path.string());
std::ifstream file_input(input_path);
count_of_file = RunReducers(file_input,
temp_file_input,
script_path,
temp_input_file_path.string(),
temp_output_file_path.string(),
all_process,
count_of_range);
file_input.close();
}
WaitingCompletionOfAllProcess(all_process);
MergeTempFiles(output_path, temp_output_file_path.string(), count_of_file);
bf::remove_all(temp_directory);
return 0;
}