getTangleDeletablesAndErasables()

This commit is contained in:
Andre Staltz 2023-04-15 16:42:27 +03:00
parent fd7b76808b
commit 01cb78c65a
3 changed files with 18 additions and 7 deletions

View File

@ -273,6 +273,8 @@ exports.init = function initDB(peer, config) {
function del(msgId, cb) { function del(msgId, cb) {
const rec = getRecord(msgId) const rec = getRecord(msgId)
if (!rec) return cb()
if (!rec.msg) return cb()
const { offset, size, seq } = rec.misc const { offset, size, seq } = rec.misc
recs[rec.misc.seq] = { misc: { offset, size, seq } } recs[rec.misc.seq] = { misc: { offset, size, seq } }
log.onDrain(() => { log.onDrain(() => {
@ -282,11 +284,19 @@ exports.init = function initDB(peer, config) {
function erase(msgId, cb) { function erase(msgId, cb) {
const rec = getRecord(msgId) const rec = getRecord(msgId)
if (!rec) return cb()
if (!rec.msg) return cb()
if (!rec.msg.content) return cb()
recs[rec.misc.seq].msg = FeedV1.erase(rec.msg) recs[rec.misc.seq].msg = FeedV1.erase(rec.msg)
// FIXME: persist this change to disk!! Not supported by AAOL yet // FIXME: persist this change to disk!! Not supported by AAOL yet
cb() cb()
} }
function getTangleDeletablesAndErasables(tangleId, msgHash) {
const tangle = new Tangle(tangleId, records())
return tangle.getDeletablesAndErasables(msgHash)
}
function* msgs() { function* msgs() {
for (let i = 0; i < recs.length; i++) { for (let i = 0; i < recs.length; i++) {
const rec = recs[i] const rec = recs[i]
@ -313,6 +323,7 @@ exports.init = function initDB(peer, config) {
del, del,
erase, erase,
onRecordAdded, onRecordAdded,
getTangleDeletablesAndErasables,
msgs, msgs,
records, records,

View File

@ -186,13 +186,13 @@ class Tangle {
} }
getDeletablesAndErasables(msgHash) { getDeletablesAndErasables(msgHash) {
const emptyables = this.#shortestPathToRoot(msgHash) const erasables = this.#shortestPathToRoot(msgHash)
const sorted = this.topoSort() const sorted = this.topoSort()
const index = sorted.indexOf(msgHash) const index = sorted.indexOf(msgHash)
const deletables = sorted.filter( const deletables = sorted.filter(
(msgHash, i) => i < index && !emptyables.includes(msgHash) (msgHash, i) => i < index && !erasables.includes(msgHash)
) )
return { deletables, emptyables } return { deletables, erasables }
} }
getMaxDepth() { getMaxDepth() {

View File

@ -163,19 +163,19 @@ test('Tangle.getLipmaaSet', (t) => {
test('Tangle.getDeletablesAndErasables basic', (t) => { test('Tangle.getDeletablesAndErasables basic', (t) => {
const tangle = new Tangle(rootPost, peer.db.records()) const tangle = new Tangle(rootPost, peer.db.records())
const { deletables, emptyables } = tangle.getDeletablesAndErasables(reply2A) const { deletables, erasables } = tangle.getDeletablesAndErasables(reply2A)
t.deepEquals(deletables, [reply1Hi], 'deletables') t.deepEquals(deletables, [reply1Hi], 'deletables')
t.deepEquals(emptyables, [reply1Lo, rootPost], 'emptyables') t.deepEquals(erasables, [reply1Lo, rootPost], 'erasables')
t.end() t.end()
}) })
test('Tangle.getDeletablesAndErasables with lipmaa', (t) => { test('Tangle.getDeletablesAndErasables with lipmaa', (t) => {
const tangle = new Tangle(rootPost, peer.db.records()) const tangle = new Tangle(rootPost, peer.db.records())
const { deletables, emptyables } = tangle.getDeletablesAndErasables(reply3Lo) const { deletables, erasables } = tangle.getDeletablesAndErasables(reply3Lo)
t.deepEquals(deletables, [reply1Lo, reply1Hi, reply2A], 'deletables') t.deepEquals(deletables, [reply1Lo, reply1Hi, reply2A], 'deletables')
t.deepEquals(emptyables, [rootPost], 'emptyables') t.deepEquals(erasables, [rootPost], 'erasables')
t.end() t.end()
}) })