prefix-hash-tree 0.0.4 → 0.0.6

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
@@ -31,7 +31,7 @@ class PrefixHashTree {
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) {
@@ -73,13 +73,17 @@ class PrefixHashTree {
73
73
  return result
74
74
  }
75
75
 
76
- return await this._linearSearch(nodeLabel)
76
+ return this._linearSearch(nodeLabel)
77
77
  }
78
78
 
79
79
  unhashedKeyFrom(string) {
80
- const phtKey = b4a.alloc(this.bitDomain / 8)
81
- phtKey.write(string, 0, this.keyEncoding)
82
- return phtKey
80
+ const key = b4a.alloc(this.bitDomain / 8)
81
+ const length = key.write(string, 0, this.keyEncoding)
82
+ return { key, length }
83
+ }
84
+
85
+ _keyToCompactString(key) {
86
+ return key.key.toString(this.keyEncoding, 0, key.length)
83
87
  }
84
88
 
85
89
  prefixFrom(string) {
@@ -97,7 +101,7 @@ class PrefixHashTree {
97
101
  }
98
102
 
99
103
  async insert(key, val) {
100
- const nodeLabel = PrefixHashTree._bufferToBase2String(key)
104
+ const nodeLabel = PrefixHashTree._bufferToBase2String(key.key)
101
105
  const { phtNode: leaf } = await this._fullSearch(nodeLabel)
102
106
 
103
107
  // TODO: alert the caller upon replication failure
@@ -105,7 +109,7 @@ class PrefixHashTree {
105
109
  return
106
110
  }
107
111
 
108
- const serializedKey = key.toString(this.keyEncoding)
112
+ const serializedKey = this._keyToCompactString(key)
109
113
  const oldState = get(leaf, serializedKey)
110
114
  const newState = this.transform(oldState, val)
111
115
 
@@ -117,7 +121,7 @@ class PrefixHashTree {
117
121
  .map(([oldKey, oldVal]) => [this.unhashedKeyFrom(oldKey), oldVal])
118
122
  .concat([[key, newState]])
119
123
 
120
- const keysAsBase2Strings = pairs.map(([key, _]) => PrefixHashTree._bufferToBase2String(key))
124
+ const keysAsBase2Strings = pairs.map(([key]) => PrefixHashTree._bufferToBase2String(key.key))
121
125
 
122
126
  const lcp = PrefixHashTree._lcp(keysAsBase2Strings)
123
127
  await this._split({ pairs, lcp, depth: label(leaf).length, leaf })
@@ -140,8 +144,9 @@ class PrefixHashTree {
140
144
  if (depth === lcp.length) {
141
145
  // Base case: we've reached our final depth, so distribute the keys to the children
142
146
  pairs.forEach(([key, val]) => {
143
- const dest = PrefixHashTree._bufferToBase2String(key)[lcp.length] === '0' ? child0 : child1
144
- const serializedKey = key.toString(this.keyEncoding)
147
+ const dest =
148
+ PrefixHashTree._bufferToBase2String(key.key)[lcp.length] === '0' ? child0 : child1
149
+ const serializedKey = this._keyToCompactString(key)
145
150
  set(dest, serializedKey, val)
146
151
  })
147
152
 
@@ -158,7 +163,8 @@ class PrefixHashTree {
158
163
  await this.putFunc(this._labelHash(label(parent)), parent)
159
164
 
160
165
  const next =
161
- PrefixHashTree._lcp([lcp, label(child0)]) > PrefixHashTree._lcp([lcp, label(child1)])
166
+ PrefixHashTree._lcp([lcp, label(child0)]).length >
167
+ PrefixHashTree._lcp([lcp, label(child1)]).length
162
168
  ? child0
163
169
  : child1
164
170
 
@@ -167,8 +173,8 @@ class PrefixHashTree {
167
173
  }
168
174
 
169
175
  async searchLeaf(key) {
170
- const keyLabel = PrefixHashTree._bufferToBase2String(key)
171
- return await this._fullSearch(keyLabel)
176
+ const keyLabel = PrefixHashTree._bufferToBase2String(key.key)
177
+ return this._fullSearch(keyLabel)
172
178
  }
173
179
 
174
180
  async searchExact(key) {
@@ -178,7 +184,7 @@ class PrefixHashTree {
178
184
  return null
179
185
  }
180
186
 
181
- const val = get(leaf, key.toString(this.keyEncoding))
187
+ const val = get(leaf, this._keyToCompactString(key))
182
188
  return val !== undefined ? val : null
183
189
  }
184
190
 
@@ -196,7 +202,7 @@ class PrefixHashTree {
196
202
  return null
197
203
  }
198
204
 
199
- return await this._doPrefixQuery({ phtNode: startNode, prefix: prefix })
205
+ return this._doPrefixQuery({ phtNode: startNode, prefix })
200
206
  }
201
207
 
202
208
  async _doPrefixQuery({ phtNode, prefix } = {}) {
@@ -207,7 +213,6 @@ class PrefixHashTree {
207
213
 
208
214
  const subtree0 = `${label(phtNode)}0`
209
215
  const subtree1 = `${label(phtNode)}1`
210
- const prefixLabel = PrefixHashTree._bufferToBase2String(prefix)
211
216
 
212
217
  const results = await Promise.all([
213
218
  (async () => {
@@ -226,9 +231,7 @@ class PrefixHashTree {
226
231
  }
227
232
 
228
233
  static _bufferToBase2String(buf) {
229
- return Array.from(buf)
230
- .map((byte) => byte.toString(2).padStart(8, '0'))
231
- .join('')
234
+ return Array.from(buf, (byte) => byte.toString(2).padStart(8, '0')).join('')
232
235
  }
233
236
 
234
237
  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.4",
3
+ "version": "0.0.6",
4
4
  "description": "An extensible prefix hash tree for indexing over distributed hash tables",
5
5
  "main": "index.js",
6
6
  "exports": {