This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
template.js
261 lines (228 loc) · 6.06 KB
/
template.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
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
/**
* Template batch script
*
* call as mongo db template.js <realbatchscript.js>
*
* The strucure of the whole script is:
* run
* start
* processCursors (loop, find <step> rows)
* processCursor
* beforeCursor
* (loop, for each row)
* process
* afterCursor
* finish
*/
/**
* A template batch class
* Include the contents of this file before your specific batches
*/
Batch = function(options, run) {
this.name = 'Template';
this.options = {
collection: 'collectionname',
conditions: {},
fields: {},
sort: { _id: 1 },
step: 0,
logLevel: 3
};
for (i in options) {
this.options[i] = options[i];
}
/**
* process
*
* Main process function - What do you want to do to each row returned from the db?
* this is the row of data from the cursor
*
* @return void.
*/
this.process = function() {
if (this.name !== 'Template') {
throw new Error("You need to define the process function for your batch.");
}
this.out('processing ' + this.currentRow._id, 4);
return;
try {
db[this.options.collection].update(
{ _id: this.currentRow._id },
{ $set: {
}
}
);
} catch (err) {
this.out(err.message, 1);
}
}
/**
* start
*
* What to do at the start of the batch. Aborts everything if it returns false
*
* @return bool.
*/
this.start = function() {
this.out('Starting ' + this.name + ' batch..', 1);
try {
this.total = db[this.options.collection].count(this.options.conditions);
} catch (err) {
this.out(err.message, 1);
return false;
}
this.out('Found ' + this.total + ' rows in ' + this.options.collection + ' to process', 1);
if (!this.total) {
return true;
}
if (!this.options.step) {
if (this.total > 10000) {
this.options.step = Math.pow(10, this.total.toString().length - 3);
} else {
this.options.step = 100;
}
this.out('Step size not defined, processing in slices of ' + this.options.step + ' rows per cursor', 2);
}
return true;
}
/**
* finish
*
* Called at the end of the batch process
*
* @return void.
*/
this.finish = function() {
this.out('Found ' + this.total + ' rows in ' + this.options.collection + ' to process', 3);
this.out('All finished', 1);
}
/**
* beforeCursor
*
* Called before issuing a find, can abort all further processing by returning false
*
* @return bool.
*/
this.beforeCursor = function() {
return true;
}
/**
* afterCursor
*
* Called after processing a cursor (after processing x rows). Could be used to issue buffered
* bulk-update statements from the cursor run. Can abort further processing by returning false
*
* @param count $count.
* @return bool.
*/
this.afterCursor = function(count) {
this.processed += count;
this.out(this.processed + ' ' + this.options.collection + ' processed, last id: ' + this.currentRow._id, 3);
this.options.conditions._id = {'$gt': this.currentRow._id};
return true;
}
/**
* processCursor
*
* Runs a query using the collection, conditions and step defined at the top of the script
* if beforeCursor returns false - no query is performed and the whole batch process is halted
* Otherwise, it loops on the cursor passing each row to the process function. The last
* step for each cursor is to call afterCursor - which can also abort further processing
* by returning false
*
* @return bool.
*/
this.processCursor = function() {
if (!this.beforeCursor()) {
this.out('beforeCursor returned false - aborting further processing', 4);
return false;
}
var cursor,
count = 0;
try {
cursor = db[this.options.collection]
.find(this.options.conditions, this.options.fields)
.sort(this.options.sort)
.limit(this.options.step);
} catch (err) {
this.out(err.message, 1);
return false;
}
while (cursor.hasNext()) {
this.currentRow = cursor.next();
this.process();
count++;
}
return this.afterCursor(count);
}
/**
* processCursors
*
* Loop on all cursors until there are none left or one fails.
*
* @return void.
*/
this.processCursors = function() {
while (this.processed < this.total || this.total === true) {
if (!this.processCursor()) {
this.out('Last slice failed - aborting further processing in processCursors', 4);
return;
}
}
}
/**
* run
*
* Run the start function - if it returns false there's nothing to do or something wrong. stop.
*
* Else, call process cursors, and the finish function
*
* @return void.
*/
this.run = function() {
this.startTime = new Date().getTime();
this.processed = 0;
this.total = this.options.total || 0;
this.currentRow = null;
if (!this.start()) {
this.out('start returned false - aborting further processing', 1);
return false;
}
this.processCursors();
this.finish();
}
/**
* out - wrapper for printing output
*
* If the msgLevel is greater than the configured logLevel - do nothing
* Otherwise, prefix with time since the script started
*
* @param msg
* @param msgLevel
* @return void.
*/
this.out = function(msg, msgLevel) {
if (msgLevel === undefined) {
msgLevel = 2;
}
if (msgLevel > this.options.logLevel) {
return;
}
var digits = 6,
time = (new Date().getTime() - this.startTime) / 1000;
if (time > 1000) {
digits = 9;
}
function pad(n, len) {
s = n.toString();
if (s.length < len) {
s = (' ' + s).slice(-len);
}
return s;
}
print('[' + pad(time.toFixed(2), digits) + 's] ' + msg);
}
if (typeof run === 'undefined' || run === true) {
this.run();
}
};