git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit f2256af140b4bd89e54041e23499b0902cb869c4

coverted nodes to a single object

wanderer committed on 8/4/2017, 8:06:42 PM
Parent: 2adb9b0653c1e154ac7159d623207f66d623e599

Files changed

index.jschanged
tests/index.jschanged
index.jsView
@@ -23,11 +23,11 @@
2323 async _get (key) {
2424 let index = 0
2525 let root = this.root
2626 while (1) {
27- if (isExtension(root)) {
27 + if (hasExtension(root)) {
2828 let extensionIndex = 0
29- const extensionLen = getLength(root)
29 + const extensionLen = getExLength(root)
3030 const extension = getExtension(root)
3131 let subKey
3232 subKey = key.slice(index, index + extensionLen)
3333
@@ -50,24 +50,25 @@
5050 if (keySegment !== undefined) {
5151 const branch = getBranch(root)
5252 await this.graph.get(branch, keySegment)
5353 // preseves the '/'
54- root = branch[keySegment]
54 + const nextRoot = branch[keySegment]
55 + if (!nextRoot) {
56 + return {
57 + root: root,
58 + index: index
59 + }
60 + } else {
61 + root = nextRoot
62 + }
5563 } else {
5664 break
5765 }
5866
5967 index++
6068 }
6169
62- const node = getBranch(root)
63- // get the value
64- let value
65- if (Array.isArray(node)) {
66- value = node[this.radix]
67- } else {
68- value = node
69- }
70 + let value = getValue(root)
7071
7172 if (value.length >= 32) {
7273 value = await this.graph.get(root, root.length - 1)
7374 }
@@ -79,69 +80,53 @@
7980 }
8081 }
8182
8283 async get (key) {
83- key = this.formatKey(key)
84 + key = RadixTree.formatKey(key)
8485 const result = await this._get(key)
8586 return result.value
8687 }
8788
8889 async set (key, value) {
89- key = this.formatKey(key)
90 + key = RadixTree.formatKey(key)
9091
9192 // initial set
9293 if (this.root['/'] === null) {
93- this.root['/'] = createExtension(key, value)['/']
94 + this.root['/'] = createNode(value, key)['/']
9495 return
9596 }
9697
9798 const result = await this._get(key)
9899 let root = result.root
99- let keySegment = key[result.index]
100100
101 + if (result.value) {
102 + setValue(root, value)
103 + return
104 + }
105 +
101106 if (result.extensionIndex !== undefined) {
102107 // split the extension node in two
103108 let extension = getExtension(root)
104109 const extensionKey = extension[result.extensionIndex]
105110 const remExtension = extension.subarray(result.extensionIndex + 1)
106111 extension = extension.subarray(0, result.extensionIndex)
107112
108- const node = getNode(root)
109- let newNode
110- // create the new extension node
111- if (extension.length) {
112- setExtension(root, extension)
113- newNode = []
114- setNode(root, newNode)
115- } else {
116- newNode = root['/'] = []
117- }
118-
119- // save the remainer of the extension node
120- if (remExtension.length) {
121- newNode[extensionKey] = createExtension(remExtension, node)
122- } else {
123- newNode[extensionKey] = node
124- }
113 + setExtension(root, remExtension)
114 + const branch = []
115 + branch[extensionKey] = {'/': root['/']}
116 + root['/'] = createNode(null, extension, branch)['/']
125117 }
126118
127- let newNode
128- if (result.index + 1 < key.length) {
129- // if there are remaning key segments create an extension node
119 + // if there are remaning key segments create an extension node
120 + if (result.index < key.length) {
121 + const keySegment = key[result.index]
130122 const extension = key.subarray(result.index + 1, key.length)
131- newNode = createExtension(extension, value)
123 + const newNode = createNode(value, extension)
124 + const rootBranch = getBranch(root)
125 + rootBranch[keySegment] = newNode
132126 } else {
133- newNode = value
127 + setValue(root, value)
134128 }
135-
136- let targetNode = getBranch(root)
137-
138- // set the value
139- if (keySegment === undefined) {
140- targetNode[this.radix] = newNode
141- } else {
142- targetNode[keySegment] = newNode
143- }
144129 }
145130
146131 // async delete (key) {
147132 // key = new this.ArrayConstructor(key)
@@ -185,12 +170,9 @@
185170 // }
186171 // }
187172 // }
188173 // }
189- isEmptyNode (node) {
190- return node.evey(el => !el)
191- }
192- formatKey (key) {
174 + static formatKey (key) {
193175 if (typeof key === 'string') {
194176 key = encoder.encode(key)
195177 return new RadixTree.ArrayConstructor(key.buffer)
196178 } else {
@@ -199,45 +181,50 @@
199181 }
200182 }
201183
202184 function getBranch (node) {
203- if (isExtension(node)) {
204- return getNode(node)
205- } else {
206- return node['/']
207- }
185 + return node['/'].branch
208186 }
209187
210-function isExtension (node) {
188 +function getValue (node) {
189 + return node['/'].value
190 +}
191 +
192 +function hasExtension (node) {
211193 return !!node['/'].extension
212194 }
213195
214196 function getExtension (node) {
215- return RadixTree.toTypedArray(node['/'].extension).subarray(0, getLength(node))
197 + return RadixTree.toTypedArray(node['/'].extension[1]).subarray(0, getExLength(node))
216198 }
217199
218-function getNode (node) {
219- return node['/'].node
200 +function getExLength (node) {
201 + return node['/'].extension[0]
220202 }
221203
222-function getLength (node) {
223- return node['/'].length
224-}
225-
226204 function setExtension (node, ex) {
227- node['/'].extension = new Buffer(ex.buffer)
228- node['/'].length = ex.length
205 + if (ex && ex.length) {
206 + node['/'].extension = [ex.length, new Buffer(ex.buffer)]
207 + } else {
208 + node['/'].extension = null
209 + }
229210 }
230211
231-function setNode (node, val) {
232- node['/'].node = val
212 +function setValue (node, val) {
213 + node['/'].value = val
233214 }
234215
235-function createExtension (ex, node) {
216 +function createNode (value, ex, branch = []) {
217 + if (ex && ex.length) {
218 + ex = [ex.length, new Buffer(ex.buffer)]
219 + } else {
220 + ex = null
221 + }
222 +
236223 return {
237224 '/': {
238- extension: new Buffer(ex.buffer),
239- node: node,
240- length: ex.length
225 + extension: ex,
226 + branch: branch,
227 + value: value
241228 }
242229 }
243230 }
tests/index.jsView
@@ -17,8 +17,9 @@
1717 let val = await tree.get('test')
1818 t.equals(val, 'cat')
1919 await tree.set('te', 'blop')
2020 val = await tree.get('test')
21 + // console.log('val', val)
2122 t.equals(val, 'cat')
2223
2324 val = await tree.get('te')
2425 t.equals(val, 'blop')
@@ -26,8 +27,12 @@
2627 await tree.set('rad', 'cat2')
2728
2829 val = await tree.get('rad')
2930 t.equals(val, 'cat2')
31 +
32 + await tree.set('test', 'cat111')
33 + val = await tree.get('test')
34 + t.equals(val, 'cat111')
3035 } catch (e) {
3136 console.log(e)
3237 }
3338 t.end()
@@ -43,13 +48,17 @@
4348 let key1 = new RadixTree.ArrayConstructor([0, 1, 0, 1])
4449 await tree.set(key1, 'cat2')
4550 let val = await tree.get(key0)
4651 t.equals(val, 'cat')
52 +
4753 val = await tree.get(key1)
4854 t.equals(val, 'cat2')
4955
5056 let key3 = new RadixTree.ArrayConstructor([0, 1, 0, 1, 1])
5157 await tree.set(key3, 'test')
58 + val = await tree.get(key3)
59 +
60 + t.equals(val, 'test')
5261 } catch (e) {
5362 console.log(e)
5463 }
5564 t.end()

Built with git-ssb-web