We have 16384 hash slots. The hash slot of a given key is obtained * as the least significant 14 bits of the crc16 of the key. * * However if the key contains the {...} pattern, only the part between * { and } is hashed. This may be useful in the future to force certain * keys to be in the same node (assuming no resharding is in progress). */
| 786 | * { and } is hashed. This may be useful in the future to force certain |
| 787 | * keys to be in the same node (assuming no resharding is in progress). */ |
| 788 | unsigned int keyHashSlot(const char *key, int keylen) { |
| 789 | int s, e; /* start-end indexes of { and } */ |
| 790 | |
| 791 | for (s = 0; s < keylen; s++) |
| 792 | if (key[s] == '{') break; |
| 793 | |
| 794 | /* No '{' ? Hash the whole key. This is the base case. */ |
| 795 | if (s == keylen) return crc16(key,keylen) & 0x3FFF; |
| 796 | |
| 797 | /* '{' found? Check if we have the corresponding '}'. */ |
| 798 | for (e = s+1; e < keylen; e++) |
| 799 | if (key[e] == '}') break; |
| 800 | |
| 801 | /* No '}' or nothing between {} ? Hash the whole key. */ |
| 802 | if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF; |
| 803 | |
| 804 | /* If we are here there is both a { and a } on its right. Hash |
| 805 | * what is in the middle between { and }. */ |
| 806 | return crc16(key+s+1,e-s-1) & 0x3FFF; |
| 807 | } |
| 808 | |
| 809 | /* ----------------------------------------------------------------------------- |
| 810 | * CLUSTER node API |