Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use open method and fork of level-js #128

Merged
merged 5 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"npm": ">=3.0.0"
},
"devDependencies": {
"aegir": "^11.0.0",
"aegir": "^11.0.1",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"lodash": "^4.17.4",
Expand All @@ -53,13 +53,13 @@
"async": "^2.1.5",
"base32.js": "^0.1.0",
"cids": "^0.4.2",
"datastore-core": "^0.1.0",
"datastore-fs": "^0.1.1",
"datastore-level": "^0.1.0",
"datastore-core": "^0.2.0",
"datastore-fs": "^0.2.0",
"datastore-level": "^0.3.0",
"debug": "^2.6.3",
"interface-datastore": "^0.1.1",
"interface-datastore": "^0.2.0",
"ipfs-block": "~0.6.0",
"level-js": "^2.2.4",
"level-js": "timkuijsten/level.js#idbunwrapper",
"leveldown": "^1.6.0",
"lock-me": "^1.0.2",
"multiaddr": "^2.2.2",
Expand All @@ -81,4 +81,4 @@
"nginnever <ginneversource@gmail.com>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
23 changes: 21 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class IpfsRepo {
lock: 'fs'
}, options)
this._fsOptions = Object.assign({}, options.fsOptions)

const FsStore = this.options.fs
this._fsStore = new FsStore(this.path, Object.assign({}, this._fsOptions, {
extension: ''
Expand All @@ -81,7 +80,14 @@ class IpfsRepo {
*/
init (config, callback) {
log('initializing at: %s', this.path)

series([
(cb) => this._fsStore.open((err) => {
if (err && err.message === 'Already open') {
return cb()
}
cb(err)
}),
(cb) => this.config.set(config, cb),
(cb) => this.version.set(repoVersion, cb)
], callback)
Expand All @@ -102,6 +108,12 @@ class IpfsRepo {

// check if the repo is already initialized
waterfall([
(cb) => this._fsStore.open((err) => {
if (err && err.message === 'Already open') {
return cb()
}
cb(err)
}),
(cb) => this._isInitialized(cb),
(cb) => this._locker.lock(this.path, cb),
(lck, cb) => {
Expand Down Expand Up @@ -155,10 +167,12 @@ class IpfsRepo {
* @returns {void}
*/
_isInitialized (callback) {
log('init check')
parallel([
(cb) => this.config.exists(cb),
(cb) => this.version.check(repoVersion, cb)
], (err, res) => {
log('init', err, res)
if (err) {
return callback(err)
}
Expand Down Expand Up @@ -192,10 +206,15 @@ class IpfsRepo {
(cb) => this.store.close(cb),
(cb) => this._fsStore.close(cb),
(cb) => {
log('unlocking')
this.closed = true
this.lockfile.close(cb)
},
(cb) => {
this.lockfile = null
cb()
}
], callback)
], (err) => callback(err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass the callback directly.

}

/**
Expand Down
9 changes: 9 additions & 0 deletions test/blockstore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ module.exports = (repo) => {
], done)
})

it('empty value', (done) => {
const d = new Buffer(0)
multihashing(d, 'sha2-256', (err, multihash) => {
expect(err).to.not.exist()
const empty = new Block(d, new CID(multihash))
repo.blockstore.put(empty, done)
})
})

it('massive multiwrite', (done) => {
waterfall([
(cb) => map(_.range(100), (i, cb) => {
Expand Down
33 changes: 26 additions & 7 deletions test/repo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const series = require('async/series')
const waterfall = require('async/waterfall')

const Repo = require('../src')

Expand Down Expand Up @@ -61,14 +62,32 @@ module.exports = (repo) => {
})

it('set version', (done) => {
repo.version.set(9000, (err) => {
expect(err).to.not.exist()
repo.version.get((err, version) => {
expect(err).to.not.exist()
waterfall([
(cb) => repo.version.set(9000, cb),
(cb) => repo.version.get(cb),
(version, cb) => {
expect(version).to.equal(9000)
done()
})
})
cb()
},
(cb) => repo.version.set(5, cb)
], done)
})
})

describe('lifecycle', () => {
it('close and open', (done) => {
waterfall([
(cb) => repo.close(cb),
(cb) => repo.open(cb),
(cb) => repo.close(cb),
(cb) => repo.open(cb),
(cb) => repo.version.get(cb),
(version, cb) => {
console.log(version)
expect(version).to.exist()
cb()
}
], done)
})
})
})
Expand Down