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

Integration with bitswap #19

Merged
merged 3 commits into from
May 7, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 0 additions & 68 deletions src/block-service.js

This file was deleted.

106 changes: 105 additions & 1 deletion src/index.js
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, bitswap will check the repo if it already has the block? (Thought that logic would be here, but having bitswap doing the job is also fine)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can docs be improved along the way too, please?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, bitswap will check the repo if it already has the block?

Yes, I need that logic in bitswap to be sure that no inefficiencies happen, so no need to replicate it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can docs be improved along the way too, please?

@diasdavid done

} 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)
})
}
}
Loading