From 142281dc79a1428a8deae4f531e7340851e66797 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 7 Feb 2014 16:59:24 +0100 Subject: [PATCH] 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. --- src/cluster.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 75eeba8b..5510c5e5 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -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; } /* -----------------------------------------------------------------------------