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)
41 func (rs RootSorter) GetSortedRoots() []string {
42 sorted := make([]string, len(rs.order))
43 for i := range rs.order {
44 sorted[i] = rs.root[rs.order[i]]
49 // Less is really More here: the heaviest root will be at the front of the list.
50 func (rs RootSorter) Less(i, j int) bool {
51 return rs.weight[rs.order[j]] < rs.weight[rs.order[i]]
54 func (rs RootSorter) Len() int {
58 func (rs RootSorter) Swap(i, j int) {
59 sort.IntSlice(rs.order).Swap(i, j)