-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
62 lines (53 loc) · 1.55 KB
/
test.c
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mapreduce.h"
void Map(char *file_name) {
FILE *fp = fopen(file_name, "r");
assert(fp != NULL);
char *line = NULL;
size_t size = 0;
while (getline(&line, &size, fp) != -1) {
char *token, *dummy = line;
while ((token = strsep(&dummy, " \t\n\r")) != NULL) {
MR_EmitToCombiner(token, "1");
printf("exited emit to combiner\n");
}
}
free(line);
fclose(fp);
}
void Combine(char *key, CombineGetter get_next) {
int count = 0;
char *value;
while ((value = get_next(key)) != NULL) {
count ++; // Emmited Map values are "1"s
}
// Convert integer (count) to string (value)
value = (char*)malloc(10 * sizeof(char));
sprintf(value, "%d", count);
MR_EmitToReducer(key, value);
free(value);
}
void Reduce(char *key, ReduceStateGetter get_state,
ReduceGetter get_next, int partition_number) {
// `get_state` is only being used for "eager mode" (explained later)
assert(get_state == NULL);
int count = 0;
char *value;
while ((value = get_next(key, partition_number)) != NULL) {
count += atoi(value);
}
// Convert integer (count) to string (value)
value = (char*)malloc(10 * sizeof(char));
sprintf(value, "%d", count);
printf("%s %s\n", key, value);
free(value);
}
int main(int argc, char *argv[]) {
for (int i = 0; i < 3; i++) {
MR_Run(argc, argv, Map, 10,
Reduce, 10, Combine, MR_DefaultHashPartition);
}
}