feat(storage): Linked documents - get total size

This commit is contained in:
yflory 2026-06-29 17:16:56 +02:00
parent e1edac8cb1
commit 2098a6932c
2 changed files with 29 additions and 2 deletions

View File

@ -44,7 +44,12 @@ Linked.getLinkedDocuments = (Env, data, cb) => {
});
};
Linked.listLinkedDocuments = (Env, channel, cb) => {
Linked.listLinkedDocuments = (Env, channel, _cb) => {
const cb = Util.mkAsync(_cb);
if (channel.length !== HK.STANDARD_CHANNEL_LENGTH) {
return void cb(void 0, []);
}
const list = new Set();
Linked.getLinkedDocuments(Env, { channel }, (err, json) => {
if (err) { return void cb(err); }
@ -54,7 +59,7 @@ Linked.listLinkedDocuments = (Env, channel, cb) => {
if (!Array.isArray(data)) { return; }
// Media or channel:
if (type !== 'checkpoints') {
data.forEach(list.add);
data.forEach(id => { list.add(id); });
return;
}
// Checkpoint:

View File

@ -7,6 +7,7 @@ const Core = require("./core");
const Pinning = module.exports;
const Util = require("../common-util");
const nThen = require("nthen");
const Linked = require('./linked');
const escapeKeyCharacters = Util.escapeKeyCharacters;
const unescapeKeyCharacters = Util.unescapeKeyCharacters;
@ -74,6 +75,25 @@ var getChannelList = Pinning.getChannelList = function (Env, safeKey, _cb) {
});
};
var addLinkedDocuments = (Env, channels, cb) => {
var arr = Array.from(channels);
var n = nThen;
arr.forEach(chan => {
// For each channel, add their linked documents
n = n(w => {
Linked.listLinkedDocuments(Env, chan, w((err, linked) => {
if (err || !linked) { return; }
linked.forEach(id => {
channels.add(id);
});
}));
}).nThen;
});
n(() => {
cb();
});
};
Pinning.getTotalSize = function (Env, safeKey, cb) {
var unsafeKey = unescapeKeyCharacters(safeKey);
var limit = Env.limits[unsafeKey];
@ -108,6 +128,8 @@ Pinning.getTotalSize = function (Env, safeKey, cb) {
}));
});
}
}).nThen(function (waitFor) {
addLinkedDocuments(Env, channels, waitFor());
}).nThen(function () {
Env.getTotalSize(Array.from(channels), done);
});