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)
| 42 | // it returns a series of operations that if these operations are applied to |
| 43 | // oldList, a newList equavuilance can be obtained. |
| 44 | func 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 | } |