OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-vdrive commits / 297c8ec

Compact the store before anyone else opens it
RocksDB's footprint carries the write churn of a publish. The CLI compacted separately; publishTree did not, so the CI route left it behind — the first real CI-published drive read 76MB where the same publish into a scratch store compacts to 0.58MB. Timing is the whole point. On a daemon the seeder takes an exclusive lock the moment the drive is announced, so the finally block, after the store closes and before anything else opens it, is the only window in which the churn can be reclaimed without stopping the daemon. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
297c8ec86c670f4a4064ac252b34d5009c2be4ee
by plankatron <fleeky@gmail.com> · 2026-08-25T00:07:26.000Z

Changed files (1) +22 -2

lib/publish.cjs +22 -2
……@@ -14,6 +14,7 @@ const fs = isBare ? require('bare-fs') : require('fs')
1414const idEnc = require('hypercore-id-encoding')
1515const Corestore = require('corestore')
1616const Hyperdrive = require('hyperdrive')
17const CorestoreStorage = require('hypercore-storage')
1718const { ContentStore, build, stats } = require('./storage.cjs')
1819const { saveLocations, loadLocations } = require('./locations.cjs')
1920
……@@ -34,7 +35,8 @@ function writeChunk (ws, buf) {
3435 * @param {object} [opts.corestoreOpts]
3536 * @returns {Promise<{link, key, blocks, files, bytes, pointerBlocks, literalBlocks}>}
3637 */
37async function publishTree ({ storeDir, files, chunk, root, corestoreOpts } = {}) {
38async function publishTree ({ storeDir, files, chunk, root, compact, corestoreOpts } = {}) {
39 let result = null
3840 if (!storeDir) throw new Error('publishTree requires { storeDir }')
3941 if (!Array.isArray(files) || files.length === 0) throw new Error('publishTree requires files')
4042 if (typeof chunk !== 'function') throw new Error('publishTree requires a chunk function')
……@@ -81,7 +83,7 @@ async function publishTree ({ storeDir, files, chunk, root, corestoreOpts } = {}
8183 // re-chunking every published file on boot.
8284 const locations = root ? saveLocations(storeDir, content.locations, root) : 0
8385
84 return {
86 result = {
8587 locations,
8688 priorLocations,
8789 link: 'pear://' + idEnc.normalize(drive.key),
……@@ -92,10 +94,28 @@ async function publishTree ({ storeDir, files, chunk, root, corestoreOpts } = {}
9294 pointerBlocks: stats.putRedirected - before.redirected,
9395 literalBlocks: stats.putStored - before.literal
9496 }
97 return result
9598 } finally {
9699 await drive.close().catch(() => {})
97100 await store.close().catch(() => {})
98101 content.close()
102
103 // Compact BEFORE anyone else opens the store. RocksDB's footprint carries
104 // the write churn of the publish — measured at 76 MB against a 609 KB
105 // steady state — and on a daemon the seeder takes the exclusive lock the
106 // moment the drive is announced, so this is the only window in which it can
107 // be reclaimed without stopping the daemon.
108 if (compact !== false) {
109 try {
110 const s = new CorestoreStorage(storeDir)
111 await s.ready()
112 await s.compact()
113 await s.close()
114 if (result) result.compacted = true
115 } catch {
116 if (result) result.compacted = false
117 }
118 }
99119 }
100120}
101121