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

Commit 08c1e0e

Browse files
committed
feat: implementation of the new remove() function
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.
1 parent 743e679 commit 08c1e0e

File tree

7 files changed

+87
-75
lines changed

7 files changed

+87
-75
lines changed

src/index.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,39 @@ class IPLDResolver {
392392
return p
393393
}
394394

395-
remove (cids, callback) {
396-
this.bs.delete(cids, callback)
395+
/**
396+
* Remove IPLD Nodes by the given CIDs.
397+
*
398+
* Throws an error if any of the Blocks can’t be removed. This operation is
399+
* *not* atomic, some Blocks might have already been removed.
400+
*
401+
* @param {Iterable.<CID>} cids - The CIDs of the IPLD Nodes that should be removed
402+
* @return {void}
403+
*/
404+
remove (cids) {
405+
if (!typical.isIterable(cids) || typical.isString(cids) ||
406+
Buffer.isBuffer(cids)) {
407+
throw new Error('`cids` must be an iterable of CIDs')
408+
}
409+
410+
const next = () => {
411+
// End iteration if there are no more nodes to remove
412+
if (cids.length === 0) {
413+
return Promise.resolve({ done: true })
414+
}
415+
416+
return new Promise((resolve, reject) => {
417+
const cid = cids.shift()
418+
this.bs.delete(cid, (err) => {
419+
if (err) {
420+
return reject(err)
421+
}
422+
return resolve({ done: false, value: cid })
423+
})
424+
})
425+
}
426+
427+
return fancyIterator(next)
397428
}
398429

399430
/* */

test/ipld-bitcoin.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,13 @@ module.exports = (repo) => {
178178
expect(sameAsNode1).to.deep.equal(node1)
179179
return remove()
180180

181-
function remove () {
182-
return new Promise((resolve, reject) => {
183-
resolver.remove(cid, (err) => {
184-
expect(err).to.not.exist()
185-
const resultGet = resolver.get([cid])
186-
expect(resultGet.next()).to.eventually.be.rejected()
187-
.then(() => resolve())
188-
.catch((err) => reject(err))
189-
})
190-
})
181+
async function remove () {
182+
const resultRemove = resolver.remove([cid])
183+
// The items are deleted through iteration
184+
await resultRemove.last()
185+
// Verify that the item got really deleted
186+
const resultGet = resolver.get([cid])
187+
await expect(resultGet.next()).to.eventually.be.rejected()
191188
}
192189
})
193190
})

test/ipld-dag-cbor.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -247,27 +247,23 @@ module.exports = (repo) => {
247247
)
248248
})
249249

250-
// // TODO vmx 2018-11-30: remove this `get()` call with the new `get()`
251-
// it('resolver.remove', (done) => {
252-
// resolver.put(node1, { cid: cid1 }, (err) => {
253-
// expect(err).to.not.exist()
254-
// resolver.get(cid1, (err, result) => {
255-
// expect(err).to.not.exist()
256-
// expect(node1).to.eql(result.value)
257-
// remove()
258-
// })
259-
// })
260-
//
261-
// function remove () {
262-
// resolver.remove(cid1, (err) => {
263-
// expect(err).to.not.exist()
264-
// resolver.get(cid1, (err) => {
265-
// expect(err).to.exist()
266-
// done()
267-
// })
268-
// })
269-
// }
270-
// })
250+
it('resolver.remove', async () => {
251+
const resultPut = resolver.put([node1], multicodec.DAG_CBOR)
252+
const cid = await resultPut.first()
253+
const resultGet = resolver.get([cid])
254+
const sameAsNode1 = await resultGet.first()
255+
expect(sameAsNode1).to.deep.equal(node1)
256+
return remove()
257+
258+
async function remove () {
259+
const resultRemove = resolver.remove([cid])
260+
// The items are deleted through iteration
261+
await resultRemove.last()
262+
// Verify that the item got really deleted
263+
const resultGet = resolver.get([cid])
264+
await expect(resultGet.next()).to.eventually.be.rejected()
265+
}
266+
})
271267
})
272268
})
273269
}

