Skip to content

Commit

Permalink
feat: add deleteMany method (#230)
Browse files Browse the repository at this point in the history
Similar to `repo.blocks.putMany` this adds a `repo.blocks.deleteMany` method that allows for deleting lots of blocks in one go.
  • Loading branch information
achingbrain authored May 4, 2020
1 parent d1773b1 commit 3210db9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer> 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)
Expand Down Expand Up @@ -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`
Expand Down
5 changes: 5 additions & 0 deletions src/blockstore-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

Expand Down
27 changes: 15 additions & 12 deletions src/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -40,9 +38,6 @@ function createBaseStore (store) {
* @returns {Promise<Block>}
*/
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 {
Expand Down Expand Up @@ -110,10 +105,6 @@ function createBaseStore (store) {
* @returns {Promise<bool>}
*/
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)
Expand All @@ -127,11 +118,23 @@ function createBaseStore (store) {
* @returns {Promise<void>}
*/
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<CID>} cids
* @returns {Promise<void>}
*/
async deleteMany (cids) {
const batch = store.batch()

for await (const cid of cids) {
batch.delete(cidToKey(cid))
}

return batch.commit()
},
/**
* Close the store
*
Expand Down
12 changes: 12 additions & 0 deletions test/blockstore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
}

0 comments on commit 3210db9

Please sign in to comment.