prefix-hash-tree 0.0.5 → 0.0.7
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 +16 -12
- package/lib/pht-node.d.ts +1 -2
- package/lib/pht-node.js +5 -7
- 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
|
|
81
|
-
|
|
82
|
-
return
|
|
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 =
|
|
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
|
|
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 })
|
|
@@ -133,15 +137,15 @@ class PrefixHashTree {
|
|
|
133
137
|
|
|
134
138
|
const parent = createPHTNode({
|
|
135
139
|
label: label(leaf),
|
|
136
|
-
|
|
137
|
-
child1: label(child1)
|
|
140
|
+
isLeaf: false
|
|
138
141
|
})
|
|
139
142
|
|
|
140
143
|
if (depth === lcp.length) {
|
|
141
144
|
// Base case: we've reached our final depth, so distribute the keys to the children
|
|
142
145
|
pairs.forEach(([key, val]) => {
|
|
143
|
-
const dest =
|
|
144
|
-
|
|
146
|
+
const dest =
|
|
147
|
+
PrefixHashTree._bufferToBase2String(key.key)[lcp.length] === '0' ? child0 : child1
|
|
148
|
+
const serializedKey = this._keyToCompactString(key)
|
|
145
149
|
set(dest, serializedKey, val)
|
|
146
150
|
})
|
|
147
151
|
|
|
@@ -168,7 +172,7 @@ class PrefixHashTree {
|
|
|
168
172
|
}
|
|
169
173
|
|
|
170
174
|
async searchLeaf(key) {
|
|
171
|
-
const keyLabel = PrefixHashTree._bufferToBase2String(key)
|
|
175
|
+
const keyLabel = PrefixHashTree._bufferToBase2String(key.key)
|
|
172
176
|
return this._fullSearch(keyLabel)
|
|
173
177
|
}
|
|
174
178
|
|
|
@@ -179,7 +183,7 @@ class PrefixHashTree {
|
|
|
179
183
|
return null
|
|
180
184
|
}
|
|
181
185
|
|
|
182
|
-
const val = get(leaf,
|
|
186
|
+
const val = get(leaf, this._keyToCompactString(key))
|
|
183
187
|
return val !== undefined ? val : null
|
|
184
188
|
}
|
|
185
189
|
|
package/lib/pht-node.d.ts
CHANGED
package/lib/pht-node.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
function createPHTNode({ label,
|
|
1
|
+
function createPHTNode({ label, isLeaf, lPtr, rPtr, keys } = {}) {
|
|
2
2
|
return {
|
|
3
3
|
label: label || '',
|
|
4
|
-
|
|
5
|
-
child1: child1 || null,
|
|
4
|
+
isLeaf: isLeaf !== undefined ? isLeaf : true,
|
|
6
5
|
lPtr: lPtr || null,
|
|
7
6
|
rPtr: rPtr || null,
|
|
8
7
|
keys: keys || {}
|
|
@@ -12,7 +11,7 @@ function createPHTNode({ label, child0, child1, lPtr, rPtr, keys } = {}) {
|
|
|
12
11
|
function isNode(obj) {
|
|
13
12
|
if (typeof obj !== 'object' || obj === null) return false
|
|
14
13
|
|
|
15
|
-
const validKeys = new Set(['label', '
|
|
14
|
+
const validKeys = new Set(['label', 'isLeaf', 'lPtr', 'rPtr', 'keys'])
|
|
16
15
|
|
|
17
16
|
const keys = Object.keys(obj)
|
|
18
17
|
|
|
@@ -21,8 +20,7 @@ function isNode(obj) {
|
|
|
21
20
|
|
|
22
21
|
return (
|
|
23
22
|
isBinaryString(obj.label) &&
|
|
24
|
-
|
|
25
|
-
isBinaryStringOrNull(obj.child1) &&
|
|
23
|
+
typeof obj.isLeaf === 'boolean' &&
|
|
26
24
|
isBinaryStringOrNull(obj.lPtr) &&
|
|
27
25
|
isBinaryStringOrNull(obj.rPtr) &&
|
|
28
26
|
typeof obj.keys === 'object' &&
|
|
@@ -46,7 +44,7 @@ function isBinaryStringOrNull(v) {
|
|
|
46
44
|
}
|
|
47
45
|
|
|
48
46
|
function isLeaf(node) {
|
|
49
|
-
return node.
|
|
47
|
+
return node.isLeaf
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
function get(node, key) {
|