1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
11 type RootSorter struct {
17 func NewRootSorter(serviceRoots map[string]string, hash string) *RootSorter {
19 rs.root = make([]string, len(serviceRoots))
20 rs.weight = make([]string, len(serviceRoots))
21 rs.order = make([]int, len(serviceRoots))
23 for uuid, root := range serviceRoots {
25 rs.weight[i] = rs.getWeight(hash, uuid)
33 func (rs RootSorter) getWeight(hash string, uuid string) string {
35 return Md5String(hash + uuid[12:])
37 // Only useful for testing, a set of one service root, etc.
38 return Md5String(hash + uuid)
42 func (rs RootSorter) GetSortedRoots() []string {
43 sorted := make([]string, len(rs.order))
44 for i := range rs.order {
45 sorted[i] = rs.root[rs.order[i]]
50 // Less is really More here: the heaviest root will be at the front of the list.
51 func (rs RootSorter) Less(i, j int) bool {
52 return rs.weight[rs.order[j]] < rs.weight[rs.order[i]]
55 func (rs RootSorter) Len() int {
59 func (rs RootSorter) Swap(i, j int) {
60 sort.IntSlice(rs.order).Swap(i, j)