hypercore-storage 0.0.36 → 0.0.37
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.
- package/index.js +70 -9
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -267,7 +267,24 @@ class ReadBatch {
|
|
|
267
267
|
}
|
|
268
268
|
}
|
|
269
269
|
|
|
270
|
-
class
|
|
270
|
+
class Lock {
|
|
271
|
+
constructor (mutex) {
|
|
272
|
+
this.mutex = mutex
|
|
273
|
+
this.refs = 0
|
|
274
|
+
this.promise = null
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
acquire () {
|
|
278
|
+
if (this.refs++ === 0) this.promise = this.mutex.lock()
|
|
279
|
+
return this
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
release () {
|
|
283
|
+
if (--this.refs === 0) return this.mutex.unlock()
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
class Atom {
|
|
271
288
|
constructor (db) {
|
|
272
289
|
this.db = db
|
|
273
290
|
this.batch = null
|
|
@@ -276,6 +293,11 @@ class Atomizer {
|
|
|
276
293
|
this.flushing = null
|
|
277
294
|
this.resolve = null
|
|
278
295
|
this.reject = null
|
|
296
|
+
|
|
297
|
+
this._executing = null
|
|
298
|
+
this._waiting = []
|
|
299
|
+
this._queue = []
|
|
300
|
+
this._enqueue = (lock, resolve, reject) => this._queue.push({ lock, resolve, reject })
|
|
279
301
|
}
|
|
280
302
|
|
|
281
303
|
enter () {
|
|
@@ -286,6 +308,43 @@ class Atomizer {
|
|
|
286
308
|
if (--this.refs === 0) this._commit()
|
|
287
309
|
}
|
|
288
310
|
|
|
311
|
+
acquire (mutex) {
|
|
312
|
+
const lock = this._lock(mutex)
|
|
313
|
+
return new Promise(this._enqueue.bind(this, lock))
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
_lock (mutex) {
|
|
317
|
+
for (const lock of this._waiting) {
|
|
318
|
+
if (lock.mutex === mutex) return lock.acquire()
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const lock = new Lock(mutex)
|
|
322
|
+
this._waiting.push(lock)
|
|
323
|
+
|
|
324
|
+
if (this._executing === null) this._executing = this._execute()
|
|
325
|
+
|
|
326
|
+
return lock.acquire()
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async _execute () {
|
|
330
|
+
this.enter()
|
|
331
|
+
for (const { promise } of this._waiting) await promise
|
|
332
|
+
|
|
333
|
+
const queue = this._queue
|
|
334
|
+
|
|
335
|
+
this._waiting = []
|
|
336
|
+
this._queue = []
|
|
337
|
+
this._executing = null
|
|
338
|
+
|
|
339
|
+
for (const { lock, resolve, reject } of queue) {
|
|
340
|
+
if (this.destroyed) reject(new Error('Atomizer destroyed'))
|
|
341
|
+
else resolve(lock)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
this._ensureTick() // allow caller to enter
|
|
345
|
+
this.exit()
|
|
346
|
+
}
|
|
347
|
+
|
|
289
348
|
createBatch () {
|
|
290
349
|
if (this.refs === 0) this._ensureTick()
|
|
291
350
|
this.enter()
|
|
@@ -299,6 +358,8 @@ class Atomizer {
|
|
|
299
358
|
}
|
|
300
359
|
|
|
301
360
|
async _commit () {
|
|
361
|
+
if (this.batch === null) return
|
|
362
|
+
|
|
302
363
|
const batch = this.batch
|
|
303
364
|
const resolve = this.resolve
|
|
304
365
|
const reject = this.reject
|
|
@@ -420,8 +481,8 @@ module.exports = class CoreStorage {
|
|
|
420
481
|
return this.db.close()
|
|
421
482
|
}
|
|
422
483
|
|
|
423
|
-
|
|
424
|
-
return new
|
|
484
|
+
atom () {
|
|
485
|
+
return new Atom(this.db)
|
|
425
486
|
}
|
|
426
487
|
|
|
427
488
|
async clear () {
|
|
@@ -512,8 +573,8 @@ class HypercoreStorage {
|
|
|
512
573
|
return this.dbSnapshot !== null
|
|
513
574
|
}
|
|
514
575
|
|
|
515
|
-
|
|
516
|
-
return this.root.
|
|
576
|
+
atom () {
|
|
577
|
+
return this.root.atom()
|
|
517
578
|
}
|
|
518
579
|
|
|
519
580
|
dependencyLength () {
|
|
@@ -535,14 +596,14 @@ class HypercoreStorage {
|
|
|
535
596
|
return storage
|
|
536
597
|
}
|
|
537
598
|
|
|
538
|
-
async registerBatch (name, head) {
|
|
599
|
+
async registerBatch (name, head, atom) {
|
|
539
600
|
await this.mutex.write.lock()
|
|
540
601
|
|
|
541
602
|
const storage = new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, null)
|
|
542
603
|
|
|
543
604
|
try {
|
|
544
605
|
const info = await getStorageInfo(this.db)
|
|
545
|
-
const write = this.db.write()
|
|
606
|
+
const write = atom ? atom.createBatch() : this.db.write()
|
|
546
607
|
|
|
547
608
|
storage.dataPointer = info.free++
|
|
548
609
|
|
|
@@ -620,10 +681,10 @@ class HypercoreStorage {
|
|
|
620
681
|
return new ReadBatch(this, this.db.read({ snapshot }))
|
|
621
682
|
}
|
|
622
683
|
|
|
623
|
-
createWriteBatch (
|
|
684
|
+
createWriteBatch (atom) {
|
|
624
685
|
assert(this.destroyed === false)
|
|
625
686
|
|
|
626
|
-
if (
|
|
687
|
+
if (atom) return new WriteBatch(this, atom.createBatch(), atom)
|
|
627
688
|
|
|
628
689
|
return new WriteBatch(this, this.db.write(), null)
|
|
629
690
|
}
|