MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / keyHashSlot

Function keyHashSlot

src/cluster.cpp:788–807  ·  view source on GitHub ↗

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). */

Source from the content-addressed store, hash-verified

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). */
788unsigned 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

Callers 6

slotToKeyUpdateKeyCoreFunction · 0.85
clusterCommandFunction · 0.85
getNodeByQueryFunction · 0.85
prefixKeyFunction · 0.85
enumerate_hashslotMethod · 0.85

Calls 1

crc16Function · 0.85

Tested by 1

enumerate_hashslotMethod · 0.68