github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/cache/tools/hash.go (about) 1 package tools 2 3 import ( 4 "encoding/binary" 5 "math" 6 ) 7 8 // HashPoint returns a point in our hash space for the ith of n nodes. 9 // Note that it is quite important that both client and server agree 10 // about the implementation of this function. 11 func HashPoint(i, n int) uint32 { 12 return uint32(i * math.MaxUint32 / n) 13 } 14 15 // Hash returns the point in our hash space for a given artifact hash. 16 func Hash(h []byte) uint32 { 17 return binary.LittleEndian.Uint32(h) 18 } 19 20 // AlternateHash returns the alternate point in our hash space for a given artifact hash, 21 // i.e. on the second node we'd replicate it to. 22 func AlternateHash(h []byte) uint32 { 23 const halfway = 1 << 31 24 point := Hash(h) 25 if point > halfway { 26 return point - halfway 27 } 28 return point + halfway 29 }