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

Commit c57e26f

Browse files
authored
feat: add deleteMany method (#92)
Similar to the putMany and getMany methods, deleteMany allows passing streams of CIDs into the module.
1 parent b0568ec commit c57e26f

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

README.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@
3131

3232
## Table of Contents
3333

34-
- [Install](#install)
35-
- [npm](#npm)
36-
- [Usage](#usage)
37-
- [Node.js](#nodejs)
38-
- [Example](#example)
39-
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
40-
- [Browser: `<script>` Tag](#browser-script-tag)
41-
- [API](#api)
42-
- [Contribute](#contribute)
43-
- [License](#license)
34+
- [IPFS Block Service](#ipfs-block-service)
35+
- [Lead Maintainer](#lead-maintainer)
36+
- [Table of Contents](#table-of-contents)
37+
- [Install](#install)
38+
- [npm](#npm)
39+
- [Usage](#usage)
40+
- [Node.js](#nodejs)
41+
- [Example](#example)
42+
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
43+
- [Browser: `<script>` Tag](#browser-script-tag)
44+
- [API](#api)
45+
- [Contribute](#contribute)
46+
- [License](#license)
4447

4548
## Install
4649

@@ -63,7 +66,7 @@ const BlockService = require('ipfs-block-service')
6366

6467
```js
6568
const BlockService = require('ipfs-block-service')
66-
const Block = require('ipfs-block')
69+
const Block = require('ipld-block')
6770
const multihashing = require('multihashing-async')
6871
const IPFSRepo = require('ipfs-repo') // storage repo
6972

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"cids": "^0.8.0",
3838
"dirty-chai": "^2.0.1",
3939
"fs-extra": "^9.0.0",
40-
"ipfs-repo": "^2.0.0",
40+
"ipfs-repo": "^2.1.0",
4141
"ipld-block": "^0.9.1",
4242
"lodash": "^4.17.11",
4343
"multihashing-async": "^0.8.1"

src/index.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BlockService {
7070
/**
7171
* Put a multiple blocks to the underlying datastore.
7272
*
73-
* @param {Array<Block>} blocks
73+
* @param {AsyncIterator<Block>} blocks
7474
* @param {Object} [options] - Options is an object with the following properties
7575
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
7676
* @returns {Promise}
@@ -102,10 +102,10 @@ class BlockService {
102102
/**
103103
* Get multiple blocks back from an array of cids.
104104
*
105-
* @param {Array<CID>} cids
105+
* @param {AsyncIterator<CID>} cids
106106
* @param {Object} [options] - Options is an object with the following properties
107107
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
108-
* @returns {Iterator<Block>}
108+
* @returns {AsyncIterator<Block>}
109109
*/
110110
getMany (cids, options) {
111111
if (!Array.isArray(cids)) {
@@ -135,6 +135,28 @@ class BlockService {
135135

136136
return this._repo.blocks.delete(cid, options)
137137
}
138+
139+
/**
140+
* Delete multiple blocks from the blockstore.
141+
*
142+
* @param {AsyncIterator<CID>} cids
143+
* @param {Object} [options] - Options is an object with the following properties
144+
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
145+
* @returns {Promise}
146+
*/
147+
deleteMany (cids, options) {
148+
const repo = this._repo
149+
150+
return this._repo.blocks.deleteMany((async function * () {
151+
for await (const cid of cids) {
152+
if (!await repo.blocks.has(cid)) {
153+
throw errcode(new Error('blockstore: block not found'), 'ERR_BLOCK_NOT_FOUND')
154+
}
155+
156+
yield cid
157+
}
158+
}()), options)
159+
}
138160
}
139161

140162
module.exports = BlockService

test/block-service-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ module.exports = (repo) => {
9191
)
9292
})
9393

94+
it('deletes lots of blocks', async () => {
95+
const data = Buffer.from('Will not live that much')
96+
97+
const hash = await multihashing(data, 'sha2-256')
98+
const b = new Block(data, new CID(hash))
99+
100+
await bs.put(b)
101+
await bs.deleteMany([b.cid])
102+
const res = await bs._repo.blocks.has(b.cid)
103+
expect(res).to.be.eql(false)
104+
})
105+
106+
it('does not delete a blocks it does not have', async () => {
107+
const data = Buffer.from('Will not live that much ' + Date.now())
108+
const cid = new CID(await multihashing(data, 'sha2-256'))
109+
110+
await bs.deleteMany([cid])
111+
.then(
112+
() => expect.fail('Should have thrown'),
113+
(err) => expect(err).to.have.property('code', 'ERR_BLOCK_NOT_FOUND')
114+
)
115+
})
116+
94117
it('stores and gets lots of blocks', async function () {
95118
this.timeout(8 * 1000)
96119

0 commit comments

Comments
 (0)