mirror of
https://github.com/fluencelabs/redis
synced 2025-03-16 15:40:49 +00:00
Cluster: keys slot computation now supports hash tags.
Currently this is marginally useful, only to make sure two keys are in the same hash slot when the cluster is stable (no rehashing in progress). In the future it is possible that support will be added to run mutli-keys operations with keys in the same hash slot.
This commit is contained in:
parent
2d6eb68993
commit
142281dc79
@ -410,9 +410,30 @@ void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
/* 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. */
|
||||
* 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). */
|
||||
unsigned int keyHashSlot(char *key, int keylen) {
|
||||
return crc16(key,keylen) & 0x3FFF;
|
||||
int s, e; /* start-end indexes of { and } */
|
||||
|
||||
for (s = 0; s < keylen; s++)
|
||||
if (key[s] == '{') break;
|
||||
|
||||
/* No '{' ? Hash the whole key. This is the base case. */
|
||||
if (s == keylen) return crc16(key,keylen) & 0x3FFF;
|
||||
|
||||
/* '{' found? Check if we have the corresponding '}'. */
|
||||
for (e = s+1; e < keylen; e++)
|
||||
if (key[e] == '}') break;
|
||||
|
||||
/* No '}' or nothing betweeen {} ? Hash the whole key. */
|
||||
if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF;
|
||||
|
||||
/* If we are here there is both a { and a } on its right. Hash
|
||||
* what is in the middle between { and }. */
|
||||
return crc16(key+s+1,e-s-1) & 0x3FFF;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user