Consistent Hashingの小ネタ
- 16. val serverList = new TreeMap[String, Long]()
for (server <- servers) {
serverList.put(hash(server.ip), server.ip)
}
def findKey(key: String): String = {
serverList.tailMap(hash(key)).firstKey
}
- 19. val md5 = MessageDigest.getInstance("MD5")
md5.reset()
md5.update(in.getBytes)
val bKey: Array[Byte] = md5.digest()
val res: Long =
(bKey(3) & 0xFF << 24 ).asInstanceOf[Long] |
(bKey(2) & 0xFF << 16 ).asInstanceOf[Long] |
(bKey(1) & 0xFF << 8 ).asInstanceOf[Long] |
(bKey(0) & 0xFF ).asInstanceOf[Long]
res