Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
perf: remove named links from object
Browse files Browse the repository at this point in the history
This change was motivated by performance issues, but it also
really is cleaner to keep the IPLD objects lean and not cluttered
with IPFS related things.

BREAKING CHANGE: named links are no longer part of an object

Access to named links is only possible with calling `resolve()`.
Hence they are also not part of `tree()` anymore.

Named links are a feature of IPFS and only supported for
backwards compatibility, they are not really part of IPLD.
  • Loading branch information
vmx committed Jun 27, 2019
1 parent 5d43cec commit 4dbe00d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 42 deletions.
25 changes: 0 additions & 25 deletions src/dag-node/addNamedLink.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/dag-node/dagNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const assert = require('assert')
const withIs = require('class-is')
const addNamedLink = require('./addNamedLink')
const visibility = require('../visibility')

class DAGNode {
Expand All @@ -18,13 +17,6 @@ class DAGNode {
// Make sure we have a nice public API that can be used by an IPLD resolver
visibility.hidePrivateFields(this)
visibility.addEnumerableGetters(this, ['Data', 'Links'])

// Add getters for existing links by the name of the link
// This is how paths are traversed in IPFS. Links with names won't
// override existing fields like `data` or `links`.
links.forEach((link, position) => {
addNamedLink(this, link.Name, position)
})
}

toJSON () {
Expand Down
11 changes: 11 additions & 0 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ exports.resolve = (binaryBlob, path) => {
while (parts.length) {
const key = parts.shift()
if (node[key] === undefined) {
// There might be a matching named link
for (const link of node.Links) {
if (link.Name === key) {
return {
value: link.Hash,
remainderPath: parts.join('/')
}
}
}

// There wasn't even a matching named link
throw new Error(`Object has no property '${key}'`)
}

Expand Down
5 changes: 0 additions & 5 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ module.exports = (repo) => {
expect(node1b.Links.length).to.equal(1)
expect(node1b.Links[0].Tsize).to.eql(node2.size)
expect(node1b.Links[0].Name).to.eql('banana')
expect(Object.keys(node1b)).to.include('banana')
})

it('addLink - add several links', async () => {
Expand Down Expand Up @@ -207,9 +206,7 @@ module.exports = (repo) => {

const node1b = await DAGNode.addLink(node1a, link)
expect(node1b.Links.length).to.eql(1)
expect(Object.keys(node1b)).to.include('banana')
const node1c = DAGNode.rmLink(node1b, 'banana')
expect(Object.keys(node1c)).to.not.include('banana')
expect(node1c.Links.length).to.eql(0)
expect(node1c.toJSON()).to.eql(withoutLink)
})
Expand All @@ -224,9 +221,7 @@ module.exports = (repo) => {

const node1b = await DAGNode.addLink(node1a, link)
expect(node1b.Links.length).to.eql(1)
expect(Object.keys(node1b)).to.include('banana')
const node1c = DAGNode.rmLink(node1b, node1b.Links[0].Hash)
expect(Object.keys(node1c)).to.not.include('banana')
expect(node1c.Links.length).to.eql(0)
expect(node1c.toJSON()).to.eql(withoutLink)
})
Expand Down
6 changes: 2 additions & 4 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ describe('IPLD Format resolver (local)', () => {
'Links/1/Name',
'Links/1/Tsize',
'Links/1/Hash',
'Data',
'named link'
'Data'
])
})
})
Expand Down Expand Up @@ -206,8 +205,7 @@ describe('IPLD Format resolver (local)', () => {
'Links/1/Name',
'Links/1/Tsize',
'Links/1/Hash',
'Data',
'named link'
'Data'
])
})
})
Expand Down

0 comments on commit 4dbe00d

Please sign in to comment.