diff --git a/benchmark/index.js b/benchmark/index.js index 65c2a688..73c1575d 100644 --- a/benchmark/index.js +++ b/benchmark/index.js @@ -357,6 +357,33 @@ test('initial indexing', async (t) => { await ended.promise }) +test('initial indexing leveldb about-self', async (t) => { + await sleep(500) // some silence to make it easier to read the CPU profiler + const ended = DeferredPromise() + + const sbot = SecretStack({ appKey: caps.shs }) + .use(require('../')) + .use(require('../about-self')) + .call(null, { keys, path: dir }) + + const start = Date.now() + + sbot.db.onDrain('base', () => { + sbot.db.onDrain('aboutSelf', () => { + const duration = Date.now() - start + t.pass(`duration: ${duration}ms`) + fs.appendFileSync( + reportPath, + `| Initial indexing leveldb about-self | ${duration}ms |\n` + ) + updateMaxRAM() + global.gc() + sbot.close(() => ended.resolve()) + }) + }) + + await ended.promise +}) test('initial indexing maxcpu 86', async (t) => { rimraf.sync(indexesPath) diff --git a/indexes/about-self.js b/indexes/about-self.js index b5194c33..c04ed1ac 100644 --- a/indexes/about-self.js +++ b/indexes/about-self.js @@ -1,6 +1,7 @@ const bipf = require('bipf') const pull = require('pull-stream') const pl = require('pull-level') +const fastJson = require('fast-json-stringify') const Plugin = require('./plugin') const bValue = Buffer.from('value') @@ -9,9 +10,33 @@ const bContent = Buffer.from('content') const bType = Buffer.from('type') const bAbout = Buffer.from('about') +const stringify = fastJson({ + title: 'AboutSelf Value', + type: 'object', + properties: { + name: { + type: 'string', + }, + image: { + type: 'string', + }, + description: { + type: 'string', + }, + }, +}) + +const valueEncoding = { + encode: stringify, + decode: JSON.parse, + buffer: false, + type: 'json', +} + // feedId => hydratedAboutObj module.exports = class AboutSelf extends Plugin { constructor(log, dir) { + // super(log, dir, 'aboutSelf', 3, undefined, valueEncoding) super(log, dir, 'aboutSelf', 3, 'json', 'json') this.profiles = {} } @@ -57,7 +82,11 @@ module.exports = class AboutSelf extends Plugin { } updateProfileData(author, content) { - let profile = this.profiles[author] || {} + let profile = this.profiles[author] || { + name: '', + description: '', + image: '', + } if (content.name) profile.name = content.name diff --git a/indexes/plugin.js b/indexes/plugin.js index daa15836..2bd858eb 100644 --- a/indexes/plugin.js +++ b/indexes/plugin.js @@ -117,12 +117,18 @@ module.exports = class Plugin { // doesn't support it, but `level-post` is a dependency in `pull-level`. // Note, this._keyEncoding and this._valueEncoding are strings. get keyEncoding() { - if (encodings[this._keyEncoding]) return encodings[this._keyEncoding] + const e = this._keyEncoding + if (typeof e === 'object' && e) return e + else if (typeof e === 'string' && encodings[e]) return encodings[e] + // if (encodings[this._keyEncoding]) return encodings[this._keyEncoding] else return undefined } get valueEncoding() { - if (encodings[this._valueEncoding]) return encodings[this._valueEncoding] + const e = this._valueEncoding + if (typeof e === 'object' && e) return e + else if (typeof e === 'string' && encodings[e]) return encodings[e] + // if (encodings[this._valueEncoding]) return encodings[this._valueEncoding] else return undefined } diff --git a/package.json b/package.json index 010cc7b1..656271f9 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "binary-search-bounds": "^2.0.4", "bipf": "~1.5.0", "debug": "~4.3.1", + "fast-json-stringify": "^2.5.2", "fastintcompression": "0.0.4", "flumecodec": "0.0.1", "flumelog-offset": "3.4.4",