forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.js
131 lines (112 loc) · 2.86 KB
/
batch.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
'use strict';
var async = require('async');
var db = require('./database');
var utils = require('./utils');
var DEFAULT_BATCH_SIZE = 100;
exports.processSortedSet = function (setKey, process, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
callback = typeof callback === 'function' ? callback : function () {};
options = options || {};
if (typeof process !== 'function') {
return callback(new Error('[[error:process-not-a-function]]'));
}
// Progress bar handling (upgrade scripts)
if (options.progress) {
db.sortedSetCard(setKey, function (err, total) {
if (!err) {
options.progress.total = total;
}
});
}
options.batch = options.batch || DEFAULT_BATCH_SIZE;
// use the fast path if possible
if (db.processSortedSet && typeof options.doneIf !== 'function' && !utils.isNumber(options.alwaysStartAt)) {
return db.processSortedSet(setKey, process, options, callback);
}
// custom done condition
options.doneIf = typeof options.doneIf === 'function' ? options.doneIf : function () {};
var start = 0;
var stop = options.batch;
var done = false;
async.whilst(
function () {
return !done;
},
function (next) {
async.waterfall([
function (next) {
db['getSortedSetRange' + (options.withScores ? 'WithScores' : '')](setKey, start, stop, next);
},
function (ids, _next) {
if (!ids.length || options.doneIf(start, stop, ids)) {
done = true;
return next();
}
process(ids, function (err) {
_next(err);
});
},
function (next) {
start += utils.isNumber(options.alwaysStartAt) ? options.alwaysStartAt : options.batch + 1;
stop = start + options.batch;
if (options.interval) {
setTimeout(next, options.interval);
} else {
next();
}
},
], next);
},
callback
);
};
exports.processArray = function (array, process, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
callback = typeof callback === 'function' ? callback : function () {};
options = options || {};
if (!Array.isArray(array) || !array.length) {
return callback();
}
if (typeof process !== 'function') {
return callback(new Error('[[error:process-not-a-function]]'));
}
var batch = options.batch || DEFAULT_BATCH_SIZE;
var start = 0;
var done = false;
async.whilst(
function () {
return !done;
},
function (next) {
var currentBatch = array.slice(start, start + batch);
if (!currentBatch.length) {
done = true;
return next();
}
async.waterfall([
function (next) {
process(currentBatch, function (err) {
next(err);
});
},
function (next) {
start += batch;
if (options.interval) {
setTimeout(next, options.interval);
} else {
next();
}
},
], next);
},
function (err) {
callback(err);
}
);
};