-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from ipfs/pull
[WIP] Move to pull-streams
- Loading branch information
Showing
46 changed files
with
602 additions
and
509 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ language: node_js | |
node_js: | ||
- 4 | ||
- 5 | ||
- stable | ||
|
||
# Make sure we have new NPM. | ||
before_install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,56 @@ | ||
'use strict' | ||
|
||
const assert = require('assert') | ||
|
||
const stores = require('./stores') | ||
|
||
function Repo (repoPath, options) { | ||
if (!(this instanceof Repo)) { | ||
return new Repo(repoPath, options) | ||
module.exports = class Repo { | ||
constructor (repoPath, options) { | ||
assert.equal(typeof repoPath, 'string', 'missing repoPath') | ||
assert(options, 'missing options') | ||
assert(options.stores, 'missing options.stores') | ||
|
||
this.path = repoPath | ||
|
||
const blobStores = initializeBlobStores(options.stores) | ||
|
||
const setup = (name, needs) => { | ||
needs = needs || {} | ||
const args = [repoPath, blobStores[name]] | ||
if (needs.locks) { | ||
args.push(this.locks) | ||
} | ||
|
||
if (needs.config) { | ||
args.push(this.config) | ||
} | ||
|
||
return stores[name].setUp.apply(stores[name], args) | ||
} | ||
|
||
this.locks = setup('locks') | ||
this.version = setup('version', {locks: true}) | ||
this.config = setup('config', {locks: true}) | ||
this.keys = setup('keys', {locks: true, config: true}) | ||
this.blockstore = setup('blockstore', {locks: true}) | ||
} | ||
|
||
exists (callback) { | ||
this.version.exists(callback) | ||
} | ||
if (!options) { throw new Error('missing options param') } | ||
if (!options.stores) { throw new Error('missing options.stores param') } | ||
|
||
// If options.stores is an abstract-blob-store instead of a map, use it for | ||
// all stores. | ||
if (options.stores.prototype && options.stores.prototype.createWriteStream) { | ||
const store = options.stores | ||
options.stores = { | ||
} | ||
|
||
function initializeBlobStores (store) { | ||
if (store.constructor) { | ||
return { | ||
keys: store, | ||
config: store, | ||
datastore: store, | ||
blockstore: store, | ||
logs: store, | ||
locks: store, | ||
version: store | ||
} | ||
} | ||
|
||
this.path = repoPath | ||
|
||
this.locks = stores | ||
.locks | ||
.setUp(repoPath, options.stores.locks) | ||
|
||
this.exists = (callback) => { | ||
this.version.exists(callback) | ||
} | ||
|
||
this.version = stores | ||
.version | ||
.setUp(repoPath, options.stores.version, this.locks) | ||
|
||
this.config = stores | ||
.config | ||
.setUp(repoPath, options.stores.config, this.locks) | ||
|
||
this.keys = stores | ||
.keys | ||
.setUp(repoPath, options.stores.keys, this.locks, this.config) | ||
|
||
this.datastore = stores | ||
.datastore | ||
.setUp(repoPath, options.stores.datastore, this.locks) | ||
return store | ||
} | ||
|
||
exports = module.exports = Repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
'use strict' | ||
|
||
const Block = require('ipfs-block') | ||
const pull = require('pull-stream') | ||
const Lock = require('lock') | ||
const base32 = require('base32.js') | ||
const path = require('path') | ||
const write = require('pull-write') | ||
const parallel = require('run-parallel') | ||
const through = require('pull-through') | ||
|
||
const PREFIX_LENGTH = 5 | ||
|
||
exports = module.exports | ||
|
||
function multihashToPath (multihash, extension) { | ||
extension = extension || 'data' | ||
const encoder = new base32.Encoder() | ||
const hash = encoder.write(multihash).finalize() | ||
const filename = `${hash}.${extension}` | ||
const folder = filename.slice(0, PREFIX_LENGTH) | ||
|
||
return path.join(folder, filename) | ||
} | ||
|
||
exports.setUp = (basePath, BlobStore, locks) => { | ||
const store = new BlobStore(basePath + '/blocks') | ||
const lock = new Lock() | ||
|
||
function writeBlock (block, cb) { | ||
if (!block || !block.data) { | ||
return cb(new Error('Invalid block')) | ||
} | ||
|
||
const key = multihashToPath(block.key, block.extension) | ||
|
||
lock(key, (release) => pull( | ||
pull.values([block.data]), | ||
store.write(key, release((err) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
cb(null, {key}) | ||
})) | ||
)) | ||
} | ||
|
||
return { | ||
getStream (key, extension) { | ||
if (!key) { | ||
return pull.error(new Error('Invalid key')) | ||
} | ||
|
||
const p = multihashToPath(key, extension) | ||
|
||
const ext = extension === 'data' ? 'protobuf' : extension | ||
let data = [] | ||
|
||
return pull( | ||
store.read(p), | ||
through(function (values) { | ||
data = data.concat(values) | ||
}, function () { | ||
this.queue(new Block(Buffer.concat(data), ext)) | ||
this.queue(null) | ||
}) | ||
) | ||
}, | ||
|
||
putStream () { | ||
let ended = false | ||
let written = [] | ||
let push = null | ||
|
||
const sink = write((blocks, cb) => { | ||
parallel(blocks.map((block) => (cb) => { | ||
writeBlock(block, (err, meta) => { | ||
if (err) return cb(err) | ||
if (push) { | ||
const read = push | ||
push = null | ||
read(null, meta) | ||
return cb() | ||
} | ||
|
||
written.push(meta) | ||
cb() | ||
}) | ||
}), cb) | ||
}, null, 100, (err) => { | ||
ended = err || true | ||
if (push) push(ended) | ||
}) | ||
|
||
const source = (end, cb) => { | ||
if (end) ended = end | ||
if (ended) return cb(ended) | ||
|
||
if (written.length) { | ||
return cb(null, written.shift()) | ||
} | ||
|
||
push = cb | ||
} | ||
|
||
return {source, sink} | ||
}, | ||
|
||
has (key, extension, cb) { | ||
if (typeof extension === 'function') { | ||
cb = extension | ||
extension = undefined | ||
} | ||
|
||
if (!key) { | ||
return cb(new Error('Invalid key')) | ||
} | ||
|
||
const p = multihashToPath(key, extension) | ||
store.exists(p, cb) | ||
}, | ||
|
||
delete (key, extension, cb) { | ||
if (typeof extension === 'function') { | ||
cb = extension | ||
extension = undefined | ||
} | ||
|
||
if (!key) { | ||
return cb(new Error('Invalid key')) | ||
} | ||
|
||
const p = multihashToPath(key, extension) | ||
store.remove(p, cb) | ||
} | ||
} | ||
} |
Oops, something went wrong.