From 3b6eaebdf94ea0fd15a92f59993bc8f3b33bb5a7 Mon Sep 17 00:00:00 2001 From: Kira Oakley Date: Fri, 29 May 2020 17:34:45 -0700 Subject: [PATCH] fix: add clearIndex lifecycle to channel-membership --- views/channel-membership.js | 26 ++++++++++++++++++++++++-- views/moderation.js | 1 + 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/views/channel-membership.js b/views/channel-membership.js index ade4c67..a4fb220 100644 --- a/views/channel-membership.js +++ b/views/channel-membership.js @@ -1,4 +1,6 @@ -var EventEmitter = require('events').EventEmitter +const EventEmitter = require('events').EventEmitter +const pump = require('pump') +const Writable = require('readable-stream').Writable /* view data structure, the value (1) doesn't matter @@ -14,7 +16,7 @@ module.exports = function (lvl) { var events = new EventEmitter() return { - maxBatch: 100, + maxBatch: 500, map: function (msgs, next) { // 1. go over each msg // 2. check if it's a leave/join msg (skip if not) @@ -123,6 +125,26 @@ module.exports = function (lvl) { else cb(null, Buffer.from(state, 'base64')) }) }, + + clearIndex: function (cb) { + var batch = [] + var maxSize = 5000 + lvl.open(function () { + pump(lvl.createKeyStream(), new Writable({ + objectMode: true, + write: function (key, enc, next) { + batch.push({ type: 'del', key }) + if (batch.length >= maxSize) { + lvl.batch(batch, next) + } else next() + }, + final: function (next) { + if (batch.length > 0) lvl.batch(batch, next) + else next() + } + }), cb) + }) + } } } diff --git a/views/moderation.js b/views/moderation.js index 35c485a..657fdb2 100644 --- a/views/moderation.js +++ b/views/moderation.js @@ -126,6 +126,7 @@ module.exports = function (cabal, authDb, infoDb) { }) return { + maxBatch: 500, map: wrap(map), api: { events: events,