diff --git a/README.md b/README.md index cd20cdcd..ea3b086e 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ This is the implementation of the [IPFS repo spec](https://github.com/ipfs/specs - [`Promise repo.blocks.put (block:Block)`](#promise-repoblocksput-blockblock) - [`Promise repo.blocks.putMany (blocks)`](#promise-repoblocksputmany-blocks) - [`Promise repo.blocks.get (cid)`](#promisebuffer-repoblocksget-cid) + - [`Promise repo.blocks.delete (cid:CID)`](#promise-repoblocksdelete-cidcid) + - [`Promise repo.blocks.deleteMany (cids)`](#promise-repoblocksdeletemany-cids) - [`repo.datastore`](#repodatastore) - [Config](#config) - [`Promise repo.config.set(key:string, value)`](#promise-repoconfigsetkeystring-value) @@ -236,6 +238,18 @@ Get block. * `cid` is the content id of [type CID](https://github.com/ipld/js-cid#readme). +#### `Promise repo.blocks.delete (cid:CID)` + +* `cid` should be of the [type CID](https://github.com/ipld/js-cid#readme). + +Delete a block + +#### `Promise repo.blocks.deleteMany (cids)` + +* `cids` should be an Iterable or AsyncIterable that yields entries of the [type CID](https://github.com/ipld/js-cid#readme). + +Delete many blocks + Datastore: #### `repo.datastore` diff --git a/src/blockstore-utils.js b/src/blockstore-utils.js index a95e015e..eda398c2 100644 --- a/src/blockstore-utils.js +++ b/src/blockstore-utils.js @@ -3,6 +3,7 @@ const { Key } = require('interface-datastore') const CID = require('cids') const multibase = require('multibase') +const errcode = require('err-code') /** * Transform a cid to the appropriate datastore key. @@ -11,6 +12,10 @@ const multibase = require('multibase') * @returns {Key} */ exports.cidToKey = cid => { + if (!CID.isCID(cid)) { + throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID') + } + return new Key('/' + multibase.encode('base32', cid.buffer).toString().slice(1).toUpperCase(), false) } diff --git a/src/blockstore.js b/src/blockstore.js index 8cc3675c..a6464a80 100644 --- a/src/blockstore.js +++ b/src/blockstore.js @@ -3,8 +3,6 @@ const core = require('datastore-core') const ShardingStore = core.ShardingDatastore const Block = require('ipld-block') -const CID = require('cids') -const errcode = require('err-code') const { cidToKey } = require('./blockstore-utils') module.exports = async (filestore, options) => { @@ -40,9 +38,6 @@ function createBaseStore (store) { * @returns {Promise} */ async get (cid) { - if (!CID.isCID(cid)) { - throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID') - } const key = cidToKey(cid) let blockData try { @@ -110,10 +105,6 @@ function createBaseStore (store) { * @returns {Promise} */ async has (cid) { - if (!CID.isCID(cid)) { - throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID') - } - const exists = await store.has(cidToKey(cid)) if (exists) return exists const otherCid = cidToOtherVersion(cid) @@ -127,11 +118,23 @@ function createBaseStore (store) { * @returns {Promise} */ async delete (cid) { // eslint-disable-line require-await - if (!CID.isCID(cid)) { - throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID') - } return store.delete(cidToKey(cid)) }, + /** + * Delete a block from the store + * + * @param {AsyncIterable} cids + * @returns {Promise} + */ + async deleteMany (cids) { + const batch = store.batch() + + for await (const cid of cids) { + batch.delete(cidToKey(cid)) + } + + return batch.commit() + }, /** * Close the store * diff --git a/test/blockstore-test.js b/test/blockstore-test.js index efaeb217..178ab12e 100644 --- a/test/blockstore-test.js +++ b/test/blockstore-test.js @@ -281,5 +281,17 @@ module.exports = (repo) => { return expect(repo.blocks.delete('foo')).to.eventually.be.rejected().with.property('code', 'ERR_INVALID_CID') }) }) + + describe('.deleteMany', () => { + it('simple', async () => { + await repo.blocks.deleteMany([b.cid]) + const exists = await repo.blocks.has(b.cid) + expect(exists).to.equal(false) + }) + + it('throws when passed an invalid cid', () => { + return expect(repo.blocks.deleteMany(['foo'])).to.eventually.be.rejected().with.property('code', 'ERR_INVALID_CID') + }) + }) }) }