-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcw.mapReduce.js
172 lines (168 loc) · 2.86 KB
/
cw.mapReduce.js
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
(function(cw) {
function rWorker(fun, callback) {
var obj = {
fun: fun,
data: function(dat) {
if (!this._r) {
this._r = dat;
}
else {
this._r = this.fun(this._r, dat);
}
},
fetch: function() {
this.fire('msg', this._r);
},
close: function(silent) {
if (!silent) {
this.fire('msg', this._r);
}
self.terminate;
}
};
var worker = cw(obj);
worker.on('msg', callback);
return worker;
}
cw.mapReduce = function(threads) {
var w = {};
var len = 0;
var promise;
var workers = [];
var data = [];
var idle = threads;
var reducer;
var waiting = false;
var closing = false;
var status = {
map: false,
reduce: false,
data: false
};
var checkStatus = function() {
if (status.map && status.reduce && status.data) {
return go();
}
else {
return w;
}
};
w.map = function(fun, t) {
if (status.map) {
return w;
}
var i = 0;
function makeMapWorker() {
var dd;
function thenFunc(d) {
if (typeof d !== undefined) {
reducer.data(d);
}
if (len > 0) {
len--;
dd = data.pop();
if (t) {
mw2.data(dd, [dd]);
}
else {
mw2.data(dd);
}
}
else {
idle++;
if (idle === threads) {
status.data = false;
if (closing) {
closeUp();
}
else if (waiting) {
waiting = false;
reducer.fetch();
}
}
}
}
var mw1 = cw(fun);
var mw2 = {
data: function(data) {
mw1.data(data).then(thenFunc);
},
close: function() {
mw1.close();
}
};
workers.push(mw2);
}
while (i < threads) {
makeMapWorker();
i++;
}
status.map = true;
return checkStatus();
};
w.reduce = function(fun) {
if (status.reduce) {
return w;
}
reducer = rWorker(fun, function(d) {
if (promise) {
promise.resolve(d);
promise = false;
}
});
status.reduce = true;
return checkStatus();
};
w.data = function(d) {
if (closing) {
return;
}
len = len + d.length;
data = data.concat(d);
status.data = true;
return checkStatus();
};
function go() {
var i = 0;
var wlen = workers.length;
while (i < wlen && len > 0 && idle > 0) {
len--;
workers[i].data(data.pop());
i++;
idle--;
}
return w;
}
w.fetch = function(now) {
if (!promise) {
promise = cw.deferred();
}
if (idle < threads && !now) {
waiting = true;
}
else {
reducer.fetch();
}
return promise.promise;
};
w.close = function() {
if (!promise) {
promise = cw.deferred();
}
if (idle < threads) {
closing = true;
}
else {
closeUp();
}
return promise.promise;
};
function closeUp() {
reducer.close();
workers.forEach(function(v) {
v.close();
});
}
return w;
};
})(cw);