From 889d003be49ef4833de13e9ee710fa3677f5a3f0 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 26 Sep 2023 15:35:15 +0300 Subject: [PATCH] cleanup can erase or del based on record purpose --- lib/index.js | 18 ++++++++++++------ test/feed-holes.test.js | 11 ++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7673780..398ac19 100644 --- a/lib/index.js +++ b/lib/index.js @@ -26,17 +26,23 @@ module.exports = { // State const debug = makeDebug('ppppp:gc') + /** + * Deletes messages that don't correspond to any goal. + * @private + */ function cleanup(cb) { debug('cleanup goalless started') const done = multicb({ pluck: 1 }) - let waitingForDels = false + let waiting = false for (const rec of peer.db.records()) { if (!rec.msg) continue - const goals = peer.goals.getByRec(rec) - if (goals.length === 0) { + const purpose = peer.goals.getRecordPurpose(rec) + if (purpose === 'none') { peer.db.del(rec.id, done()) - waitingForDels = true - } else { + waiting = true + } else if (purpose === 'trail') { + peer.db.erase(rec.id, done()) + waiting = true } } function whenEnded(err) { @@ -45,7 +51,7 @@ module.exports = { else debug('cleanup goalless ended') cb() } - if (waitingForDels) done(whenEnded) + if (waiting) done(whenEnded) else whenEnded() } diff --git a/test/feed-holes.test.js b/test/feed-holes.test.js index 50cd3d4..d1f95ef 100644 --- a/test/feed-holes.test.js +++ b/test/feed-holes.test.js @@ -40,25 +40,26 @@ test('feed holes', async (t) => { await p(alice.db.del)(posts[3]) await p(alice.db.del)(posts[4]) await p(alice.db.del)(posts[5]) - await p(alice.db.del)(posts[6]) + await p(alice.db.erase)(posts[6]) // vital as trail from A7 assert('alice deleted the middle part of the feed') assert.deepEqual( getTexts([...alice.db.msgs()]), - ['A0', 'A1', 'A2', 'A7', 'A8', 'A9'], + ['A0', 'A1', 'A2', /* */ 'A7', 'A8', 'A9'], 'alice has the beginning and the end of the feed' ) alice.goals.set(aliceID, 'all') // alice wants her account tangle const postFeedID = alice.db.feed.getID(aliceID, 'post') - alice.goals.set(postFeedID, 'newest-3') - assert('alice set a goal for newest-3 of post feed') + // notice 4 on purpose, because we want to make sure A2 is deleted + alice.goals.set(postFeedID, 'newest-4') + assert('alice set a goal for newest-4 of post feed') await p(alice.gc.forceImmediately)() assert.deepEqual( getTexts([...alice.db.msgs()]), - ['A7', 'A8', 'A9'], + [/* */ 'A7', 'A8', 'A9'], 'alice has only the end of the feed' )