dont erase a trail msg redundantly

This commit is contained in:
Andre Staltz 2024-02-20 16:51:06 +02:00
parent 94f5160f07
commit 9075f983d8
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 49 additions and 25 deletions

View File

@ -55,12 +55,12 @@ function initGC(peer, config) {
const done = multicb({ pluck: 1 }) const done = multicb({ pluck: 1 })
/** /**
* @param {string} explanation * @param {string} errExplanation
*/ */
function makeRecCB(explanation) { function makeRecCB(errExplanation) {
const cb = done() const cb = done()
return (/**@type {Error=}*/ err) => { return (/**@type {Error=}*/ err) => {
if (err) debug('%s: %s', explanation, flattenCauseChain(err)) if (err) debug('%s: %s', errExplanation, flattenCauseChain(err))
cb() cb()
} }
} }
@ -70,29 +70,41 @@ function initGC(peer, config) {
if (!rec.msg) continue if (!rec.msg) continue
const { id: msgID, msg } = rec const { id: msgID, msg } = rec
const [purpose, details] = peer.goals.getMsgPurpose(msgID, msg) const [purpose, details] = peer.goals.getMsgPurpose(msgID, msg)
if (purpose === 'goal') continue // don't cleanup switch (purpose) {
if (purpose === 'none') { case 'goal': {
const recCB = makeRecCB('Failed to delete msg when cleaning up') continue // don't cleanup
debug('Deleting msg %s with purpose=none', msgID) }
peer.db.del(msgID, recCB) case 'none': {
waiting = true const recCB = makeRecCB('Failed to delete msg when cleaning up')
} else if (purpose === 'ghost') { debug('Deleting msg %s with purpose=none', msgID)
const { tangleID, span } = details
const recCB = makeRecCB('Failed to delete ghost msg when cleaning up')
// TODO: Could one msg be a ghostable in MANY tangles? Or just one?
debug('Deleting and ghosting msg %s with purpose=ghost', msgID)
peer.db.ghosts.add({ tangleID, msgID, span }, (err) => {
if (err) return recCB(err)
peer.db.del(msgID, recCB) peer.db.del(msgID, recCB)
}) waiting = true
waiting = true continue
} else if (purpose === 'trail') { }
const recCB = makeRecCB('Failed to erase trail msg when cleaning up') case 'ghost': {
debug('Erasing msg %s with purpose=trail', msgID) const { tangleID, span } = details
peer.db.erase(msgID, recCB) const recCB = makeRecCB('Failed to delete ghost msg when cleaning up')
waiting = true // TODO: Could one msg be a ghostable in MANY tangles? Or just one?
} else { debug('Deleting and ghosting msg %s with purpose=ghost', msgID)
cb(new Error('Unreachable')) peer.db.ghosts.add({ tangleID, msgID, span }, (err) => {
if (err) return recCB(err)
peer.db.del(msgID, recCB)
})
waiting = true
continue
}
case 'trail': {
if (!msg.data) continue // it's already erased
const recCB = makeRecCB('Failed to erase trail msg when cleaning up')
debug('Erasing msg %s with purpose=trail', msgID)
peer.db.erase(msgID, recCB)
waiting = true
continue
}
default: {
cb(new Error('Unreachable'))
return
}
} }
} }

View File

@ -52,7 +52,16 @@ test('Feed holes', async (t) => {
alice.goals.set(postFeedID, 'newest-4') alice.goals.set(postFeedID, 'newest-4')
assert('alice set a goal for newest-4 of post feed') assert('alice set a goal for newest-4 of post feed')
// Mock db.erase so we can inspect how many times it was called
const prevErase = alice.db.erase
const calledErase = []
alice.db.erase = (msgID, cb) => {
calledErase.push(msgID)
prevErase(msgID, cb)
}
await p(alice.gc.forceImmediately)() await p(alice.gc.forceImmediately)()
assert.deepEqual(calledErase, [posts[2]], 'erased A2')
assert.deepEqual( assert.deepEqual(
getTexts([...alice.db.msgs()]), getTexts([...alice.db.msgs()]),
@ -60,5 +69,8 @@ test('Feed holes', async (t) => {
'alice has only the end of the feed' 'alice has only the end of the feed'
) )
await p(alice.gc.forceImmediately)()
assert.deepEqual(calledErase, [posts[2]], 'no double erasing')
await p(alice.close)(true) await p(alice.close)(true)
}) })