From d9714ffc8b939289774994c894de2a3d68a28a4e Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Sun, 24 Apr 2022 17:29:07 +0300 Subject: [PATCH] post-compaction reindex also also resets state in memory --- indexes/about-self.js | 4 ++++ indexes/base.js | 4 ++++ indexes/plugin.js | 2 ++ package.json | 2 +- test/compaction.js | 53 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/indexes/about-self.js b/indexes/about-self.js index ce4082c8..a6de1826 100644 --- a/indexes/about-self.js +++ b/indexes/about-self.js @@ -68,6 +68,10 @@ module.exports = class AboutSelf extends Plugin { return true } + reset() { + this.profiles = {} + } + updateProfileData(author, content) { let profile = this.profiles[author] || {} diff --git a/indexes/base.js b/indexes/base.js index fa2c122e..869ae545 100644 --- a/indexes/base.js +++ b/indexes/base.js @@ -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() { diff --git a/indexes/plugin.js b/indexes/plugin.js index d424e0d3..80a31002 100644 --- a/indexes/plugin.js +++ b/indexes/plugin.js @@ -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 diff --git a/package.json b/package.json index 6f1a743f..611041fd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/compaction.js b/test/compaction.js index 24e13907..1beaf7c3 100644 --- a/test/compaction.js +++ b/test/compaction.js @@ -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() +})