Skip to content

Commit 548dd87

Browse files
committed
Working graph patching with tests.
1 parent 8936fec commit 548dd87

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

index.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ class ComplexIPLDGraph {
2020
}
2121
this.cbor = cbor((...args) => store.get(...args))
2222
this.store = store
23+
this._clear()
24+
}
25+
_clear () {
2326
this._pending = new Map()
2427
this._patches = new Map()
28+
this._bulk = null
2529
}
2630
shardPath (path, handler) {
2731
path = path.split('/').filter(x => x)
@@ -69,6 +73,7 @@ class ComplexIPLDGraph {
6973
path = await this._realKey(path)
7074
this._bulk.put(block.cid, block.data)
7175
nest(this._patches, path, block.cid)
76+
this._draining = null
7277
}
7378
})()
7479
}
@@ -86,6 +91,7 @@ class ComplexIPLDGraph {
8691
root = this.root
8792
}
8893
if (!root) throw new Error('No root node.')
94+
root = mkcid(root)
8995
await this._kick()
9096
await this._kick()
9197

@@ -104,17 +110,18 @@ class ComplexIPLDGraph {
104110
let _iter = async (map, node) => {
105111
for (let [key, value] of map.entries()) {
106112
if (value instanceof Map) {
113+
let _node
114+
let cid
107115
if (node[key]) {
108-
let cid = mkcid(node[key]['/'])
109-
let _node = this.get(cid)
110-
let _cid = await _iter(value, _node)
111-
node[key] = toLink(_cid)
112-
if (clobber &&
113-
_cid.toBaseEncodedString() !== cid.toBaseEncodedString()) {
114-
this._bulk.del(cid)
115-
}
116+
cid = mkcid(node[key]['/'])
117+
_node = await this.get(cid)
116118
} else {
117-
node[key] = toLink(await _iter(value, {}))
119+
_node = {}
120+
}
121+
node[key] = toLink(await _iter(value, _node))
122+
if (clobber && cid &&
123+
node[key]['/'] !== cid.toBaseEncodedString()) {
124+
this._bulk.del(cid)
118125
}
119126
} else {
120127
if (!(value instanceof CID)) throw new Error('Value not CID.')
@@ -123,14 +130,22 @@ class ComplexIPLDGraph {
123130
}
124131
return mkcbor(node)
125132
}
133+
126134
let start = Date.now()
127135
let cid = await _iter(this._patches, await this.get(root))
128136
this._graphBuildTime = Date.now() - start
129137

138+
if (clobber &&
139+
root.toBaseEncodedString() !== cid.toBaseEncodedString()) {
140+
this._bulk.del(root)
141+
}
142+
130143
start = Date.now()
131144
await this._bulk.flush()
132145
this._flushTime = Date.now() - start
133146

147+
this._clear()
148+
134149
return cid
135150
}
136151

test/test-graph-building.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,31 @@ graphTest('basic graph build', async (t, graph) => {
3838
t.ok(cid)
3939
i++
4040
}
41-
t.same(i, 6)
41+
t.same(i, 5)
4242
let two = await graph.resolve('/one/two', newroot)
4343
t.ok(two.value.three)
4444
let leaf = await graph.resolve('/one/three/four', newroot)
4545
t.same(leaf.value, {test: 1234})
4646
})
47+
48+
graphTest('graph updates', async (t, graph) => {
49+
let block = await serialize({test: 1234})
50+
graph.add('/one/two/edge', block)
51+
let root = await graph.flush((await empty).cid)
52+
graph.add('/one/three/four', block)
53+
54+
let newroot = await graph.flush(root)
55+
56+
let i = 0
57+
for await (let cid of graph.store.cids()) {
58+
t.ok(cid)
59+
i++
60+
}
61+
t.same(i, 5)
62+
let two = await graph.resolve('/one/two', newroot)
63+
t.ok(two.value.edge)
64+
let three = await graph.resolve('/one/three', newroot)
65+
t.ok(three.value.four['/'])
66+
let leaf = await graph.resolve('/one/three/four', newroot)
67+
t.same(leaf.value, {test: 1234})
68+
})

0 commit comments

Comments
 (0)