Files: 1fc94adc199518c16723617727d7d19d75724d5b / index.js
8593 bytesRaw
1 | const Graph = require('ipld-graph-builder') |
2 | const Uint1Array = require('uint1array') |
3 | const TextEncoder = require('text-encoding').TextEncoder |
4 | const DataStore = require('./datastore.js') |
5 | const treeNode = require('./treeNode.js') |
6 | |
7 | const encoder = new TextEncoder('utf-8') |
8 | |
9 | module.exports = class RadixTree { |
10 | /** |
11 | * @param opts |
12 | * @param opts.root {object} a merkle root to a radix tree. If none, RadixTree will create an new root. |
13 | * @param opts.db {object} a level db instance; alternatively, `opts.graph` can be used |
14 | * @param opts.graph {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder); alternatively, `opts.dag` can be used |
15 | * @param opts.dag {object} an instance if [ipfs.dag](https://github.com/ipfs/js-ipfs#dag). If there is no `opts.graph` this will be used to create a new graph instance. |
16 | * @param opts.decoder {object} a cbor decoder |
17 | */ |
18 | constructor (opts) { |
19 | this._root = opts.root || { |
20 | '/': RadixTree.emptyTreeState |
21 | } |
22 | |
23 | this.dag = opts.dag || new DataStore(opts.db, opts.decoder) |
24 | this.graph = opts.graph || new Graph(this.dag) |
25 | this._setting = Promise.resolve() |
26 | } |
27 | |
28 | /** |
29 | * the root of the tree |
30 | * @type {Buffer} |
31 | */ |
32 | get root () { |
33 | return this._root['/'] |
34 | } |
35 | |
36 | set root (root) { |
37 | this._root['/'] = root |
38 | } |
39 | |
40 | /** |
41 | * gets a value given a key |
42 | * @param {*} key |
43 | * @return {Promise} |
44 | */ |
45 | async get (key) { |
46 | key = this.formatKey(key) |
47 | await this.done() |
48 | return this._get(key) |
49 | } |
50 | |
51 | async _get (key) { |
52 | let index = 0 |
53 | let root = this._root |
54 | let parent |
55 | |
56 | while (1) { |
57 | // load the root |
58 | const exNode = await this.graph.get(root, treeNode.EXTENSION, true) |
59 | if (exNode) { |
60 | let subKey = key.subarray(index) |
61 | const {extensionIndex, extensionLen, extension} = findMatchBits(subKey, root) |
62 | index += extensionIndex |
63 | // check if we complete traversed the extension |
64 | if (extensionIndex !== extensionLen) { |
65 | return {index, root, extension, extensionIndex} |
66 | } |
67 | } |
68 | |
69 | let keySegment = key[index] |
70 | if (keySegment !== undefined) { |
71 | const branch = treeNode.getBranch(root) |
72 | await this.graph.get(branch, keySegment, true) |
73 | // preserves the '/' |
74 | const nextRoot = branch[keySegment] |
75 | if (!nextRoot) { |
76 | return {root, index} |
77 | } else { |
78 | parent = root |
79 | root = nextRoot |
80 | } |
81 | } else { |
82 | break |
83 | } |
84 | |
85 | index++ |
86 | } |
87 | |
88 | const value = treeNode.getValue(root) |
89 | const node = root['/'] |
90 | |
91 | return {value, root, node, parent, index} |
92 | } |
93 | |
94 | /** |
95 | * stores a value at a given key returning the tree node that the value was saved in |
96 | * @param {*} key |
97 | * @return {Promise} |
98 | */ |
99 | set (key, value) { |
100 | key = this.formatKey(key) |
101 | return this._mutationLock(this._set.bind(this, key, value)) |
102 | } |
103 | |
104 | async _set (key, value) { |
105 | if (treeNode.isEmpty(this._root)) { |
106 | this._root['/'] = createNode(key, [null, null], value)['/'] |
107 | return this._root['/'] |
108 | } else { |
109 | let {node, root, extensionIndex, extension, index, value: rValue} = await this._get(key) |
110 | |
111 | if (rValue) { |
112 | treeNode.setValue(root, value) |
113 | return node |
114 | } else { |
115 | if (extensionIndex !== undefined) { |
116 | // split the extension node in two |
117 | const extensionKey = extension[extensionIndex] |
118 | const remExtension = extension.subarray(extensionIndex + 1) |
119 | extension = extension.subarray(0, extensionIndex) |
120 | |
121 | treeNode.setExtension(root, remExtension) |
122 | const branch = [null, null] |
123 | branch[extensionKey] = { |
124 | '/': root['/'] |
125 | } |
126 | root['/'] = createNode(extension, branch)['/'] |
127 | } |
128 | |
129 | // if there are remaning key segments create an extension node |
130 | if (index < key.length) { |
131 | const keySegment = key[index] |
132 | const extension = key.subarray(index + 1, key.length) |
133 | const newNode = createNode(extension, [null, null], value) |
134 | const rootBranch = treeNode.getBranch(root) |
135 | rootBranch[keySegment] = newNode |
136 | treeNode.setBranch(root, rootBranch) |
137 | return newNode['/'] |
138 | } else { |
139 | treeNode.setValue(root, value) |
140 | return root['/'] |
141 | } |
142 | } |
143 | } |
144 | } |
145 | |
146 | /** |
147 | *smContainer.js deletes a value at a given key |
148 | * @param {*} key |
149 | * @return {Promise} |
150 | */ |
151 | delete (key) { |
152 | key = this.formatKey(key) |
153 | return this._mutationLock(this._delete.bind(this, key)) |
154 | } |
155 | |
156 | async _delete (key) { |
157 | const results = await this._get(key) |
158 | if (results.value !== undefined) { |
159 | const root = results.root |
160 | const parent = results.parent |
161 | |
162 | treeNode.deleteValue(root) |
163 | |
164 | const branch = treeNode.getBranch(root) |
165 | if (branch.some(el => el !== null)) { |
166 | joinNodes(root) |
167 | } else { |
168 | if (!parent) { |
169 | root['/'] = RadixTree.emptyTreeState |
170 | } else { |
171 | let branch = treeNode.getBranch(parent) |
172 | branch = branch.map(node => node === root ? null : node) |
173 | treeNode.setBranch(parent, branch) |
174 | await this.graph.tree(parent, 2) |
175 | |
176 | joinNodes(parent) |
177 | } |
178 | } |
179 | } |
180 | |
181 | function joinNodes (root) { |
182 | if (treeNode.getValue(root) === undefined) { |
183 | let index |
184 | const branch = treeNode.getBranch(root) |
185 | const nodes = branch.filter((node, i) => { |
186 | if (node) { |
187 | index = i |
188 | return true |
189 | } |
190 | }) |
191 | |
192 | if (nodes.length === 1) { |
193 | const child = nodes[0] |
194 | const pExtension = treeNode.getExtension(root) |
195 | const childExtension = treeNode.getExtension(child) |
196 | const newExtension = new RadixTree.ArrayConstructor(pExtension.length + childExtension.length + 1) |
197 | |
198 | newExtension.set(pExtension) |
199 | newExtension[pExtension.length] = index |
200 | newExtension.set(childExtension, pExtension.length + 1) |
201 | |
202 | treeNode.setExtension(child, newExtension) |
203 | root['/'] = child['/'] |
204 | } |
205 | } |
206 | } |
207 | } |
208 | |
209 | /** |
210 | * returns a promise that resolve when the tree is done with all of its writes |
211 | * @returns {Promise} |
212 | */ |
213 | async done () { |
214 | let setting |
215 | while (this._setting !== setting) { |
216 | setting = this._setting |
217 | await setting |
218 | } |
219 | } |
220 | |
221 | _mutationLock (func) { |
222 | const setting = this._setting |
223 | this._setting = new Promise((resolve, reject) => { |
224 | return setting.then(() => { |
225 | return func().then(resolve).catch(reject) |
226 | }) |
227 | }) |
228 | return this._setting |
229 | } |
230 | |
231 | /** |
232 | * creates a merkle root for the current tree and stores the data persistently |
233 | * @returns {Promise} |
234 | */ |
235 | async flush () { |
236 | await this.done() |
237 | return this.graph.flush(this._root) |
238 | } |
239 | |
240 | formatKey (key) { |
241 | key = RadixTree.formatKey(key) |
242 | return key |
243 | } |
244 | |
245 | /** |
246 | * Checks if a given root exists or not |
247 | * @param {Buffer} root |
248 | * @return {Promise<boolean>} |
249 | */ |
250 | async rootExists (root) { |
251 | await this.flush() |
252 | try { |
253 | await this.dag.get(root) |
254 | } catch (e) { |
255 | return false |
256 | } |
257 | return true |
258 | } |
259 | |
260 | static formatKey (key) { |
261 | if (typeof key === 'string') { |
262 | key = encoder.encode(key) |
263 | } |
264 | |
265 | if (key.constructor !== RadixTree.ArrayConstructor) { |
266 | if (Buffer.isBuffer(key)) { |
267 | return new RadixTree.ArrayConstructor(new Uint8Array(key).buffer) |
268 | } else { |
269 | return new RadixTree.ArrayConstructor(key.buffer) |
270 | } |
271 | } else { |
272 | return key |
273 | } |
274 | } |
275 | |
276 | /** |
277 | * returns the state of an empty tree |
278 | */ |
279 | static get emptyTreeState () { |
280 | return [null, null, null] |
281 | } |
282 | |
283 | /** |
284 | * returns an Uint1Array constructor which is used to represent keys |
285 | * @returns {object} |
286 | */ |
287 | static get ArrayConstructor () { |
288 | return Uint1Array |
289 | } |
290 | |
291 | /** |
292 | * returns a merkle link for some given data |
293 | * @param {Buffer} data - the data which you would like to hash |
294 | * @returns {Buffer} |
295 | */ |
296 | static getMerkleLink (data) { |
297 | return DataStore.getMerkleLink(data) |
298 | } |
299 | } |
300 | |
301 | function createNode (ex, branch, value) { |
302 | const node = { |
303 | '/': [] |
304 | } |
305 | |
306 | treeNode.setValue(node, value) |
307 | treeNode.setExtension(node, ex) |
308 | treeNode.setBranch(node, branch) |
309 | |
310 | return node |
311 | } |
312 | |
313 | function findMatchBits (key, node) { |
314 | let extensionIndex = 0 |
315 | const extension = treeNode.getExtension(node) |
316 | const extensionLen = extension.length |
317 | |
318 | // checks the extension against the key |
319 | while (extensionIndex < extensionLen && extension[extensionIndex] === key[extensionIndex]) { |
320 | extensionIndex++ |
321 | } |
322 | |
323 | return {extensionIndex, extensionLen, extension} |
324 | } |
325 |
Built with git-ssb-web