hyperbee2 0.0.2 → 1.0.0

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/context.js CHANGED
@@ -49,14 +49,14 @@ class CoreContext {
49
49
 
50
50
  // TODO: prop use a map...
51
51
  for (let i = 0; i < this.cores.length; i++) {
52
- const k = this.cores[i]
52
+ const k = this.cores[i].key
53
53
  if (b4a.equals(k, key)) {
54
54
  return i + 1
55
55
  }
56
56
  }
57
57
 
58
58
  this.changed = true
59
- this.cores.push(key)
59
+ this.cores.push({ key, fork: 0, length: 0, treeHash: null })
60
60
  return this.cores.length
61
61
  }
62
62
 
@@ -66,7 +66,7 @@ class CoreContext {
66
66
  if (b4a.equals(key, this.core.key)) return 0
67
67
 
68
68
  for (let i = 0; i < this.cores.length; i++) {
69
- const k = this.cores[i]
69
+ const k = this.cores[i].key
70
70
  if (b4a.equals(k, key)) {
71
71
  return i + 1
72
72
  }
@@ -75,22 +75,23 @@ class CoreContext {
75
75
  await this.update(activeRequests)
76
76
 
77
77
  for (let i = 0; i < this.cores.length; i++) {
78
- const k = this.cores[i]
78
+ const k = this.cores[i].key
79
79
  if (b4a.equals(k, key)) {
80
80
  return i + 1
81
81
  }
82
82
  }
83
83
 
84
84
  this.changed = true
85
- this.cores.push(key)
85
+ this.cores.push({ key, fork: 0, length: 0, treeHash: null })
86
86
  return this.cores.length
87
87
  }
88
88
 
89
89
  getCore(index) {
90
90
  if (index === 0) return this.core
91
91
  if (index > this.cores.length) throw new Error('Bad core index: ' + index)
92
- if (this.opened[index - 1] === null)
93
- this.opened[index - 1] = this.store.get(this.cores[index - 1])
92
+ if (this.opened[index - 1] === null) {
93
+ this.opened[index - 1] = this.store.get(this.cores[index - 1].key)
94
+ }
94
95
  return this.opened[index - 1]
95
96
  }
96
97
 
@@ -122,7 +123,7 @@ class CoreContext {
122
123
  if (core > this.cores.length) await this.update(activeRequests)
123
124
  if (core > this.cores.length) throw new Error('Bad core index: ' + core)
124
125
 
125
- const hex = b4a.toString(this.cores[core - 1], 'hex')
126
+ const hex = b4a.toString(this.cores[core - 1].key, 'hex')
126
127
  if (this.other.has(hex)) return this.other.get(hex)
127
128
 
128
129
  const hc = this.getCore(core)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyperbee2",
3
- "version": "0.0.2",
3
+ "version": "1.0.0",
4
4
  "description": "btree",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -125,15 +125,48 @@ const encoding4 = {
125
125
  }
126
126
  }
127
127
 
128
+ // @bee/core
129
+ const encoding5 = {
130
+ preencode(state, m) {
131
+ c.fixed32.preencode(state, m.key)
132
+ state.end++ // max flag is 4 so always one byte
133
+
134
+ if (m.fork) c.uint.preencode(state, m.fork)
135
+ if (m.length) c.uint.preencode(state, m.length)
136
+ if (m.treeHash) c.fixed32.preencode(state, m.treeHash)
137
+ },
138
+ encode(state, m) {
139
+ const flags = (m.fork ? 1 : 0) | (m.length ? 2 : 0) | (m.treeHash ? 4 : 0)
140
+
141
+ c.fixed32.encode(state, m.key)
142
+ c.uint.encode(state, flags)
143
+
144
+ if (m.fork) c.uint.encode(state, m.fork)
145
+ if (m.length) c.uint.encode(state, m.length)
146
+ if (m.treeHash) c.fixed32.encode(state, m.treeHash)
147
+ },
148
+ decode(state) {
149
+ const r0 = c.fixed32.decode(state)
150
+ const flags = c.uint.decode(state)
151
+
152
+ return {
153
+ key: r0,
154
+ fork: (flags & 1) !== 0 ? c.uint.decode(state) : 0,
155
+ length: (flags & 2) !== 0 ? c.uint.decode(state) : 0,
156
+ treeHash: (flags & 4) !== 0 ? c.fixed32.decode(state) : null
157
+ }
158
+ }
159
+ }
160
+
128
161
  // @bee/block.tree
129
- const encoding5_4 = c.array(encoding3)
162
+ const encoding6_4 = c.array(encoding3)
130
163
  // @bee/block.data
131
- const encoding5_5 = c.array(encoding4)
164
+ const encoding6_5 = c.array(encoding4)
132
165
  // @bee/block.cores
133
- const encoding5_6 = c.array(c.fixed32)
166
+ const encoding6_6 = c.array(encoding5)
134
167
 
135
168
  // @bee/block
136
- const encoding5 = {
169
+ const encoding6 = {
137
170
  preencode(state, m) {
138
171
  c.uint.preencode(state, m.type)
139
172
  c.uint.preencode(state, m.checkpoint)
@@ -141,9 +174,9 @@ const encoding5 = {
141
174
  state.end++ // max flag is 8 so always one byte
142
175
 
143
176
  if (m.previous) encoding1.preencode(state, m.previous)
144
- if (m.tree) encoding5_4.preencode(state, m.tree)
145
- if (m.data) encoding5_5.preencode(state, m.data)
146
- if (m.cores) encoding5_6.preencode(state, m.cores)
177
+ if (m.tree) encoding6_4.preencode(state, m.tree)
178
+ if (m.data) encoding6_5.preencode(state, m.data)
179
+ if (m.cores) encoding6_6.preencode(state, m.cores)
147
180
  },
148
181
  encode(state, m) {
149
182
  const flags = (m.previous ? 1 : 0) | (m.tree ? 2 : 0) | (m.data ? 4 : 0) | (m.cores ? 8 : 0)
@@ -154,9 +187,9 @@ const encoding5 = {
154
187
  c.uint.encode(state, flags)
155
188
 
156
189
  if (m.previous) encoding1.encode(state, m.previous)
157
- if (m.tree) encoding5_4.encode(state, m.tree)
158
- if (m.data) encoding5_5.encode(state, m.data)
159
- if (m.cores) encoding5_6.encode(state, m.cores)
190
+ if (m.tree) encoding6_4.encode(state, m.tree)
191
+ if (m.data) encoding6_5.encode(state, m.data)
192
+ if (m.cores) encoding6_6.encode(state, m.cores)
160
193
  },
161
194
  decode(state) {
162
195
  const r0 = c.uint.decode(state)
@@ -169,9 +202,9 @@ const encoding5 = {
169
202
  checkpoint: r1,
170
203
  batch: r2,
171
204
  previous: (flags & 1) !== 0 ? encoding1.decode(state) : null,
172
- tree: (flags & 2) !== 0 ? encoding5_4.decode(state) : null,
173
- data: (flags & 4) !== 0 ? encoding5_5.decode(state) : null,
174
- cores: (flags & 8) !== 0 ? encoding5_6.decode(state) : null
205
+ tree: (flags & 2) !== 0 ? encoding6_4.decode(state) : null,
206
+ data: (flags & 4) !== 0 ? encoding6_5.decode(state) : null,
207
+ cores: (flags & 8) !== 0 ? encoding6_6.decode(state) : null
175
208
  }
176
209
  }
177
210
  }
@@ -209,8 +242,10 @@ function getEncoding(name) {
209
242
  return encoding3
210
243
  case '@bee/data':
211
244
  return encoding4
212
- case '@bee/block':
245
+ case '@bee/core':
213
246
  return encoding5
247
+ case '@bee/block':
248
+ return encoding6
214
249
  default:
215
250
  throw new Error('Encoder not found ' + name)
216
251
  }
@@ -109,6 +109,35 @@
109
109
  }
110
110
  ]
111
111
  },
112
+ {
113
+ "name": "core",
114
+ "namespace": "bee",
115
+ "compact": true,
116
+ "flagsPosition": 1,
117
+ "fields": [
118
+ {
119
+ "name": "key",
120
+ "required": true,
121
+ "type": "fixed32",
122
+ "version": 1
123
+ },
124
+ {
125
+ "name": "fork",
126
+ "type": "uint",
127
+ "version": 1
128
+ },
129
+ {
130
+ "name": "length",
131
+ "type": "uint",
132
+ "version": 1
133
+ },
134
+ {
135
+ "name": "treeHash",
136
+ "type": "fixed32",
137
+ "version": 1
138
+ }
139
+ ]
140
+ },
112
141
  {
113
142
  "name": "block",
114
143
  "namespace": "bee",
@@ -153,7 +182,7 @@
153
182
  {
154
183
  "name": "cores",
155
184
  "array": true,
156
- "type": "fixed32",
185
+ "type": "@bee/core",
157
186
  "version": 1
158
187
  }
159
188
  ]