rename ghosts.add() opts.max to opts.span

This commit is contained in:
Andre Staltz 2023-10-26 10:42:08 +03:00
parent 4fff37ad02
commit 3fccd4d661
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 21 additions and 21 deletions

View File

@ -118,23 +118,23 @@ class Ghosts {
* @param {string} tangleID
* @param {string} msgID
* @param {number} depth
* @param {number} max
* @param {number} span
* @param {CB<void>} cb
*/
save(tangleID, msgID, depth, max, cb) {
save(tangleID, msgID, depth, span, cb) {
this.#loaded.onReady(() => {
if (!this.#maps.has(tangleID)) this.#maps.set(tangleID, new Map())
const map = /** @type {Map<string, number>} */ (this.#maps.get(tangleID))
const newMap = new Map(map)
newMap.set(msgID, depth)
// Garbage collect any ghost smaller than largestDepth - max
// Garbage collect any ghost smaller than largestDepth - span
let largestDepth = -1
for (const depth of newMap.values()) {
if (depth > largestDepth) largestDepth = depth
}
for (const [x, depth] of newMap.entries()) {
if (depth <= largestDepth - max) newMap.delete(x)
if (depth <= largestDepth - span) newMap.delete(x)
}
atomic.writeFile(

View File

@ -945,7 +945,7 @@ function initDB(peer, config) {
}
/**
* @param {{ tangleID: MsgID; msgID: MsgID; max: number; }} opts
* @param {{ tangleID: MsgID; msgID: MsgID; span: number; }} opts
* @param {CB<void>} cb
*/
function addGhost(opts, cb) {
@ -955,8 +955,8 @@ function initDB(peer, config) {
// prettier-ignore
if (!opts.msgID || typeof opts.msgID !== 'string') return cb(new Error('ghosts.add() requires msgID of the deleted msg in `opts.msgID`'))
// prettier-ignore
if (!opts.max || typeof opts.max !== 'number') return cb(new Error('ghosts.add() requires max depth distance in `opts.max`'))
const { tangleID, msgID, max } = opts
if (!opts.span || typeof opts.span !== 'number') return cb(new Error('ghosts.add() requires span in `opts.span`'))
const { tangleID, msgID, span} = opts
const rec = getRecord(msgID)
if (!rec) return cb()
if (!rec.msg) return cb()
@ -965,7 +965,7 @@ function initDB(peer, config) {
if (!tangleData) return cb(new Error(`ghosts.add() opts.msg "${opts.msgID}" does not belong to opts.tangle "${opts.tangleID}"`))
const depth = tangleData.depth
ghosts.save(tangleID, msgID, depth, max, (err) => {
ghosts.save(tangleID, msgID, depth, span, (err) => {
// prettier-ignore
if (err) cb(new Error('ghosts.add() failed to save to disk', { cause: err }))
else cb()

View File

@ -21,7 +21,7 @@ test('ghosts.add, ghosts.get, ghosts.getMinDepth', async (t) => {
await peer.db.loaded()
const account = await p(peer.db.account.create)({ domain: 'person' })
const MAX = 5
const SPAN = 5
let msgIDs = []
for (let i = 0; i < 10; i++) {
@ -32,27 +32,27 @@ test('ghosts.add, ghosts.get, ghosts.getMinDepth', async (t) => {
})
msgIDs.push(rec.id)
}
const feedID = peer.db.feed.getID(account, 'post')
const tangleID = peer.db.feed.getID(account, 'post')
const ghosts0 = peer.db.ghosts.get(feedID)
const ghosts0 = peer.db.ghosts.get(tangleID)
assert.deepEqual(ghosts0, [], 'no ghosts so far')
await p(peer.db.ghosts.add)({ msgID: msgIDs[0], tangleID: feedID, max: MAX })
await p(peer.db.ghosts.add)({ msgID: msgIDs[1], tangleID: feedID, max: MAX })
await p(peer.db.ghosts.add)({ msgID: msgIDs[2], tangleID: feedID, max: MAX })
await p(peer.db.ghosts.add)({ msgID: msgIDs[3], tangleID: feedID, max: MAX })
await p(peer.db.ghosts.add)({ msgID: msgIDs[4], tangleID: feedID, max: MAX })
await p(peer.db.ghosts.add)({ msgID: msgIDs[0], tangleID, span: SPAN })
await p(peer.db.ghosts.add)({ msgID: msgIDs[1], tangleID, span: SPAN })
await p(peer.db.ghosts.add)({ msgID: msgIDs[2], tangleID, span: SPAN })
await p(peer.db.ghosts.add)({ msgID: msgIDs[3], tangleID, span: SPAN })
await p(peer.db.ghosts.add)({ msgID: msgIDs[4], tangleID, span: SPAN })
const ghostsA = peer.db.ghosts.get(feedID)
const ghostsA = peer.db.ghosts.get(tangleID)
assert.deepEqual(ghostsA, msgIDs.slice(0, 5), 'ghosts so far')
const depthA = peer.db.ghosts.getMinDepth(feedID)
const depthA = peer.db.ghosts.getMinDepth(tangleID)
assert.equal(depthA, 1, 'min depth so far')
await p(peer.db.ghosts.add)({ msgID: msgIDs[5], tangleID: feedID, max: MAX })
await p(peer.db.ghosts.add)({ msgID: msgIDs[5], tangleID, span: SPAN })
const ghostsB = peer.db.ghosts.get(feedID)
const ghostsB = peer.db.ghosts.get(tangleID)
assert.deepEqual(ghostsB, msgIDs.slice(1, 6), 'ghosts so far')
const depthB = peer.db.ghosts.getMinDepth(feedID)
const depthB = peer.db.ghosts.getMinDepth(tangleID)
assert.equal(depthB, 2, 'min depth so far')
await p(peer.close)(true)