-
Notifications
You must be signed in to change notification settings - Fork 8
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
Reorangized and implementation of a basic dag store #2
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
204b920
Reorangized and implementation of a basic dag store
travisperson dd96085
Removed unused module
travisperson a87a223
Valid package.json
travisperson 1b4826a
codestyle and tests pass, woot!
daviddias 38d572e
add gitignore
daviddias 159eb1b
add travis
daviddias 7bee9a2
badgers and typos
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,67 +1,17 @@ | ||
var ipfs = require('ipfs-api') | ||
var stream = require('stream') | ||
var multihash = require('multihashes') | ||
var base58 = require('base58-native') | ||
var block = require('./lib/block') | ||
var dag = require('./lib/dag') | ||
|
||
module.exports = function (opts) { | ||
if (!opts) opts = {} | ||
|
||
opts.host = opts.host || '127.0.0.1' | ||
opts.port = opts.port || 5001 | ||
|
||
var store = {} | ||
|
||
var node = ipfs(opts.host, opts.port) | ||
|
||
store.createWriteStream = function (opts, cb) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
if (typeof opts === 'function') return store.createWriteStream(null, opts) | ||
|
||
var bufferStream = new stream.PassThrough() | ||
|
||
node.block.put(bufferStream, function (err, data) { | ||
if (err) return cb(err) | ||
|
||
cb(null, { | ||
key: data.Key, | ||
size: data.Size | ||
}) | ||
}) | ||
|
||
return bufferStream | ||
} | ||
|
||
store.createReadStream = function (opts) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
var bufferStream = new stream.PassThrough() | ||
node.block.get(opts.key, function (err, stream) { | ||
if (err && !stream) return bufferStream.emit('error', 'No stream') | ||
stream.pipe(bufferStream) | ||
}) | ||
|
||
return bufferStream | ||
return { | ||
block: block(node), | ||
dag: dag(node) | ||
} | ||
|
||
store.exists = function (opts, cb) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
var buf | ||
|
||
try { | ||
buf = base58.decode(opts.key) | ||
void multihash.decode(buf) | ||
} catch (e) { | ||
return cb(null, false) | ||
} | ||
|
||
node.block.get(opts.key, function (err, stream) { | ||
if (err) return cb(err) | ||
cb(null, true) | ||
}) | ||
} | ||
|
||
store.remove = function (opts, cb) { | ||
cb(null, false) | ||
} | ||
|
||
return store | ||
} |
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,61 @@ | ||
var stream = require('stream') | ||
var multihash = require('multihashes') | ||
var base58 = require('base58-native') | ||
|
||
module.exports = function(node) { | ||
var store = {} | ||
|
||
store.createWriteStream = function (opts, cb) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
if (typeof opts === 'function') return store.createWriteStream(null, opts) | ||
|
||
var bufferStream = new stream.PassThrough() | ||
|
||
node.block.put(bufferStream, function (err, data) { | ||
if (err) return cb(err) | ||
|
||
cb(null, { | ||
key: data.Key, | ||
size: data.Size | ||
}) | ||
}) | ||
|
||
return bufferStream | ||
} | ||
|
||
store.createReadStream = function (opts) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
|
||
var bufferStream = new stream.PassThrough() | ||
|
||
node.block.get(opts.key, function (err, stream) { | ||
if (err && !stream) return bufferStream.emit('error', err) | ||
stream.pipe(bufferStream) | ||
}) | ||
|
||
return bufferStream | ||
} | ||
|
||
store.exists = function (opts, cb) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
var buf | ||
|
||
try { | ||
buf = base58.decode(opts.key) | ||
void multihash.decode(buf) | ||
} catch (e) { | ||
return cb(null, false) | ||
} | ||
|
||
node.block.get(opts.key, function (err, stream) { | ||
if (err) return cb(err) | ||
cb(null, true) | ||
}) | ||
} | ||
|
||
store.remove = function (opts, cb) { | ||
cb(null, false) | ||
} | ||
|
||
return store; | ||
} |
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,140 @@ | ||
var stream = require('stream') | ||
var multihash = require('multihashes') | ||
var base58 = require('base58-native') | ||
var merkledag = require('node-ipfs-mdag') | ||
var async = require('async') | ||
|
||
var Node = function() { | ||
this.Links = [] | ||
this.Data = new Buffer('') | ||
} | ||
|
||
var Link = function() { | ||
this.Hash = '' | ||
this.Size = 0 | ||
this.Name = '' | ||
} | ||
|
||
module.exports = function(node) { | ||
var store = {} | ||
var root = new Node() | ||
var root_hash = '' | ||
var map = {} | ||
|
||
store.createWriteStream = function (opts, cb) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
if (typeof opts === 'function') return store.createWriteStream(null, opts) | ||
if (typeof opts === 'undefined' || opts === null) opts = {} | ||
|
||
var bufferStream = new stream.PassThrough() | ||
var buffer | ||
|
||
bufferStream.on('data', function(chunk) { | ||
if(!buffer) buffer = chunk | ||
else { | ||
buffer = Buffer.concat([buffer,chunk], buffer.length + chunk.length) | ||
} | ||
}) | ||
|
||
bufferStream.on('end', function() { | ||
blob = new Node() | ||
blob.Data = buffer.toString() | ||
async.waterfall([ | ||
function (callback) { | ||
var raw = new Buffer(JSON.stringify(blob)) | ||
node.object.put(raw, 'json', callback) | ||
}, | ||
function (res, callback) { | ||
node.object.stat(res.Hash, function(err, stat) { | ||
callback(err, res, stat) | ||
}) | ||
}, | ||
function (res, stat, callback) { | ||
var link = new Link() | ||
|
||
link.Hash = res.Hash | ||
link.Size = stat.CumulativeSize | ||
link.Name = opts.key || res.Hash | ||
|
||
root.Links = root.Links.filter(function(item) { | ||
return item.Name != opts.key | ||
}) | ||
|
||
root.Links.push(link) | ||
|
||
root.Data = root_hash | ||
var raw = new Buffer(JSON.stringify(root)) | ||
node.object.put(raw, 'json', function(err, new_root) { | ||
callback(err, link, new_root) | ||
}) | ||
}, function (link, new_root, callback) { | ||
map[opts.key] = true | ||
|
||
callback(null, { | ||
key: link.Name, | ||
root: new_root.Hash, | ||
hash: link.Hash, | ||
size: link.Size | ||
}) | ||
|
||
root_hash = new_root.Hash | ||
} | ||
], function(err, meta) { | ||
if (err) return cb(err) | ||
cb(null, meta) | ||
}) | ||
}) | ||
|
||
return bufferStream | ||
} | ||
|
||
store.createReadStream = function (opts) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
|
||
var bufferStream = new stream.PassThrough() | ||
node.object.data((opts.root || root_hash) + '/' + opts.key, function (err, stream) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You want |
||
if (err && !stream) return bufferStream.emit('error', err) | ||
stream.pipe(bufferStream) | ||
}) | ||
|
||
return bufferStream | ||
} | ||
|
||
store.exists = function (opts, cb) { | ||
if (typeof opts === 'string') opts = {key: opts} | ||
if(!root_hash) return cb(null, false) | ||
node.object.stat((opts.root || root_hash) + '/' + opts.key, function(err, stat) { | ||
if(err) cb(null, false) | ||
else cb(null, true) | ||
}) | ||
} | ||
|
||
store.remove = function (opts, cb) { | ||
async.waterfall([ | ||
function (callback) { | ||
|
||
root.Links = root.Links.filter(function(item) { | ||
return item.Name != opts.key | ||
}) | ||
|
||
root.Data = root_hash | ||
var raw = new Buffer(JSON.stringify(root)) | ||
node.object.put(raw, 'json', function(err, new_root) { | ||
callback(err, new_root) | ||
}) | ||
}, | ||
function (new_root, callback) { | ||
map[opts.key] = false | ||
|
||
callback(null, true) | ||
|
||
root_hash = new_root.Hash | ||
} | ||
], function(err, meta) { | ||
if (err) return cb(err, false) | ||
cb(null, meta) | ||
}) | ||
} | ||
|
||
return store; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var test = require('tape') | ||
var abstractBlobTests = require('abstract-blob-store/tests') | ||
var blobs = require('./') | ||
|
||
var common = { | ||
setup: function (t, cb) { | ||
var store = blobs().block | ||
cb(null, store) | ||
}, | ||
teardown: function (t, store, blob, cb) { | ||
cb() | ||
} | ||
} | ||
|
||
abstractBlobTests(test, common) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this module coming from? symlink to
node-ipfs
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not suppose to be there, I was using it, and might go back to it, but for now it wasn't doing what I wanted. I guess I forgot to delete it.