Skip to content

Commit

Permalink
feat: cid agnostic blockstore .get and .has (#184)
Browse files Browse the repository at this point in the history
* feat: cid agnostic get
* fix: add return
* test: add tests for CID version agnostic get
* test: add tests for CID agnostic .has
* chore: appease linter

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw authored and jacobheun committed Dec 7, 2018
1 parent 1531ed1 commit 18cca08
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 25 deletions.
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Repo = require('ipfs-repo')
const repo = new Repo('/Users/awesome/.jsipfs')

repo.init({my: 'config'}, (err) => {
repo.init({ my: 'config' }, (err) => {
if (err) {
throw err
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"npm": ">=3.0.0"
},
"devDependencies": {
"aegir": "^15.3.1",
"aegir": "^17.1.1",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"lodash": "^4.17.11",
"memdown": "^1.4.1",
"memdown": "^3.0.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.1",
"ncp": "^2.0.0",
Expand All @@ -52,17 +52,17 @@
"async": "^2.6.1",
"base32.js": "~0.1.0",
"big.js": "^5.2.2",
"cids": "~0.5.5",
"cids": "~0.5.7",
"datastore-core": "~0.6.0",
"datastore-fs": "~0.7.0",
"datastore-level": "~0.10.0",
"debug": "^4.1.0",
"interface-datastore": "~0.6.0",
"ipfs-block": "~0.7.1",
"ipfs-block": "~0.8.0",
"lodash.get": "^4.4.2",
"lodash.has": "^4.5.2",
"lodash.set": "^4.3.2",
"multiaddr": "^5.0.0",
"multiaddr": "^6.0.0",
"proper-lockfile": "^3.2.0",
"pull-stream": "^3.6.9",
"sort-keys": "^2.0.0"
Expand Down
45 changes: 41 additions & 4 deletions src/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,29 @@ function createBaseStore (store) {
})
}

const k = cidToDsKey(cid)
store.get(k, (err, blockData) => {
if (err) { return callback(err) }
const key = cidToDsKey(cid)
store.get(key, (err, blockData) => {
if (err) {
// If not found, we try with the other CID version.
// If exists, then store that block under the CID that was requested.
// Some duplication occurs.
if (err.code === 'ERR_NOT_FOUND') {
const otherCid = cidToOtherVersion(cid)
if (!otherCid) return callback(err)

const otherKey = cidToDsKey(otherCid)
return store.get(otherKey, (err, blockData) => {
if (err) return callback(err)

store.put(key, blockData, (err) => {
if (err) return callback(err)
callback(null, new Block(blockData, cid))
})
})
}

return callback(err)
}

callback(null, new Block(blockData, cid))
})
Expand Down Expand Up @@ -140,7 +160,16 @@ function createBaseStore (store) {
})
}

store.has(cidToDsKey(cid), callback)
store.has(cidToDsKey(cid), (err, exists) => {
if (err) return callback(err)
if (exists) return callback(null, true)

// If not found, we try with the other CID version.
const otherCid = cidToOtherVersion(cid)
if (!otherCid) return callback(null, false)

store.has(cidToDsKey(otherCid), callback)
})
},
/**
* Delete a block from the store
Expand All @@ -164,3 +193,11 @@ function createBaseStore (store) {
}
}
}

function cidToOtherVersion (cid) {
try {
return cid.version === 0 ? cid.toV1() : cid.toV0()
} catch (err) {
return null
}
}
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class IpfsRepo {
* @param {object} options - Configuration
*/
constructor (repoPath, options) {
assert.equal(typeof repoPath, 'string', 'missing repoPath')
assert.strictEqual(typeof repoPath, 'string', 'missing repoPath')

this.options = buildOptions(options)
this.closed = true
Expand Down Expand Up @@ -170,7 +170,7 @@ class IpfsRepo {
return callback(err, null)
}

assert.equal(typeof lockfile.close, 'function', 'Locks must have a close method')
assert.strictEqual(typeof lockfile.close, 'function', 'Locks must have a close method')
callback(null, lockfile)
})
}
Expand Down Expand Up @@ -273,7 +273,7 @@ class IpfsRepo {
options = {}
}

options = Object.assign({}, {human: false}, options)
options = Object.assign({}, { human: false }, options)

parallel({
storageMax: (cb) => this.config.get('Datastore.StorageMax', (err, max) => {
Expand Down
14 changes: 8 additions & 6 deletions src/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ exports.lock = (dir, callback) => {
const file = path.join(dir, lockFile)
log('locking %s', file)

lock(dir, {lockfilePath: file, stale: STALE_TIME})
lock(dir, { lockfilePath: file, stale: STALE_TIME })
.then(release => {
callback(null, {close: (cb) => {
release()
.then(() => cb())
.catch(err => cb(err))
}})
callback(null, {
close: (cb) => {
release()
.then(() => cb())
.catch(err => cb(err))
}
})
})
.catch(err => callback(err))
}
84 changes: 82 additions & 2 deletions test/blockstore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = (repo) => {
})

it('empty value', (done) => {
const d = new Buffer(0)
const d = Buffer.alloc(0)
multihashing(d, 'sha2-256', (err, multihash) => {
expect(err).to.not.exist()
const empty = new Block(d, new CID(multihash))
Expand All @@ -70,7 +70,7 @@ module.exports = (repo) => {
this.timeout(15000) // add time for ci
waterfall([
(cb) => map(_.range(50), (i, cb) => {
const d = new Buffer('many' + Math.random())
const d = Buffer.from('many' + Math.random())
multihashing(d, 'sha2-256', (err, hash) => {
if (err) {
return cb(err)
Expand Down Expand Up @@ -135,6 +135,46 @@ module.exports = (repo) => {
done()
})
})

it('should get block stored under v0 CID with a v1 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.get(cid.toV1(), (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(data)
done()
})
})
})
})

it('should get block stored under v1 CID with a v0 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(1, 'dag-pb', hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.get(cid.toV0(), (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(data)
done()
})
})
})
})
})

