mirror of https://codeberg.org/pzp/pzp-gc.git
clean up feed decay
This commit is contained in:
parent
56ac4f986e
commit
34cf50b320
36
lib/index.js
36
lib/index.js
|
@ -7,13 +7,28 @@ module.exports = {
|
||||||
permissions: {
|
permissions: {
|
||||||
anonymous: {},
|
anonymous: {},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {any} peer
|
||||||
|
* @param {{
|
||||||
|
* gc?: {
|
||||||
|
* maxLogBytes?: number
|
||||||
|
* }
|
||||||
|
* }} config
|
||||||
|
*/
|
||||||
init(peer, config) {
|
init(peer, config) {
|
||||||
|
// Assertions
|
||||||
if (!peer.goals) throw new Error('gc requires the goals plugin')
|
if (!peer.goals) throw new Error('gc requires the goals plugin')
|
||||||
|
if (typeof config.gc?.maxLogBytes !== 'number') {
|
||||||
|
throw new Error('gc requires config.gc.maxLogBytes')
|
||||||
|
}
|
||||||
|
|
||||||
|
// State
|
||||||
const debug = makeDebug('ppppp:gc')
|
const debug = makeDebug('ppppp:gc')
|
||||||
|
|
||||||
function purgeGoallessMsgs(cb) {
|
function cleanup(cb) {
|
||||||
debug('purge goalless msgs')
|
debug('cleanup goalless started')
|
||||||
const done = multicb()
|
const done = multicb({ pluck: 1 })
|
||||||
let waitingForDels = false
|
let waitingForDels = false
|
||||||
for (const rec of peer.db.records()) {
|
for (const rec of peer.db.records()) {
|
||||||
if (!rec.msg) continue
|
if (!rec.msg) continue
|
||||||
|
@ -21,17 +36,22 @@ module.exports = {
|
||||||
if (goals.length === 0) {
|
if (goals.length === 0) {
|
||||||
peer.db.del(rec.id, done())
|
peer.db.del(rec.id, done())
|
||||||
waitingForDels = true
|
waitingForDels = true
|
||||||
|
} else {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (waitingForDels) done(cb)
|
function whenEnded(err) {
|
||||||
else cb()
|
// prettier-ignore
|
||||||
|
if (err) debug('cleanup goalless ended with an error %s', err.message ?? err)
|
||||||
|
else debug('cleanup goalless ended')
|
||||||
|
cb()
|
||||||
|
}
|
||||||
|
if (waitingForDels) done(whenEnded)
|
||||||
|
else whenEnded()
|
||||||
}
|
}
|
||||||
|
|
||||||
function initiate() {}
|
|
||||||
|
|
||||||
function forceImmediately(cb) {
|
function forceImmediately(cb) {
|
||||||
debug('force immediately')
|
debug('force immediately')
|
||||||
purgeGoallessMsgs(cb)
|
cleanup(cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
const test = require('node:test')
|
||||||
|
const assert = require('node:assert')
|
||||||
|
const p = require('node:util').promisify
|
||||||
|
const { createPeer } = require('./util')
|
||||||
|
|
||||||
|
function getTexts(msgs) {
|
||||||
|
return msgs.filter((msg) => msg.data?.text).map((msg) => msg.data.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
test('feed decay', async (t) => {
|
||||||
|
const alice = createPeer({
|
||||||
|
name: 'alice',
|
||||||
|
gc: { maxLogBytes: 100 * 1024 * 1024 },
|
||||||
|
})
|
||||||
|
|
||||||
|
await alice.db.loaded()
|
||||||
|
|
||||||
|
// Alice creates her own account
|
||||||
|
const aliceID = await p(alice.db.account.create)({
|
||||||
|
domain: 'account',
|
||||||
|
_nonce: 'alice',
|
||||||
|
})
|
||||||
|
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
await p(alice.db.feed.publish)({
|
||||||
|
account: aliceID,
|
||||||
|
domain: 'post',
|
||||||
|
data: { text: 'A' + i },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
getTexts([...alice.db.msgs()]),
|
||||||
|
['A0', 'A1', 'A2', 'A3', 'A4'],
|
||||||
|
'alice has the whole 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')
|
||||||
|
|
||||||
|
await p(alice.gc.forceImmediately)()
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
getTexts([...alice.db.msgs()]),
|
||||||
|
['A2', 'A3', 'A4'],
|
||||||
|
'alice has only latest 3 msgs in the feed'
|
||||||
|
)
|
||||||
|
|
||||||
|
await p(alice.close)(true)
|
||||||
|
})
|
|
@ -11,8 +11,11 @@ function getTexts(msgs) {
|
||||||
return msgs.filter((msg) => msg.data?.text).map((msg) => msg.data.text)
|
return msgs.filter((msg) => msg.data?.text).map((msg) => msg.data.text)
|
||||||
}
|
}
|
||||||
|
|
||||||
test('purge an orphan weave', async (t) => {
|
test('orphan weave msgs', async (t) => {
|
||||||
const alice = createPeer({ name: 'alice' })
|
const alice = createPeer({
|
||||||
|
name: 'alice',
|
||||||
|
gc: { maxLogBytes: 100 * 1024 * 1024 },
|
||||||
|
})
|
||||||
|
|
||||||
await alice.db.loaded()
|
await alice.db.loaded()
|
||||||
|
|
||||||
|
@ -27,7 +30,7 @@ test('purge an orphan weave', async (t) => {
|
||||||
keypair: bobKeypair,
|
keypair: bobKeypair,
|
||||||
_nonce: 'bob',
|
_nonce: 'bob',
|
||||||
})
|
})
|
||||||
// Alice creates Bob
|
// Alice creates Carol
|
||||||
const carolID = await p(alice.db.account.create)({
|
const carolID = await p(alice.db.account.create)({
|
||||||
domain: 'account',
|
domain: 'account',
|
||||||
keypair: carolKeypair,
|
keypair: carolKeypair,
|
Loading…
Reference in New Issue