@@ -26,6 +26,7 @@ const {
2626 loadBitsetFile,
2727 safeFilename,
2828 listFiles,
29+ EmptyFile,
2930} = require ( './files' )
3031
3132module . exports = function ( log , indexesPath ) {
@@ -41,6 +42,9 @@ module.exports = function (log, indexesPath) {
4142 const indexes = { }
4243 let isReady = false
4344 let waiting = [ ]
45+ let compacting = false
46+ let compactStartOffset = null
47+ const postCompactReindexPath = path . join ( indexesPath , 'post-compact-reindex' )
4448 const waitingCompaction = [ ]
4549 const coreIndexNames = [ 'seq' , 'timestamp' , 'sequence' ]
4650 const indexingActive = Obv ( ) . set ( 0 )
@@ -87,9 +91,40 @@ module.exports = function (log, indexesPath) {
8791 }
8892
8993 log . compactionProgress ( ( stats ) => {
90- if ( stats . done && waitingCompaction . length > 0 ) {
91- for ( const cb of waitingCompaction ) cb ( stats . holesFound )
92- waitingCompaction . length = 0
94+ if ( typeof stats . startOffset === 'number' && compactStartOffset === null ) {
95+ compactStartOffset = stats . startOffset
96+ }
97+
98+ if ( ! stats . done && ! compacting ) {
99+ compacting = true
100+ EmptyFile . create ( postCompactReindexPath )
101+ } else if ( stats . done && compacting ) {
102+ compacting = false
103+ const offset = compactStartOffset || 0
104+ compactStartOffset = null
105+
106+ if ( stats . sizeDiff > 0 ) {
107+ EmptyFile . exists ( postCompactReindexPath , ( err , exists ) => {
108+ if ( exists ) {
109+ reindex ( offset , ( err ) => {
110+ if ( err ) console . error ( 'reindex jitdb after compact' , err )
111+ conclude ( )
112+ } )
113+ } else {
114+ conclude ( )
115+ }
116+ } )
117+ } else {
118+ conclude ( )
119+ }
120+
121+ function conclude ( ) {
122+ EmptyFile . delete ( postCompactReindexPath , ( ) => {
123+ if ( waitingCompaction . length === 0 ) return
124+ for ( const cb of waitingCompaction ) cb ( stats . holesFound )
125+ waitingCompaction . length = 0
126+ } )
127+ }
93128 }
94129 } )
95130
@@ -1519,6 +1554,8 @@ module.exports = function (log, indexesPath) {
15191554 }
15201555 }
15211556
1557+ if ( index . bitset ) index . bitset . removeRange ( seq , Infinity )
1558+
15221559 index . offset = prevOffset
15231560 }
15241561 }
0 commit comments