Skip to content

Commit 14c1667

Browse files
committed
keep state file to signal reindex post-compaction
1 parent 7cfc91a commit 14c1667

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

files.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,58 @@ function safeFilename(filename) {
206206
return sanitize(result)
207207
}
208208

209+
const EmptyFile = {
210+
create(filename) {
211+
if (typeof window !== 'undefined') {
212+
// browser
213+
const IdbKvStore = require('idb-kv-store')
214+
const store = new IdbKvStore(filename, { disableBroadcast: true })
215+
store.set('x', 'y', () => {})
216+
} else {
217+
// node.js
218+
const fs = require('fs')
219+
fs.closeSync(fs.openSync(filename, 'w'))
220+
}
221+
},
222+
223+
exists(filename, cb) {
224+
if (typeof window !== 'undefined') {
225+
// browser
226+
const IdbKvStore = require('idb-kv-store')
227+
const store = new IdbKvStore(filename, { disableBroadcast: true })
228+
store.get('x', (err, y) => {
229+
if (err) return cb(null, false)
230+
cb(null, y === 'y')
231+
})
232+
} else {
233+
// node.js
234+
const fs = require('fs')
235+
cb(null, fs.existsSync(filename))
236+
}
237+
},
238+
239+
delete(filename, cb) {
240+
EmptyFile.exists(filename, (err, exists) => {
241+
if (cb) return cb(err)
242+
if (!exists) cb(null)
243+
else EmptyFile._actuallyDelete(filename, cb)
244+
})
245+
},
246+
247+
_actuallyDelete(filename, cb) {
248+
if (typeof window !== 'undefined') {
249+
// browser
250+
const IdbKvStore = require('idb-kv-store')
251+
const store = new IdbKvStore(filename, { disableBroadcast: true })
252+
store.remove('x', cb)
253+
} else {
254+
// node.js
255+
const rimraf = require('rimraf')
256+
cb(null, rimraf.sync(filename))
257+
}
258+
},
259+
}
260+
209261
module.exports = {
210262
saveTypedArrayFile,
211263
loadTypedArrayFile,
@@ -214,5 +266,6 @@ module.exports = {
214266
saveBitsetFile,
215267
loadBitsetFile,
216268
listFiles,
269+
EmptyFile,
217270
safeFilename,
218271
}

index.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
loadBitsetFile,
2727
safeFilename,
2828
listFiles,
29+
EmptyFile,
2930
} = require('./files')
3031

3132
module.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
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"!example.js"
1515
],
1616
"dependencies": {
17-
"atomic-file-rw": "^0.2.1",
17+
"atomic-file-rw": "^0.3.0",
1818
"binary-search-bounds": "^2.0.4",
1919
"bipf": "^1.6.2",
2020
"crc": "3.6.0",
@@ -33,16 +33,17 @@
3333
"pull-stream": "^3.6.14",
3434
"push-stream": "^11.0.0",
3535
"push-stream-to-pull-stream": "^1.0.3",
36+
"rimraf": "^3.0.2",
3637
"sanitize-filename": "^1.6.3",
3738
"traverse": "^0.6.6",
3839
"typedarray-to-buffer": "^4.0.0",
3940
"typedfastbitset": "~0.2.1"
4041
},
4142
"peerDependencies": {
42-
"async-append-only-log": "^4.2.1"
43+
"async-append-only-log": "^4.3.2"
4344
},
4445
"devDependencies": {
45-
"async-append-only-log": "^4.3.1",
46+
"async-append-only-log": "^4.3.2",
4647
"expose-gc": "^1.0.0",
4748
"flumecodec": "0.0.1",
4849
"flumelog-offset": "3.4.4",
@@ -53,7 +54,6 @@
5354
"pretty-bytes": "^5.6.0",
5455
"pretty-quick": "^3.1.0",
5556
"pull-pushable": "^2.2.0",
56-
"rimraf": "^3.0.2",
5757
"ssb-fixtures": "2.2.0",
5858
"ssb-keys": "^8.1.0",
5959
"ssb-ref": "^2.14.3",

0 commit comments

Comments
 (0)