Skip to content

Commit

Permalink
post-compaction reindex also also resets state in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Apr 24, 2022
1 parent 20af4f4 commit d9714ff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions indexes/about-self.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ module.exports = class AboutSelf extends Plugin {
return true
}

reset() {
this.profiles = {}
}

updateProfileData(author, content) {
let profile = this.profiles[author] || {}

Expand Down
4 changes: 4 additions & 0 deletions indexes/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ module.exports = function makeBaseIndex(privateIndex) {
this.privateIndex.saveIndexes(cb)
}

reset() {
this.authorLatest.clear()
}

// pull-stream where each item is { key, value }
// where key is the authorId and value is { offset, sequence }
getAllLatest() {
Expand Down
2 changes: 2 additions & 0 deletions indexes/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ module.exports = class Plugin {
}
})

const subClassReset = this.reset
this.reset = (cb) => {
if (subClassReset) subClassReset.call(this)
this.level.clear(() => {
processedSeq = 0
processedOffset = -1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"operators/*.js"
],
"dependencies": {
"async-append-only-log": "^4.2.1",
"async-append-only-log": "^4.2.2",
"atomic-file-rw": "^0.2.1",
"binary-search-bounds": "^2.0.4",
"bipf": "^1.5.4",
Expand Down
53 changes: 53 additions & 0 deletions test/compaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,56 @@ test('queries are queued if compaction is in progress', async (t) => {
await pify(sbot.close)(true)
t.end()
})

test('post-compaction reindex resets state in memory too', async (t) => {
t.timeoutAfter(20e3)

const dir = '/tmp/ssb-db2-compaction3'

rimraf.sync(dir)
mkdirp.sync(dir)

const author = ssbKeys.loadOrCreateSync(path.join(dir, 'secret'))

const sbot = SecretStack({ appKey: caps.shs })
.use(require('../'))
.use(require('../about-self'))
.call(null, {
keys: author,
path: dir,
})

const msg1 = await pify(sbot.db.publish)({
type: 'about',
about: author.id,
name: 'Alice',
})
t.pass('published name about')
const msg2 = await pify(sbot.db.publish)({
type: 'about',
about: author.id,
description: 'In Wonderland',
})
t.pass('published description about')

await pify(sbot.db.onDrain)('aboutSelf')

const profileBefore = sbot.db.getIndex('aboutSelf').getProfile(author.id)
t.equal(profileBefore.name, 'Alice')
t.equal(profileBefore.description, 'In Wonderland')

await pify(sbot.db.del)(msg2.key)
t.pass('deleted description about')

await pify(sbot.db.compact)()
t.pass('compacted the log')

await pify(setTimeout)(1000)

const profileAfter = sbot.db.getIndex('aboutSelf').getProfile(author.id)
t.equal(profileAfter.name, 'Alice')
t.notOk(profileAfter.description)

await pify(sbot.close)(true)
t.end()
})

0 comments on commit d9714ff

Please sign in to comment.