Commit c5000f3abc5af622b60625081dd851aae564cd02
abstract extension node interaction
wanderer committed on 8/4/2017, 8:06:42 PMParent: 5368b260f7cabad0bf9d35cd4525524ae4bf8efe
Files changed
index.js | changed |
index.js | |||
---|---|---|---|
@@ -32,11 +32,11 @@ | |||
32 | 32 … | async _get (key) { | |
33 | 33 … | let index = 0 | |
34 | 34 … | let root = this.root | |
35 | 35 … | while (key.length > index) { | |
36 | - if (root['/'].extension) { | ||
36 … | + if (isExtension(root)) { | ||
37 | 37 … | let extensionIndex = 0 | |
38 | - const extension = this.toTypedArray(root['/'].extension) | ||
38 … | + const extension = this.toTypedArray(getExtension(root)) | ||
39 | 39 … | let subKey | |
40 | 40 … | // alll extension are padded to 8 bit alignment. So we need to update | |
41 | 41 … | // the index later to count for the added padding | |
42 | 42 … | let padLen | |
@@ -116,31 +116,27 @@ | |||
116 | 116 … | let keySegment = key[result.index] | |
117 | 117 … | ||
118 | 118 … | if (result.extensionIndex !== undefined) { | |
119 | 119 … | // split the extension node in two | |
120 | - let extension = this.toTypedArray(root['/'].extension) | ||
120 … | + let extension = this.toTypedArray(getExtension(root)) | ||
121 | 121 … | const extensionKey = extension[result.extensionIndex + 1] | |
122 | 122 … | const remExtension = extension.subarray(1 - result.extensionIndex) | |
123 | 123 … | extension = extension.subarray(0, result.extensionIndex) | |
124 | 124 … | ||
125 | - const node = root['/'].node | ||
125 … | + const node = getNode(root) | ||
126 | 126 … | let newNode | |
127 | 127 … | // create the new extension node | |
128 | 128 … | 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) | ||
131 | 132 … | } else { | |
132 | 133 … | newNode = root['/'] = [] | |
133 | 134 … | } | |
134 | 135 … | ||
135 | 136 … | // save the remainer of the extension node | |
136 | 137 … | 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) | ||
143 | 139 … | } else { | |
144 | 140 … | newNode[extensionKey] = node | |
145 | 141 … | } | |
146 | 142 … | } | |
@@ -148,14 +144,9 @@ | |||
148 | 144 … | let newNode | |
149 | 145 … | if (result.index + 1 < key.length) { | |
150 | 146 … | // if there are remaning key segments create an extension node | |
151 | 147 … | 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) | ||
158 | 149 … | } else { | |
159 | 150 … | newNode = value | |
160 | 151 … | } | |
161 | 152 … | ||
@@ -223,11 +214,39 @@ | |||
223 | 214 … | } | |
224 | 215 … | } | |
225 | 216 … | ||
226 | 217 … | function getBranch (node) { | |
227 | - if (node['/'].extension) { | ||
228 | - return node['/'].node | ||
218 … | + if (isExtension(node)) { | ||
219 … | + return getNode(node) | ||
229 | 220 … | } else { | |
230 | 221 … | return root['/'] | |
231 | 222 … | } | |
232 | 223 … | } | |
233 | 224 … | ||
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