alibaba / cobar

a proxy for sharding databases and tables
Apache License 2.0
3.21k stars 1.22k forks source link

关于hash分片计算方法,请赐教 #77

Open YoungNong opened 7 years ago

YoungNong commented 7 years ago

单实例下,现有128个分库,8039709005,5907536637 这两个字符串,我用他们的hashcode取值后,对1024取模,结果是543,-383。怎么算也不在一个库里。请教下,字符串是怎么计算分片的

zhukunqian commented 7 years ago

PartitionByString.java 对字符串计算分片,里面有计算hash的函数。

hashcode & 1023 得到的应该在哪个分片上。

hexianmao commented 7 years ago

(hashcode & 1023)等价于(hashcode % 1024),主要是为了提高性能。落在哪个分片要看hashcode具体的值。 @zhukunqian