From 08c1e0e416bd53f72ba5ff7315965f3cb825582f Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Wed, 12 Dec 2018 17:12:42 +0100 Subject: [PATCH] feat: implementation of the new `remove()` function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: `remove()` has a new API. The API docs for it: > Remove IPLD Nodes by the given `cids` - `cids` (`Iterable`): the CIDs of the IPLD Nodes that should be removed. Throws an error if any of the Blocks can’t be removed. This operation is *not* atomic, some Blocks might have already been removed. --- src/index.js | 35 +++++++++++++++++++++++++++++++++-- test/ipld-bitcoin.js | 17 +++++++---------- test/ipld-dag-cbor.js | 38 +++++++++++++++++--------------------- test/ipld-dag-pb.js | 17 +++++++---------- test/ipld-eth-block.js | 21 +++++++++------------ test/ipld-git.js | 17 +++++++---------- test/ipld-zcash.js | 17 +++++++---------- 7 files changed, 87 insertions(+), 75 deletions(-) diff --git a/src/index.js b/src/index.js index 9dfa7d6..67f5218 100644 --- a/src/index.js +++ b/src/index.js @@ -392,8 +392,39 @@ class IPLDResolver { return p } - remove (cids, callback) { - this.bs.delete(cids, callback) + /** + * Remove IPLD Nodes by the given CIDs. + * + * Throws an error if any of the Blocks can’t be removed. This operation is + * *not* atomic, some Blocks might have already been removed. + * + * @param {Iterable.} cids - The CIDs of the IPLD Nodes that should be removed + * @return {void} + */ + remove (cids) { + if (!typical.isIterable(cids) || typical.isString(cids) || + Buffer.isBuffer(cids)) { + throw new Error('`cids` must be an iterable of CIDs') + } + + const next = () => { + // End iteration if there are no more nodes to remove + if (cids.length === 0) { + return Promise.resolve({ done: true }) + } + + return new Promise((resolve, reject) => { + const cid = cids.shift() + this.bs.delete(cid, (err) => { + if (err) { + return reject(err) + } + return resolve({ done: false, value: cid }) + }) + }) + } + + return fancyIterator(next) } /* */ diff --git a/test/ipld-bitcoin.js b/test/ipld-bitcoin.js index c3c6a75..7f1d33e 100644 --- a/test/ipld-bitcoin.js +++ b/test/ipld-bitcoin.js @@ -178,16 +178,13 @@ module.exports = (repo) => { expect(sameAsNode1).to.deep.equal(node1) return remove() - function remove () { - return new Promise((resolve, reject) => { - resolver.remove(cid, (err) => { - expect(err).to.not.exist() - const resultGet = resolver.get([cid]) - expect(resultGet.next()).to.eventually.be.rejected() - .then(() => resolve()) - .catch((err) => reject(err)) - }) - }) + async function remove () { + const resultRemove = resolver.remove([cid]) + // The items are deleted through iteration + await resultRemove.last() + // Verify that the item got really deleted + const resultGet = resolver.get([cid]) + await expect(resultGet.next()).to.eventually.be.rejected() } }) }) diff --git a/test/ipld-dag-cbor.js b/test/ipld-dag-cbor.js index 5a5d4e1..13f80b4 100644 --- a/test/ipld-dag-cbor.js +++ b/test/ipld-dag-cbor.js @@ -247,27 +247,23 @@ module.exports = (repo) => { ) }) - // // TODO vmx 2018-11-30: remove this `get()` call with the new `get()` - // it('resolver.remove', (done) => { - // resolver.put(node1, { cid: cid1 }, (err) => { - // expect(err).to.not.exist() - // resolver.get(cid1, (err, result) => { - // expect(err).to.not.exist() - // expect(node1).to.eql(result.value) - // remove() - // }) - // }) - // - // function remove () { - // resolver.remove(cid1, (err) => { - // expect(err).to.not.exist() - // resolver.get(cid1, (err) => { - // expect(err).to.exist() - // done() - // }) - // }) - // } - // }) + it('resolver.remove', async () => { + const resultPut = resolver.put([node1], multicodec.DAG_CBOR) + const cid = await resultPut.first() + const resultGet = resolver.get([cid]) + const sameAsNode1 = await resultGet.first() + expect(sameAsNode1).to.deep.equal(node1) + return remove() + + async function remove () { + const resultRemove = resolver.remove([cid]) + // The items are deleted through iteration + await resultRemove.last() + // Verify that the item got really deleted + const resultGet = resolver.get([cid]) + await expect(resultGet.next()).to.eventually.be.rejected() + } + }) }) }) } diff --git a/test/ipld-dag-pb.js b/test/ipld-dag-pb.js index 6908256..2dc23ea 100644 --- a/test/ipld-dag-pb.js +++ b/test/ipld-dag-pb.js @@ -212,16 +212,13 @@ module.exports = (repo) => { expect(sameAsNode.data).to.deep.equal(node.data) return remove() - function remove () { - return new Promise((resolve, reject) => { - resolver.remove(cid, (err) => { - expect(err).to.not.exist() - const resultGet = resolver.get([cid]) - expect(resultGet.next()).to.eventually.be.rejected() - .then(() => resolve()) - .catch((err) => reject(err)) - }) - }) + async function remove () { + const resultRemove = resolver.remove([cid]) + // The items are deleted through iteration + await resultRemove.last() + // Verify that the item got really deleted + const resultGet = resolver.get([cid]) + await expect(resultGet.next()).to.eventually.be.rejected() } }) }) diff --git a/test/ipld-eth-block.js b/test/ipld-eth-block.js index 4a3c4bd..f60aabf 100644 --- a/test/ipld-eth-block.js +++ b/test/ipld-eth-block.js @@ -2,8 +2,10 @@ 'use strict' const chai = require('chai') +const chaiAsProised = require('chai-as-promised') const dirtyChai = require('dirty-chai') const expect = chai.expect +chai.use(chaiAsProised) chai.use(dirtyChai) const BlockService = require('ipfs-block-service') const ipldEthBlock = require('ipld-ethereum').ethBlock @@ -136,18 +138,13 @@ module.exports = (repo) => { expect(sameAsNode1.raw).to.deep.equal(node1.raw) return remove() - function remove () { - return new Promise((resolve, reject) => { - resolver.remove(cid, (err) => { - expect(err).to.not.exist() - const resultGet = resolver.get([cid]) - expect(resultGet.first()).to.eventually.be.rejected() - // eslint-disable-next-line max-nested-callbacks - .then(() => resolve()) - // eslint-disable-next-line max-nested-callbacks - .catch((err) => reject(err)) - }) - }) + async function remove () { + const resultRemove = resolver.remove([cid]) + // The items are deleted through iteration + await resultRemove.last() + // Verify that the item got really deleted + const resultGet = resolver.get([cid]) + await expect(resultGet.next()).to.eventually.be.rejected() } }) }) diff --git a/test/ipld-git.js b/test/ipld-git.js index a6ba834..10de0a4 100644 --- a/test/ipld-git.js +++ b/test/ipld-git.js @@ -258,16 +258,13 @@ module.exports = (repo) => { expect(sameAsBlobNode).to.deep.equal(blobNode) return remove() - function remove () { - return new Promise((resolve, reject) => { - resolver.remove(cid, (err) => { - expect(err).to.not.exist() - const resultGet = resolver.get([cid]) - expect(resultGet.next()).to.eventually.be.rejected() - .then(() => resolve()) - .catch((err) => reject(err)) - }) - }) + async function remove () { + const resultRemove = resolver.remove([cid]) + // The items are deleted through iteration + await resultRemove.last() + // Verify that the item got really deleted + const resultGet = resolver.get([cid]) + await expect(resultGet.next()).to.eventually.be.rejected() } }) }) diff --git a/test/ipld-zcash.js b/test/ipld-zcash.js index 6c0c7cb..35a25d3 100644 --- a/test/ipld-zcash.js +++ b/test/ipld-zcash.js @@ -183,16 +183,13 @@ module.exports = (repo) => { expect(sameAsNode1).to.deep.equal(node1) return remove() - function remove () { - return new Promise((resolve, reject) => { - resolver.remove(cid, (err) => { - expect(err).to.not.exist() - const resultGet = resolver.get([cid]) - expect(resultGet.next()).to.eventually.be.rejected() - .then(() => resolve()) - .catch((err) => reject(err)) - }) - }) + async function remove () { + const resultRemove = resolver.remove([cid]) + // The items are deleted through iteration + await resultRemove.last() + // Verify that the item got really deleted + const resultGet = resolver.get([cid]) + await expect(resultGet.next()).to.eventually.be.rejected() } }) })