Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions lib/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 31 additions & 3 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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) {
Expand Down