This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ipfs/feat/bitswap
Integration with bitswap
- Loading branch information
Showing
5 changed files
with
374 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# API | ||
|
||
```js | ||
const BlockService = require('ipfs-block-service') | ||
``` | ||
|
||
### `new BlockService(repo)` | ||
|
||
Creates a new block service backed by [IPFS Repo][repo] `repo` for storage. | ||
|
||
### `goOnline(bitswap)` | ||
|
||
Add a bitswap instance that communicates with the network to retreive blocks | ||
that are not in the local store. | ||
|
||
If the node is online all requests for blocks first check locally and | ||
afterwards ask the network for the blocks. | ||
|
||
### `goOffline()` | ||
|
||
Remove the bitswap instance and fall back to offline mode. | ||
|
||
### `isOnline()` | ||
|
||
Returns a `Boolean` indicating if the block service is online or not. | ||
|
||
### `addBlock(block, callback(err))` | ||
|
||
Asynchronously adds a block instance to the underlying repo. | ||
|
||
### `addBlocks(blocks, callback(err))` | ||
|
||
Asynchronously adds an array of block instances to the underlying repo. | ||
|
||
*Does not guarantee atomicity.* | ||
|
||
### `getBlock(multihash, callback(err, block))` | ||
|
||
Asynchronously returns the block whose content multihash matches `multihash`. | ||
Returns an error (`err.code === 'ENOENT'`) if the block does not exist. | ||
|
||
If the block could not be found, expect `err.code` to be `'ENOENT'`. | ||
|
||
### `getBlocks(multihashes, callback(err, blocks))` | ||
|
||
Asynchronously returns the blocks whose content multihashes match the array | ||
`multihashes`. | ||
|
||
`blocks` is an object that maps each `multihash` to an object of the form | ||
|
||
```js | ||
{ | ||
err: Error | ||
block: Block | ||
} | ||
``` | ||
|
||
Expect `blocks[multihash].err.code === 'ENOENT'` and `blocks[multihash].block | ||
=== null` if a block did not exist. | ||
|
||
*Does not guarantee atomicity.* | ||
|
||
### `deleteBlock(multihash, callback(err))` | ||
|
||
Asynchronously deletes the block from the store with content multihash matching | ||
`multihash`, if it exists. | ||
|
||
### `bs.deleteBlocks(multihashes, callback(err))` | ||
|
||
Asynchronously deletes all blocks from the store with content multihashes matching | ||
from the array `multihashes`. | ||
|
||
*Does not guarantee atomicity.* | ||
|
||
[multihash]: https://github.com/jbenet/js-multihash | ||
[repo]: https://github.com/ipfs/specs/tree/master/repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,107 @@ | ||
'use strict' | ||
|
||
exports = module.exports = require('./block-service.js') | ||
const async = require('async') | ||
|
||
// BlockService is a hybrid block datastore. It stores data in a local | ||
// datastore and may retrieve data from a remote Exchange. | ||
// It uses an internal `datastore.Datastore` instance to store values. | ||
module.exports = class BlockService { | ||
constructor (ipfsRepo) { | ||
this._repo = ipfsRepo | ||
this._bitswap = null | ||
} | ||
|
||
goOnline (bitswap) { | ||
this._bitswap = bitswap | ||
} | ||
|
||
goOffline () { | ||
this._bitswap = null | ||
} | ||
|
||
isOnline () { | ||
return this._bitswap != null | ||
} | ||
|
||
addBlock (block, extension, callback) { | ||
if (this.isOnline()) { | ||
if (typeof extension === 'function') { | ||
callback = extension | ||
extension = undefined | ||
} | ||
|
||
this._bitswap.hasBlock(block, callback) | ||
} else { | ||
this._repo.datastore.put(block, extension, callback) | ||
} | ||
} | ||
|
||
addBlocks (blocks, callback) { | ||
if (!Array.isArray(blocks)) { | ||
return callback(new Error('expects an array of Blocks')) | ||
} | ||
|
||
async.eachLimit(blocks, 100, (block, next) => { | ||
this.addBlock(block, next) | ||
}, callback) | ||
} | ||
|
||
getBlock (key, extension, callback) { | ||
if (this.isOnline()) { | ||
if (typeof extension === 'function') { | ||
callback = extension | ||
extension = undefined | ||
} | ||
|
||
this._bitswap.getBlock(key, callback) | ||
} else { | ||
this._repo.datastore.get(key, extension, callback) | ||
} | ||
} | ||
|
||
getBlocks (multihashes, extension, callback) { | ||
if (typeof extension === 'function') { | ||
callback = extension | ||
extension = undefined | ||
} | ||
|
||
if (!Array.isArray(multihashes)) { | ||
return callback(new Error('Invalid batch of multihashes')) | ||
} | ||
|
||
var results = {} | ||
|
||
async.eachLimit(multihashes, 100, (multihash, next) => { | ||
this.getBlock(multihash, extension, (err, block) => { | ||
results[multihash] = { | ||
err: err, | ||
block: block | ||
} | ||
next() | ||
}) | ||
}, (err) => { | ||
callback(err, results) | ||
}) | ||
} | ||
|
||
deleteBlock (key, extension, callback) { | ||
this._repo.datastore.delete(key, extension, callback) | ||
} | ||
|
||
deleteBlocks (multihashes, extension, callback) { | ||
if (typeof extension === 'function') { | ||
callback = extension | ||
extension = undefined | ||
} | ||
|
||
if (!Array.isArray(multihashes)) { | ||
return callback(new Error('Invalid batch of multihashes')) | ||
} | ||
|
||
async.eachLimit(multihashes, 100, (multihash, next) => { | ||
this.deleteBlock(multihash, extension, next) | ||
}, (err) => { | ||
callback(err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.