git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit c5000f3abc5af622b60625081dd851aae564cd02

abstract extension node interaction

wanderer committed on 8/4/2017, 8:06:42 PM
Parent: 5368b260f7cabad0bf9d35cd4525524ae4bf8efe

Files changed

index.jschanged
index.jsView
@@ -32,11 +32,11 @@
3232 async _get (key) {
3333 let index = 0
3434 let root = this.root
3535 while (key.length > index) {
36- if (root['/'].extension) {
36 + if (isExtension(root)) {
3737 let extensionIndex = 0
38- const extension = this.toTypedArray(root['/'].extension)
38 + const extension = this.toTypedArray(getExtension(root))
3939 let subKey
4040 // alll extension are padded to 8 bit alignment. So we need to update
4141 // the index later to count for the added padding
4242 let padLen
@@ -116,31 +116,27 @@
116116 let keySegment = key[result.index]
117117
118118 if (result.extensionIndex !== undefined) {
119119 // split the extension node in two
120- let extension = this.toTypedArray(root['/'].extension)
120 + let extension = this.toTypedArray(getExtension(root))
121121 const extensionKey = extension[result.extensionIndex + 1]
122122 const remExtension = extension.subarray(1 - result.extensionIndex)
123123 extension = extension.subarray(0, result.extensionIndex)
124124
125- const node = root['/'].node
125 + const node = getNode(root)
126126 let newNode
127127 // create the new extension node
128128 if (extension.length) {
129- root['/'].extension = new Buffer(extension.buffer)
130- newNode = root['/'].node = []
129 + setExtension(root, new Buffer(extension.buffer))
130 + newNode = []
131 + setNode(root, newNode)
131132 } else {
132133 newNode = root['/'] = []
133134 }
134135
135136 // save the remainer of the extension node
136137 if (remExtension.length) {
137- newNode[extensionKey] = {
138- '/': {
139- extension: new Buffer(remExtension.buffer),
140- node: node
141- }
142- }
138 + newNode[extensionKey] = createExtension(new Buffer(remExtension.buffer), node)
143139 } else {
144140 newNode[extensionKey] = node
145141 }
146142 }
@@ -148,14 +144,9 @@
148144 let newNode
149145 if (result.index + 1 < key.length) {
150146 // if there are remaning key segments create an extension node
151147 const extension = key.subarray(result.index + 1, key.length)
152- newNode = {
153- '/': {
154- extension: new Buffer(extension.buffer),
155- node: value
156- }
157- }
148 + newNode = createExtension(new Buffer(extension.buffer), value)
158149 } else {
159150 newNode = value
160151 }
161152
@@ -223,11 +214,39 @@
223214 }
224215 }
225216
226217 function getBranch (node) {
227- if (node['/'].extension) {
228- return node['/'].node
218 + if (isExtension(node)) {
219 + return getNode(node)
229220 } else {
230221 return root['/']
231222 }
232223 }
233224
225 +function isExtension (node) {
226 + return !!node['/'].extension
227 +}
228 +
229 +function getExtension (node) {
230 + return node['/'].extension
231 +}
232 +
233 +function getNode (node) {
234 + return node['/'].node
235 +}
236 +
237 +function setExtension (node, ex) {
238 + node['/'].extension = ex
239 +}
240 +
241 +function setNode (node, val) {
242 + node['/'].node = val
243 +}
244 +
245 +function createExtension(ex, node) {
246 + return {
247 + '/': {
248 + extension: ex,
249 + node: node
250 + }
251 + }
252 +}

Built with git-ssb-web