autobee 1.0.0 → 1.0.3

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/lib/writers.js CHANGED
@@ -3,6 +3,8 @@ const encoding = require('./encoding.js')
3
3
  const Triggers = require('./triggers.js')
4
4
  const { WriterEncryption } = require('autobee-encryption')
5
5
 
6
+ const WRITER_PREFETCH = 10
7
+
6
8
  const BATCH_WRITER_SANITY = 1024
7
9
  const BATCH_OPTIMISTIC_SANITY = 32
8
10
 
@@ -80,7 +82,7 @@ class ActiveWriters {
80
82
  const info = await this.auto.system.get(this.auto.local.key)
81
83
  const writable = this.writable
82
84
 
83
- w.isIndexer = info ? info.isIndexer : false
85
+ w.weight = info ? info.weight : 0
84
86
  this.writable = info ? !info.isRemoved : bootstrapping
85
87
 
86
88
  if (writable === this.writable) return
@@ -158,23 +160,30 @@ class Writer {
158
160
  this.core = core
159
161
  this.id = id
160
162
  this.index = 0
163
+ this.weight = 0
161
164
  this.pending = null
162
165
  this.waiting = null
166
+ this.download = null
163
167
 
164
168
  this.isPending = false
165
169
  this.isClosed = false
166
170
  this.isAttached = false
167
- this.isIndexer = false
168
171
  this.isAdded = false
169
172
  this.isRemoved = false
170
173
  this.isCoupled = false
171
174
 
172
- this._onappendBound = this._onappend.bind(this)
173
- if (!local) this.core.on('append', this._onappendBound)
175
+ this._onchangeBound = this._onchange.bind(this)
176
+
177
+ if (!local) this.core.on('append', this._onchangeBound)
178
+ if (!local) this.core.on('download', this._onchangeBound)
174
179
 
175
180
  this.couple()
176
181
  }
177
182
 
183
+ get isIndexer() {
184
+ return this.weight === 2
185
+ }
186
+
178
187
  couple() {
179
188
  if (this.isCoupled) return
180
189
  // if we don't have coupler yet we can't add
@@ -216,7 +225,7 @@ class Writer {
216
225
  }
217
226
  }
218
227
 
219
- _onappend() {
228
+ _onchange() {
220
229
  if (this.waiting === null) this.bump()
221
230
  }
222
231
 
@@ -236,6 +245,8 @@ class Writer {
236
245
  detach() {
237
246
  if (!this.isAttached) return
238
247
  this.isAttached = false
248
+ this.download.destroy()
249
+ this.download = null
239
250
  this.writers.active.delete(this.id)
240
251
  this.removePending()
241
252
  }
@@ -258,6 +269,17 @@ class Writer {
258
269
  return this.close()
259
270
  }
260
271
 
272
+ async views() {
273
+ const latest = await this.getLatest()
274
+ if (!latest || !latest.views) return []
275
+
276
+ // signedLength for autobase compat
277
+ return [
278
+ { ...latest.views.system, signedLength: latest.views.system.length },
279
+ { ...latest.views.view, signedLength: latest.views.view.length }
280
+ ]
281
+ }
282
+
261
283
  async getLatest() {
262
284
  if (this.core.length === 0) return null
263
285
  const block = await this.core.get(this.core.length - 1)
@@ -273,7 +295,7 @@ class Writer {
273
295
  } else {
274
296
  this.isAdded = true
275
297
  this.isRemoved = info.isRemoved
276
- this.isIndexer = info.isIndexer
298
+ this.weight = info.weight
277
299
  }
278
300
 
279
301
  return info ? info.length : 0
@@ -289,10 +311,21 @@ class Writer {
289
311
  let optimistic = false
290
312
  let timestamp = 0
291
313
 
314
+ if (this.download) this.download.destroy()
315
+ this.download = this.core.download({ start: length, end: length + WRITER_PREFETCH })
316
+
292
317
  while (length < this.core.length) {
318
+ // @todo aim to prefetch when writer added, with read-ahead
319
+ // download the block if it's not available locally
320
+ // writer will be added back as pending automatically
321
+ if (!(await this.core.has(length))) {
322
+ this.removePending()
323
+ return null
324
+ }
325
+
293
326
  const block = await this.core.get(length++)
294
327
  const oplog = encoding.decodeOplog(block)
295
- const node = createNode(this.core, length, oplog)
328
+ const node = createNode(this.core, length, this.weight, oplog)
296
329
  const b = oplog.batch
297
330
 
298
331
  // auto correct some batch invariants
@@ -336,6 +369,9 @@ class Writer {
336
369
  if (this.core.writable && this.pending !== null) {
337
370
  for (let i = 0; i < this.pending.length; i++) {
338
371
  const node = this.pending[i]
372
+
373
+ node.batch = { start: i, end: this.pending.length - 1 - i }
374
+
339
375
  if (node.length <= length) continue
340
376
 
341
377
  if (!this.writers.writable && !node.optimistic) {
@@ -409,7 +445,12 @@ class Writer {
409
445
  value
410
446
  }
411
447
 
412
- const node = createNode(this.core, this.core.length + this.pending.length + 1, oplog)
448
+ const node = createNode(
449
+ this.core,
450
+ this.core.length + this.pending.length + 1,
451
+ this.weight,
452
+ oplog
453
+ )
413
454
 
414
455
  this.pending.push(node)
415
456
  this.addPending()
@@ -427,7 +468,11 @@ class Writer {
427
468
  const s = this.system.bee.head()
428
469
  const flushes = this.system.flushes
429
470
  this.pending[this.pending.length - 1].views = { system: s, flushes, view }
430
- for (const node of this.pending) buffers.push(encoding.encodeOplog(node))
471
+ for (let i = 0; i < this.pending.length; i++) {
472
+ const node = this.pending[i]
473
+ node.batch = { start: i, end: this.pending.length - 1 - i }
474
+ buffers.push(encoding.encodeOplog(node))
475
+ }
431
476
  this.pending = null
432
477
  return this.core.append(buffers)
433
478
  }
@@ -437,11 +482,12 @@ exports.ActiveWriters = ActiveWriters
437
482
  exports.Writer = Writer
438
483
  exports.createNode = createNode
439
484
 
440
- function createNode(core, length, oplog) {
485
+ function createNode(core, length, weight, oplog) {
441
486
  return {
442
487
  core,
443
488
  key: core.key,
444
489
  length,
490
+ weight,
445
491
  version: oplog.version,
446
492
  timestamp: oplog.timestamp,
447
493
  links: oplog.links,
package/package.json CHANGED
@@ -1,19 +1,23 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",
7
7
  "main": "index.js",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/holepunchto/autobee.git"
11
+ },
8
12
  "files": [
9
13
  "index.js",
10
14
  "lib/*.js",
11
- "spec"
15
+ "encoding"
12
16
  ],
13
17
  "scripts": {
14
18
  "format": "prettier . --write",
15
19
  "lint": "lunte && prettier . --check",
16
- "test": "prettier . --check && lunte && node test/all.js",
20
+ "test": "node test/all.js",
17
21
  "test:bare": "bare test/all.js",
18
22
  "test:encrypted": "bare test/all.js --encrypt-all",
19
23
  "test:generate": "brittle -r test/all.js test/*.js"