MCPcopy
hub / github.com/tuna/tunasync / diffMirrorConfig

Function diffMirrorConfig

worker/config_diff.go:44–97  ·  view source on GitHub ↗

diffMirrorConfig finds the difference between the oldList and the newList it returns a series of operations that if these operations are applied to oldList, a newList equavuilance can be obtained.

(oldList, newList []mirrorConfig)

Source from the content-addressed store, hash-verified

42// it returns a series of operations that if these operations are applied to
43// oldList, a newList equavuilance can be obtained.
44func diffMirrorConfig(oldList, newList []mirrorConfig) []mirrorCfgTrans {
45 operations := []mirrorCfgTrans{}
46
47 oList := make([]mirrorConfig, len(oldList))
48 nList := make([]mirrorConfig, len(newList))
49 copy(oList, oldList)
50 copy(nList, newList)
51
52 // first ensure oldList and newList are sorted
53 sort.Sort(sortableMirrorList(oList))
54 sort.Sort(sortableMirrorList(nList))
55
56 if len(oList) != 0 && len(nList) != 0 {
57 // insert a tail node to both lists
58 // as the maximum node
59 lastOld, lastNew := oList[len(oList)-1], nList[len(nList)-1]
60 maxName := lastOld.Name
61 if lastNew.Name > lastOld.Name {
62 maxName = lastNew.Name
63 }
64 Nil := mirrorConfig{Name: "~" + maxName}
65 if Nil.Name <= maxName {
66 panic("Nil.Name should be larger than maxName")
67 }
68 oList, nList = append(oList, Nil), append(nList, Nil)
69
70 // iterate over both lists to find the difference
71 for i, j := 0, 0; i < len(oList) && j < len(nList); {
72 o, n := oList[i], nList[j]
73 if n.Name < o.Name {
74 operations = append(operations, mirrorCfgTrans{diffAdd, n})
75 j++
76 } else if o.Name < n.Name {
77 operations = append(operations, mirrorCfgTrans{diffDelete, o})
78 i++
79 } else {
80 if !reflect.DeepEqual(o, n) {
81 operations = append(operations, mirrorCfgTrans{diffModify, n})
82 }
83 i++
84 j++
85 }
86 }
87 } else {
88 for i := 0; i < len(oList); i++ {
89 operations = append(operations, mirrorCfgTrans{diffDelete, oList[i]})
90 }
91 for i := 0; i < len(nList); i++ {
92 operations = append(operations, mirrorCfgTrans{diffAdd, nList[i]})
93 }
94 }
95
96 return operations
97}

Callers 2

TestConfigDiffFunction · 0.85
ReloadMirrorConfigMethod · 0.85

Calls 1

sortableMirrorListTypeAlias · 0.85

Tested by 1

TestConfigDiffFunction · 0.68