|
|
|
@ -65,7 +65,7 @@ var channelExists = function (filepath, cb) {
|
|
|
|
|
// it also allows the handler to abort reading at any time
|
|
|
|
|
const readMessagesBin = (env, id, start, msgHandler, cb) => {
|
|
|
|
|
const stream = Fs.createReadStream(mkPath(env, id), { start: start });
|
|
|
|
|
return void readFileBin(env, stream, msgHandler, cb);
|
|
|
|
|
return void readFileBin(stream, msgHandler, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// reads classic metadata from a channel log and aborts
|
|
|
|
@ -90,7 +90,7 @@ var getMetadataAtPath = function (Env, path, _cb) {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
|
return readFileBin(Env, stream, function (msgObj, readMore, abort) {
|
|
|
|
|
return readFileBin(stream, function (msgObj, readMore, abort) {
|
|
|
|
|
const line = msgObj.buff.toString('utf8');
|
|
|
|
|
|
|
|
|
|
if (!line) {
|
|
|
|
@ -164,28 +164,16 @@ var clearChannel = function (env, channelId, _cb) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* readMessages is our classic method of reading messages from the disk
|
|
|
|
|
notably doesn't provide a means of aborting if you finish early
|
|
|
|
|
notably doesn't provide a means of aborting if you finish early.
|
|
|
|
|
Internally it uses readFileBin: to avoid duplicating code and to use less memory
|
|
|
|
|
*/
|
|
|
|
|
// XXX replicate current API on top of readMessagesBin
|
|
|
|
|
var readMessages = function (path, msgHandler, cb) {
|
|
|
|
|
var remainder = '';
|
|
|
|
|
var stream = Fs.createReadStream(path, { encoding: 'utf8' });
|
|
|
|
|
var complete = function (err) {
|
|
|
|
|
var _cb = cb;
|
|
|
|
|
cb = undefined;
|
|
|
|
|
if (_cb) { _cb(err); }
|
|
|
|
|
};
|
|
|
|
|
stream.on('data', function (chunk) {
|
|
|
|
|
var lines = chunk.split('\n');
|
|
|
|
|
lines[0] = remainder + lines[0];
|
|
|
|
|
remainder = lines.pop();
|
|
|
|
|
lines.forEach(msgHandler);
|
|
|
|
|
});
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
|
msgHandler(remainder);
|
|
|
|
|
complete();
|
|
|
|
|
});
|
|
|
|
|
stream.on('error', function (e) { complete(e); });
|
|
|
|
|
var readMessages = function (path, msgHandler, _cb) {
|
|
|
|
|
var stream = Fs.createReadStream(path, { start: 0});
|
|
|
|
|
var cb = Util.once(Util.mkAsync(_cb));
|
|
|
|
|
return readFileBin(stream, function (msgObj, readMore) {
|
|
|
|
|
msgHandler(msgObj.buff.toString('utf8'));
|
|
|
|
|
readMore();
|
|
|
|
|
}, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* getChannelMetadata
|
|
|
|
@ -203,23 +191,21 @@ var getChannelMetadata = function (Env, channelId, cb) {
|
|
|
|
|
// low level method for getting just the dedicated metadata channel
|
|
|
|
|
var getDedicatedMetadata = function (env, channelId, handler, cb) {
|
|
|
|
|
var metadataPath = mkMetadataPath(env, channelId);
|
|
|
|
|
// XXX use readFileBin
|
|
|
|
|
readMessages(metadataPath, function (line) {
|
|
|
|
|
if (!line) { return; }
|
|
|
|
|
var stream = Fs.createReadStream(metadataPath, {start: 0});
|
|
|
|
|
readFileBin(stream, function (msgObj, readMore) {
|
|
|
|
|
var line = msgObj.buff.toString('utf8');
|
|
|
|
|
try {
|
|
|
|
|
var parsed = JSON.parse(line);
|
|
|
|
|
handler(null, parsed);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
handler(e, line);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
handler(err, line);
|
|
|
|
|
}
|
|
|
|
|
readMore();
|
|
|
|
|
}, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
// ENOENT => there is no metadata log
|
|
|
|
|
if (err.code === 'ENOENT') { return void cb(); }
|
|
|
|
|
if (!err || err.code === 'ENOENT') { return void cb(); }
|
|
|
|
|
// otherwise stream errors?
|
|
|
|
|
return void cb(err);
|
|
|
|
|
}
|
|
|
|
|
cb();
|
|
|
|
|
cb(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -377,7 +363,7 @@ var removeArchivedChannel = function (env, channelName, cb) {
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// XXX use ../plan.js
|
|
|
|
|
// TODO use ../plan.js for a smaller memory footprint
|
|
|
|
|
var listChannels = function (root, handler, cb) {
|
|
|
|
|
// do twenty things at a time
|
|
|
|
|
var sema = Semaphore.create(20);
|
|
|
|
@ -793,6 +779,7 @@ var message = function (env, chanName, msg, cb) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// stream messages from a channel log
|
|
|
|
|
// TODO replace getMessages with readFileBin
|
|
|
|
|
var getMessages = function (env, chanName, handler, cb) {
|
|
|
|
|
getChannel(env, chanName, function (err, chan) {
|
|
|
|
|
if (!chan) {
|
|
|
|
|