2019-02-22 18:39:29 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testrouting
|
|
|
|
|
2019-06-13 15:51:50 +01:00
|
|
|
import "storj.io/storj/pkg/storj"
|
2019-02-22 18:39:29 +00:00
|
|
|
|
|
|
|
type nodeDataDistanceSorter struct {
|
|
|
|
self storj.NodeID
|
|
|
|
nodes []*nodeData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s nodeDataDistanceSorter) Len() int { return len(s.nodes) }
|
|
|
|
|
|
|
|
func (s nodeDataDistanceSorter) Swap(i, j int) {
|
|
|
|
s.nodes[i], s.nodes[j] = s.nodes[j], s.nodes[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s nodeDataDistanceSorter) Less(i, j int) bool {
|
|
|
|
return compareByXor(s.nodes[i].node.Id, s.nodes[j].node.Id, s.self) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareByXor(left, right, reference storj.NodeID) int {
|
|
|
|
for i, r := range reference {
|
|
|
|
a, b := left[i]^r, right[i]^r
|
|
|
|
if a != b {
|
|
|
|
if a < b {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func bitAtDepth(id storj.NodeID, bitDepth int) bool {
|
|
|
|
// we could make this a fun one-liner but this is more understandable
|
|
|
|
byteDepth := bitDepth / 8
|
|
|
|
bitOffset := bitDepth % 8
|
|
|
|
power := uint(7 - bitOffset)
|
|
|
|
bitMask := byte(1 << power)
|
|
|
|
b := id[byteDepth]
|
2019-08-22 12:40:15 +01:00
|
|
|
return b&bitMask > 0
|
2019-02-22 18:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func extendPrefix(prefix string, bit bool) string {
|
|
|
|
if bit {
|
|
|
|
return prefix + "1"
|
|
|
|
}
|
|
|
|
return prefix + "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNearest(id storj.NodeID, nearest []*nodeData) bool {
|
|
|
|
for _, near := range nearest {
|
|
|
|
if near.node.Id == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|