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

Commit

Permalink
feat: implementation of the new remove() function
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `remove()` has a new API.

The API docs for it:

> Remove IPLD Nodes by the given `cids`

 - `cids` (`Iterable<CID>`): 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.
  • Loading branch information
vmx committed Mar 21, 2019
1 parent 743e679 commit 08c1e0e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 75 deletions.
35 changes: 33 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<CID>} 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)
}

/* */
Expand Down
17 changes: 7 additions & 10 deletions test/ipld-bitcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
Expand Down
38 changes: 17 additions & 21 deletions test/ipld-dag-cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
})
}
17 changes: 7 additions & 10 deletions test/ipld-dag-pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
Expand Down
21 changes: 9 additions & 12 deletions test/ipld-eth-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
})
})
Expand Down
17 changes: 7 additions & 10 deletions test/ipld-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
Expand Down
17 changes: 7 additions & 10 deletions test/ipld-zcash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
Expand Down

0 comments on commit 08c1e0e

Please sign in to comment.