Skip to content

Commit

Permalink
optimise pin update command
Browse files Browse the repository at this point in the history
This handles merkle links that aren't named. And improves the
Peergos usage from worst case 30s to ~20ms

License: MIT
Signed-off-by: Ian Preston <[email protected]>
  • Loading branch information
ianopolous committed Oct 28, 2017
1 parent 0ce9c2e commit 885de4f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions merkledag/utils/diffenum.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,34 @@ type diffpair struct {
// getLinkDiff returns a changeset between nodes 'a' and 'b'. Currently does
// not log deletions as our usecase doesnt call for this.
func getLinkDiff(a, b node.Node) []diffpair {
have := make(map[string]*node.Link)
names := make(map[string]*node.Link)
ina := make(map[string]*node.Link)
inb := make(map[string]*node.Link)
var aonly []*cid.Cid
for _, l := range b.Links() {
inb[l.Cid.KeyString()] = l
}
for _, l := range a.Links() {
have[l.Cid.KeyString()] = l
names[l.Name] = l
ina[l.Cid.KeyString()] = l
if inb[l.Cid.KeyString()] == nil {
aonly = append(aonly, l.Cid)
}
}

var out []diffpair
var aindex = 0

for _, l := range b.Links() {
if have[l.Cid.KeyString()] != nil {
if ina[l.Cid.KeyString()] != nil {
continue
}

match, ok := names[l.Name]
if !ok {
if aindex < len(aonly) {
out = append(out, diffpair{bef: aonly[aindex], aft: l.Cid})
aindex++
} else {
out = append(out, diffpair{aft: l.Cid})
continue
}

out = append(out, diffpair{bef: match.Cid, aft: l.Cid})
}
return out
}

0 comments on commit 885de4f

Please sign in to comment.