prefix-hash-tree 0.0.3 → 0.0.5

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.d.ts CHANGED
@@ -24,6 +24,6 @@ declare module 'prefix-hash-tree' {
24
24
 
25
25
  searchExact(key: Buffer): Promise<any>
26
26
 
27
- prefixQuery(prefix: Buffer): Promise<[string, any][]>
27
+ prefixQuery(prefix: Buffer): Promise<[string, any][] | null>
28
28
  }
29
29
  }
package/index.js CHANGED
@@ -27,11 +27,11 @@ 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) {
34
- return await this.getFunc(this._labelHash(nodeLabel))
34
+ return this.getFunc(this._labelHash(nodeLabel))
35
35
  }
36
36
 
37
37
  async _linearSearch(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 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
 
@@ -160,21 +158,22 @@ class PrefixHashTree {
160
158
  await this.putFunc(this._labelHash(label(parent)), parent)
161
159
 
162
160
  const next =
163
- PrefixHashTree._lcp([lcp, label(child0)]) > PrefixHashTree._lcp([lcp, label(child1)])
161
+ PrefixHashTree._lcp([lcp, label(child0)]).length >
162
+ PrefixHashTree._lcp([lcp, label(child1)]).length
164
163
  ? child0
165
164
  : child1
166
165
 
167
- await this._split({ pairs: pairs, lcp: lcp, depth: depth + 1, leaf: next })
166
+ await this._split({ pairs, lcp, depth: depth + 1, leaf: next })
168
167
  }
169
168
  }
170
169
 
171
170
  async searchLeaf(key) {
172
171
  const keyLabel = PrefixHashTree._bufferToBase2String(key)
173
- return await this._fullSearch(keyLabel)
172
+ return this._fullSearch(keyLabel)
174
173
  }
175
174
 
176
175
  async searchExact(key) {
177
- const { phtNode: leaf, metadata: _ } = await this.searchLeaf(key)
176
+ const { phtNode: leaf } = await this.searchLeaf(key)
178
177
 
179
178
  if (leaf === null) {
180
179
  return null
@@ -186,7 +185,7 @@ class PrefixHashTree {
186
185
 
187
186
  async prefixQuery(prefix) {
188
187
  const prefixLabel = PrefixHashTree._bufferToBase2String(prefix)
189
- let { phtNode: startNode, metadata: _ } = await this._dhtLookup(prefixLabel)
188
+ let { phtNode: startNode } = await this._dhtLookup(prefixLabel)
190
189
 
191
190
  if (startNode === null) {
192
191
  const result = await this._fullSearch(prefixLabel)
@@ -198,7 +197,7 @@ class PrefixHashTree {
198
197
  return null
199
198
  }
200
199
 
201
- return await this._doPrefixQuery({ phtNode: startNode, prefix: prefix })
200
+ return this._doPrefixQuery({ phtNode: startNode, prefix })
202
201
  }
203
202
 
204
203
  async _doPrefixQuery({ phtNode, prefix } = {}) {
@@ -209,38 +208,25 @@ class PrefixHashTree {
209
208
 
210
209
  const subtree0 = `${label(phtNode)}0`
211
210
  const subtree1 = `${label(phtNode)}1`
212
- const prefixLabel = PrefixHashTree._bufferToBase2String(prefix)
213
-
214
- const subtrieTraversals = []
215
211
 
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
- }
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
+ ])
235
224
 
236
- const results = await Promise.all(subtrieTraversals)
237
225
  return results.flat()
238
226
  }
239
227
 
240
228
  static _bufferToBase2String(buf) {
241
- return Array.from(buf)
242
- .map((byte) => byte.toString(2).padStart(8, '0'))
243
- .join('')
229
+ return Array.from(buf, (byte) => byte.toString(2).padStart(8, '0')).join('')
244
230
  }
245
231
 
246
232
  static _lcp(strings = []) {
package/lib/pht-node.d.ts CHANGED
@@ -22,7 +22,7 @@ declare module 'prefix-hash-tree' {
22
22
 
23
23
  export function size(node: PHTNode): number
24
24
 
25
- export function setPointers(node: PHTNode, left: string, right: string): void
25
+ export function setPointers(node: PHTNode, left: string | null, right: string | null): void
26
26
 
27
27
  export function pointerLeft(node: PHTNode): string | null
28
28
 
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.5",
4
4
  "description": "An extensible prefix hash tree for indexing over distributed hash tables",
5
5
  "main": "index.js",
6
6
  "exports": {