|
5 | 5 | "io/ioutil" |
6 | 6 | "path/filepath" |
7 | 7 | "reflect" |
8 | | - "strings" |
9 | 8 |
|
10 | 9 | "github.com/golang/glog" |
11 | 10 | ) |
@@ -48,63 +47,30 @@ type PackageInfo struct { |
48 | 47 | Size string |
49 | 48 | } |
50 | 49 |
|
51 | | -func contains(info1 []PackageInfo, keys1 []string, key string, value PackageInfo) (int, bool) { |
52 | | - if len(info1) != len(keys1) { |
53 | | - return 0, false |
54 | | - } |
55 | | - for i, currVal := range info1 { |
56 | | - if !reflect.DeepEqual(currVal, value) { |
57 | | - continue |
58 | | - } |
59 | | - // Check if both global or local installations by trimming img-id and layer hash |
60 | | - tempPath1 := strings.SplitN(key, "/", 3) |
61 | | - tempPath2 := strings.SplitN(keys1[i], "/", 3) |
62 | | - if len(tempPath1) != 3 || len(tempPath2) != 3 { |
63 | | - continue |
64 | | - } |
65 | | - if tempPath1[2] == tempPath2[2] { |
66 | | - return i, true |
67 | | - } |
68 | | - } |
69 | | - return 0, false |
70 | | -} |
71 | | - |
72 | | -func multiVersionDiff(infoDiff []MultiVersionInfo, key string, map1, map2 map[string]PackageInfo) []MultiVersionInfo { |
73 | | - diff1Possible := []PackageInfo{} |
74 | | - diff1PossibleKeys := []string{} |
75 | | - |
76 | | - for key1, value1 := range map1 { |
77 | | - _, ok := map2[key1] |
78 | | - if !ok { |
79 | | - diff1Possible = append(diff1Possible, value1) |
80 | | - diff1PossibleKeys = append(diff1PossibleKeys, key1) |
81 | | - } else { |
82 | | - // if key in both maps, means layer hash is the same therefore packages are the same |
83 | | - delete(map2, key1) |
84 | | - } |
85 | | - } |
86 | | - |
| 50 | +func multiVersionDiff(infoDiff []MultiVersionInfo, packageName string, map1, map2 map[string]PackageInfo) []MultiVersionInfo { |
87 | 51 | diff1 := []PackageInfo{} |
88 | 52 | diff2 := []PackageInfo{} |
89 | | - for key2, value2 := range map2 { |
90 | | - index, ok := contains(diff1Possible, diff1PossibleKeys, key2, value2) |
| 53 | + for path, packInfo1 := range map1 { |
| 54 | + packInfo2, ok := map2[path] |
91 | 55 | if !ok { |
92 | | - diff2 = append(diff2, value2) |
| 56 | + diff1 = append(diff1, packInfo1) |
| 57 | + continue |
93 | 58 | } else { |
94 | | - if index == 0 { |
95 | | - diff1Possible = diff1Possible[1:] |
96 | | - diff1PossibleKeys = diff1PossibleKeys[1:] |
| 59 | + if reflect.DeepEqual(packInfo1, packInfo2) { |
| 60 | + delete(map2, path) |
| 61 | + } else { |
| 62 | + diff1 = append(diff1, packInfo1) |
| 63 | + diff2 = append(diff2, packInfo2) |
| 64 | + delete(map2, path) |
97 | 65 | } |
98 | | - diff1Possible = append(diff1Possible[:index], diff1Possible[index:]...) |
99 | | - diff1PossibleKeys = append(diff1PossibleKeys[:index], diff1PossibleKeys[index:]...) |
100 | 66 | } |
101 | 67 | } |
102 | | - |
103 | | - for _, val := range diff1Possible { |
104 | | - diff1 = append(diff1, val) |
| 68 | + for _, packInfo2 := range map2 { |
| 69 | + diff2 = append(diff2, packInfo2) |
105 | 70 | } |
| 71 | + |
106 | 72 | if len(diff1) > 0 || len(diff2) > 0 { |
107 | | - infoDiff = append(infoDiff, MultiVersionInfo{key, diff1, diff2}) |
| 73 | + infoDiff = append(infoDiff, MultiVersionInfo{packageName, diff1, diff2}) |
108 | 74 | } |
109 | 75 | return infoDiff |
110 | 76 | } |
@@ -166,27 +132,27 @@ func diffMaps(map1, map2 interface{}) interface{} { |
166 | 132 | infoDiff := []Info{} |
167 | 133 | multiInfoDiff := []MultiVersionInfo{} |
168 | 134 |
|
169 | | - for _, key1 := range map1Value.MapKeys() { |
170 | | - value1 := map1Value.MapIndex(key1) |
171 | | - value2 := map2Value.MapIndex(key1) |
172 | | - if !value2.IsValid() { |
173 | | - diff1.SetMapIndex(key1, value1) |
174 | | - } else if !reflect.DeepEqual(value2.Interface(), value1.Interface()) { |
| 135 | + for _, pack := range map1Value.MapKeys() { |
| 136 | + packageEntry1 := map1Value.MapIndex(pack) |
| 137 | + packageEntry2 := map2Value.MapIndex(pack) |
| 138 | + if !packageEntry2.IsValid() { |
| 139 | + diff1.SetMapIndex(pack, packageEntry1) |
| 140 | + } else if !reflect.DeepEqual(packageEntry2.Interface(), packageEntry1.Interface()) { |
175 | 141 | if multiV { |
176 | | - multiInfoDiff = multiVersionDiff(multiInfoDiff, key1.String(), |
177 | | - value1.Interface().(map[string]PackageInfo), value2.Interface().(map[string]PackageInfo)) |
| 142 | + multiInfoDiff = multiVersionDiff(multiInfoDiff, pack.String(), |
| 143 | + packageEntry1.Interface().(map[string]PackageInfo), packageEntry2.Interface().(map[string]PackageInfo)) |
178 | 144 | } else { |
179 | | - infoDiff = append(infoDiff, Info{key1.String(), value1.Interface().(PackageInfo), |
180 | | - value2.Interface().(PackageInfo)}) |
| 145 | + infoDiff = append(infoDiff, Info{pack.String(), packageEntry1.Interface().(PackageInfo), |
| 146 | + packageEntry2.Interface().(PackageInfo)}) |
181 | 147 | } |
182 | | - map2Value.SetMapIndex(key1, reflect.Value{}) |
| 148 | + map2Value.SetMapIndex(pack, reflect.Value{}) |
183 | 149 | } else { |
184 | | - map2Value.SetMapIndex(key1, reflect.Value{}) |
| 150 | + map2Value.SetMapIndex(pack, reflect.Value{}) |
185 | 151 | } |
186 | 152 | } |
187 | 153 | for _, key2 := range map2Value.MapKeys() { |
188 | | - value2 := map2Value.MapIndex(key2) |
189 | | - diff2.SetMapIndex(key2, value2) |
| 154 | + packageEntry2 := map2Value.MapIndex(key2) |
| 155 | + diff2.SetMapIndex(key2, packageEntry2) |
190 | 156 | } |
191 | 157 | if multiV { |
192 | 158 | return MultiVersionPackageDiff{Packages1: diff1.Interface().(map[string]map[string]PackageInfo), |
|
0 commit comments