pipeproc
Version:
Multi-process log processing for nodejs
66 lines (65 loc) • 2.22 kB
JavaScript
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 transaction_1 = require("./transaction");
const tones_1 = require("./tones");
const d = debug_1.default("pipeproc:node");
function commitLog(db, activeTopics, log, callback) {
d("new log(s):\n%O", log);
const tx = transaction_1.transaction(db);
const creationTime = Date.now();
if (Array.isArray(log)) {
log.forEach(l => tx.add(preCommit(db, activeTopics, l, creationTime)));
}
else {
tx.add(preCommit(db, activeTopics, log, creationTime));
}
tx.commitUpdate(function (err, commit) {
if (err) {
callback(err);
}
else if (typeof commit === "string" || (Array.isArray(commit) && commit.length > 0)) {
callback(null, commit);
}
else {
callback();
}
});
}
exports.commitLog = commitLog;
function preCommit(db, activeTopics, log, creationTime) {
const tx = transaction_1.transaction(db);
if (!activeTopics[log.topic]) {
activeTopics[log.topic] = {
createdAt: creationTime,
currentTone: tones_1.ZERO_TONE
};
tx.add([
{ key: `~~system~~#activeTopics#${log.topic}`, value: `${creationTime}` },
{ key: `~~system~~#currentTone#${log.topic}`, value: tones_1.ZERO_TONE }
]);
}
const nextTone = tones_1.incrementCurrentTone(activeTopics[log.topic].currentTone);
const id = `${creationTime}-${nextTone}`;
const key = `topic#${log.topic}#key#${id}`;
const idKey = `~~internal~~#topic#${log.topic}#idKey#${nextTone}`;
tx.add([{
key: key,
value: log.body
}, {
key: idKey,
value: key
}, {
key: `~~system~~#currentTone#${log.topic}`,
value: nextTone
}]);
activeTopics[log.topic].currentTone = nextTone;
tx.done(function () {
return id;
}, "commits");
return tx;
}
exports.preCommit = preCommit;
;