git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit fda15f34eac1f4b4ff2d0ef6a7a5fad66936f6ad

returns rawNode

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 10/20/2017, 5:35:34 PM
Parent: 4d4aa45d37fc25f0759d091f2ed9005d37479164

Files changed

README.mdchanged
index.jschanged
tests/index.jschanged
README.mdView
@@ -42,10 +42,10 @@
4242 db: db,
4343 root: merkleroot
4444 })
4545
46- const val = await verifier.get('monkey')
47- console.log(val.toString())
46 + const {value} = await verifier.get('monkey')
47 + console.log(value.toString())
4848 }
4949
5050 main()
5151 ```
index.jsView
@@ -104,9 +104,11 @@
104104 }
105105 }
106106
107107 /**
108- * gets a value given a key
108 + * gets a value given a key. The promise resolves with an object containing
109 + * `node` the node in the merkle tree and `value` the value of the that the
110 + * node contains
109111 * @param {*} key
110112 * @return {Promise}
111113 */
112114 async get (key, decode) {
@@ -115,9 +117,9 @@
115117 if (decode && Buffer.isBuffer(value)) {
116118 value = cbor.decode(value)
117119 treeNode.setValue(root, value)
118120 }
119- return value
121 + return {node: root, value}
120122 }
121123
122124 /**
123125 * stores a value at a given key
@@ -142,9 +144,11 @@
142144 extension = extension.subarray(0, extensionIndex)
143145
144146 treeNode.setExtension(root, remExtension)
145147 const branch = [null, null]
146- branch[extensionKey] = {'/': root['/']}
148 + branch[extensionKey] = {
149 + '/': root['/']
150 + }
147151 root['/'] = createNode(extension, branch)['/']
148152 }
149153
150154 // if there are remaning key segments create an extension node
tests/index.jsView
@@ -14,24 +14,24 @@
1414 })
1515
1616 await tree.set('test', Buffer.from('cat'))
1717 let val = await tree.get('test')
18- t.equals(val.toString(), 'cat')
18 + t.equals(val.value.toString(), 'cat')
1919 await tree.set('te', Buffer.from('blop'))
2020 val = await tree.get('test')
21- t.equals(val.toString(), 'cat')
21 + t.equals(val.value.toString(), 'cat')
2222
2323 val = await tree.get('te')
24- t.equals(val.toString(), 'blop')
24 + t.equals(val.value.toString(), 'blop')
2525
2626 await tree.set('rad', Buffer.from('cat2'))
2727
2828 val = await tree.get('rad')
29- t.equals(val.toString(), 'cat2')
29 + t.equals(val.value.toString(), 'cat2')
3030
3131 await tree.set('test', Buffer.from('cat111'))
3232 val = await tree.get('test')
33- t.equals(val.toString(), 'cat111')
33 + t.equals(val.value.toString(), 'cat111')
3434
3535 const stateRoot = await tree.flush()
3636
3737 // try reteriving node from ipfs
@@ -40,15 +40,15 @@
4040 root: stateRoot
4141 })
4242
4343 val = await tree.get('te')
44- t.equals(val.toString(), 'blop')
44 + t.equals(val.value.toString(), 'blop')
4545
4646 val = await tree.get('rad')
47- t.equals(val.toString(), 'cat2')
47 + t.equals(val.value.toString(), 'cat2')
4848
4949 val = await tree.get('test')
50- t.equals(val.toString(), 'cat111')
50 + t.equals(val.value.toString(), 'cat111')
5151 // console.log(JSON.stringify(tree.root, null, 2))
5252 t.end()
5353 })
5454
@@ -67,15 +67,15 @@
6767 let key3 = new RadixTree.ArrayConstructor([0, 0])
6868 await tree.set(key3, Buffer.from('cat3'))
6969
7070 let val = await tree.get(key0)
71- t.equals(val.toString(), 'cat')
71 + t.equals(val.value.toString(), 'cat')
7272 val = await tree.get(key1)
73- t.equals(val.toString(), 'cat2')
73 + t.equals(val.value.toString(), 'cat2')
7474 val = await tree.get(key2)
75- t.equals(val.toString(), 'cat')
75 + t.equals(val.value.toString(), 'cat')
7676 val = await tree.get(key3)
77- t.equals(val.toString(), 'cat3')
77 + t.equals(val.value.toString(), 'cat3')
7878
7979 t.end()
8080 })
8181
@@ -95,9 +95,9 @@
9595 await tree.set('te', Buffer.from('cat2'))
9696 await tree.set('ter', Buffer.from('cat3'))
9797 await tree.delete('te')
9898 let val = await tree.get('test')
99- t.equals(val.toString(), 'cat')
99 + t.equals(val.value.toString(), 'cat')
100100
101101 // tests delete end branchs
102102 await tree.set('te', 'cat2')
103103 await tree.delete('ter')
@@ -113,9 +113,9 @@
113113 })
114114 const saved = Buffer.alloc(33).fill(1)
115115 await tree.set('test', saved)
116116 const value = await tree.get('test')
117- t.equals(value.toString(), saved.toString())
117 + t.equals(value.value.toString(), saved.toString())
118118 t.end()
119119 })
120120
121121 tape('encoding / decoding', async t => {
@@ -129,9 +129,9 @@
129129 })
130130 await tree.flush()
131131
132132 let r = await tree.get('test', true)
133- t.equals(r.something, 1, 'should correctly decode value')
133 + t.equals(r.value.something, 1, 'should correctly decode value')
134134 t.end()
135135 })
136136
137137 tape('errors', async t => {
@@ -164,9 +164,9 @@
164164
165165 for (let i = 0; i < entries; i++) {
166166 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
167167 const value = await tree.get(key)
168- t.equals(value[0], i)
168 + t.equals(value.value[0], i)
169169 }
170170
171171 await tree.flush()
172172 for (let i = 0; i < entries; i++) {

Built with git-ssb-web