Skip to content

Commit 31ff3e0

Browse files
feat(blockstore): move to base32 encoding for blocks
This follows go-ipfs, which already made the move. Existing repos can be migrated using fs-repo-migrations. Closes #67 BREAKING: Changes the repo layout
1 parent d0771f4 commit 31ff3e0

File tree

33 files changed

+49
-31
lines changed

33 files changed

+49
-31
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
"fs-pull-blob-store": "^0.3.0",
3737
"idb-pull-blob-store": "^0.3.0",
3838
"lodash": "^4.15.0",
39+
"multihashes": "^0.2.2",
3940
"ncp": "^2.0.0",
4041
"pre-commit": "^1.1.3",
4142
"rimraf": "^2.5.4"
4243
},
4344
"dependencies": {
4445
"babel-runtime": "^6.11.6",
46+
"base32.js": "^0.1.0",
4547
"ipfs-block": "^0.3.0",
4648
"lock": "^0.1.2",
4749
"multihashes": "^0.2.2",

src/stores/blockstore.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
const Block = require('ipfs-block')
44
const pull = require('pull-stream')
55
const Lock = require('lock')
6+
const base32 = require('base32.js')
7+
const path = require('path')
68

7-
const PREFIX_LENGTH = 8
9+
const PREFIX_LENGTH = 5
810

911
exports = module.exports
1012

1113
function multihashToPath (multihash, extension) {
1214
extension = extension || 'data'
13-
const filename = `${multihash.toString('hex')}.${extension}`
15+
const encoder = new base32.Encoder()
16+
const hash = encoder.write(multihash).finalize()
17+
const filename = `${hash}.${extension}`
1418
const folder = filename.slice(0, PREFIX_LENGTH)
15-
const path = folder + '/' + filename
1619

17-
return path
20+
return path.join(folder, filename)
1821
}
1922

2023
exports.setUp = (basePath, BlobStore, locks) => {
@@ -79,8 +82,8 @@ exports.setUp = (basePath, BlobStore, locks) => {
7982
return cb(new Error('Invalid key'))
8083
}
8184

82-
const path = multihashToPath(key, extension)
83-
store.exists(path, cb)
85+
const p = multihashToPath(key, extension)
86+
store.exists(p, cb)
8487
},
8588

8689
delete (key, extension, cb) {
@@ -93,8 +96,8 @@ exports.setUp = (basePath, BlobStore, locks) => {
9396
return cb(new Error('Invalid key'))
9497
}
9598

96-
const path = multihashToPath(key, extension)
97-
store.remove(path, cb)
99+
const p = multihashToPath(key, extension)
100+
store.remove(p, cb)
98101
}
99102
}
100103
}

test/blockstore-test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
const expect = require('chai').expect
55
const Block = require('ipfs-block')
6+
const mh = require('multihashes')
67

78
module.exports = (repo) => {
89
describe('blockstore', () => {
9-
const helloKey = '1220b94d/1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9.data'
10-
const helloIpldKey = '1220ed12/1220ed12932f3ef94c0792fbc55263968006e867e522cf9faa88274340a2671d4441.ipld'
10+
const helloKey = 'CIQLS/CIQLSTJHXGJU2PQIUUXFFV62PWV7VREE57RXUU4A52IIR55M4LX432I.data'
11+
const helloIpldKey = 'CIQO2/CIQO2EUTF47PSTAHSL54KUTDS2AAN2DH4URM7H5KRATUGQFCM4OUIQI.ipld'
1112

1213
describe('.put', () => {
1314
it('simple', (done) => {
@@ -150,5 +151,22 @@ module.exports = (repo) => {
150151
})
151152
})
152153
})
154+
155+
describe('interop', () => {
156+
it('reads welcome-to-ipfs', (done) => {
157+
const welcomeHash = mh.fromHexString(
158+
'1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec'
159+
)
160+
repo.blockstore.get(welcomeHash, (err, block) => {
161+
expect(err).to.not.exist
162+
expect(
163+
block.data.toString()
164+
).to.match(
165+
/Hello and Welcome to IPFS/
166+
)
167+
done()
168+
})
169+
})
170+
})
153171
})
154172
}

test/test-repo/datastore/CURRENT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
MANIFEST-000007
1+
MANIFEST-000009

test/test-repo/datastore/LOG

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
=============== Dec 10, 2015 (PST) ===============
2-
07:50:02.056578 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
3-
07:50:02.057231 db@open opening
4-
07:50:02.057312 journal@recovery F·1
5-
07:50:02.057514 journal@recovery recovering @3
6-
07:50:02.058921 mem@flush created L0@5 N·4 S·1KiB "/ip..\xf6\xe4\xa9,v5":"/pk..\xf6\xe4\xa9,v6"
7-
07:50:02.059983 db@janitor F·4 G·0
8-
07:50:02.060001 db@open done T·2.755926ms
9-
07:50:02.073183 db@close closing
10-
07:50:02.073285 db@close done T·97.522µs
1+
=============== Aug 16, 2016 (CEST) ===============
2+
12:27:26.257958 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
3+
12:27:26.258463 db@open opening
4+
12:27:26.260891 db@janitor F·4 G·0
5+
12:27:26.260933 db@open done T·2.438178ms

test/test-repo/datastore/LOG.old

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=============== Dec 10, 2015 (PST) ===============
2-
07:49:57.048841 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
3-
07:49:57.049014 db@open opening
4-
07:49:57.049066 journal@recovery F·1
5-
07:49:57.049233 journal@recovery recovering @1
6-
07:49:57.049693 mem@flush created L0@22211B "/lo..oot,v2":"/lo..ins,v1"
7-
07:49:57.050381 db@janitor F·3 G·0
8-
07:49:57.050397 db@open done T·1.375431ms
9-
07:49:57.064580 db@close closing
10-
07:49:57.064655 db@close done T·72.59µs
2+
07:50:02.056578 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
3+
07:50:02.057231 db@open opening
4+
07:50:02.057312 journal@recovery F·1
5+
07:50:02.057514 journal@recovery recovering @3
6+
07:50:02.058921 mem@flush created L0@541KiB "/ip..\xf6\xe4\xa9,v5":"/pk..\xf6\xe4\xa9,v6"
7+
07:50:02.059983 db@janitor F·4 G·0
8+
07:50:02.060001 db@open done T·2.755926ms
9+
07:50:02.073183 db@close closing
10+
07:50:02.073285 db@close done T·97.522µs
-293 Bytes
Binary file not shown.
192 Bytes
Binary file not shown.

test/test-repo/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3
1+
4

0 commit comments

Comments
 (0)