describe('.has', () => {
Expand All @@ -153,6 +193,46 @@ module.exports = (repo) => {
done()
})
})

it('should have block stored under v0 CID with a v1 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.has(cid.toV1(), (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(true)
done()
})
})
})
})

it('should have block stored under v1 CID with a v0 CID', done => {
const data = Buffer.from(`TEST${Date.now()}`)

multihashing(data, 'sha2-256', (err, hash) => {
if (err) return done(err)

const cid = new CID(1, 'dag-pb', hash)

repo.blocks.put(new Block(data, cid), err => {
if (err) return done(err)

repo.blocks.has(cid.toV0(), (err, exists) => {
expect(err).to.not.exist()
expect(exists).to.eql(true)
done()
})
})
})
})
})

describe('.delete', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/repo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ module.exports = (repo) => {

it('set config', (done) => {
series([
(cb) => repo.config.set({a: 'b'}, cb),
(cb) => repo.config.set({ a: 'b' }, cb),
(cb) => repo.config.get((err, config) => {
if (err) return cb(err)
expect(config).to.deep.equal({a: 'b'})
expect(config).to.deep.equal({ a: 'b' })
cb()
})
], done)
Expand All @@ -54,7 +54,7 @@ module.exports = (repo) => {
(cb) => repo.config.set('c.x', 'd', cb),
(cb) => repo.config.get((err, config) => {
if (err) return cb(err)
expect(config).to.deep.equal({a: 'b', c: { x: 'd' }})
expect(config).to.deep.equal({ a: 'b', c: { x: 'd' } })
cb()
})
], done)
Expand Down
2 changes: 1 addition & 1 deletion test/stat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = (repo) => {
})

it('get human stats', (done) => {
repo.stat({human: true}, (err, stats) => {
repo.stat({ human: true }, (err, stats) => {
expect(err).to.not.exist()
expect(stats).to.exist()
expect(stats).to.have.property('numObjects')
Expand Down

0 comments on commit 18cca08

Please sign in to comment.