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

Commit af3918e

Browse files
clean up and updates
1 parent 70ceaf5 commit af3918e

File tree

3 files changed

+37
-59
lines changed

3 files changed

+37
-59
lines changed

API.md

+25-34
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ const BlockService = require('ipfs-block-service')
66

77
### `new BlockService(repo)`
88

9+
- `repo: Repo`
10+
911
Creates a new block service backed by [IPFS Repo][repo] `repo` for storage.
1012

1113
### `goOnline(bitswap)`
1214

15+
- `bitswap: Bitswap`
16+
1317
Add a bitswap instance that communicates with the network to retreive blocks
1418
that are not in the local store.
1519

@@ -24,53 +28,40 @@ Remove the bitswap instance and fall back to offline mode.
2428

2529
Returns a `Boolean` indicating if the block service is online or not.
2630

27-
### `addBlock(block, callback(err))`
31+
### `put(block, callback)`
32+
33+
- `block: Block`
34+
- `callback: Function`
2835

2936
Asynchronously adds a block instance to the underlying repo.
3037

31-
### `addBlocks(blocks, callback(err))`
38+
### `putStream()`
3239

33-
Asynchronously adds an array of block instances to the underlying repo.
40+
Returns a through pull-stream, which `Block`s can be written to, and
41+
that emits the meta data about the written block.
3442

35-
*Does not guarantee atomicity.*
43+
### `get(multihash [, extension], callback)`
3644

37-
### `getBlock(multihash, callback(err, block))`
45+
- `multihash: Multihash`
46+
- `extension: String`, defaults to 'data'
47+
- `callback: Function`
3848

3949
Asynchronously returns the block whose content multihash matches `multihash`.
40-
Returns an error (`err.code === 'ENOENT'`) if the block does not exist.
41-
42-
If the block could not be found, expect `err.code` to be `'ENOENT'`.
43-
44-
### `getBlocks(multihashes, callback(err, blocks))`
45-
46-
Asynchronously returns the blocks whose content multihashes match the array
47-
`multihashes`.
48-
49-
`blocks` is an object that maps each `multihash` to an object of the form
50-
51-
```js
52-
{
53-
err: Error
54-
block: Block
55-
}
56-
```
57-
58-
Expect `blocks[multihash].err.code === 'ENOENT'` and `blocks[multihash].block
59-
=== null` if a block did not exist.
6050

61-
*Does not guarantee atomicity.*
51+
### `getStream(multihash [, extension])`
6252

63-
### `deleteBlock(multihash, callback(err))`
53+
- `multihash: Multihash`
54+
- `extension: String`, defaults to 'data'
6455

65-
Asynchronously deletes the block from the store with content multihash matching
66-
`multihash`, if it exists.
56+
Returns a source pull-stream, which emits the requested block.
6757

68-
### `bs.deleteBlocks(multihashes, callback(err))`
58+
### `delete(multihashes, [, extension], callback)`
6959

70-
Asynchronously deletes all blocks from the store with content multihashes matching
71-
from the array `multihashes`.
60+
- `multihashes: Multihash|[]Multihash`
61+
- `extension: String`, defaults to 'data'- `extension: String`, defaults to 'data'
62+
- `callback: Function`
7263

73-
*Does not guarantee atomicity.*
64+
Deletes all blocks referenced by multihashes.
7465

75-
[multihash]: https://github.com/jbenet/js-multihash
66+
[multihash]: https://github.com/multiformats/js-multihash
7667
[repo]: https://github.com/ipfs/specs/tree/master/repo

src/index.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,18 @@ module.exports = class BlockService {
6868
return this._repo.blockstore.getStream(key, extension)
6969
}
7070

71-
deleteBlock (key, extension, callback) {
72-
this._repo.blockstore.delete(key, extension, callback)
73-
}
74-
75-
deleteBlocks (multihashes, extension, callback) {
71+
delete (keys, extension, callback) {
7672
if (typeof extension === 'function') {
7773
callback = extension
7874
extension = undefined
7975
}
8076

81-
if (!Array.isArray(multihashes)) {
82-
return callback(new Error('Invalid batch of multihashes'))
77+
if (!Array.isArray(keys)) {
78+
keys = [keys]
8379
}
8480

85-
parallelLimit(multihashes.map((multihash) => (next) => {
86-
this.deleteBlock(multihash, extension, next)
87-
}), 100, (err) => {
88-
callback(err)
89-
})
81+
parallelLimit(keys.map((key) => (next) => {
82+
this._repo.blockstore.delete(key, extension, next)
83+
}), 100, callback)
9084
}
9185
}

test/block-service-test.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ module.exports = (repo) => {
108108
const b = new Block('Will not live that much')
109109
bs.put(b, (err) => {
110110
expect(err).to.not.exist
111-
bs.deleteBlock(b.key, (err) => {
111+
bs.delete(b.key, (err) => {
112112
expect(err).to.not.exist
113113
bs.get(b.key, (err, block) => {
114114
expect(err).to.exist
@@ -118,8 +118,8 @@ module.exports = (repo) => {
118118
})
119119
})
120120

121-
it('deleteBlock: bad invocation', (done) => {
122-
bs.deleteBlock(null, (err) => {
121+
it('delete: bad invocation', (done) => {
122+
bs.delete(null, (err) => {
123123
expect(err).to.be.an('error')
124124
done()
125125
})
@@ -129,7 +129,7 @@ module.exports = (repo) => {
129129
const b = new Block('Will not live that much', 'ext')
130130
bs.put(b, (err) => {
131131
expect(err).to.not.exist
132-
bs.deleteBlock(b.key, 'ext', (err) => {
132+
bs.delete(b.key, 'ext', (err) => {
133133
expect(err).to.not.exist
134134
bs.get(b.key, 'ext', (err, block) => {
135135
expect(err).to.exist
@@ -141,7 +141,7 @@ module.exports = (repo) => {
141141

142142
it('delete a non existent block', (done) => {
143143
const b = new Block('I do not exist')
144-
bs.deleteBlock(b.key, (err) => {
144+
bs.delete(b.key, (err) => {
145145
expect(err).to.not.exist
146146
done()
147147
})
@@ -152,19 +152,12 @@ module.exports = (repo) => {
152152
const b2 = new Block('2')
153153
const b3 = new Block('3')
154154

155-
bs.deleteBlocks([b1, b2, b3], 'data', (err) => {
155+
bs.delete([b1, b2, b3], 'data', (err) => {
156156
expect(err).to.not.exist
157157
done()
158158
})
159159
})
160160

161-
it('deleteBlocks: bad invocation', (done) => {
162-
bs.deleteBlocks(null, (err) => {
163-
expect(err).to.be.an('error')
164-
done()
165-
})
166-
})
167-
168161
it('stores and gets lots of blocks', function (done) {
169162
this.timeout(60 * 1000)
170163

0 commit comments

Comments
 (0)