Skip to content
Open
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
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,6 @@ class Hypercore extends EventEmitter {
}

async purge() {
await this._closeAllSessions(null)
await this.core.purge()
}

Expand Down
11 changes: 11 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,17 @@ module.exports = class Core {
for (const s of weakSessions) s.close().catch(noop)
}

async purge() {
if (this.opened === false) await this.opening
const tx = this.storage.write()
tx.deleteTreeNodeRange(0, -1)
tx.deleteBlockRange(0, -1)
this.bitfield.clear(tx)
await tx.flush()
await this.closeAllSessions()
await this.close()
}

async _close() {
if (this.opened === false) await this.opening
if (this.hasSession() === true) throw new Error('Cannot close while sessions are open')
Expand Down
2 changes: 1 addition & 1 deletion test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function runTests() {
await import('./move-to.js')
await import('./mutex.js')
await import('./preload.js')
// await import('./purge.js') // todo: implement purge
await import('./purge.js')
await import('./push.js')
await import('./remote-bitfield.js')
await import('./remote-length.js')
Expand Down
6 changes: 6 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ exports.replicateDebugStream = function replicate(a, b, t, opts = {}) {
exports.eventFlush = async function eventFlush() {
await new Promise((resolve) => setImmediate(resolve))
}

exports.toArray = async function toArray(stream) {
const all = []
for await (const data of stream) all.push(data)
return all
}
39 changes: 20 additions & 19 deletions test/purge.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
const test = require('brittle')
const fs = require('fs')
const Path = require('path')

const Hypercore = require('..')
const Hypercore = require('../')
const { createStorage, toArray } = require('./helpers')

test('basic purge', async function (t) {
const dir = await t.tmp()
const core = new Hypercore(dir)
const storage = await createStorage(t, dir)
const core = new Hypercore(storage)

await core.append(['a', 'b', 'c'])

const oplogLoc = Path.join(dir, 'oplog')
const treeLoc = Path.join(dir, 'tree')
const bitfieldLoc = Path.join(dir, 'bitfield')
const dataLoc = Path.join(dir, 'data')
// Sanity check for core having data
t.is(core.length, 3)

t.is(fs.existsSync(oplogLoc), true)
t.is(fs.existsSync(treeLoc), true)
t.is(fs.existsSync(bitfieldLoc), true)
t.is(fs.existsSync(dataLoc), true)
t.is(fs.readdirSync(dir).length, 4) // Sanity check
const discoveryKey = core.discoveryKey

await core.purge()

t.is(core.closed, true)
t.is(fs.existsSync(oplogLoc), false)
t.is(fs.existsSync(treeLoc), false)
t.is(fs.existsSync(bitfieldLoc), false)
t.is(fs.existsSync(dataLoc), false)
t.is(fs.readdirSync(dir).length, 0) // Nothing remains
const reopenedStorage = await createStorage(t, dir)
const coreStorage = await reopenedStorage.resumeCore(discoveryKey)
t.teardown(() => reopenedStorage.close())

const allBlocks = await toArray(coreStorage.createBlockStream())
t.is(allBlocks.length, 0)

const allTreeNodes = await toArray(coreStorage.createTreeNodeStream())
t.is(allTreeNodes.length, 0)

const allBitfieldPages = await toArray(coreStorage.createBitfieldStream())
t.is(allBitfieldPages.length, 0)
})

test('purge closes all sessions', async function (t) {
Expand Down
Loading