From b9e46fddd4152b3be7260ebe5f52d4a1f9febb44 Mon Sep 17 00:00:00 2001 From: Dominic Cassidy Date: Tue, 6 Oct 2026 09:37:58 +0300 Subject: [PATCH] Delete the default session's data in deleteCore deleteCore cleared the core's own rows and the data of its named sessions, but not the data of the default session, so blocks, tree nodes, bitfield pages and user data of a deleted core stayed in the store forever. Co-Authored-By: Claude Opus 5.5 --- index.js | 3 +++ test/basic.js | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6cb5d63..2e28113 100644 --- a/index.js +++ b/index.js @@ -598,6 +598,9 @@ class CorestoreStorage { const end = core.core(ptr.corePointer + 1) tx.tryDeleteRange(start, end) + // clear the default session's data, the sessions below only list named ones + tx.tryDeleteRange(core.data(ptr.dataPointer), core.data(ptr.dataPointer + 1)) + if (sessions) { for (const { dataPointer } of sessions) { const start = core.data(dataPointer) diff --git a/test/basic.js b/test/basic.js index cdfdea5..de7cf8e 100644 --- a/test/basic.js +++ b/test/basic.js @@ -2,7 +2,8 @@ const test = require('brittle') const b4a = require('b4a') const tmp = require('test-tmp') const Storage = require('../') -const { create } = require('./helpers') +const { create, writeBlocks, readBlocks } = require('./helpers') +const { core: keys } = require('../lib/keys.js') test('make storage and core', async function (t) { const s = await create(t) @@ -269,3 +270,40 @@ test('can get info from store efficiently', async function (t) { await s.close() }) + +test('deleteCore removes all of its data and leaves other cores alone', async function (t) { + const s = await create(t) + + const a = await s.createCore({ key: b4a.alloc(32, 1), discoveryKey: b4a.alloc(32, 1) }) + const b = await s.createCore({ key: b4a.alloc(32, 2), discoveryKey: b4a.alloc(32, 2) }) + + await writeBlocks(a, 3) + await writeBlocks(b, 3) + + const named = await a.createSession('named', null) + await writeBlocks(named, 2, { pre: 'named-' }) + + const ptr = a.core + const namedDataPointer = named.core.dataPointer + + await named.close() + await a.close() + + await s.deleteCore(ptr) + + t.is(await s.hasCore(b4a.alloc(32, 1)), false) + t.is(await count(s, keys.core(ptr.corePointer), keys.core(ptr.corePointer + 1)), 0) + t.is(await count(s, keys.data(ptr.dataPointer), keys.data(ptr.dataPointer + 1)), 0) + t.is(await count(s, keys.data(namedDataPointer), keys.data(namedDataPointer + 1)), 0) + + t.alike(await readBlocks(b, 3), [b4a.from('block0'), b4a.from('block1'), b4a.from('block2')]) + + await b.close() + await s.close() +}) + +async function count(s, gte, lt) { + let n = 0 + for await (const _ of s.db.iterator({ gte, lt })) n++ + return n +}