Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat(core): migrate to awesome dag-pb
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Nov 24, 2016
1 parent 12325a2 commit db550a1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 88 deletions.
9 changes: 3 additions & 6 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,13 @@ function prepareFile (self, file, callback) {
const bs58mh = multihashes.toB58String(file.multihash)
waterfall([
(cb) => self.object.get(file.multihash, cb),
(node, cb) => node.size((err, size) => {
if (err) {
return cb(err)
}
(node, cb) => {
cb(null, {
path: file.path || bs58mh,
hash: bs58mh,
size: size
size: node.size
})
})
}
], callback)
}

Expand Down
127 changes: 59 additions & 68 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@ function parseBuffer (buf, encoding, callback) {
}

function parseJSONBuffer (buf, callback) {
let node
let data
let links

try {
const parsed = JSON.parse(buf.toString())
const links = (parsed.Links || []).map((link) => {

links = (parsed.Links || []).map((link) => {
return new DAGLink(
link.Name,
link.Size,
mh.fromB58String(link.Hash)
link.Name || link.name,
link.Size || link.size,
mh.fromB58String(link.Hash || link.hash || link.multihash)
)
})
node = new DAGNode(new Buffer(parsed.Data), links)
data = new Buffer(parsed.Data)
} catch (err) {
return callback(new Error('failed to parse JSON: ' + err))
}
callback(null, node)

DAGNode.create(data, links, callback)
}

function parseProtoBuffer (buf, callback) {
Expand All @@ -68,15 +72,15 @@ module.exports = function object (self) {
self.object.get(multihash, options, cb)
},
(node, cb) => {
node = edit(node)

node.multihash((err, multihash) => {
// edit applies the edit func passed to
// editAndSave
edit(node, (err, node) => {
if (err) {
return cb(err)
}
self._ipldResolver.put({
node: node,
cid: new CID(multihash)
cid: new CID(node.multihash)
}, (err) => {
cb(err, node)
})
Expand All @@ -88,16 +92,14 @@ module.exports = function object (self) {

return {
new: promisify((callback) => {
const node = new DAGNode()

node.multihash((err, multihash) => {
DAGNode.create(new Buffer(0), (err, node) => {
if (err) {
return callback(err)
}
self._ipldResolver.put({
node: node,
cid: new CID(multihash)
}, function (err) {
cid: new CID(node.multihash)
}, (err) => {
if (err) {
return callback(err)
}
Expand Down Expand Up @@ -126,34 +128,40 @@ module.exports = function object (self) {
})
return
} else {
node = new DAGNode(obj)
DAGNode.create(obj, (err, _node) => {
if (err) {
return callback(err)
}
node = _node
next()
})
}
} else if (obj.multihash) {
// already a dag node
node = obj
next()
} else if (typeof obj === 'object') {
node = new DAGNode(obj.Data, obj.Links)
DAGNode.create(obj.Data, obj.Links, (err, _node) => {
if (err) {
return callback(err)
}
node = _node
next()
})
} else {
return callback(new Error('obj not recognized'))
}

next()

function next () {
node.multihash((err, multihash) => {
self._ipldResolver.put({
node: node,
cid: new CID(node.multihash)
}, (err) => {
if (err) {
return callback(err)
}
self._ipldResolver.put({
node: node,
cid: new CID(multihash)
}, (err, block) => {
if (err) {
return callback(err)
}

self.object.get(multihash, callback)
})
self.object.get(node.multihash, callback)
})
}
}),
Expand Down Expand Up @@ -223,64 +231,47 @@ module.exports = function object (self) {
const blockSize = serialized.length
const linkLength = node.links.reduce((a, l) => a + l.size, 0)

node.toJSON((err, nodeJSON) => {
if (err) {
return callback(err)
}
const nodeJSON = node.toJSON()

callback(null, {
Hash: nodeJSON.Hash,
NumLinks: node.links.length,
BlockSize: blockSize,
LinksSize: blockSize - node.data.length,
DataSize: node.data.length,
CumulativeSize: blockSize + linkLength
})
callback(null, {
Hash: nodeJSON.multihash,
NumLinks: node.links.length,
BlockSize: blockSize,
LinksSize: blockSize - node.data.length,
DataSize: node.data.length,
CumulativeSize: blockSize + linkLength
})
})
})
}),

patch: promisify({
addLink (multihash, link, options, callback) {
editAndSave((node) => {
node.addRawLink(link)
return node
editAndSave((node, cb) => {
DAGNode.addLink(node, link, cb)
})(multihash, options, callback)
},

rmLink (multihash, linkRef, options, callback) {
editAndSave((node) => {
node.links = node.links.filter((link) => {
if (typeof linkRef === 'string') {
return link.name !== linkRef
}

if (Buffer.isBuffer(linkRef)) {
return !link.hash.equals(linkRef)
}

if (linkRef.name) {
return link.name !== linkRef.name
}

return !link.hash.equals(linkRef.hash)
})
return node
editAndSave((node, cb) => {
if (linkRef.constructor &&
linkRef.constructor.name === 'DAGLink') {
linkRef = linkRef._name
}
DAGNode.rmLink(node, linkRef, cb)
})(multihash, options, callback)
},

appendData (multihash, data, options, callback) {
editAndSave((node) => {
node.data = Buffer.concat([node.data, data])
return node
editAndSave((node, cb) => {
const newData = Buffer.concat([node.data, data])
DAGNode.create(newData, node.links, cb)
})(multihash, options, callback)
},

setData (multihash, data, options, callback) {
editAndSave((node) => {
node.data = data
return node
editAndSave((node, cb) => {
DAGNode.create(data, node.links, cb)
})(multihash, options, callback)
}
})
Expand Down
28 changes: 16 additions & 12 deletions test/core/both/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ describe('bitswap', () => {

function addNode (num, done) {
num = leftPad(num, 3, 0)

const apiUrl = `/ip4/127.0.0.1/tcp/31${num}`
const remoteNode = new API(apiUrl)

connectNodes(remoteNode, inProcNode, (err) => {
console.log('connected')
done(err, remoteNode)
})
}
Expand Down Expand Up @@ -205,17 +207,24 @@ describe('bitswap', () => {

it('2 peers', (done) => {
const file = new Buffer(`I love IPFS <3 ${Math.random()}`)
console.log('1')

waterfall([
// 0. Start node
(cb) => addNode(12, cb),
// 1. Add file to tmp instance
(remote, cb) => remote.add([{
path: 'awesome.txt',
content: file
}], cb),
(remote, cb) => {
console.log('2')
remote.files.add([{
path: 'awesome.txt',
content: file
}], cb)
},
// 2. Request file from local instance
(val, cb) => inProcNode.files.cat(val[0].hash, cb),
(val, cb) => {
console.log('3')
inProcNode.files.cat(val[0].hash, cb)
},
(res, cb) => res.pipe(bl(cb))
], (err, res) => {
expect(err).to.not.exist
Expand All @@ -237,13 +246,8 @@ describe('bitswap', () => {
it('returns an array of wanted blocks', (done) => {
inProcNode.goOnline((err) => {
expect(err).to.not.exist

expect(
inProcNode.bitswap.wantlist()
).to.be.eql(
[]
)

expect(inProcNode.bitswap.wantlist())
.to.be.eql([])
inProcNode.goOffline(done)
})
})
Expand Down
9 changes: 7 additions & 2 deletions test/utils/temp-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ function createTempNode (num, callback) {
num = leftPad(num, 3, 0)

series([
(cb) => ipfs.init({ emptyRepo: true, bits: 1024 }, cb),
(cb) => ipfs.init({
emptyRepo: true,
bits: 1024
}, cb),
(cb) => setAddresses(repo, num, cb),
(cb) => ipfs.load(cb)
], (err) => {
if (err) return callback(err)
if (err) {
return callback(err)
}
callback(null, ipfs)
})
}
Expand Down

0 comments on commit db550a1

Please sign in to comment.