Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit 1328e3c

Browse files
authored
Add sparse option, sans core.contiguousByteLength (#100)
* Add `sparse` option, sans `core.contiguousByteLength` * Don't inherit `sparse` * Start continuous replication in non-sparse mode * Assert tree length * Adjust description * Fix typo
1 parent 44f36e2 commit 1328e3c

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Note that `tree`, `data`, and `bitfield` are normally heavily sparse files.
6262
{
6363
createIfMissing: true, // create a new Hypercore key pair if none was present in storage
6464
overwrite: false, // overwrite any old Hypercore that might already exist
65+
sparse: true, // enable sparse mode, counting unavailable blocks towards core.length and core.byteLength
6566
valueEncoding: 'json' | 'utf-8' | 'binary', // defaults to binary
6667
encodeBatch: batch => { ... }, // optionally apply an encoding to complete batches
6768
keyPair: kp, // optionally pass the public key and secret key as a key pair
@@ -235,13 +236,13 @@ Buffer containing the optional block encryption key of this core. Will be `null`
235236

236237
#### `core.length`
237238

238-
How many blocks of data are available on this core?
239+
How many blocks of data are available on this core? If `sparse: false`, this will equal `core.contiguousLength`.
239240

240241
Populated after `ready` has been emitted. Will be `0` before the event.
241242

242243
#### `core.byteLength`
243244

244-
How much data is available on this core in bytes?
245+
How much data is available on this core in bytes? If `sparse: false`, this will equal `core.contiguousByteLength`.
245246

246247
Populated after `ready` has been emitted. Will be `0` before the event.
247248

@@ -251,6 +252,12 @@ How many blocks are contiguously available starting from the first block of this
251252

252253
Populated after `ready` has been emitted. Will be `0` before the event.
253254

255+
#### `core.contiguousByteLength`
256+
257+
How much data is contiguously available starting from the first block of this core?
258+
259+
Populated after `ready` has been emitted. Will be `0` before the event.
260+
254261
#### `core.fork`
255262

256263
What is the current fork id of this core?

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module.exports = class Hypercore extends EventEmitter {
6666
this.opened = false
6767
this.closed = false
6868
this.snapshotted = !!opts.snapshot
69+
this.sparse = opts.sparse !== false
6970
this.sessions = opts._sessions || [this]
7071
this.auth = opts.auth || null
7172
this.autoClose = !!opts.autoClose
@@ -271,6 +272,9 @@ module.exports = class Hypercore extends EventEmitter {
271272
this.encodeBatch = opts.encodeBatch
272273
}
273274

275+
// Start continous replication if not in sparse mode.
276+
if (!this.sparse) this.download({ start: 0, end: -1 })
277+
274278
// This is a hidden option that's only used by Corestore.
275279
// It's required so that corestore can load a name from userData before 'ready' is emitted.
276280
if (opts._preready) await opts._preready(this)
@@ -411,21 +415,27 @@ module.exports = class Hypercore extends EventEmitter {
411415
}
412416

413417
get length () {
414-
return this._snapshot
415-
? this._snapshot.length
416-
: (this.core === null ? 0 : this.core.tree.length)
418+
if (this._snapshot) return this._snapshot.length
419+
if (this.core === null) return 0
420+
if (!this.sparse) return this.contiguousLength
421+
return this.core.tree.length
417422
}
418423

419424
get byteLength () {
420-
return this._snapshot
421-
? this._snapshot.byteLength
422-
: (this.core === null ? 0 : this.core.tree.byteLength - (this.core.tree.length * this.padding))
425+
if (this._snapshot) return this._snapshot.byteLength
426+
if (this.core === null) return 0
427+
if (!this.sparse) return this.contiguousByteLength
428+
return this.core.tree.byteLength - (this.core.tree.length * this.padding)
423429
}
424430

425431
get contiguousLength () {
426432
return this.core === null ? 0 : this.core.header.contiguousLength
427433
}
428434

435+
get contiguousByteLength () {
436+
return 0
437+
}
438+
429439
get fork () {
430440
return this.core === null ? 0 : this.core.tree.fork
431441
}

test/replicate.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,20 @@ test('one inflight request to a peer per block', async function (t) {
598598

599599
t.is(uploads, 1)
600600
})
601+
602+
test('non-sparse replication', async function (t) {
603+
t.plan(6)
604+
605+
const a = await create()
606+
const b = await create(a.key, { sparse: false })
607+
608+
await a.append(['a', 'b', 'c', 'd', 'e'])
609+
610+
replicate(a, b, t)
611+
612+
b
613+
.once('download', () => t.is(b.core.tree.length, 5))
614+
.on('download', (i) => {
615+
t.is(b.length, b.contiguousLength, `block ${i}`)
616+
})
617+
})

0 commit comments

Comments
 (0)