prefix-hash-tree 0.0.2 → 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.
- package/index.js +31 -43
- 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
|
|
30
|
+
return hash
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
async _dhtLookup(nodeLabel) {
|
|
@@ -35,7 +35,7 @@ class PrefixHashTree {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
async _linearSearch(nodeLabel) {
|
|
38
|
-
for (let i = 0; i
|
|
38
|
+
for (let i = 0; i <= nodeLabel.length; i += 1) {
|
|
39
39
|
const { phtNode, metadata } = await this._dhtLookup(nodeLabel.substring(0, i))
|
|
40
40
|
|
|
41
41
|
if (phtNode !== null && isLeaf(phtNode)) {
|
|
@@ -47,19 +47,19 @@ class PrefixHashTree {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
async _binarySearch(nodeLabel) {
|
|
50
|
-
let
|
|
51
|
-
let
|
|
50
|
+
let lo = 0
|
|
51
|
+
let hi = nodeLabel.length
|
|
52
52
|
|
|
53
|
-
while (
|
|
54
|
-
const
|
|
55
|
-
const { phtNode, metadata } = await this._dhtLookup(nodeLabel.substring(0,
|
|
53
|
+
while (lo <= hi) {
|
|
54
|
+
const mid = Math.floor((lo + hi) / 2)
|
|
55
|
+
const { phtNode, metadata } = await this._dhtLookup(nodeLabel.substring(0, mid))
|
|
56
56
|
|
|
57
57
|
if (phtNode !== null && isLeaf(phtNode)) {
|
|
58
58
|
return { phtNode, metadata }
|
|
59
59
|
} else if (phtNode !== null) {
|
|
60
|
-
|
|
60
|
+
lo = mid + 1
|
|
61
61
|
} else {
|
|
62
|
-
|
|
62
|
+
hi = mid - 1
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
@@ -67,15 +67,13 @@ class PrefixHashTree {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
async _fullSearch(nodeLabel) {
|
|
70
|
-
|
|
70
|
+
const result = await this._binarySearch(nodeLabel)
|
|
71
71
|
|
|
72
|
-
if (phtNode
|
|
73
|
-
|
|
74
|
-
phtNode = result.phtNode
|
|
75
|
-
metadata = result.metadata
|
|
72
|
+
if (result.phtNode !== null) {
|
|
73
|
+
return result
|
|
76
74
|
}
|
|
77
75
|
|
|
78
|
-
return
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|