prefix-hash-tree 0.0.5 → 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.
Files changed (2) hide show
  1. package/index.js +15 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -77,9 +77,13 @@ class PrefixHashTree {
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
 
@@ -168,7 +173,7 @@ class PrefixHashTree {
168
173
  }
169
174
 
170
175
  async searchLeaf(key) {
171
- const keyLabel = PrefixHashTree._bufferToBase2String(key)
176
+ const keyLabel = PrefixHashTree._bufferToBase2String(key.key)
172
177
  return this._fullSearch(keyLabel)
173
178
  }
174
179
 
@@ -179,7 +184,7 @@ class PrefixHashTree {
179
184
  return null
180
185
  }
181
186
 
182
- const val = get(leaf, key.toString(this.keyEncoding))
187
+ const val = get(leaf, this._keyToCompactString(key))
183
188
  return val !== undefined ? val : null
184
189
  }
185
190
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prefix-hash-tree",
3
- "version": "0.0.5",
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": {