Skip to content

Commit cef6a3d

Browse files
vasco-santosjacobheun
authored andcommitted
WIP: fix: interoperability with go repo (#173)
* fix: interoperability with go repo * fix: interoperability with go repo - added tests * chore: minor refactor * fix: interoperability with go repo - added tests * chore: minor refactor
1 parent 43183b8 commit cef6a3d

File tree

9 files changed

+134
-6
lines changed

9 files changed

+134
-6
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
"lodash.has": "^4.5.2",
6565
"lodash.set": "^4.3.2",
6666
"multiaddr": "^4.0.0",
67-
"pull-stream": "^3.6.7"
67+
"pull-stream": "^3.6.7",
68+
"sort-keys": "^2.0.0"
6869
},
6970
"license": "MIT",
7071
"contributors": [

src/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
22

33
module.exports = {
4-
repoVersion: 6
4+
repoVersion: 7
55
}

src/default-datastore.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
// Default configuration for the datastore spec in node.js
4+
module.exports = {
5+
Spec: {
6+
type: 'mount',
7+
mounts: [
8+
{
9+
mountpoint: '/blocks',
10+
type: 'measure',
11+
prefix: 'flatfs.datastore',
12+
child: {
13+
type: 'flatfs',
14+
path: 'blocks',
15+
sync: true,
16+
shardFunc: '/repo/flatfs/shard/v1/next-to-last/2'
17+
}
18+
},
19+
{
20+
mountpoint: '/',
21+
type: 'measure',
22+
prefix: 'leveldb.datastore',
23+
child: {
24+
type: 'levelds',
25+
path: 'datastore',
26+
compression: 'none'
27+
}
28+
}
29+
]
30+
}
31+
}

src/index.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const waterfall = require('async/waterfall')
44
const series = require('async/series')
55
const parallel = require('async/parallel')
66
const each = require('async/each')
7+
const _get = require('lodash.get')
78
const assert = require('assert')
89
const path = require('path')
910
const debug = require('debug')
@@ -13,9 +14,11 @@ const pull = require('pull-stream')
1314
const backends = require('./backends')
1415
const version = require('./version')
1516
const config = require('./config')
17+
const spec = require('./spec')
1618
const apiAddr = require('./api-addr')
1719
const blockstore = require('./blockstore')
1820
const defaultOptions = require('./default-options')
21+
const defaultDatastore = require('./default-datastore')
1922
const ERRORS = require('./errors')
2023

2124
const log = debug('repo')
@@ -50,6 +53,7 @@ class IpfsRepo {
5053
this.root = backends.create('root', this.path, this.options)
5154
this.version = version(this.root)
5255
this.config = config(this.root)
56+
this.spec = spec(this.root)
5357
this.apiAddr = apiAddr(this.root)
5458
}
5559

@@ -65,7 +69,8 @@ class IpfsRepo {
6569

6670
series([
6771
(cb) => this.root.open(ignoringAlreadyOpened(cb)),
68-
(cb) => this.config.set(config, cb),
72+
(cb) => this.config.set(buildConfig(config), cb),
73+
(cb) => this.spec.set(buildDatastoreSpec(config), cb),
6974
(cb) => this.version.set(repoVersion, cb)
7075
], callback)
7176
}
@@ -209,6 +214,7 @@ class IpfsRepo {
209214
parallel(
210215
{
211216
config: (cb) => this.config.exists(cb),
217+
spec: (cb) => this.spec.exists(cb),
212218
version: (cb) => this.version.check(repoVersion, cb)
213219
},
214220
(err, res) => {
@@ -377,3 +383,24 @@ function buildOptions (_options) {
377383

378384
return options
379385
}
386+
387+
// TODO this should come from js-ipfs instead
388+
function buildConfig (_config) {
389+
_config.datastore = Object.assign({}, defaultDatastore, _get(_config, 'datastore', {}))
390+
391+
return _config
392+
}
393+
394+
function buildDatastoreSpec (_config) {
395+
const spec = Object.assign({}, defaultDatastore.Spec, _get(_config, 'datastore.Spec', {}))
396+
397+
return {
398+
type: spec.type,
399+
mounts: spec.mounts.map((mounting) => ({
400+
mountpoint: mounting.mountpoint,
401+
type: mounting.child.type,
402+
path: mounting.child.path,
403+
shardFunc: mounting.child.shardFunc
404+
}))
405+
}
406+
}

src/spec.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const Key = require('interface-datastore').Key
4+
const sortKeys = require('sort-keys')
5+
6+
const specKey = new Key('datastore_spec')
7+
8+
module.exports = (store) => {
9+
return {
10+
/**
11+
* Check if a datastore spec file exists.
12+
*
13+
* @param {function(Error, bool)} callback
14+
* @returns {void}
15+
*/
16+
exists (callback) {
17+
store.has(specKey, callback)
18+
},
19+
/**
20+
* Get the current datastore spec.
21+
*
22+
* @param {function(Error, number)} callback
23+
* @returns {void}
24+
*/
25+
get (callback) {
26+
store.get(specKey, (err, buf) => {
27+
if (err) {
28+
return callback(err)
29+
}
30+
callback(null, JSON.parse(buf.toString()))
31+
})
32+
},
33+
/**
34+
* Set the datastore spec of the repo, writing it to the underlying store.
35+
*
36+
* @param {number} spec
37+
* @param {function(Error)} callback
38+
* @returns {void}
39+
*/
40+
set (spec, callback) {
41+
store.put(specKey, Buffer.from(JSON.stringify(sortKeys(spec, { deep: true }))), callback)
42+
}
43+
}
44+
}

src/version.js

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ module.exports = (store) => {
5353
return callback(err)
5454
}
5555
log('comparing version: %s and %s', version, expected)
56+
57+
// Version 6 and 7 are the same
58+
expected = expected === 6 ? expected = 7 : expected
59+
5660
if (version !== expected) {
5761
return callback(new Error(`version mismatch: expected v${expected}, found v${version}`))
5862
}

test/repo-test.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,31 @@ module.exports = (repo) => {
6161
})
6262
})
6363

64+
describe('spec', () => {
65+
it('get spec', (done) => {
66+
repo.spec.get((err) => {
67+
expect(err).to.not.exist()
68+
done()
69+
})
70+
})
71+
72+
it('set spec', (done) => {
73+
series([
74+
(cb) => repo.spec.set({ a: 'b' }, cb),
75+
(cb) => repo.spec.get((err, spec) => {
76+
if (err) return cb(err)
77+
expect(spec).to.deep.equal({ a: 'b' })
78+
cb()
79+
})
80+
], done)
81+
})
82+
})
83+
6484
describe('version', () => {
6585
it('get version', (done) => {
6686
repo.version.get((err, version) => {
6787
expect(err).to.not.exist()
68-
expect(version).to.equal(6)
88+
expect(version).to.equal(7)
6989
done()
7090
})
7191
})
@@ -78,7 +98,7 @@ module.exports = (repo) => {
7898
expect(version).to.equal(9000)
7999
cb()
80100
},
81-
(cb) => repo.version.set(6, cb)
101+
(cb) => repo.version.set(7, cb)
82102
], done)
83103
})
84104
})

test/test-repo/datastore_spec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"mounts":[{"mountpoint":"/blocks","path":"blocks","shardFunc":"/repo/flatfs/shard/v1/next-to-last/2","type":"flatfs"},{"mountpoint":"/","path":"datastore","type":"levelds"}],"type":"mount"}

test/test-repo/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6
1+
7

0 commit comments

Comments
 (0)