git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



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.jschanged
index.jsView
@@ -49,25 +49,26 @@
4949 while (1) {
5050 // load the root
5151 const exNode = await this.graph.get(root, EXTENSION, true)
5252 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)
5854
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)
6363 index += extensionIndex
6464 // check if we compelete travered the extension
6565 if (extensionIndex !== extensionLen) {
6666 return {
67- extensionIndex: extensionIndex,
67 + index: index,
6868 root: root,
69- index: index
69 + extension: extension,
70 + extensionIndex: extensionIndex
7071 }
7172 }
7273 }
7374
@@ -132,31 +133,29 @@
132133
133134 if (isEmpty(this.root)) {
134135 this.root['/'] = createNode(key, [null, null], value)['/']
135136 } 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)
138138
139- if (result.value) {
139 + if (rValue) {
140140 setValue(root, value)
141141 } else {
142- if (result.extensionIndex !== undefined) {
142 + if (extensionIndex !== undefined) {
143143 // 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)
148147
149148 setExtension(root, remExtension)
150149 const branch = [null, null]
151150 branch[extensionKey] = {'/': root['/']}
152151 root['/'] = createNode(extension, branch)['/']
153152 }
154153
155154 // 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)
159158 const newNode = createNode(extension, [null, null], value)
160159 const rootBranch = getBranch(root)
161160 rootBranch[keySegment] = newNode
162161 setBranch(root, rootBranch)
@@ -314,4 +313,21 @@
314313 function isEmpty (node) {
315314 const branch = getBranch(node)
316315 return node['/'].length === 2 && branch[0] === null && branch[1] === null
317316 }
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