Skip to content

Commit 3210db9

Browse files
authored
feat: add deleteMany method (#230)
Similar to `repo.blocks.putMany` this adds a `repo.blocks.deleteMany` method that allows for deleting lots of blocks in one go.
1 parent d1773b1 commit 3210db9

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ This is the implementation of the [IPFS repo spec](https://github.com/ipfs/specs
4141
- [`Promise repo.blocks.put (block:Block)`](#promise-repoblocksput-blockblock)
4242
- [`Promise repo.blocks.putMany (blocks)`](#promise-repoblocksputmany-blocks)
4343
- [`Promise<Buffer> repo.blocks.get (cid)`](#promisebuffer-repoblocksget-cid)
44+
- [`Promise repo.blocks.delete (cid:CID)`](#promise-repoblocksdelete-cidcid)
45+
- [`Promise repo.blocks.deleteMany (cids)`](#promise-repoblocksdeletemany-cids)
4446
- [`repo.datastore`](#repodatastore)
4547
- [Config](#config)
4648
- [`Promise repo.config.set(key:string, value)`](#promise-repoconfigsetkeystring-value)
@@ -236,6 +238,18 @@ Get block.
236238

237239
* `cid` is the content id of [type CID](https://github.com/ipld/js-cid#readme).
238240

241+
#### `Promise repo.blocks.delete (cid:CID)`
242+
243+
* `cid` should be of the [type CID](https://github.com/ipld/js-cid#readme).
244+
245+
Delete a block
246+
247+
#### `Promise repo.blocks.deleteMany (cids)`
248+
249+
* `cids` should be an Iterable or AsyncIterable that yields entries of the [type CID](https://github.com/ipld/js-cid#readme).
250+
251+
Delete many blocks
252+
239253
Datastore:
240254

241255
#### `repo.datastore`

src/blockstore-utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { Key } = require('interface-datastore')
44
const CID = require('cids')
55
const multibase = require('multibase')
6+
const errcode = require('err-code')
67

78
/**
89
* Transform a cid to the appropriate datastore key.
@@ -11,6 +12,10 @@ const multibase = require('multibase')
1112
* @returns {Key}
1213
*/
1314
exports.cidToKey = cid => {
15+
if (!CID.isCID(cid)) {
16+
throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID')
17+
}
18+
1419
return new Key('/' + multibase.encode('base32', cid.buffer).toString().slice(1).toUpperCase(), false)
1520
}
1621

src/blockstore.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const core = require('datastore-core')
44
const ShardingStore = core.ShardingDatastore
55
const Block = require('ipld-block')
6-
const CID = require('cids')
7-
const errcode = require('err-code')
86
const { cidToKey } = require('./blockstore-utils')
97

108
module.exports = async (filestore, options) => {
@@ -40,9 +38,6 @@ function createBaseStore (store) {
4038
* @returns {Promise<Block>}
4139
*/
4240
async get (cid) {
43-
if (!CID.isCID(cid)) {
44-
throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID')
45-
}
4641
const key = cidToKey(cid)
4742
let blockData
4843
try {
@@ -110,10 +105,6 @@ function createBaseStore (store) {
110105
* @returns {Promise<bool>}
111106
*/
112107
async has (cid) {
113-
if (!CID.isCID(cid)) {
114-
throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID')
115-
}
116-
117108
const exists = await store.has(cidToKey(cid))
118109
if (exists) return exists
119110
const otherCid = cidToOtherVersion(cid)
@@ -127,11 +118,23 @@ function createBaseStore (store) {
127118
* @returns {Promise<void>}
128119
*/
129120
async delete (cid) { // eslint-disable-line require-await
130-
if (!CID.isCID(cid)) {
131-
throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID')
132-
}
133121
return store.delete(cidToKey(cid))
134122
},
123+
/**
124+
* Delete a block from the store
125+
*
126+
* @param {AsyncIterable<CID>} cids
127+
* @returns {Promise<void>}
128+
*/
129+
async deleteMany (cids) {
130+
const batch = store.batch()
131+
132+
for await (const cid of cids) {
133+
batch.delete(cidToKey(cid))
134+
}
135+
136+
return batch.commit()
137+
},
135138
/**
136139
* Close the store
137140
*

test/blockstore-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,17 @@ module.exports = (repo) => {
281281
return expect(repo.blocks.delete('foo')).to.eventually.be.rejected().with.property('code', 'ERR_INVALID_CID')
282282
})
283283
})
284+
285+
describe('.deleteMany', () => {
286+
it('simple', async () => {
287+
await repo.blocks.deleteMany([b.cid])
288+
const exists = await repo.blocks.has(b.cid)
289+
expect(exists).to.equal(false)
290+
})
291+
292+
it('throws when passed an invalid cid', () => {
293+
return expect(repo.blocks.deleteMany(['foo'])).to.eventually.be.rejected().with.property('code', 'ERR_INVALID_CID')
294+
})
295+
})
284296
})
285297
}

0 commit comments

Comments
 (0)