hypercore-storage 0.0.28 → 0.0.29

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.
@@ -115,6 +115,9 @@ class MemoryOverlay {
115
115
  this.bitfields = null
116
116
 
117
117
  this.snapshotted = false
118
+ this.snapshotShared = null
119
+ this.activeSnapshots = 0
120
+ this.copyOnWrite = false
118
121
  }
119
122
 
120
123
  async registerBatch (name, length, overwrite) {
@@ -122,29 +125,46 @@ class MemoryOverlay {
122
125
  }
123
126
 
124
127
  snapshot () {
128
+ if (this.snapshotShared !== null) return this.snapshotShared.snapshot()
129
+
130
+ this.activeSnapshots++
131
+ this.copyOnWrite = true
132
+
125
133
  const snap = new MemoryOverlay(this.storage.snapshot())
126
- snap.merge(this)
127
134
 
128
- if (this.userData !== null) snap.userData = new Map()
129
- if (this.blocks !== null) snap.blocks = new TipList()
130
- if (this.treeNodes !== null) snap.treeNodes = new Map()
131
- if (this.bitfields !== null) snap.bitfields = new TipList()
135
+ snap.snapshotted = true
136
+ snap.snapshotShared = this
132
137
 
133
- // clone state
134
- if (this.head !== null) snap.head = Object.assign(this.head)
135
- if (this.auth !== null) snap.auth = Object.assign(this.auth)
136
- if (this.localKeyPair !== null) snap.localKeyPair = Object.assign(this.localKeyPair)
137
- if (this.encryptionKey !== null) snap.encryptionKey = Object.assign(this.encryptionKey)
138
- if (this.dataDependency !== null) snap.dataDependency = Object.assign(this.dataDependency)
139
- if (this.dataInfo !== null) snap.dataInfo = Object.assign(this.dataInfo)
140
- if (this.userData !== null) snap.userData = mergeMap(snap.userData, this.userData)
141
- if (this.blocks !== null) snap.blocks = mergeTip(snap.blocks, this.blocks)
142
- if (this.treeNodes !== null) snap.treeNodes = mergeMap(snap.treeNodes, this.treeNodes)
143
- if (this.bitfields !== null) snap.bitfields = mergeTip(snap.bitfields, this.bitfields)
138
+ if (this.head !== null) snap.head = this.head
139
+ if (this.auth !== null) snap.auth = this.auth
140
+ if (this.localKeyPair !== null) snap.localKeyPair = this.localKeyPair
141
+ if (this.encryptionKey !== null) snap.encryptionKey = this.encryptionKey
142
+ if (this.dataDependency !== null) snap.dataDependency = this.dataDependency
143
+ if (this.dataInfo !== null) snap.dataInfo = this.dataInfo
144
+ if (this.userData !== null) snap.userData = this.userData
145
+ if (this.blocks !== null) snap.blocks = this.blocks
146
+ if (this.treeNodes !== null) snap.treeNodes = this.treeNodes
147
+ if (this.bitfields !== null) snap.bitfields = this.bitfields
144
148
 
145
149
  return snap
146
150
  }
147
151
 
152
+ _copy () {
153
+ this.copyOnWrite = false
154
+
155
+ // clone state
156
+ if (this.head !== null) this.head = Object.assign(this.head)
157
+ if (this.auth !== null) this.auth = Object.assign(this.auth)
158
+ if (this.localKeyPair !== null) this.localKeyPair = Object.assign(this.localKeyPair)
159
+ if (this.encryptionKey !== null) this.encryptionKey = Object.assign(this.encryptionKey)
160
+ if (this.dataDependency !== null) this.dataDependency = Object.assign(this.dataDependency)
161
+ if (this.dataInfo !== null) this.dataInfo = Object.assign(this.dataInfo)
162
+ if (this.userData !== null) this.userData = mergeMap(new Map(), this.userData)
163
+ if (this.blocks !== null) this.blocks = mergeTip(new TipList(), this.blocks)
164
+ if (this.treeNodes !== null) this.treeNodes = mergeMap(new Map(), this.treeNodes)
165
+ if (this.bitfields !== null) this.bitfields = mergeTip(new TipList(), this.bitfields)
166
+ }
167
+
148
168
  get dependencies () {
149
169
  return this.storage.dependencies
150
170
  }
@@ -159,6 +179,7 @@ class MemoryOverlay {
159
179
  }
160
180
 
161
181
  createWriteBatch () {
182
+ if (this.copyOnWrite) this._copy()
162
183
  return new MemoryOverlayWriteBatch(this)
163
184
  }
164
185
 
@@ -193,7 +214,12 @@ class MemoryOverlay {
193
214
  return (page && (!disk || index > disk.index)) ? { index, page } : disk
194
215
  }
195
216
 
196
- destroy () {}
217
+ destroy () {
218
+ if (this.snapshotted) this.storage.destroy()
219
+ if (this.snapshotShared === null) return
220
+ if (--this.snapshotShared.activeSnapshots === 0) this.snapshotShared.copyOnWrite = false
221
+ this.snapshotShared = null
222
+ }
197
223
 
198
224
  merge (overlay) {
199
225
  if (overlay.head !== null) this.head = overlay.head
package/lib/messages.js CHANGED
@@ -25,13 +25,13 @@ exports.KeyPair = {
25
25
 
26
26
  exports.StorageInfo = {
27
27
  preencode (state, m) {
28
- c.uint.preencode(state, m.version)
28
+ c.uint.preencode(state, 0) // version
29
29
  c.uint.preencode(state, 0) // flags, reserved
30
30
  c.uint.preencode(state, m.free)
31
31
  c.uint.preencode(state, m.total)
32
32
  },
33
33
  encode (state, m) {
34
- c.uint.encode(state, m.version)
34
+ c.uint.encode(state, 0) // version
35
35
  c.uint.encode(state, 0) // flags, reserved
36
36
  c.uint.encode(state, m.free)
37
37
  c.uint.encode(state, m.total)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",