Commit 889c7246 authored by Ian Preston's avatar Ian Preston

optimise pin update command

This handles merkle links that aren't named. And improves the
Peergos usage from worst case 30s to ~20ms

License: MIT
Signed-off-by: default avatarIan Preston <ianopolous@protonmail.com>
parent a2d6f25e
......@@ -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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment