Code coverage report for mongopatch/source/streams.js

Statements: 95.93% (118 / 123)      Branches: 73.08% (19 / 26)      Functions: 100% (39 / 39)      Lines: 95.93% (118 / 123)      Ignored: none     

All files » mongopatch/source/ » streams.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 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 2871 1 1 1 1   1   1   1 392     1 66 20     46 62 62     46     1 86     1 160             160     1 80           80       1 80 80   80   80     80             80 80     80         1 24         24         24 82 82   82   82                     82 82     82 82 82   82       82         82     74     8 8   8 8         8       8       8               1 20         20         20 78   78     78 78 78   78     72     6 6     6         1 22 6 6     22 22   22 138   138 138 1         1   137 2     135       22         22     1 18 18 18 18 18   18 94 94   94 94   94                       94       1 10     1 10 39       1 12     1 12 41       1   1 1 1 1   1 1  
var async = require('async');
var streams = require('stream-wrapper');
var parallel = require('parallel-transform');
var speedometer = require('speedometer');
var bson = new (require('bson').pure().BSON)();
 
var diff = require('./diff');
 
var DEFAULT_CONCURRENCY = 1;
 
var bsonCopy = function(obj) {
	return bson.deserialize(bson.serialize(obj));
};
 
var extend = function(dest, src) {
	if(!src) {
		return dest;
	}
 
	Object.keys(src).forEach(function(key) {
		var v = src[key];
		dest[key] = v === undefined ? dest[key] : v;
	});
 
	return dest;
};
 
var noopCallback = function(doc, callback) {
	callback();
};
 
var applyAfterCallback = function(afterCallback, patch, callback) {
	var update = bsonCopy({
		before: patch.before,
		after: patch.after,
		modified: patch.modified,
		diff: patch.diff
	});
 
	afterCallback(update, callback);
};
 
var applyUpdate = function(patch, callback) {
	patch.collection.findAndModify({
		query: { _id: patch.before._id },
		'new': true,
		update: patch.modifier
	}, function(err, after) {
		// Ensure arity
		callback(err, after);
	});
};
 
var applyTmp = function(tmpCollection, patch, callback) {
	var id = patch.before._id;
	var after;
 
	async.waterfall([
		function(next) {
			tmpCollection.save(patch.before, next);
		},
		function(savedDocument, _, next) {
			tmpCollection.findAndModify({
				query: { _id: id },
				'new': true,
				update: patch.modifier
			}, next);
		},
		function(result, _, next) {
			after = result;
			tmpCollection.remove({ _id: id }, next);
		},
		function() {
			callback(null, after);
		}
	], callback);
};
 
var loggedTransformStream = function(logCollection, options, fn) {
	Iif(!fn) {
		fn = options;
		options = null;
	}
 
	options = extend({
		afterCallback: noopCallback,
		concurrency: DEFAULT_CONCURRENCY
	}, options);
 
	return parallel(options.concurrency, function(patch, callback) {
		var id = patch.before._id;
		var logDocument;
 
		async.waterfall([
			function(next) {
				logCollection.insert({
					_id: id,
					before: patch.before,
					collection: patch.collection.toString(),
					query: JSON.stringify(patch.query),
					modifier: JSON.stringify(patch.modifier),
					modified: false,
					createdAt: new Date()
				}, next);
			},
			function(result, next) {
				logDocument = result[0];
				fn(patch, next);
			},
			function(after, next) {
				patch.after = after;
				patch.diff = diff.deep(patch.before, patch.after);
				patch.modified = !!Object.keys(patch.diff).length;
 
				logCollection.update(
					{ _id: logDocument._id },
					{ $set: { after: after, modified: patch.modified, diff: patch.diff } },
					function(err) {
						next(err);
					}
				);
			},
			function(next) {
				applyAfterCallback(options.afterCallback, patch, next);
			},
			function() {
				callback(null, patch);
			}
		], function(err) {
			Eif(err) {
				err.patch = patch;
			}
			Eif(err && logDocument) {
				var documentError = {
					message: err.message,
					stack: err.stack
				};
 
				logCollection.update(
					{ _id: logDocument._id },
					{ $set: { error: documentError } },
					function() {
						callback(err);
					}
				);
 
				return;
			}
 
			callback(err);
		});
	});
};
 
var transformStream = function(options, fn) {
	Iif(!fn) {
		fn = options;
		options = null;
	}
 
	options = extend({
		afterCallback: noopCallback,
		concurrency: DEFAULT_CONCURRENCY
	}, options);
 
	return parallel(options.concurrency, function(patch, callback) {
		async.waterfall([
			function(next) {
				fn(patch, next);
			},
			function(after, next) {
				patch.after = after;
				patch.diff = diff.deep(patch.before, patch.after);
				patch.modified = !!Object.keys(patch.diff).length;
 
				applyAfterCallback(options.afterCallback, patch, next);
			},
			function() {
				callback(null, patch);
			}
		], function(err) {
			Eif(err) {
				err.patch = patch;
			}
 
			callback(err);
		});
	});
};
 
var patchStream = function(collection, query, options, worker) {
	if(!worker) {
		worker = options;
		options = null;
	}
 
	query = query || {};
	options = extend({ concurrency: DEFAULT_CONCURRENCY }, options);
 
	var patch = parallel(options.concurrency, function(document, callback) {
		var clone = bsonCopy(document);
 
		worker(document, function(err, modifier) {
			if (err) {
				err.patch = {
					before: clone,
					modifier: modifier
				};
 
				return callback(err);
			}
			if(!modifier) {
				return callback();
			}
 
			callback(null, { modifier:modifier, before:clone, query:query, collection:collection });
		});
	});
 
	collection
		.find(query)
		.sort({ _id: 1 })
		.pipe(patch);
 
	return patch;
};
 
var progressStream = function(total) {
	var delta = {};
	var count = 0;
	var modified = 0;
	var started = Date.now();
	var speed = speedometer(); // documents per second
 
	return streams.transform({ objectMode: true }, function(patch, encoding, callback) {
		count++;
		modified += (patch.modified ? 1 : 0);
 
		var currentSpeed = speed(1);
		var remaining = Math.max(total - count, 0);
 
		patch.progress = {
			total: total,
			count: count,
			modified: modified,
			speed: currentSpeed,
			remaining: remaining,
			eta: Math.round(remaining / currentSpeed),
			time: Math.round((Date.now() - started) / 1000),
			percentage: (100 * count / total),
			diff: bsonCopy(diff(patch.before, patch.after, { accumulate: delta, group: true }))
		};
 
		callback(null, patch);
	});
};
 
var updateStream = function(options) {
	return transformStream(options, applyUpdate);
};
 
var tmpStream = function(tmpCollection, options) {
	return transformStream(options, function(patch, callback) {
		applyTmp(tmpCollection, patch, callback);
	});
};
 
var loggedUpdateStream = function(logCollection, options) {
	return loggedTransformStream(logCollection, options, applyUpdate);
};
 
var loggedTmpStream = function(logCollection, tmpCollection, options) {
	return loggedTransformStream(logCollection, options, function(patch, callback) {
		applyTmp(tmpCollection, patch, callback);
	});
};
 
exports.logged = {};
 
exports.patch = patchStream;
exports.progress = progressStream;
exports.update = updateStream;
exports.tmp = tmpStream;
 
exports.logged.update = loggedUpdateStream;
exports.logged.tmp = loggedTmpStream;