test/ipld-dag-pb.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,13 @@ module.exports = (repo) => {
212212
expect(sameAsNode.data).to.deep.equal(node.data)
213213
return remove()
214214

215-
function remove () {
216-
return new Promise((resolve, reject) => {
217-
resolver.remove(cid, (err) => {
218-
expect(err).to.not.exist()
219-
const resultGet = resolver.get([cid])
220-
expect(resultGet.next()).to.eventually.be.rejected()
221-
.then(() => resolve())
222-
.catch((err) => reject(err))
223-
})
224-
})
215+
async function remove () {
216+
const resultRemove = resolver.remove([cid])
217+
// The items are deleted through iteration
218+
await resultRemove.last()
219+
// Verify that the item got really deleted
220+
const resultGet = resolver.get([cid])
221+
await expect(resultGet.next()).to.eventually.be.rejected()
225222
}
226223
})
227224
})

test/ipld-eth-block.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
'use strict'
33

44
const chai = require('chai')
5+
const chaiAsProised = require('chai-as-promised')
56
const dirtyChai = require('dirty-chai')
67
const expect = chai.expect
8+
chai.use(chaiAsProised)
79
chai.use(dirtyChai)
810
const BlockService = require('ipfs-block-service')
911
const ipldEthBlock = require('ipld-ethereum').ethBlock
@@ -136,18 +138,13 @@ module.exports = (repo) => {
136138
expect(sameAsNode1.raw).to.deep.equal(node1.raw)
137139
return remove()
138140

139-
function remove () {
140-
return new Promise((resolve, reject) => {
141-
resolver.remove(cid, (err) => {
142-
expect(err).to.not.exist()
143-
const resultGet = resolver.get([cid])
144-
expect(resultGet.first()).to.eventually.be.rejected()
145-
// eslint-disable-next-line max-nested-callbacks
146-
.then(() => resolve())
147-
// eslint-disable-next-line max-nested-callbacks
148-
.catch((err) => reject(err))
149-
})
150-
})
141+
async function remove () {
142+
const resultRemove = resolver.remove([cid])
143+
// The items are deleted through iteration
144+
await resultRemove.last()
145+
// Verify that the item got really deleted
146+
const resultGet = resolver.get([cid])
147+
await expect(resultGet.next()).to.eventually.be.rejected()
151148
}
152149
})
153150
})

test/ipld-git.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,13 @@ module.exports = (repo) => {
258258
expect(sameAsBlobNode).to.deep.equal(blobNode)
259259
return remove()
260260

261-
function remove () {
262-
return new Promise((resolve, reject) => {
263-
resolver.remove(cid, (err) => {
264-
expect(err).to.not.exist()
265-
const resultGet = resolver.get([cid])
266-
expect(resultGet.next()).to.eventually.be.rejected()
267-
.then(() => resolve())
268-
.catch((err) => reject(err))
269-
})
270-
})
261+
async function remove () {
262+
const resultRemove = resolver.remove([cid])
263+
// The items are deleted through iteration
264+
await resultRemove.last()
265+
// Verify that the item got really deleted
266+
const resultGet = resolver.get([cid])
267+
await expect(resultGet.next()).to.eventually.be.rejected()
271268
}
272269
})
273270
})

test/ipld-zcash.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,13 @@ module.exports = (repo) => {
183183
expect(sameAsNode1).to.deep.equal(node1)
184184
return remove()
185185

186-
function remove () {
187-
return new Promise((resolve, reject) => {
188-
resolver.remove(cid, (err) => {
189-
expect(err).to.not.exist()
190-
const resultGet = resolver.get([cid])
191-
expect(resultGet.next()).to.eventually.be.rejected()
192-
.then(() => resolve())
193-
.catch((err) => reject(err))
194-
})
195-
})
186+
async function remove () {
187+
const resultRemove = resolver.remove([cid])
188+
// The items are deleted through iteration
189+
await resultRemove.last()
190+
// Verify that the item got really deleted
191+
const resultGet = resolver.get([cid])
192+
await expect(resultGet.next()).to.eventually.be.rejected()
196193
}
197194
})
198195
})

0 commit comments

Comments
 (0)