Skip to content

Commit

Permalink
internalise snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Sep 24, 2024
1 parent edd504c commit 27e28dd
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ class Update {
}

class SessionState {
constructor (core, storage, blocks, tree, bitfield, treeLength, snapshot, overlay) {
constructor (core, storage, blocks, tree, bitfield, treeLength, overlay) {
this.core = core

this.storage = storage
this.storageSnapshot = snapshot || null

this.mutex = new Mutex()

Expand All @@ -142,7 +141,7 @@ class SessionState {
}

get isSnapshot () {
return this.storageSnapshot !== null
return this.storage.snapshotted
}

get isDefault () {
Expand All @@ -161,23 +160,18 @@ class SessionState {
}

async close () {
if (this.storageSnapshot) this.storageSnapshot.destroy()

await this.storage.close()
await this.mutex.destroy(new Error('Closed'))
}

snapshot () {
const snapshot = this.storage.snapshot()

const s = new SessionState(
this.core,
this.storage,
this.storage.snapshot(),
this.blocks,
this.tree,
this.bitfield,
this.treeLength,
snapshot,
null
)

Expand All @@ -193,15 +187,14 @@ class SessionState {
this.tree.clone(storage),
this.bitfield,
this.treeLength,
null,
storage
)

return s
}

createReadBatch () {
const reader = this.storage.createReadBatch({ snapshot: this.storageSnapshot })
const reader = this.storage.createReadBatch()
return this.overlay === null ? reader : this.overlay.createReadBatch(reader)
}

Expand All @@ -218,7 +211,7 @@ class SessionState {
}

createUpdate () {
assert(!this._activeBatch && !this.isSnapshot)
assert(!this._activeBatch && !this.storage.snapshotted)

this._activeBatch = this.overlay ? this.overlay.createWriteBatch() : this.storage.createWriteBatch()
return new Update(this._activeBatch, this.bitfield, this.core.header, this)
Expand Down

0 comments on commit 27e28dd

Please sign in to comment.