UNPKG

pipeproc

Version:

Multi-process log processing for nodejs

168 lines (167 loc) 6.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); const memdown_1 = __importDefault(require("memdown")); const d = debug_1.default("pipeproc:node:transaction"); function transaction(db) { d("starting new transaction..."); return { TRANSACTION: Symbol("PIPEPROC_TRANSACTION"), _updates: [], _resultFns: { unamed_bucket: { fns: [], position: 1 } }, commitUpdate: function (callback) { if (this._updates.length === 0) return callback(); d("commiting update..."); const updates = this._updates.map(u => { //tslint:disable no-object-literal-type-assertion prefer-type-cast return { type: "put", key: u.key, value: u.value }; //tslint: enable }); if (this._resultFns.unamed_bucket.fns.length === 0) delete this._resultFns.unamed_bucket; const results = Object.keys(this._resultFns) .map(bucket => { const bucketContent = this._resultFns[bucket]; return { fns: bucketContent.fns.map(re => re()), position: bucketContent.position }; }); results.sort((a, b) => { if (a.position < b.position) { return -1; } else if ((a.position > b.position)) { return 1; } else { return 0; } }); if (db instanceof memdown_1.default) { db.batch(updates, commitError => { if (commitError) { callback(commitError); } else { callback(null, ...(results.map(sortedBucketContent => { if (sortedBucketContent.fns.length > 1) { return sortedBucketContent.fns; } else { return sortedBucketContent.fns[0]; } }))); } }); } else { db.batch(updates, { sync: true }, commitError => { if (commitError) { callback(commitError); } else { callback(null, ...(results.map(sortedBucketContent => { if (sortedBucketContent.fns.length > 1) { return sortedBucketContent.fns; } else { return sortedBucketContent.fns[0]; } }))); } }); } }, commitDelete: function (callback) { if (this._updates.length === 0) return callback(); d("commiting delete..."); const updates = this._updates.map(u => { //tslint:disable no-object-literal-type-assertion prefer-type-cast return { type: "del", key: u.key }; //tslint: enable }); if (db instanceof memdown_1.default) { db.batch(updates, function (rollbackError) { if (rollbackError) { callback(rollbackError); } else { callback(); } }); } else { db.batch(updates, { sync: true }, function (rollbackError) { if (rollbackError) { callback(rollbackError); } else { callback(); } }); } }, add: function (u) { d("adding operations..."); if (Array.isArray(u)) { u.forEach((uu) => { if (uu.TRANSACTION) { this._updates = this._updates.concat(uu._updates); Object.keys(uu._resultFns).forEach(bucket => { if (this._resultFns[bucket]) { this._resultFns[bucket].fns = this._resultFns[bucket].fns.concat(uu._resultFns[bucket].fns); } else { this._resultFns[bucket] = uu._resultFns[bucket]; } }); } else { this._updates.push(uu); } }); } else { if (u.TRANSACTION) { this._updates = this._updates.concat(u._updates); Object.keys(u._resultFns).forEach(bucket => { if (this._resultFns[bucket]) { this._resultFns[bucket].fns = this._resultFns[bucket].fns.concat(u._resultFns[bucket].fns); } else { this._resultFns[bucket] = u._resultFns[bucket]; } }); } else { this._updates.push(u); } } }, done: function (resultCb, resultBucket) { d("tracking results..."); let bucketName; if (!resultBucket) { bucketName = "unamed_bucket"; } else { bucketName = resultBucket; } this._resultFns[bucketName] = this._resultFns[bucketName] || { fns: [], position: Object.keys(this._resultFns).length + 1 }; this._resultFns[bucketName].fns.push(resultCb); } }; } exports.transaction = transaction;