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: Ian Preston <ianopolous@protonmail.com>
Ian Preston committed
Oct 28, 2017 at 12:14 UTC
885de4f352ee5967cdb5deeedffd39131a9c6b08
1 file changed
+16
-9
merkledag/utils/diffenum.go
+16
-9
@@ -65,27 +65,34 @@ type diffpair struct {
65
// getLinkDiff returns a changeset between nodes 'a' and 'b'. Currently does
66
// not log deletions as our usecase doesnt call for this.
67
func getLinkDiff(a, b node.Node) []diffpair {
68
- have := make(map[string]*node.Link)
69
- names := make(map[string]*node.Link)
68
+ ina := make(map[string]*node.Link)
69
+ inb := make(map[string]*node.Link)
70
+ var aonly []*cid.Cid
71
+ for _, l := range b.Links() {
72
+ inb[l.Cid.KeyString()] = l
73
+ }
74
for _, l := range a.Links() {
71
- have[l.Cid.KeyString()] = l
72
- names[l.Name] = l
75
+ ina[l.Cid.KeyString()] = l
76
+ if inb[l.Cid.KeyString()] == nil {
77
+ aonly = append(aonly, l.Cid)
78
+ }
79
}
80
81
var out []diffpair
82
+ var aindex = 0
83
84
for _, l := range b.Links() {
78
- if have[l.Cid.KeyString()] != nil {
85
+ if ina[l.Cid.KeyString()] != nil {
86
continue
87
}
88
82
- match, ok := names[l.Name]
83
- if !ok {
89
+ if aindex < len(aonly) {
90
+ out = append(out, diffpair{bef: aonly[aindex], aft: l.Cid})
91
+ aindex++
92
+ } else {
93
out = append(out, diffpair{aft: l.Cid})
94
continue
95
}
87
-
88
- out = append(out, diffpair{bef: match.Cid, aft: l.Cid})
96
}
97
return out
98
}