Skip to content

feat: add deleteMany method #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
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
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')
})
})
})
}