Respond to pinning RPCs as soon as possible
(instead of waiting until you've read an unbounded number of pin logs while queries back up in memory) Also replace instances of 'publicKey' with 'safeKey' or 'unsafeKey' to clearly and correctly indicate their format.
This commit is contained in:
35
lib/pins.js
35
lib/pins.js
@@ -94,13 +94,26 @@ Pins.calculateFromLog = function (pinFile, fileName) {
|
||||
|
||||
const getSafeKeyFromPath = function (path) {
|
||||
return path.replace(/^.*\//, '').replace(/\.ndjson/, '');
|
||||
}
|
||||
};
|
||||
|
||||
const addUserPinToState = Pins.addUserPinToState = function (state, safeKey, itemId) {
|
||||
(state[itemId] = state[itemId] || {})[safeKey] = 1;
|
||||
};
|
||||
|
||||
Pins.list = function (_done, config) {
|
||||
// allow for a configurable pin store location
|
||||
const pinPath = config.pinPath || './data/pins';
|
||||
|
||||
// allow for a configurable amount of parallelism
|
||||
const plan = Plan(config.workers || 5);
|
||||
|
||||
// run a supplied handler whenever you finish reading a log
|
||||
// or noop if not supplied.
|
||||
const handler = config.handler || function () {};
|
||||
|
||||
// use and mutate a supplied object for state if it's passed
|
||||
const pinned = config.pinned || {};
|
||||
|
||||
var isDone = false;
|
||||
// ensure that 'done' is only called once
|
||||
// that it calls back asynchronously
|
||||
@@ -110,20 +123,10 @@ Pins.list = function (_done, config) {
|
||||
isDone = true;
|
||||
}));
|
||||
|
||||
// TODO externalize this via optional handlers?
|
||||
const stats = {
|
||||
logs: 0,
|
||||
dirs: 0,
|
||||
pinned: 0,
|
||||
lines: 0,
|
||||
};
|
||||
|
||||
const errorHandler = function (label, info) {
|
||||
console.log(label, info);
|
||||
};
|
||||
|
||||
const pinned = {};
|
||||
|
||||
// TODO replace this with lib-readline?
|
||||
const streamFile = function (path, cb) {
|
||||
const id = getSafeKeyFromPath(path);
|
||||
@@ -133,7 +136,6 @@ Pins.list = function (_done, config) {
|
||||
const ref = {};
|
||||
const pinHandler = createLineHandler(ref, errorHandler);
|
||||
var lines = body.split('\n');
|
||||
stats.lines += lines.length;
|
||||
lines.forEach(pinHandler);
|
||||
handler(ref, id, pinned);
|
||||
cb(void 0, ref);
|
||||
@@ -156,7 +158,7 @@ Pins.list = function (_done, config) {
|
||||
|
||||
scanDirectory(pinPath, function (err, dirs) {
|
||||
if (err) {
|
||||
if (err.code === 'ENOENT') { return void cb(void 0, {}); }
|
||||
if (err.code === 'ENOENT') { return void done(void 0, {}); }
|
||||
return void done(err);
|
||||
}
|
||||
dirs.forEach(function (dir) {
|
||||
@@ -166,21 +168,16 @@ Pins.list = function (_done, config) {
|
||||
if (nested_err) {
|
||||
return void done(err);
|
||||
}
|
||||
stats.dirs++;
|
||||
logs.forEach(function (log) {
|
||||
if (!/\.ndjson$/.test(log.path)) { return; }
|
||||
plan.job(0, function (next) {
|
||||
if (isDone) { return void next(); }
|
||||
streamFile(log.path, function (err, ref) {
|
||||
if (err) { return void done(err); }
|
||||
stats.logs++;
|
||||
|
||||
var set = ref.pins;
|
||||
for (var item in set) {
|
||||
(pinned[item] = pinned[item] || {})[log.id] = 1;
|
||||
if (!pinned.hasOwnProperty(item)) {
|
||||
stats.pinned++;
|
||||
}
|
||||
addUserPinToState(pinned, log.id, item);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user