diff --git a/lib/info.js b/lib/info.js index 029d2f2d..f90d1960 100644 --- a/lib/info.js +++ b/lib/info.js @@ -24,14 +24,25 @@ module.exports = class Info { } static async storage(session) { - const { oplog, tree, blocks, bitfield } = session.core try { - return { - oplog: await Info.bytesUsed(oplog.storage), - tree: await Info.bytesUsed(tree.storage), - blocks: await Info.bytesUsed(blocks.storage), - bitfield: await Info.bytesUsed(bitfield.storage) - } + const storage = session.state && session.state.storage + const db = storage && storage.db + const ptr = storage.core && storage.core.dataPointer + if (!db || ptr == null || typeof db.approximateSize !== 'function') return null + + // Keys are lexicographically ordered per core data section, so the byte + // size of a section is the approximate size of its key range. The old + // per-file split is mapped onto the rocksdb sections: the oplog covered + // the core header, its dependencies and its hints. + const { core } = require('hypercore-storage/lib/keys.js') + const opts = { includeMemtables: true } + const [oplog, tree, blocks, bitfield] = await Promise.all([ + db.approximateSize(core.head(ptr), core.block(ptr, 0), opts), + db.approximateSize(core.tree(ptr, 0), core.bitfield(ptr, 0, 0), opts), + db.approximateSize(core.block(ptr, 0), core.tree(ptr, 0), opts), + db.approximateSize(core.bitfield(ptr, 0, 0), core.userData(ptr, ''), opts) + ]) + return { oplog, tree, blocks, bitfield } } catch { return null } diff --git a/test/basic.js b/test/basic.js index d55ada39..e15e6726 100644 --- a/test/basic.js +++ b/test/basic.js @@ -2,6 +2,7 @@ const test = require('brittle') const b4a = require('b4a') const HypercoreStorage = require('hypercore-storage') const crypto = require('hypercore-crypto') +const safetyCatch = require('safety-catch') const Hypercore = require('../') const { create, createStorage, replicate, eventFlush } = require('./helpers') @@ -520,15 +521,42 @@ test('count named session', async function (t) { await core.close() }) -test.skip('storage info', async function (t) { - const core = await create(t) +test('storage info', async function (t) { + const db = await createStorage(t) + const core = new Hypercore(db) + await core.ready() + t.teardown(() => core.close().catch(safetyCatch), { order: 1 }) + await core.append(['a', 'b', 'c', 'd', 'e', 'f']) const info = await core.info({ storage: true }) - t.snapshot(info.storage) + t.ok(info.storage !== null) + // appending writes every section: oplog header entries, tree nodes, the + // blocks themselves and bitfield pages - none may report zero + for (const section of ['oplog', 'tree', 'blocks', 'bitfield']) { + t.ok(info.storage[section] > 0, section + ' measures its own keys') + } + + // a large block lands in the blocks range + await core.append(b4a.alloc(64 * 1024, 1)) + const grown = await core.info({ storage: true }) + t.ok(grown.storage.blocks > info.storage.blocks, 'an appended block grows the blocks section') + + // a second core on the same storage must not measure the first core's keys + const other = new Hypercore(db, crypto.keyPair().publicKey) + await other.ready() + t.teardown(() => other.close().catch(safetyCatch), { order: 1 }) + const otherInfo = await other.info({ storage: true }) + t.ok(otherInfo.storage !== null) + for (const section of ['oplog', 'tree', 'blocks', 'bitfield']) { + t.ok(otherInfo.storage[section] < grown.storage[section], section + " range excludes another core's keys") + } + + await other.close() await core.close() + await db.close() }) test('storage info, off by default', async function (t) {