hyperbee2 0.0.1 → 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,13 +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) {
105
110
  if (b4a.equals(key, this.core.key)) return this
106
111
 
107
112
  const hex = b4a.toString(key, 'hex')
108
113
  if (this.other.has(hex)) return this.other.get(hex)
109
114
 
110
- const ctx = new CoreContext(this.store, this.store.get(key))
115
+ const ctx = new CoreContext(this.store, this.local, this.store.get(key))
111
116
  this.other.set(hex, ctx)
112
117
  return ctx
113
118
  }
@@ -121,7 +126,7 @@ class CoreContext {
121
126
  if (this.other.has(hex)) return this.other.get(hex)
122
127
 
123
128
  const hc = this.getCore(core)
124
- const ctx = new CoreContext(this.store, hc)
129
+ const ctx = new CoreContext(this.store, this.local, hc)
125
130
  this.other.set(hex, ctx)
126
131
  return ctx
127
132
  }
package/lib/write.js CHANGED
@@ -288,8 +288,9 @@ module.exports = class WriteBatch {
288
288
  const update = { node: [], keys: [] }
289
289
  const batch = [update]
290
290
  const stack = [{ update, node: this.root }]
291
+ const context = this.tree.context.getLocalContext()
291
292
 
292
- await this.tree.context.update(this.tree.activeRequests)
293
+ await context.update(this.tree.activeRequests)
293
294
 
294
295
  while (stack.length > 0) {
295
296
  const { update, node } = stack.pop()
@@ -301,12 +302,8 @@ module.exports = class WriteBatch {
301
302
  const k = node.value.keys[i]
302
303
 
303
304
  if (!k.changed) {
304
- k.core = await this.tree.context.getCoreOffset(
305
- k.context,
306
- k.core,
307
- this.tree.activeRequests
308
- )
309
- k.context = this.tree.context
305
+ k.core = await context.getCoreOffset(k.context, k.core, this.tree.activeRequests)
306
+ k.context = context
310
307
  continue
311
308
  }
312
309
 
@@ -320,12 +317,8 @@ module.exports = class WriteBatch {
320
317
  const n = node.value.children[i]
321
318
 
322
319
  if (!n.changed) {
323
- n.core = await this.tree.context.getCoreOffset(
324
- n.context,
325
- n.core,
326
- this.tree.activeRequests
327
- )
328
- n.context = this.tree.context
320
+ n.core = await context.getCoreOffset(n.context, n.core, this.tree.activeRequests)
321
+ n.context = context
329
322
  continue
330
323
  }
331
324
 
@@ -340,7 +333,7 @@ module.exports = class WriteBatch {
340
333
  }
341
334
  }
342
335
 
343
- const length = this.tree.core.length
336
+ const length = context.core.length
344
337
  const blocks = new Array(batch.length)
345
338
 
346
339
  for (let i = 0; i < batch.length; i++) {
@@ -361,7 +354,7 @@ module.exports = class WriteBatch {
361
354
  if (block.data === null) block.data = []
362
355
 
363
356
  k.core = 0
364
- k.context = this.tree.context
357
+ k.context = context
365
358
  k.seq = seq
366
359
  k.offset = block.data.length
367
360
  block.data.push(k)
@@ -371,7 +364,7 @@ module.exports = class WriteBatch {
371
364
  if (block.tree === null) block.tree = []
372
365
 
373
366
  n.core = 0
374
- n.context = this.tree.context
367
+ n.context = context
375
368
  n.seq = seq
376
369
  n.offset = block.tree.length
377
370
  block.tree.push(n.value)
@@ -384,20 +377,20 @@ module.exports = class WriteBatch {
384
377
 
385
378
  if (blocks.length > 0 && this.length > 0) {
386
379
  const core = this.key
387
- ? await this.tree.context.getCoreOffsetByKey(this.key, this.tree.activeRequests)
380
+ ? await context.getCoreOffsetByKey(this.key, this.tree.activeRequests)
388
381
  : 0
389
382
  blocks[blocks.length - 1].previous = { core, seq: this.length - 1 }
390
383
  }
391
384
 
392
385
  // TODO: make this transaction safe
393
- if (this.tree.context.changed) {
394
- this.tree.context.changed = false
395
- this.tree.context.checkpoint = this.tree.core.length + blocks.length
396
- 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
397
390
  }
398
391
 
399
392
  for (let i = 0; i < blocks.length; i++) {
400
- blocks[i].checkpoint = this.tree.context.checkpoint
393
+ blocks[i].checkpoint = context.checkpoint
401
394
  buffers[i] = c.encode(Block, blocks[i])
402
395
  }
403
396
 
@@ -405,7 +398,7 @@ module.exports = class WriteBatch {
405
398
  throw new Error('Write batch is closed')
406
399
  }
407
400
 
408
- await this.tree.core.append(buffers)
401
+ await context.core.append(buffers)
409
402
 
410
403
  for (let i = 0; i < batch.length; i++) {
411
404
  const update = batch[i]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyperbee2",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "btree",
5
5
  "main": "index.js",
6
6
  "files": [