Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

feat: add support for batch puts #238

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ class IPLDResolver {
* @returns {Promise.<CID>} - Returns the CID of the serialized IPLD Nodes.
*/
async put (node, format, userOptions) {
const [cid, serialized, options] = await this.serialize(node, format, userOptions)

if (!options.onlyHash) {
const block = new Block(serialized, cid)
await this.bs.put(block)
}

return cid
}

async serialize (node, format, userOptions) {
if (format === undefined) {
throw new Error('`put` requires a format')
}
Expand All @@ -176,27 +187,22 @@ class IPLDResolver {
}

const formatImpl = await this._getFormat(format)
const defaultOptions = {
const options = mergeOptions({
hashAlg: formatImpl.defaultHashAlg,
cidVersion: 1,
onlyHash: false
}
const options = mergeOptions(defaultOptions, userOptions)
}, userOptions)

const cidOptions = {
cidVersion: options.cidVersion,
hashAlg: options.hashAlg,
onlyHash: options.onlyHash
}
const serialized = formatImpl.util.serialize(node)
const cid = await formatImpl.util.cid(serialized, cidOptions)
const cid = await formatImpl.util.cid(serialized, options)
return [cid, serialized, options]
}

putBatch (blocks, options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should take IPLD nodes and not blocks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is what putMany does.

if (!options.onlyHash) {
const block = new Block(serialized, cid)
await this.bs.put(block)
return this.bs.putMany(blocks)
}

return cid
return Promise.resolve()
}

/**
Expand Down