hypercore-storage 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,10 @@ const { CorestoreTX, CoreTX, CorestoreRX } = require('../../lib/tx.js')
12
12
  const EMPTY_NODE = b4a.alloc(40)
13
13
  const EMPTY_PAGE = b4a.alloc(4096)
14
14
 
15
+ let TREE_01_SKIP = null
16
+ let TREE_04_SKIP = null
17
+ let TREE_16_SKIP = null
18
+
15
19
  class CoreListStream extends Readable {
16
20
  constructor (storage) {
17
21
  super()
@@ -222,7 +226,7 @@ async function store (storage, { version, dryRun = true, gc = true }) {
222
226
  if (gc) await rm(primaryKeyFile)
223
227
  }
224
228
 
225
- class Slicer {
229
+ class TreeSlicer {
226
230
  constructor () {
227
231
  this.buffer = null
228
232
  this.offset = 0
@@ -238,7 +242,48 @@ class Slicer {
238
242
  this.offset += data.byteLength
239
243
  }
240
244
 
241
- take (len) {
245
+ skip () {
246
+ let skipped = 0
247
+
248
+ if (TREE_01_SKIP === null) {
249
+ TREE_16_SKIP = b4a.alloc(16 * 40 * 100)
250
+ TREE_04_SKIP = TREE_16_SKIP.subarray(0, 4 * 40 * 100)
251
+ TREE_01_SKIP = TREE_16_SKIP.subarray(0, 1 * 40 * 100)
252
+ }
253
+
254
+ while (true) {
255
+ if (this.buffer.byteLength >= TREE_16_SKIP.byteLength) {
256
+ if (b4a.equals(this.buffer.subarray(0, TREE_16_SKIP.byteLength), TREE_16_SKIP)) {
257
+ this.buffer = this.buffer.subarray(TREE_16_SKIP.byteLength)
258
+ skipped += 1600
259
+ continue
260
+ }
261
+ }
262
+
263
+ if (this.buffer.byteLength >= TREE_04_SKIP.byteLength) {
264
+ if (b4a.equals(this.buffer.subarray(0, TREE_04_SKIP.byteLength), TREE_04_SKIP)) {
265
+ this.buffer = this.buffer.subarray(TREE_04_SKIP.byteLength)
266
+ skipped += 400
267
+ continue
268
+ }
269
+ }
270
+
271
+ if (this.buffer.byteLength >= TREE_01_SKIP.byteLength) {
272
+ if (b4a.equals(this.buffer.subarray(0, TREE_01_SKIP.byteLength), TREE_01_SKIP)) {
273
+ this.buffer = this.buffer.subarray(TREE_01_SKIP.byteLength)
274
+ skipped += 100
275
+ continue
276
+ }
277
+ }
278
+ break
279
+ }
280
+
281
+ return skipped
282
+ }
283
+
284
+ take () {
285
+ const len = 40
286
+
242
287
  if (len <= this.size) {
243
288
  const chunk = this.buffer.subarray(0, len)
244
289
  this.buffer = this.buffer.subarray(len)
@@ -273,7 +318,7 @@ async function core (core, { version, dryRun = true, gc = true }) {
273
318
  const oplog = await readOplog(files.oplog)
274
319
  if (!oplog) throw new Error('No oplog available for ' + files.oplog)
275
320
 
276
- const treeData = new Slicer()
321
+ const treeData = new TreeSlicer()
277
322
 
278
323
  let treeIndex = 0
279
324
 
@@ -281,19 +326,23 @@ async function core (core, { version, dryRun = true, gc = true }) {
281
326
  for await (const data of fs.createReadStream(files.tree)) {
282
327
  treeData.push(data)
283
328
 
284
- const write = core.write()
329
+ let write = null
285
330
 
286
331
  while (true) {
287
- const buf = treeData.take(40)
332
+ const skip = treeData.skip()
333
+ treeIndex += skip
334
+
335
+ const buf = treeData.take()
288
336
  if (buf === null) break
289
337
 
290
338
  const index = treeIndex++
291
339
  if (b4a.equals(buf, EMPTY_NODE)) continue
292
340
 
341
+ if (write === null) write = core.write()
293
342
  write.putTreeNode(decodeTreeNode(index, buf))
294
343
  }
295
344
 
296
- await write.flush()
345
+ if (write !== null) await write.flush()
297
346
  }
298
347
  }
299
348
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",