Skip to content

Commit 18cca08

Browse files
Alan Shawjacobheun
Alan Shaw
authored andcommittedDec 7, 2018
feat: cid agnostic blockstore .get and .has (#184)
* 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 <alan.shaw@protocol.ai>
1 parent 1531ed1 commit 18cca08

8 files changed

+144
-25
lines changed
 

‎example.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const Repo = require('ipfs-repo')
44
const repo = new Repo('/Users/awesome/.jsipfs')
55

6-
repo.init({my: 'config'}, (err) => {
6+
repo.init({ my: 'config' }, (err) => {
77
if (err) {
88
throw err
99
}

‎package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"npm": ">=3.0.0"
3939
},
4040
"devDependencies": {
41-
"aegir": "^15.3.1",
41+
"aegir": "^17.1.1",
4242
"chai": "^4.2.0",
4343
"dirty-chai": "^2.0.1",
4444
"lodash": "^4.17.11",
45-
"memdown": "^1.4.1",
45+
"memdown": "^3.0.0",
4646
"multihashes": "~0.4.14",
4747
"multihashing-async": "~0.5.1",
4848
"ncp": "^2.0.0",
@@ -52,17 +52,17 @@
5252
"async": "^2.6.1",
5353
"base32.js": "~0.1.0",
5454
"big.js": "^5.2.2",
55-
"cids": "~0.5.5",
55+
"cids": "~0.5.7",
5656
"datastore-core": "~0.6.0",
5757
"datastore-fs": "~0.7.0",
5858
"datastore-level": "~0.10.0",
5959
"debug": "^4.1.0",
6060
"interface-datastore": "~0.6.0",
61-
"ipfs-block": "~0.7.1",
61+
"ipfs-block": "~0.8.0",
6262
"lodash.get": "^4.4.2",
6363
"lodash.has": "^4.5.2",
6464
"lodash.set": "^4.3.2",
65-
"multiaddr": "^5.0.0",
65+
"multiaddr": "^6.0.0",
6666
"proper-lockfile": "^3.2.0",
6767
"pull-stream": "^3.6.9",
6868
"sort-keys": "^2.0.0"

‎src/blockstore.js

+41-4
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,29 @@ function createBaseStore (store) {
7777
})
7878
}
7979

80-
const k = cidToDsKey(cid)
81-
store.get(k, (err, blockData) => {
82-
if (err) { return callback(err) }
80+
const key = cidToDsKey(cid)
81+
store.get(key, (err, blockData) => {
82+
if (err) {
83+
// If not found, we try with the other CID version.
84+
// If exists, then store that block under the CID that was requested.
85+
// Some duplication occurs.
86+
if (err.code === 'ERR_NOT_FOUND') {
87+
const otherCid = cidToOtherVersion(cid)
88+
if (!otherCid) return callback(err)
89+
90+
const otherKey = cidToDsKey(otherCid)
91+
return store.get(otherKey, (err, blockData) => {
92+
if (err) return callback(err)
93+
94+
store.put(key, blockData, (err) => {
95+
if (err) return callback(err)
96+
callback(null, new Block(blockData, cid))
97+
})
98+
})
99+
}
100+
101+
return callback(err)
102+
}
83103

84104
callback(null, new Block(blockData, cid))
85105
})
@@ -140,7 +160,16 @@ function createBaseStore (store) {
140160
})
141161
}
142162

143-
store.has(cidToDsKey(cid), callback)
163+
store.has(cidToDsKey(cid), (err, exists) => {
164+
if (err) return callback(err)
165+
if (exists) return callback(null, true)
166+
167+
// If not found, we try with the other CID version.
168+
const otherCid = cidToOtherVersion(cid)
169+
if (!otherCid) return callback(null, false)
170+
171+
store.has(cidToDsKey(otherCid), callback)
172+
})
144173
},
145174
/**
146175
* Delete a block from the store
@@ -164,3 +193,11 @@ function createBaseStore (store) {
164193
}
165194
}
166195
}
196+
197+
function cidToOtherVersion (cid) {
198+
try {
199+
return cid.version === 0 ? cid.toV1() : cid.toV0()
200+
} catch (err) {
201+
return null
202+
}
203+
}

‎src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class IpfsRepo {
4242
* @param {object} options - Configuration
4343
*/
4444
constructor (repoPath, options) {
45-
assert.equal(typeof repoPath, 'string', 'missing repoPath')
45+
assert.strictEqual(typeof repoPath, 'string', 'missing repoPath')
4646

4747
this.options = buildOptions(options)
4848
this.closed = true
@@ -170,7 +170,7 @@ class IpfsRepo {
170170
return callback(err, null)
171171
}
172172

173-
assert.equal(typeof lockfile.close, 'function', 'Locks must have a close method')
173+
assert.strictEqual(typeof lockfile.close, 'function', 'Locks must have a close method')
174174
callback(null, lockfile)
175175
})
176176
}
@@ -273,7 +273,7 @@ class IpfsRepo {
273273
options = {}
274274
}
275275

276-
options = Object.assign({}, {human: false}, options)
276+
options = Object.assign({}, { human: false }, options)
277277

