Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit 6ce7912

Browse files
committed
fix: style, coverage, docs for completeGraph
1 parent 9500eba commit 6ce7912

9 files changed

+68
-19
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Other create-modes may be supported in the future, such as writing to a Buffer (
7676
* [`async CarDatastore.readStreamComplete(stream)`](#CarDatastore__readStreamComplete)
7777
* [`async CarDatastore.readStreaming(stream)`](#CarDatastore__readStreaming)
7878
* [`async CarDatastore.writeStream(stream)`](#CarDatastore__writeStream)
79+
* [`async CarDatastore.completeGraph(root, get, car[, concurrency])`](#CarDatastore__completeGraph)
7980
* [`class CarDatastore`](#CarDatastore)
8081
* [`async CarDatastore#get(key)`](#CarDatastore_get)
8182
* [`async CarDatastore#has(key)`](#CarDatastore_has)
@@ -215,6 +216,24 @@ This create-mode is not available in a browser environment.
215216

216217
**Return value** _(`CarDatastore`)_: an append-only, streaming CarDatastore.
217218

219+
<a name="CarDatastore__completeGraph"></a>
220+
### `async CarDatastore.completeGraph(root, get, car[, concurrency])`
221+
222+
Read a complete IPLD graph from a provided datastore and store the blocks in
223+
a CAR file.
224+
225+
**Parameters:**
226+
227+
* **`root`** _(`Block`)_: the root of the graph to start at, this block will be
228+
included in the CAR and its CID will be set as the single root.
229+
* **`get`** _(`AsyncFunction`)_: an `async` function that takes a CID and returns
230+
a `Block`. Can be used to attach to an arbitrary data store.
231+
* **`car`** _(`CarDatastore`)_: a writable `CarDatastore` that has not yet been
232+
written to (`setRoots()` will be called on it which requires that no data
233+
has been written).
234+
* **`concurrency`** _(`number`, optional, default=`1`)_: how many asynchronous `get` operations to
235+
perform at once.
236+
218237
<a name="CarDatastore"></a>
219238
### `class CarDatastore`
220239

car.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ async function traverseBlock (block, get, car, concurrency = 1, seen = new Set()
139139
const cid = await block.cid()
140140
await car.put(cid, block.encodeUnsafe())
141141
seen.add(cid.toString('base58btc'))
142-
if (cid.codec === 'raw') return
142+
if (cid.codec === 'raw') {
143+
return
144+
}
143145
const reader = block.reader()
144-
const missing = link => !seen.has(link.toString('base58btc'))
146+
const missing = (link) => !seen.has(link.toString('base58btc'))
145147
const links = Array.from(reader.links()).filter(missing).map(([, link]) => link)
148+
146149
while (links.length) {
147150
const chunk = links.splice(0, concurrency)
148151
const blocks = chunk.map(get)
@@ -156,6 +159,25 @@ async function traverseBlock (block, get, car, concurrency = 1, seen = new Set()
156159
}
157160
}
158161

162+
/**
163+
* @name CarDatastore.completeGraph
164+
* @description
165+
* Read a complete IPLD graph from a provided datastore and store the blocks in
166+
* a CAR file.
167+
* @function
168+
* @memberof CarDatastore
169+
* @static
170+
* @async
171+
* @param {Block} root the root of the graph to start at, this block will be
172+
* included in the CAR and its CID will be set as the single root.
173+
* @param {AsyncFunction} get an `async` function that takes a CID and returns
174+
* a `Block`. Can be used to attach to an arbitrary data store.
175+
* @param {CarDatastore} car a writable `CarDatastore` that has not yet been
176+
* written to (`setRoots()` will be called on it which requires that no data
177+
* has been written).
178+
* @param {number} [concurrency=1] how many asynchronous `get` operations to
179+
* perform at once.
180+
*/
159181
async function completeGraph (root, get, car, concurrency) {
160182
await car.setRoots([root])
161183
await traverseBlock(await get(root), get, car, concurrency)

datastore.js

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class CarDatastore {
150150
}
151151

152152
async batch () {
153+
/* c8 ignore next */
153154
throw new Error('Unimplemented operation')
154155
}
155156

lib/coding.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ function encodeStream (roots, blocks) {
215215
encode(writer, roots, blocks)
216216
.then(() => {
217217
stream.end()
218-
}).catch(/* istanbul ignore next toohard */ (err) => {
218+
}).catch((err) => {
219+
/* c8 ignore next 3 */
219220
// maybe this could end up being recursive, with the promise rejection
220221
// above, depending on conditions?
221222
stream.emit('error', err)
@@ -227,7 +228,7 @@ function createStreamWriter (stream) {
227228
return (buf) => {
228229
return new Promise((resolve, reject) => {
229230
stream.write(buf, (err) => {
230-
// istanbul ignore next toohard
231+
/* c8 ignore next 3 */
231232
if (err) {
232233
reject(err)
233234
}

lib/reader-writer-iface.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ const { Errors } = require('interface-datastore')
22
const { verifyRoots, cidToKey } = require('./util')
33

44
class Reader {
5-
// istanbul ignore next
5+
/* c8 ignore next 3 */
66
get () {
77
throw new Error('Unimplemented method')
88
}
99

10-
// istanbul ignore next
10+
/* c8 ignore next 3 */
1111
has () {
1212
throw new Error('Unimplemented method')
1313
}
1414

15-
// istanbul ignore next
15+
/* c8 ignore next 3 */
1616
keys () {
1717
throw new Error('Unimplemented method')
1818
}

lib/writer-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class StreamWriter extends Writer {
1313
}
1414

1515
/* async */ setRoots (roots) {
16-
// istanbul ignore next toohard
16+
/* c8 ignore next 3 */
1717
if (this._mutex) {
1818
throw new Error('Roots already set or blocks are being written')
1919
}

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"test:browser": "npx polendina --cleanup test/test-readbuffer.js test/test-query.js",
1313
"test:node": "hundreds mocha test/test-*.js",
1414
"test": "npm run lint && npm run test:node && npm run test:browser",
15-
"docs": "npx jsdoc4readme --readme *.js lib/raw.js"
15+
"docs": "npx jsdoc4readme --readme *.js lib/raw.js",
16+
"coverage": "c8 --reporter=html --reporter=text mocha test/test-*.js && npx st -d coverage -p 8888"
1617
},
1718
"repository": {
1819
"type": "git",
@@ -32,13 +33,13 @@
3233
"devDependencies": {
3334
"bl": "^4.0.2",
3435
"garbage": "0.0.0",
35-
"hundreds": "0.0.2",
36-
"mocha": "^7.1.1"
36+
"hundreds": "0.0.7",
37+
"mocha": "^7.2.0"
3738
},
3839
"dependencies": {
3940
"@ipld/block": "^4.0.0",
4041
"cids": "^0.8.0",
41-
"interface-datastore": "^0.8.3",
42+
"interface-datastore": "^1.0.4",
4243
"multicodec": "^1.0.1",
4344
"varint": "^5.0.0"
4445
}

test/test-complete-graph.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,38 @@ const { PassThrough } = require('stream')
77

88
const same = assert.deepStrictEqual
99

10-
const all = car => {
10+
function all (car) {
1111
const _traverse = async function * (link, seen = new Set()) {
1212
link = await link
1313
seen.add(link.toString('base64'))
1414
const encoded = await car.get(link)
1515
const block = Block.create(encoded, link)
1616
yield block
1717
const cid = await block.cid()
18-
if (cid.codec === 'raw') return
18+
if (cid.codec === 'raw') {
19+
return
20+
}
21+
1922
for (const [, link] of block.reader().links()) {
20-
if (seen.has(link.toString('base64'))) continue
23+
if (seen.has(link.toString('base64'))) {
24+
continue
25+
}
2126
yield * _traverse(link, seen)
2227
}
2328
}
2429

2530
return _traverse(car.getRoots().then(([root]) => root))
2631
}
2732

28-
const createGet = async blocks => {
33+
async function createGet (blocks) {
2934
const db = new Map()
3035
for (const block of blocks) {
3136
db.set((await block.cid()).toString('base64'), block)
3237
}
33-
return cid => new Promise(resolve => resolve(db.get(cid.toString('base64'))))
38+
return (cid) => new Promise((resolve) => resolve(db.get(cid.toString('base64'))))
3439
}
3540

36-
const concat = async stream => {
41+
async function concat (stream) {
3742
const buffers = []
3843
for await (const buffer of stream) {
3944
buffers.push(buffer)

test/test-raw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('Raw', () => {
7878

7979
it('read raw using index (FileHandle)', async () => {
8080
const fd = await fs.promises.open(path.join(__dirname, 'go.car'))
81-
verifyRead(fd)
81+
await verifyRead(fd)
8282
await fd.close(fd)
8383
})
8484

0 commit comments

Comments
 (0)