hyperbee2 0.0.0 → 0.0.2

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 CHANGED
@@ -12,7 +12,7 @@ class Hyperbee {
12
12
  const {
13
13
  key = null,
14
14
  core = key ? store.get(key) : store.get({ key, name: 'bee' }),
15
- context = new CoreContext(store, core),
15
+ context = new CoreContext(store, core, core),
16
16
  maxCacheSize = 4096,
17
17
  cache = new NodeCache(maxCacheSize),
18
18
  root = null,
@@ -64,9 +64,9 @@ class Hyperbee {
64
64
  return this.store.replicate(...opts)
65
65
  }
66
66
 
67
- _makeView(root, writable, unbatch) {
67
+ _makeView(context, root, writable, unbatch) {
68
68
  return new Hyperbee(this.store, {
69
- context: this.context,
69
+ context,
70
70
  cache: this.cache,
71
71
  root,
72
72
  view: true,
@@ -78,15 +78,15 @@ class Hyperbee {
78
78
  checkout({ length = this.core.length, key = null, writable = false } = {}) {
79
79
  const context = key ? this.context.getContextByKey(key) : this.context
80
80
  const root = length === 0 ? EMPTY : new TreeNodePointer(context, 0, length - 1, 0, false, null)
81
- return this._makeView(root, writable, 0)
81
+ return this._makeView(context, root, writable, 0)
82
82
  }
83
83
 
84
84
  snapshot() {
85
- return this._makeView(this.root, false, 0)
85
+ return this._makeView(this.context, this.root, false, 0)
86
86
  }
87
87
 
88
88
  undo(n) {
89
- return this._makeView(this.root, true, n)
89
+ return this._makeView(this.context, this.root, true, n)
90
90
  }
91
91
 
92
92
  write(opts) {
package/lib/context.js CHANGED
@@ -2,8 +2,9 @@ const b4a = require('b4a')
2
2
  const { decodeBlock } = require('./encoding.js')
3
3
 
4
4
  class CoreContext {
5
- constructor(store, core, other = new Map()) {
5
+ constructor(store, local, core, other = new Map()) {
6
6
  this.store = store
7
+ this.local = local
7
8
  this.core = core
8
9
  this.other = other
9
10
  this.length = 0
@@ -101,11 +102,17 @@ class CoreContext {
101
102
  return block
102
103
  }
103
104
 
105
+ getLocalContext() {
106
+ return this.getContextByKey(this.local.key)
107
+ }
108
+
104
109
  getContextByKey(key) {
110
+ if (b4a.equals(key, this.core.key)) return this
111
+
105
112
  const hex = b4a.toString(key, 'hex')
106
113
  if (this.other.has(hex)) return this.other.get(hex)
107
114
 
108
- const ctx = new CoreContext(this.store, this.store.get(key))
115
+ const ctx = new CoreContext(this.store, this.local, this.store.get(key))
109
116
  this.other.set(hex, ctx)
110
117
  return ctx
111
118
  }
@@ -119,7 +126,7 @@ class CoreContext {
119
126
  if (this.other.has(hex)) return this.other.get(hex)
120
127
 
121
128
  const hc = this.getCore(core)
122
- const ctx = new CoreContext(this.store, hc)
129
+ const ctx = new CoreContext(this.store, this.local, hc)
123
130
  this.other.set(hex, ctx)
124
131
  return ctx
125
132
  }
package/lib/write.js CHANGED
@@ -29,6 +29,16 @@ module.exports = class WriteBatch {
29
29
  this.length = 0
30
30
  }
31
31
 
32
+ _getContext(root) {
33
+ if (!this.key && !root) return this.tree.context
34
+ return this.key ? this.tree.context.getContextByKey(this.key) : root.context
35
+ }
36
+
37
+ _getLength(root) {
38
+ if (this.length > -1) return this.length
39
+ return root ? root.seq + 1 : 0
40
+ }
41
+
32
42
  async flush() {
33
43
  if (this.flushing) throw new Error('Already flushed')
34
44
  this.flushing = true
@@ -36,15 +46,14 @@ module.exports = class WriteBatch {
36
46
  const ops = this.ops
37
47
  this.ops = []
38
48
 
39
- await this.tree.ready()
49
+ const root = await this.tree.bootstrap()
40
50
 
41
- const length = this.length === -1 ? this.tree.core.length : this.length
51
+ const length = this._getLength(root)
52
+ const context = this._getContext(root)
42
53
 
43
54
  const changed = length === 0
44
55
  const seq = length === 0 ? 0 : length - 1
45
56
 
46
- const context = this.key ? this.tree.context.getContextByKey(this.key) : this.tree.context
47
-
48
57
  this.length = length
49
58
  this.root = new TreeNodePointer(
50
59
  context,
@@ -279,8 +288,9 @@ module.exports = class WriteBatch {
279
288
  const update = { node: [], keys: [] }
280
289
  const batch = [update]
281
290
  const stack = [{ update, node: this.root }]
291
+ const context = this.tree.context.getLocalContext()
282
292
 
283
- await this.tree.context.update(this.tree.activeRequests)
293
+ await context.update(this.tree.activeRequests)
284
294
 
285
295
  while (stack.length > 0) {
286
296
  const { update, node } = stack.pop()
@@ -292,12 +302,8 @@ module.exports = class WriteBatch {
292
302
  const k = node.value.keys[i]
293
303
 
294
304
  if (!k.changed) {
295
- k.core = await this.tree.context.getCoreOffset(
296
- k.context,
297
- k.core,
298
- this.tree.activeRequests
299
- )
300
- k.context = this.tree.context
305
+ k.core = await context.getCoreOffset(k.context, k.core, this.tree.activeRequests)
306
+ k.context = context
301
307
  continue
302
308
  }
303
309
 
@@ -311,12 +317,8 @@ module.exports = class WriteBatch {
311
317
  const n = node.value.children[i]
312
318
 
313
319
  if (!n.changed) {
314
- n.core = await this.tree.context.getCoreOffset(
315
- n.context,
316
- n.core,
317
- this.tree.activeRequests
318
- )
319
- n.context = this.tree.context
320
+ n.core = await context.getCoreOffset(n.context, n.core, this.tree.activeRequests)
321
+ n.context = context
320
322
  continue
321
323
  }
322
324
 
@@ -331,7 +333,7 @@ module.exports = class WriteBatch {
331
333
  }
332
334
  }
333
335
 
334
- const length = this.tree.core.length
336
+ const length = context.core.length
335
337
  const blocks = new Array(batch.length)
336
338
 
337
339
  for (let i = 0; i < batch.length; i++) {
@@ -352,7 +354,7 @@ module.exports = class WriteBatch {
352
354
  if (block.data === null) block.data = []
353
355
 
354
356
  k.core = 0
355
- k.context = this.tree.context
357
+ k.context = context
356
358
  k.seq = seq
357
359
  k.offset = block.data.length
358
360
  block.data.push(k)
@@ -362,7 +364,7 @@ module.exports = class WriteBatch {
362
364
  if (block.tree === null) block.tree = []
363
365
 
364
366
  n.core = 0
365
- n.context = this.tree.context
367
+ n.context = context
366
368
  n.seq = seq
367
369
  n.offset = block.tree.length
368
370
  block.tree.push(n.value)
@@ -375,20 +377,20 @@ module.exports = class WriteBatch {
375
377
 
376
378
  if (blocks.length > 0 && this.length > 0) {
377
379
  const core = this.key
378
- ? await this.tree.context.getCoreOffsetByKey(this.key, this.tree.activeRequests)
380
+ ? await context.getCoreOffsetByKey(this.key, this.tree.activeRequests)
379
381
  : 0
380
382
  blocks[blocks.length - 1].previous = { core, seq: this.length - 1 }
381
383
  }
382
384
 
383
385
  // TODO: make this transaction safe
384
- if (this.tree.context.changed) {
385
- this.tree.context.changed = false
386
- this.tree.context.checkpoint = this.tree.core.length + blocks.length
387
- blocks[blocks.length - 1].cores = this.tree.context.cores
386
+ if (context.changed) {
387
+ context.changed = false
388
+ context.checkpoint = context.core.length + blocks.length
389
+ blocks[blocks.length - 1].cores = context.cores
388
390
  }
389
391
 
390
392
  for (let i = 0; i < blocks.length; i++) {
391
- blocks[i].checkpoint = this.tree.context.checkpoint
393
+ blocks[i].checkpoint = context.checkpoint
392
394
  buffers[i] = c.encode(Block, blocks[i])
393
395
  }
394
396
 
@@ -396,7 +398,7 @@ module.exports = class WriteBatch {
396
398
  throw new Error('Write batch is closed')
397
399
  }
398
400
 
399
- await this.tree.core.append(buffers)
401
+ await context.core.append(buffers)
400
402
 
401
403
  for (let i = 0; i < batch.length; i++) {
402
404
  const update = batch[i]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyperbee2",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "description": "btree",
5
5
  "main": "index.js",
6
6
  "files": [