From 6ddb52dcedf2df245255b78a213dc826f4a88f47 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 4 Jun 2018 10:09:01 +0100 Subject: [PATCH 1/5] chore: update aegir --- package.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/package.json b/package.json index 0e979db..42dcf25 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,6 @@ "coverage-publish": "aegir coverage --provider coveralls", "docs": "aegir docs" }, - "pre-commit": [ - "lint", - "test" - ], "repository": { "type": "git", "url": "git+https://github.com/ipfs/js-ipfs-block-service.git" @@ -44,7 +40,6 @@ "lodash": "^4.17.11", "multihashing-async": "~0.5.1", "ncp": "^2.0.0", - "pre-commit": "^1.2.2", "rimraf": "^2.6.2" }, "engines": { From 5dacaeca9035d398db9ddd56a9c8c7da76ccaa85 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 4 Jun 2018 10:20:11 +0100 Subject: [PATCH 2/5] feat: .getMany --- src/index.js | 20 ++++++++++++++++++++ test/block-service-test.js | 25 ++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index c38a245..b003665 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,7 @@ 'use strict' +const asyncMap = require('async/map') + /** * BlockService is a hybrid block datastore. It stores data in a local * datastore and may retrieve data from a remote Exchange. @@ -93,6 +95,24 @@ class BlockService { return this._repo.blocks.get(cid, callback) } + /** + * Get multiple blocks a block by cid. + * + * @param {CID} cid + * @param {function(Error, Block)} callback + * @returns {void} + */ + getMany (cids, callback) { + if (!Array.isArray(cids)) { + return callback(new Error('first arg must be an array of cids')) + } + if (this.hasExchange()) { + return this._bitswap.getMany(cids, callback) + } + + asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback) + } + /** * Delete a block from the blockstore. * diff --git a/test/block-service-test.js b/test/block-service-test.js index 01e5002..b572c80 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -55,6 +55,7 @@ module.exports = (repo) => { it('store many blocks', (done) => { const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')] + map(data, (d, cb) => { multihashing(d, 'sha2-256', (err, hash) => { expect(err).to.not.exist() @@ -66,8 +67,9 @@ module.exports = (repo) => { }) }) - it('get many blocks', (done) => { + it('get many blocks through get', (done) => { const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')] + waterfall([ (cb) => map(data, (d, cb) => { multihashing(d, 'sha2-256', (err, hash) => { @@ -87,6 +89,27 @@ module.exports = (repo) => { ], done) }) + it('get many blocks through getMany', (done) => { + const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')] + + waterfall([ + (cb) => map(data, (d, cb) => { + multihashing(d, 'sha2-256', (err, hash) => { + expect(err).to.not.exist() + cb(null, new Block(d, new CID(hash))) + }) + }, cb), + (blocks, cb) => map(blocks, (b, cb) => cb(null, b.cid), (err, cids) => { + expect(err).to.not.exist() + bs.getMany(cids, (err, _blocks) => { + expect(err).to.not.exist() + expect(blocks).to.eql(blocks) + cb() + }) + }) + ], done) + }) + it('delete a block', (done) => { const data = Buffer.from('Will not live that much') multihashing(data, 'sha2-256', (err, hash) => { From e265231f4fce3402a58cbafe88f9c77045e278bb Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 4 Jun 2018 10:28:17 +0100 Subject: [PATCH 3/5] test: refactor offline tests --- test/block-service-test.js | 123 +++++++++++++++---------------------- 1 file changed, 51 insertions(+), 72 deletions(-) diff --git a/test/block-service-test.js b/test/block-service-test.js index b572c80..0054bde 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -17,101 +17,80 @@ const BlockService = require('../src') module.exports = (repo) => { describe('block-service', () => { let bs + let testBlocks - before(() => { + before((done) => { bs = new BlockService(repo) + + const data = [ + Buffer.from('1'), + Buffer.from('2'), + Buffer.from('3'), + Buffer.from('A random data block') + ] + + map(data, (d, cb) => { + multihashing(d, 'sha2-256', (err, hash) => { + expect(err).to.not.exist() + cb(null, new Block(d, new CID(hash))) + }) + }, (err, blocks) => { + expect(err).to.not.exist() + testBlocks = blocks + done() + }) }) - describe('offline', () => { + describe('fetch only from local Repo', () => { it('store and get a block', (done) => { - const data = Buffer.from('A random data block') - multihashing(data, 'sha2-256', (err, hash) => { - expect(err).to.not.exist() - const b = new Block(data, new CID(hash)) + const b = testBlocks[3] - waterfall([ - (cb) => bs.put(b, cb), - (cb) => bs.get(b.cid, cb), - (res, cb) => { - expect(res).to.be.eql(b) - cb() - } - ], done) - }) + waterfall([ + (cb) => bs.put(b, cb), + (cb) => bs.get(b.cid, cb), + (res, cb) => { + expect(res).to.eql(b) + cb() + } + ], done) }) - it('get a non existent block', (done) => { - const data = Buffer.from('Not stored') + it('get a non stored yet block', (done) => { + const b = testBlocks[2] - multihashing(data, 'sha2-256', (err, hash) => { - expect(err).to.not.exist() - bs.get(new CID(hash), (err, block) => { - expect(err).to.exist() - expect(block).to.not.exist() - done() - }) + bs.get(b.cid, (err, block) => { + expect(err).to.exist() + expect(block).to.not.exist() + done() }) }) it('store many blocks', (done) => { - const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')] + bs.putMany(testBlocks, done) + }) - map(data, (d, cb) => { - multihashing(d, 'sha2-256', (err, hash) => { - expect(err).to.not.exist() - cb(null, new Block(d, new CID(hash))) - }) - }, (err, blocks) => { + it('get many blocks through .get', (done) => { + map(testBlocks, (b, cb) => bs.get(b.cid, cb), (err, blocks) => { expect(err).to.not.exist() - bs.putMany(blocks, done) + expect(blocks).to.eql(testBlocks) + done() }) }) - it('get many blocks through get', (done) => { - const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')] - - waterfall([ - (cb) => map(data, (d, cb) => { - multihashing(d, 'sha2-256', (err, hash) => { - expect(err).to.not.exist() - cb(null, new Block(d, new CID(hash))) - }) - }, cb), - (blocks, cb) => map( - blocks, - (b, cb) => bs.get(b.cid, cb), - (err, res) => { - expect(err).to.not.exist() - expect(res).to.be.eql(blocks) - cb() - } - ) - ], done) - }) - - it('get many blocks through getMany', (done) => { - const data = [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')] - - waterfall([ - (cb) => map(data, (d, cb) => { - multihashing(d, 'sha2-256', (err, hash) => { - expect(err).to.not.exist() - cb(null, new Block(d, new CID(hash))) - }) - }, cb), - (blocks, cb) => map(blocks, (b, cb) => cb(null, b.cid), (err, cids) => { + it('get many blocks through .getMany', (done) => { + map(testBlocks, (b, cb) => cb(null, b.cid), (err, cids) => { + expect(err).to.not.exist() + bs.getMany(cids, (err, _blocks) => { expect(err).to.not.exist() - bs.getMany(cids, (err, _blocks) => { - expect(err).to.not.exist() - expect(blocks).to.eql(blocks) - cb() - }) + expect(testBlocks).to.eql(testBlocks) + done() }) - ], done) + }) }) it('delete a block', (done) => { const data = Buffer.from('Will not live that much') + multihashing(data, 'sha2-256', (err, hash) => { expect(err).to.not.exist() const b = new Block(data, new CID(hash)) @@ -163,7 +142,7 @@ module.exports = (repo) => { }) }) - describe('has exchange', () => { + describe('fetch through Bitswap (has exchange)', () => { beforeEach(() => { bs = new BlockService(repo) }) From 48a3687efa5fc9028324b7b8995ae54432dc00e4 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 4 Jun 2018 10:31:14 +0100 Subject: [PATCH 4/5] chore: fix jsdocs --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b003665..6ae7978 100644 --- a/src/index.js +++ b/src/index.js @@ -98,7 +98,7 @@ class BlockService { /** * Get multiple blocks a block by cid. * - * @param {CID} cid + * @param {Array} cids * @param {function(Error, Block)} callback * @returns {void} */ From fcf773574ced6cd844d66158b6cc8b1c960382f5 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 27 Oct 2018 18:38:45 +0200 Subject: [PATCH 5/5] chore: apply CR --- src/index.js | 31 +++++++++++++++---------------- test/block-service-test.js | 2 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 6ae7978..ea282cf 100644 --- a/src/index.js +++ b/src/index.js @@ -59,10 +59,10 @@ class BlockService { */ put (block, callback) { if (this.hasExchange()) { - return this._bitswap.put(block, callback) + this._bitswap.put(block, callback) + } else { + this._repo.blocks.put(block, callback) } - - this._repo.blocks.put(block, callback) } /** @@ -74,10 +74,10 @@ class BlockService { */ putMany (blocks, callback) { if (this.hasExchange()) { - return this._bitswap.putMany(blocks, callback) + this._bitswap.putMany(blocks, callback) + } else { + this._repo.blocks.putMany(blocks, callback) } - - this._repo.blocks.putMany(blocks, callback) } /** @@ -89,14 +89,14 @@ class BlockService { */ get (cid, callback) { if (this.hasExchange()) { - return this._bitswap.get(cid, callback) + this._bitswap.get(cid, callback) + } else { + this._repo.blocks.get(cid, callback) } - - return this._repo.blocks.get(cid, callback) } /** - * Get multiple blocks a block by cid. + * Get multiple blocks back from an array of cids. * * @param {Array} cids * @param {function(Error, Block)} callback @@ -104,13 +104,12 @@ class BlockService { */ getMany (cids, callback) { if (!Array.isArray(cids)) { - return callback(new Error('first arg must be an array of cids')) + callback(new Error('first arg must be an array of cids')) + } else if (this.hasExchange()) { + this._bitswap.getMany(cids, callback) + } else { + asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback) } - if (this.hasExchange()) { - return this._bitswap.getMany(cids, callback) - } - - asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback) } /** diff --git a/test/block-service-test.js b/test/block-service-test.js index 0054bde..76936c7 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -82,7 +82,7 @@ module.exports = (repo) => { expect(err).to.not.exist() bs.getMany(cids, (err, _blocks) => { expect(err).to.not.exist() - expect(testBlocks).to.eql(testBlocks) + expect(testBlocks).to.eql(_blocks) done() }) })