Skip to content

Commit e41c99e

Browse files
committed
fix: interoperability with go repo
1 parent a7ea45b commit e41c99e

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
"lodash.has": "^4.5.2",
7070
"lodash.set": "^4.3.2",
7171
"multiaddr": "^4.0.0",
72-
"pull-stream": "^3.6.7"
72+
"pull-stream": "^3.6.7",
73+
"sort-keys": "^2.0.0"
7374
},
7475
"license": "MIT",
7576
"contributors": [

src/default-datastore.js

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

src/index.js

+27-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,23 @@ function buildOptions (_options) {
377383

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

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, parseInt(buf.toString().trim(), 10))
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+
}

0 commit comments

Comments
 (0)