compute metadata in the same child process that builds indexes

This commit is contained in:
ansuz
2020-03-24 17:43:15 -04:00
parent 479b76f848
commit 471e374533
4 changed files with 95 additions and 54 deletions

View File

@@ -5,6 +5,7 @@ const HK = require("../hk-util");
const Store = require("../storage/file");
const Util = require("../common-util");
const nThen = require("nthen");
const Meta = require("../metadata");
const Env = {};
@@ -46,7 +47,13 @@ const tryParse = function (Env, str) {
* including the initial metadata line, if it exists
*/
const computeIndex = function (channelName, cb) {
const computeIndex = function (data, cb) {
if (!data || !data.channel) {
return void cb('E_NO_CHANNEL');
}
const channelName = data.channel;
const cpIndex = [];
let messageBuf = [];
let i = 0;
@@ -125,45 +132,60 @@ const computeIndex = function (channelName, cb) {
});
};
const computeMetadata = function (data, cb, errorHandler) {
const ref = {};
const lineHandler = Meta.createLineHandler(ref, errorHandler);
return void store.readChannelMetadata(data.channel, lineHandler, function (err) {
if (err) {
// stream errors?
return void cb(err);
}
cb(void 0, ref.meta);
});
};
const COMMANDS = {
COMPUTE_INDEX: computeIndex,
COMPUTE_METADATA: computeMetadata,
};
process.on('message', function (data) {
if (!data || !data.txid) {
return void process.send({
error:'E_INVAL'
});
}
const txid = data.txid;
if (!ready) {
return void init(data.config, function (err) {
if (err) {
return void process.send({
txid: txid,
error: err,
});
}
ready = true;
process.send({txid: txid,});
});
}
const channel = data.args;
if (!channel) {
return void process.send({
error: 'E_NO_CHANNEL',
});
}
// computeIndex
computeIndex(channel, function (err, index) {
const cb = function (err, value) {
if (err) {
return void process.send({
txid: txid,
txid: data.txid,
error: err,
});
}
return void process.send({
txid: txid,
value: index,
process.send({
txid: data.txid,
value: value,
});
};
if (!ready) {
return void init(data.config, function (err) {
if (err) { return void cb(err); }
ready = true;
cb();
});
}
const command = COMMANDS[data.command];
if (typeof(command) !== 'function') {
return void cb("E_BAD_COMMAND");
}
command(data, cb, function (label, info) {
// for streaming errors
process.send({
error: label,
value: info,
});
});
});