prefix-hash-tree 0.0.3 → 0.0.4

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.
Files changed (2) hide show
  1. package/index.js +23 -35
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -27,7 +27,7 @@ class PrefixHashTree {
27
27
  _labelHash(nodeLabel) {
28
28
  const hash = b4a.allocUnsafe(32)
29
29
  sodium.crypto_generichash(hash, b4a.from(`${this.indexID}${nodeLabel}`))
30
- return b4a.toBuffer(hash)
30
+ return hash
31
31
  }
32
32
 
33
33
  async _dhtLookup(nodeLabel) {
@@ -67,15 +67,13 @@ class PrefixHashTree {
67
67
  }
68
68
 
69
69
  async _fullSearch(nodeLabel) {
70
- let { phtNode, metadata } = await this._binarySearch(nodeLabel)
70
+ const result = await this._binarySearch(nodeLabel)
71
71
 
72
- if (phtNode === null) {
73
- const result = await this._linearSearch(nodeLabel)
74
- phtNode = result.phtNode
75
- metadata = result.metadata
72
+ if (result.phtNode !== null) {
73
+ return result
76
74
  }
77
75
 
78
- return { phtNode, metadata }
76
+ return await this._linearSearch(nodeLabel)
79
77
  }
80
78
 
81
79
  unhashedKeyFrom(string) {
@@ -89,7 +87,7 @@ class PrefixHashTree {
89
87
  }
90
88
 
91
89
  async init() {
92
- const { phtNode: rootNode, metadata: _ } = await this._dhtLookup('')
90
+ const { phtNode: rootNode } = await this._dhtLookup('')
93
91
 
94
92
  if (rootNode !== null) {
95
93
  return
@@ -100,7 +98,7 @@ class PrefixHashTree {
100
98
 
101
99
  async insert(key, val) {
102
100
  const nodeLabel = PrefixHashTree._bufferToBase2String(key)
103
- const { phtNode: leaf, metadata: _ } = await this._fullSearch(nodeLabel)
101
+ const { phtNode: leaf } = await this._fullSearch(nodeLabel)
104
102
 
105
103
  // TODO: alert the caller upon replication failure
106
104
  if (leaf === null) {
@@ -122,7 +120,7 @@ class PrefixHashTree {
122
120
  const keysAsBase2Strings = pairs.map(([key, _]) => PrefixHashTree._bufferToBase2String(key))
123
121
 
124
122
  const lcp = PrefixHashTree._lcp(keysAsBase2Strings)
125
- await this._split({ pairs: pairs, lcp: lcp, depth: label(leaf).length, leaf: leaf })
123
+ await this._split({ pairs, lcp, depth: label(leaf).length, leaf })
126
124
  }
127
125
  }
128
126
 
@@ -164,7 +162,7 @@ class PrefixHashTree {
164
162
  ? child0
165
163
  : child1
166
164
 
167
- await this._split({ pairs: pairs, lcp: lcp, depth: depth + 1, leaf: next })
165
+ await this._split({ pairs, lcp, depth: depth + 1, leaf: next })
168
166
  }
169
167
  }
170
168
 
@@ -174,7 +172,7 @@ class PrefixHashTree {
174
172
  }
175
173
 
176
174
  async searchExact(key) {
177
- const { phtNode: leaf, metadata: _ } = await this.searchLeaf(key)
175
+ const { phtNode: leaf } = await this.searchLeaf(key)
178
176
 
179
177
  if (leaf === null) {
180
178
  return null
@@ -186,7 +184,7 @@ class PrefixHashTree {
186
184
 
187
185
  async prefixQuery(prefix) {
188
186
  const prefixLabel = PrefixHashTree._bufferToBase2String(prefix)
189
- let { phtNode: startNode, metadata: _ } = await this._dhtLookup(prefixLabel)
187
+ let { phtNode: startNode } = await this._dhtLookup(prefixLabel)
190
188
 
191
189
  if (startNode === null) {
192
190
  const result = await this._fullSearch(prefixLabel)
@@ -211,29 +209,19 @@ class PrefixHashTree {
211
209
  const subtree1 = `${label(phtNode)}1`
212
210
  const prefixLabel = PrefixHashTree._bufferToBase2String(prefix)
213
211
 
214
- const subtrieTraversals = []
212
+ const results = await Promise.all([
213
+ (async () => {
214
+ // TODO: alert caller upon replication failure
215
+ const { phtNode: child } = await this._dhtLookup(subtree0)
216
+ return child !== null ? this._doPrefixQuery({ phtNode: child, prefix }) : []
217
+ })(),
218
+ (async () => {
219
+ // TODO: alert caller upon replication failure
220
+ const { phtNode: child } = await this._dhtLookup(subtree1)
221
+ return child !== null ? this._doPrefixQuery({ phtNode: child, prefix }) : []
222
+ })()
223
+ ])
215
224
 
216
- if (prefixLabel.startsWith(subtree0) || subtree0.startsWith(prefixLabel)) {
217
- subtrieTraversals.push(
218
- (async () => {
219
- // TODO: alert caller upon replication failure
220
- const { phtNode: child, metadata: _ } = await this._dhtLookup(subtree0)
221
- return child !== null ? this._doPrefixQuery({ phtNode: child, prefix }) : []
222
- })()
223
- )
224
- }
225
-
226
- if (prefixLabel.startsWith(subtree1) || subtree1.startsWith(prefixLabel)) {
227
- subtrieTraversals.push(
228
- (async () => {
229
- // TODO: alert caller upon replication failure
230
- const { phtNode: child, metadata: _ } = await this._dhtLookup(subtree1)
231
- return child !== null ? this._doPrefixQuery({ phtNode: child, prefix }) : []
232
- })()
233
- )
234
- }
235
-
236
- const results = await Promise.all(subtrieTraversals)
237
225
  return results.flat()
238
226
  }
239
227
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prefix-hash-tree",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "An extensible prefix hash tree for indexing over distributed hash tables",
5
5
  "main": "index.js",
6
6
  "exports": {