278278
parallel({
279279
storageMax: (cb) => this.config.get('Datastore.StorageMax', (err, max) => {

‎src/lock.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ exports.lock = (dir, callback) => {
2929
const file = path.join(dir, lockFile)
3030
log('locking %s', file)
3131

32-
lock(dir, {lockfilePath: file, stale: STALE_TIME})
32+
lock(dir, { lockfilePath: file, stale: STALE_TIME })
3333
.then(release => {
34-
callback(null, {close: (cb) => {
35-
release()
36-
.then(() => cb())
37-
.catch(err => cb(err))
38-
}})
34+
callback(null, {
35+
close: (cb) => {
36+
release()
37+
.then(() => cb())
38+
.catch(err => cb(err))
39+
}
40+
})
3941
})
4042
.catch(err => callback(err))
4143
}

‎test/blockstore-test.js

+82-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = (repo) => {
4545
})
4646

4747
it('empty value', (done) => {
48-
const d = new Buffer(0)
48+
const d = Buffer.alloc(0)
4949
multihashing(d, 'sha2-256', (err, multihash) => {
5050
expect(err).to.not.exist()
5151
const empty = new Block(d, new CID(multihash))
@@ -70,7 +70,7 @@ module.exports = (repo) => {
7070
this.timeout(15000) // add time for ci
7171
waterfall([
7272
(cb) => map(_.range(50), (i, cb) => {
73-
const d = new Buffer('many' + Math.random())
73+
const d = Buffer.from('many' + Math.random())
7474
multihashing(d, 'sha2-256', (err, hash) => {
7575
if (err) {
7676
return cb(err)
@@ -135,6 +135,46 @@ module.exports = (repo) => {
135135
done()
136136
})
137137
})
138+
139+
it('should get block stored under v0 CID with a v1 CID', done => {
140+
const data = Buffer.from(`TEST${Date.now()}`)
141+
142+
multihashing(data, 'sha2-256', (err, hash) => {
143+
if (err) return done(err)
144+
145+
const cid = new CID(hash)
146+
147+
repo.blocks.put(new Block(data, cid), err => {
148+
if (err) return done(err)
149+
150+
repo.blocks.get(cid.toV1(), (err, block) => {
151+
expect(err).to.not.exist()
152+
expect(block.data).to.eql(data)
153+
done()
154+
})
155+
})
156+
})
157+
})
158+
159+
it('should get block stored under v1 CID with a v0 CID', done => {
160+
const data = Buffer.from(`TEST${Date.now()}`)
161+
162+
multihashing(data, 'sha2-256', (err, hash) => {
163+
if (err) return done(err)
164+
165+
const cid = new CID(1, 'dag-pb', hash)
166+
167+
repo.blocks.put(new Block(data, cid), err => {
168+
if (err) return done(err)
169+
170+
repo.blocks.get(cid.toV0(), (err, block) => {
171+
expect(err).to.not.exist()
172+
expect(block.data).to.eql(data)
173+
done()
174+
})
175+
})
176+
})
177+
})
138178
})
139179

140180
describe('.has', () => {
@@ -153,6 +193,46 @@ module.exports = (repo) => {
153193
done()
154194
})
155195
})
196+
197+
it('should have block stored under v0 CID with a v1 CID', done => {
198+
const data = Buffer.from(`TEST${Date.now()}`)
199+
200+
multihashing(data, 'sha2-256', (err, hash) => {
201+
if (err) return done(err)
202+
203+
const cid = new CID(hash)
204+
205+
repo.blocks.put(new Block(data, cid), err => {
206+
if (err) return done(err)
207+
208+
repo.blocks.has(cid.toV1(), (err, exists) => {
209+
expect(err).to.not.exist()
210+
expect(exists).to.eql(true)
211+
done()
212+
})
213+
})
214+
})
215+
})
216+
217+
it('should have block stored under v1 CID with a v0 CID', done => {
218+
const data = Buffer.from(`TEST${Date.now()}`)
219+
220+
multihashing(data, 'sha2-256', (err, hash) => {
221+
if (err) return done(err)
222+
223+
const cid = new CID(1, 'dag-pb', hash)
224+
225+
repo.blocks.put(new Block(data, cid), err => {
226+
if (err) return done(err)
227+
228+
repo.blocks.has(cid.toV0(), (err, exists) => {
229+
expect(err).to.not.exist()
230+
expect(exists).to.eql(true)
231+
done()
232+
})
233+
})
234+
})
235+
})
156236
})
157237

158238
describe('.delete', () => {

‎test/repo-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ module.exports = (repo) => {
3232

3333
it('set config', (done) => {
3434
series([
35-
(cb) => repo.config.set({a: 'b'}, cb),
35+
(cb) => repo.config.set({ a: 'b' }, cb),
3636
(cb) => repo.config.get((err, config) => {
3737
if (err) return cb(err)
38-
expect(config).to.deep.equal({a: 'b'})
38+
expect(config).to.deep.equal({ a: 'b' })
3939
cb()
4040
})
4141
], done)
@@ -54,7 +54,7 @@ module.exports = (repo) => {
5454
(cb) => repo.config.set('c.x', 'd', cb),
5555
(cb) => repo.config.get((err, config) => {
5656
if (err) return cb(err)
57-
expect(config).to.deep.equal({a: 'b', c: { x: 'd' }})
57+
expect(config).to.deep.equal({ a: 'b', c: { x: 'd' } })
5858
cb()
5959
})
6060
], done)

‎test/stat-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = (repo) => {
2626
})
2727

2828
it('get human stats', (done) => {
29-
repo.stat({human: true}, (err, stats) => {
29+
repo.stat({ human: true }, (err, stats) => {
3030
expect(err).to.not.exist()
3131
expect(stats).to.exist()
3232
expect(stats).to.have.property('numObjects')

0 commit comments

Comments
 (0)
Please sign in to comment.