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

feat: add deleteMany method #92

Merged
merged 5 commits into from
May 5, 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
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@

## Table of Contents

- [Install](#install)
- [npm](#npm)
- [Usage](#usage)
- [Node.js](#nodejs)
- [Example](#example)
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
- [Browser: `<script>` Tag](#browser-script-tag)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)
- [IPFS Block Service](#ipfs-block-service)
- [Lead Maintainer](#lead-maintainer)
- [Table of Contents](#table-of-contents)
- [Install](#install)
- [npm](#npm)
- [Usage](#usage)
- [Node.js](#nodejs)
- [Example](#example)
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
- [Browser: `<script>` Tag](#browser-script-tag)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)

## Install

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

```js
const BlockService = require('ipfs-block-service')
const Block = require('ipfs-block')
const Block = require('ipld-block')
const multihashing = require('multihashing-async')
const IPFSRepo = require('ipfs-repo') // storage repo

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"cids": "^0.8.0",
"dirty-chai": "^2.0.1",
"fs-extra": "^9.0.0",
"ipfs-repo": "^2.0.0",
"ipfs-repo": "^2.1.0",
"ipld-block": "^0.9.1",
"lodash": "^4.17.11",
"multihashing-async": "^0.8.1"
Expand Down
28 changes: 25 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class BlockService {
/**
* Put a multiple blocks to the underlying datastore.
*
* @param {Array<Block>} blocks
* @param {AsyncIterator<Block>} blocks
* @param {Object} [options] - Options is an object with the following properties
* @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
* @returns {Promise}
Expand Down Expand Up @@ -102,10 +102,10 @@ class BlockService {
/**
* Get multiple blocks back from an array of cids.
*
* @param {Array<CID>} cids
* @param {AsyncIterator<CID>} cids
* @param {Object} [options] - Options is an object with the following properties
* @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
* @returns {Iterator<Block>}
* @returns {AsyncIterator<Block>}
*/
getMany (cids, options) {
if (!Array.isArray(cids)) {
Expand Down Expand Up @@ -135,6 +135,28 @@ class BlockService {

return this._repo.blocks.delete(cid, options)
}

/**
* Delete multiple blocks from the blockstore.
*
* @param {AsyncIterator<CID>} cids
* @param {Object} [options] - Options is an object with the following properties
* @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
* @returns {Promise}
*/
deleteMany (cids, options) {
const repo = this._repo

return this._repo.blocks.deleteMany((async function * () {
for await (const cid of cids) {
if (!await repo.blocks.has(cid)) {
throw errcode(new Error('blockstore: block not found'), 'ERR_BLOCK_NOT_FOUND')
}

yield cid
}
}()), options)
}
}

module.exports = BlockService
23 changes: 23 additions & 0 deletions test/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ module.exports = (repo) => {
)
})

it('deletes lots of blocks', async () => {
const data = Buffer.from('Will not live that much')

const hash = await multihashing(data, 'sha2-256')
const b = new Block(data, new CID(hash))

await bs.put(b)
await bs.deleteMany([b.cid])
const res = await bs._repo.blocks.has(b.cid)
expect(res).to.be.eql(false)
})

it('does not delete a blocks it does not have', async () => {
const data = Buffer.from('Will not live that much ' + Date.now())
const cid = new CID(await multihashing(data, 'sha2-256'))

await bs.deleteMany([cid])
.then(
() => expect.fail('Should have thrown'),
(err) => expect(err).to.have.property('code', 'ERR_BLOCK_NOT_FOUND')
)
})

it('stores and gets lots of blocks', async function () {
this.timeout(8 * 1000)

Expand Down