Slot to Key API. This is used by Redis Cluster in order to obtain in * a fast way a key that belongs to a specified hash slot. This is useful * while rehashing the cluster and in other conditions when we need to * understand if we have keys for a given hash slot. */
| 1914 | * while rehashing the cluster and in other conditions when we need to |
| 1915 | * understand if we have keys for a given hash slot. */ |
| 1916 | void slotToKeyUpdateKey(sds key, int add) { |
| 1917 | size_t keylen = sdslen(key); |
| 1918 | unsigned int hashslot = keyHashSlot(key,keylen); |
| 1919 | unsigned char buf[64]; |
| 1920 | unsigned char *indexed = buf; |
| 1921 | |
| 1922 | server.cluster->slots_keys_count[hashslot] += add ? 1 : -1; |
| 1923 | if (keylen+2 > 64) indexed = zmalloc(keylen+2); |
| 1924 | indexed[0] = (hashslot >> 8) & 0xff; |
| 1925 | indexed[1] = hashslot & 0xff; |
| 1926 | memcpy(indexed+2,key,keylen); |
| 1927 | if (add) { |
| 1928 | raxInsert(server.cluster->slots_to_keys,indexed,keylen+2,NULL,NULL); |
| 1929 | } else { |
| 1930 | raxRemove(server.cluster->slots_to_keys,indexed,keylen+2,NULL); |
| 1931 | } |
| 1932 | if (indexed != buf) zfree(indexed); |
| 1933 | } |
| 1934 | |
| 1935 | void slotToKeyAdd(sds key) { |
| 1936 | slotToKeyUpdateKey(key,1); |