Skip to content

Commit

Permalink
system updates indexers when demoted
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Sep 19, 2024
1 parent e4f451f commit a4bcf38
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ module.exports = class SystemView extends ReadyResource {
if (this.pendingIndexers.length > 0) {
for (let i = 0; i < this.pendingIndexers.length; i++) {
if (!b4a.equals(this.pendingIndexers[i], h.key)) continue
this._updateIndexer(h.key, h.length, false, i)
this._updateIndexer(h.key, h.length, true, i)
return true
}
}
Expand All @@ -243,10 +243,10 @@ module.exports = class SystemView extends ReadyResource {
return false
}

_updateIndexer (key, length, isRemoved, i) {
_updateIndexer (key, length, isIndexer, i) {
const hex = b4a.toString(key, 'hex')

if (isRemoved) {
if (!isIndexer) {
const existing = this._indexerMap.get(hex)
if (existing) {
this.indexerUpdate = true
Expand All @@ -260,7 +260,7 @@ module.exports = class SystemView extends ReadyResource {
if (b4a.equals(this.pendingIndexers[i], key)) break
}

if (!isRemoved && length === 0) {
if (isIndexer && length === 0) {
if (i >= this.pendingIndexers.length) this.pendingIndexers.push(key)
return
}
Expand Down Expand Up @@ -290,6 +290,7 @@ module.exports = class SystemView extends ReadyResource {

async add (key, { isIndexer = false, length = this._seenLength(key) } = {}) {
let wasTracked = false
let wasIndexer = false

if (length === 0) { // a bit hacky atm due to cas limitations...
const node = await this._batch.get(key, { valueEncoding: Member, keyEncoding: MEMBERS })
Expand All @@ -310,6 +311,7 @@ module.exports = class SystemView extends ReadyResource {
const n = newer.value

if (!o.isRemoved) wasTracked = true
if (o.isIndexer) wasIndexer = true

if (length === 0 && o.length) length = o.length

Expand All @@ -319,7 +321,7 @@ module.exports = class SystemView extends ReadyResource {

if (!wasTracked) this.members++

if (isIndexer) this._updateIndexer(key, length, false, 0)
if (wasIndexer || isIndexer) this._updateIndexer(key, length, isIndexer, 0)
}

async remove (key) {
Expand All @@ -330,7 +332,7 @@ module.exports = class SystemView extends ReadyResource {
if (isIndexer) break
}

if (isIndexer) this._updateIndexer(key, null, true, 0)
if (isIndexer) this._updateIndexer(key, null, false, 0)

let wasTracked = false

Expand Down

0 comments on commit a4bcf38

Please sign in to comment.