git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit ca8952130fa954fc6fad3847bdd48dd40fc3228f

removed the need to wait on mutating ops

wanderer committed on 12/1/2017, 3:13:31 AM
Parent: fe23acfc35a0ab24e2c45f4921dafef3d0192b4d

Files changed

index.jschanged
package-lock.jsonchanged
package.jsonchanged
tests/index.jschanged
index.jsView
@@ -21,10 +21,29 @@
2121 }
2222
2323 this.dag = opts.dag || new DataStore(opts.db)
2424 this.graph = opts.graph || new Graph(this.dag)
25 + this._setting = Promise.resolve()
2526 }
2627
28 + async _mutationLockWait () {
29 + let setting
30 + while (this._setting !== setting) {
31 + setting = this._setting
32 + await setting
33 + }
34 + }
35 +
36 + _mutationLock (func) {
37 + const setting = this._setting
38 + this._setting = new Promise((resolve, reject) => {
39 + return setting.then(() => {
40 + return func().then(resolve).catch(reject)
41 + })
42 + })
43 + return this._setting
44 + }
45 +
2746 /**
2847 * returns the state of an empty tree
2948 */
3049 static get emptyTreeState () {
@@ -111,8 +130,9 @@
111130 * @param {*} key
112131 * @return {Promise}
113132 */
114133 async get (key, decode) {
134 + await this._mutationLockWait()
115135 key = RadixTree.formatKey(key)
116136 let {root, value} = await this._get(key)
117137 if (decode && Buffer.isBuffer(value)) {
118138 value = cbor.decode(value)
@@ -125,9 +145,13 @@
125145 * stores a value at a given key
126146 * @param {*} key
127147 * @return {Promise}
128148 */
129- async set (key, value) {
149 + set (key, value) {
150 + return this._mutationLock(this._set.bind(this, key, value))
151 + }
152 +
153 + async _set (key, value) {
130154 key = RadixTree.formatKey(key)
131155
132156 if (treeNode.isEmpty(this.root)) {
133157 this.root['/'] = createNode(key, [null, null], value)['/']
@@ -170,9 +194,13 @@
170194 * deletes a value at a given key
171195 * @param {*} key
172196 * @return {Promise}
173197 */
174- async delete (key) {
198 + delete (key) {
199 + return this._mutationLock(this._delete.bind(this, key))
200 + }
201 +
202 + async _delete (key) {
175203 key = RadixTree.formatKey(key)
176204 const results = await this._get(key)
177205 if (results.value !== undefined) {
178206 const root = results.root
@@ -228,9 +256,10 @@
228256 /**
229257 * creates a merkle root for the current tree and stores the data perstantly
230258 * @returns {Promise}
231259 */
232- flush () {
260 + async flush () {
261 + await this._mutationLockWait()
233262 return this.graph.flush(this.root)
234263 }
235264
236265 static formatKey (key) {
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 295420 bytes
New file size: 298081 bytes
package.jsonView
@@ -20,10 +20,10 @@
2020 ],
2121 "devDependencies": {
2222 "coveralls": "^3.0.0",
2323 "documentation": "^5.1.1",
24- "level": "^2.0.0",
25- "nyc": "^11.2.1",
24 + "level-browserify": "^1.1.1",
25 + "nyc": "^11.3.0",
2626 "standard": "^10.0.0",
2727 "tape": "^4.6.3"
2828 },
2929 "dependencies": {
tests/index.jsView
@@ -1,7 +1,7 @@
11 const tape = require('tape')
22 const crypto = require('crypto')
3-const level = require('level')
3 +const level = require('level-browserify')
44 const RadixTree = require('../')
55 const db = level('./testdb')
66
77 tape('set and get', async t => {
@@ -12,24 +12,24 @@
1212 let tree = new RadixTree({
1313 db: db
1414 })
1515
16- await tree.set('test', Buffer.from('cat'))
16 + tree.set('test', Buffer.from('cat'))
1717 let val = await tree.get('test')
1818 t.equals(val.value.toString(), 'cat')
19- await tree.set('te', Buffer.from('blop'))
19 + tree.set('te', Buffer.from('blop'))
2020 val = await tree.get('test')
2121 t.equals(val.value.toString(), 'cat')
2222
2323 val = await tree.get('te')
2424 t.equals(val.value.toString(), 'blop')
2525
26- await tree.set('rad', Buffer.from('cat2'))
26 + tree.set('rad', Buffer.from('cat2'))
2727
2828 val = await tree.get('rad')
2929 t.equals(val.value.toString(), 'cat2')
3030
31- await tree.set('test', Buffer.from('cat111'))
31 + tree.set('test', Buffer.from('cat111'))
3232 val = await tree.get('test')
3333 t.equals(val.value.toString(), 'cat111')
3434
3535 const stateRoot = await tree.flush()
@@ -57,17 +57,17 @@
5757 db: db
5858 })
5959
6060 let key0 = new RadixTree.ArrayConstructor([1, 1])
61- await tree.set(key0, Buffer.from('cat'))
6261 let key1 = new RadixTree.ArrayConstructor([0, 1])
63- await tree.set(key1, Buffer.from('cat2'))
64-
6562 let key2 = new RadixTree.ArrayConstructor([1, 0])
66- await tree.set(key2, Buffer.from('cat'))
6763 let key3 = new RadixTree.ArrayConstructor([0, 0])
68- await tree.set(key3, Buffer.from('cat3'))
6964
65 + tree.set(key0, Buffer.from('cat'))
66 + tree.set(key1, Buffer.from('cat2'))
67 + tree.set(key2, Buffer.from('cat'))
68 + tree.set(key3, Buffer.from('cat3'))
69 +
7070 let val = await tree.get(key0)
7171 t.equals(val.value.toString(), 'cat')
7272 val = await tree.get(key1)
7373 t.equals(val.value.toString(), 'cat2')
@@ -82,27 +82,27 @@
8282 tape('delete', async t => {
8383 const tree = new RadixTree({
8484 db: db
8585 })
86- await tree.set('test', Buffer.from('cat'))
87- await tree.set('ter', Buffer.from('cat3'))
88- await tree.delete('te')
89- await tree.delete('test')
86 + tree.set('test', Buffer.from('cat'))
87 + tree.set('ter', Buffer.from('cat3'))
88 + tree.delete('te')
89 + tree.delete('test')
9090 await tree.delete('ter')
9191 t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
9292
9393 // tests delete midle branchs
94- await tree.set('test', Buffer.from('cat'))
95- await tree.set('te', Buffer.from('cat2'))
96- await tree.set('ter', Buffer.from('cat3'))
94 + tree.set('test', Buffer.from('cat'))
95 + tree.set('te', Buffer.from('cat2'))
96 + tree.set('ter', Buffer.from('cat3'))
9797 await tree.delete('te')
9898 let val = await tree.get('test')
9999 t.equals(val.value.toString(), 'cat')
100100
101101 // tests delete end branchs
102- await tree.set('te', 'cat2')
103- await tree.delete('ter')
104- await tree.delete('te')
102 + tree.set('te', 'cat2')
103 + tree.delete('ter')
104 + tree.delete('te')
105105 await tree.delete('test')
106106 t.deepEquals(tree.root['/'], RadixTree.emptyTreeState)
107107 t.end()
108108 })
@@ -111,9 +111,9 @@
111111 const tree = new RadixTree({
112112 db: db
113113 })
114114 const saved = Buffer.alloc(33).fill(1)
115- await tree.set('test', saved)
115 + tree.set('test', saved)
116116 const value = await tree.get('test')
117117 t.equals(value.value.toString(), saved.toString())
118118 t.end()
119119 })
@@ -155,9 +155,9 @@
155155 })
156156 const entries = 100
157157 for (let i = 0; i < entries; i++) {
158158 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
159- await tree.set(key, Buffer.from([i]))
159 + tree.set(key, Buffer.from([i]))
160160 }
161161 // console.log(JSON.stringify(tree.root, null, 2))
162162
163163 await tree.flush()

Built with git-ssb-web