Skip to content

Commit bf8defa

Browse files
author
Mauve Signweaver
committed
feat: core.purge with rocksdb storage
1 parent d134aba commit bf8defa

5 files changed

Lines changed: 37 additions & 21 deletions

File tree

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,6 @@ class Hypercore extends EventEmitter {
867867
}
868868

869869
async purge() {
870-
await this._closeAllSessions(null)
871870
await this.core.purge()
872871
}
873872

lib/core.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,17 @@ module.exports = class Core {
909909
for (const s of weakSessions) s.close().catch(noop)
910910
}
911911

912+
async purge() {
913+
if (this.opened === false) await this.opening
914+
const tx = this.storage.write()
915+
tx.deleteTreeNodeRange(0, -1)
916+
tx.deleteBlockRange(0, -1)
917+
this.bitfield.clear(tx)
918+
await tx.flush()
919+
await this.closeAllSessions()
920+
await this.close()
921+
}
922+
912923
async _close() {
913924
if (this.opened === false) await this.opening
914925
if (this.hasSession() === true) throw new Error('Cannot close while sessions are open')

test/all.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function runTests() {
2626
await import('./move-to.js')
2727
await import('./mutex.js')
2828
await import('./preload.js')
29-
// await import('./purge.js') // todo: implement purge
29+
await import('./purge.js')
3030
await import('./push.js')
3131
await import('./remote-bitfield.js')
3232
await import('./remote-length.js')

test/helpers/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,9 @@ exports.replicateDebugStream = function replicate(a, b, t, opts = {}) {
109109
exports.eventFlush = async function eventFlush() {
110110
await new Promise((resolve) => setImmediate(resolve))
111111
}
112+
113+
exports.toArray = async function toArray(stream) {
114+
const all = []
115+
for await (const data of stream) all.push(data)
116+
return all
117+
}

test/purge.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
const test = require('brittle')
2-
const fs = require('fs')
3-
const Path = require('path')
42

5-
const Hypercore = require('..')
3+
const Hypercore = require('../')
4+
const { createStorage, toArray } = require('./helpers')
65

76
test('basic purge', async function (t) {
87
const dir = await t.tmp()
9-
const core = new Hypercore(dir)
8+
const storage = await createStorage(t, dir)
9+
const core = new Hypercore(storage)
10+
1011
await core.append(['a', 'b', 'c'])
1112

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

17-
t.is(fs.existsSync(oplogLoc), true)
18-
t.is(fs.existsSync(treeLoc), true)
19-
t.is(fs.existsSync(bitfieldLoc), true)
20-
t.is(fs.existsSync(dataLoc), true)
21-
t.is(fs.readdirSync(dir).length, 4) // Sanity check
16+
const discoveryKey = core.discoveryKey
2217

2318
await core.purge()
2419

25-
t.is(core.closed, true)
26-
t.is(fs.existsSync(oplogLoc), false)
27-
t.is(fs.existsSync(treeLoc), false)
28-
t.is(fs.existsSync(bitfieldLoc), false)
29-
t.is(fs.existsSync(dataLoc), false)
30-
t.is(fs.readdirSync(dir).length, 0) // Nothing remains
20+
const reopenedStorage = await createStorage(t, dir)
21+
const coreStorage = await reopenedStorage.resumeCore(discoveryKey)
22+
23+
const allBlocks = await toArray(coreStorage.createBlockStream())
24+
t.is(allBlocks.length, 0)
25+
26+
const allTreeNodes = await toArray(coreStorage.createTreeNodeStream())
27+
t.is(allTreeNodes.length, 0)
28+
29+
const allBitfieldPages = await toArray(coreStorage.createBitfieldStream())
30+
t.is(allBitfieldPages.length, 0)
3131
})
3232

3333
test('purge closes all sessions', async function (t) {

0 commit comments

Comments
 (0)