Commit 1deb40ab1712a608872e7ad75b4b5393128c4181
clean up extension handling
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 9/27/2017, 10:59:28 PM
Parent: e27db1ccc7aad65596f14513efb0e9de9ad37b34
Files changed
index.js | changed |
index.js | |||
---|---|---|---|
@@ -49,25 +49,26 @@ | |||
49 | 49 … | while (1) { | |
50 | 50 … | // load the root | |
51 | 51 … | const exNode = await this.graph.get(root, EXTENSION, true) | |
52 | 52 … | if (exNode) { | |
53 | - let extensionIndex = 0 | ||
54 | - const extension = getExtension(root) | ||
55 | - const extensionLen = extension.length | ||
56 | - let subKey | ||
57 | - subKey = key.subarray(index, index + extensionLen) | ||
53 … | + let subKey = key.subarray(index) | ||
58 | 54 … | ||
59 | - // checks the extension against the key | ||
60 | - while (extensionIndex < extensionLen && extension[extensionIndex] === subKey[extensionIndex]) { | ||
61 | - extensionIndex++ | ||
62 | - } | ||
55 … | + // let extensionIndex = 0 | ||
56 … | + // const extension = getExtension(root) | ||
57 … | + // const extensionLen = extension.length | ||
58 … | + // // checks the extension against the key | ||
59 … | + // while (extensionIndex < extensionLen && extension[extensionIndex] === subKey[extensionIndex]) { | ||
60 … | + // extensionIndex++ | ||
61 … | + // } | ||
62 … | + const {extensionIndex, extensionLen, extension} = findMatchBits(subKey, root) | ||
63 | 63 … | index += extensionIndex | |
64 | 64 … | // check if we compelete travered the extension | |
65 | 65 … | if (extensionIndex !== extensionLen) { | |
66 | 66 … | return { | |
67 | - extensionIndex: extensionIndex, | ||
67 … | + index: index, | ||
68 | 68 … | root: root, | |
69 | - index: index | ||
69 … | + extension: extension, | ||
70 … | + extensionIndex: extensionIndex | ||
70 | 71 … | } | |
71 | 72 … | } | |
72 | 73 … | } | |
73 | 74 … | ||
@@ -132,31 +133,29 @@ | |||
132 | 133 … | ||
133 | 134 … | if (isEmpty(this.root)) { | |
134 | 135 … | this.root['/'] = createNode(key, [null, null], value)['/'] | |
135 | 136 … | } else { | |
136 | - const result = await this._get(key) | ||
137 | - let root = result.root | ||
137 … | + let {root, extensionIndex, extension, index, value: rValue} = await this._get(key) | ||
138 | 138 … | ||
139 | - if (result.value) { | ||
139 … | + if (rValue) { | ||
140 | 140 … | setValue(root, value) | |
141 | 141 … | } else { | |
142 | - if (result.extensionIndex !== undefined) { | ||
142 … | + if (extensionIndex !== undefined) { | ||
143 | 143 … | // split the extension node in two | |
144 | - let extension = getExtension(root) | ||
145 | - const extensionKey = extension[result.extensionIndex] | ||
146 | - const remExtension = extension.subarray(result.extensionIndex + 1) | ||
147 | - extension = extension.subarray(0, result.extensionIndex) | ||
144 … | + const extensionKey = extension[extensionIndex] | ||
145 … | + const remExtension = extension.subarray(extensionIndex + 1) | ||
146 … | + extension = extension.subarray(0, extensionIndex) | ||
148 | 147 … | ||
149 | 148 … | setExtension(root, remExtension) | |
150 | 149 … | const branch = [null, null] | |
151 | 150 … | branch[extensionKey] = {'/': root['/']} | |
152 | 151 … | root['/'] = createNode(extension, branch)['/'] | |
153 | 152 … | } | |
154 | 153 … | ||
155 | 154 … | // if there are remaning key segments create an extension node | |
156 | - if (result.index < key.length) { | ||
157 | - const keySegment = key[result.index] | ||
158 | - const extension = key.subarray(result.index + 1, key.length) | ||
155 … | + if (index < key.length) { | ||
156 … | + const keySegment = key[index] | ||
157 … | + const extension = key.subarray(index + 1, key.length) | ||
159 | 158 … | const newNode = createNode(extension, [null, null], value) | |
160 | 159 … | const rootBranch = getBranch(root) | |
161 | 160 … | rootBranch[keySegment] = newNode | |
162 | 161 … | setBranch(root, rootBranch) | |
@@ -314,4 +313,21 @@ | |||
314 | 313 … | function isEmpty (node) { | |
315 | 314 … | const branch = getBranch(node) | |
316 | 315 … | return node['/'].length === 2 && branch[0] === null && branch[1] === null | |
317 | 316 … | } | |
317 … | + | ||
318 … | +function findMatchBits (key, node) { | ||
319 … | + let extensionIndex = 0 | ||
320 … | + const extension = getExtension(node) | ||
321 … | + const extensionLen = extension.length | ||
322 … | + | ||
323 … | + // checks the extension against the key | ||
324 … | + while (extensionIndex < extensionLen && extension[extensionIndex] === key[extensionIndex]) { | ||
325 … | + extensionIndex++ | ||
326 … | + } | ||
327 … | + | ||
328 … | + return { | ||
329 … | + extensionIndex: extensionIndex, | ||
330 … | + extensionLen: extensionLen, | ||
331 … | + extension: extension | ||
332 … | + } | ||
333 … | +} |
Built with git-ssb-web