Skip to content

Commit

Permalink
disable live in level indexes during reindexing
Browse files Browse the repository at this point in the history
  • Loading branch information
staltz committed Jun 29, 2022
1 parent 9e7136f commit 0811219
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions indexes/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module.exports = class Plugin {
constructor(log, dir, name, version, keyEncoding, valueEncoding) {
this.log = log
this.name = name
this.levelPutListeners = []
this.levelDelListeners = []
this.levelBatchListeners = []
this._keyEncoding = keyEncoding
this._valueEncoding = valueEncoding
const debug = Debug('ssb:db2:' + name)
Expand Down Expand Up @@ -76,6 +79,9 @@ module.exports = class Plugin {
// prettier-ignore
if (err3) cb(clarify(err3, 'failed to persist META when flushing'))
else {
if (processedOffsetAtFlush === this.log.since.value) {
this.enableLive()
}
this.offset.set(processedOffsetAtFlush)
cb()
}
Expand Down Expand Up @@ -142,6 +148,7 @@ module.exports = class Plugin {

const subClassReset = this.reset
this.reset = (cb) => {
this.disableLive()
if (subClassReset) subClassReset.call(this)
this.batch = []
this.offset.set(-1)
Expand Down Expand Up @@ -180,6 +187,24 @@ module.exports = class Plugin {
throw new Error('processRecord() is missing an implementation')
}

disableLive() {
this.levelPutListeners = this.level.rawListeners('put')
this.levelDelListeners = this.level.rawListeners('del')
this.levelBatchListeners = this.level.rawListeners('batch')
this.level.removeAllListeners('put')
this.level.removeAllListeners('del')
this.level.removeAllListeners('batch')
}

enableLive() {
for (const fn of this.levelPutListeners) this.level.on('put', fn)
for (const fn of this.levelDelListeners) this.level.on('del', fn)
for (const fn of this.levelBatchListeners) this.level.on('batch', fn)
this.levelPutListeners.length = 0
this.levelDelListeners.length = 0
this.levelBatchListeners.length = 0
}

// used for reindexing encrypted content
indexesContent() {
return true
Expand Down

0 comments on commit 0811219

Please sign in to comment.