Repository: valkey-io/valkey
File: src/cluster_legacy.c (C)

Code before:
}

static void clusterNodeIterReset(ClusterNodeIterator *iter) {
    if (iter->type == ITER_DICT) {
        dictResetIterator(&iter->di);
    } else if (iter->type == ITER_NODE) {
        iter->node = NULL;
    }
}

/* Helpers to access the migrating/importing slot dictionaries. */
static clusterNode *getMigratingSlot(int slot) {
    dictEntry *de = dictFind(server.cluster->migrating_slots_to, &slot);
    return de ? dictGetVal(de) : NULL;
}

static void setMigratingSlot(int slot, clusterNode *node) {
    dictEntry *de = dictFind(server.cluster->migrating_slots_to, &slot);
    if (node == NULL) {
        if (de) dictDelete(server.cluster->migrating_slots_to, &slot);
        return;
    }
    if (de) {
        dictSetVal(server.cluster->migrating_slots_to, de, node);
    } else {
        int *k = zmalloc(sizeof(int));
        *k = slot;
        dictAdd(server.cluster->migrating_slots_to, k, node);
    }
}

static clusterNode *getImportingSlot(int slot) {
    dictEntry *de = dictFind(server.cluster->importing_slots_from, &slot);
    return de ? dictGetVal(de) : NULL;
}

static void setImportingSlot(int slot, clusterNode *node) {
    dictEntry *de = dictFind(server.cluster->importing_slots_from, &slot);
    if (node == NULL) {
        if (de) dictDelete(server.cluster->importing_slots_from, &slot);
        return;
    }
    if (de) {
        dictSetVal(server.cluster->importing_slots_from, de, node);
    } else {
        int *k = zmalloc(sizeof(int));
        *k = slot;
        dictAdd(server.cluster->importing_slots_from, k, node);
    }
}


Review comment (suggestion):
Instead of storing int* as the key and dynamically allocating it- could we just cast `int` to `void *` and store it directly?

I had a need for something similar here: https://github.com/valkey-io/valkey/pull/1949/files#diff-56dde90ed71d382a4bb92d3d07e2be5df85a40e91f2b4ad56e0b75bc87abc95eR104-R117

Code after:
    serverPanic("Unknown iterator type %d", iter->type);
}

static void clusterNodeIterReset(ClusterNodeIterator *iter) {
    if (iter->type == ITER_DICT) {
        dictResetIterator(&iter->di);
    } else if (iter->type == ITER_NODE) {
        iter->node = NULL;
    }
}

/* Helpers to access the migrating/importing slot dictionaries. */
clusterNode *getMigratingSlotDest(int slot) {
    dictEntry *de = dictFind(server.cluster->migrating_slots_to, (void *)(intptr_t)slot);
    return de ? dictGetVal(de) : NULL;
}

static void setMigratingSlotDest(int slot, clusterNode *node) {
    dictEntry *de = dictFind(server.cluster->migrating_slots_to, (void *)(intptr_t)slot);
    if (node == NULL) {
        if (de) dictDelete(server.cluster->migrating_slots_to, (void *)(intptr_t)slot);
        return;
    }
    if (de) {
        dictSetVal(server.cluster->migrating_slots_to, de, node);
    } else {
        dictAdd(server.cluster->migrating_slots_to, (void *)(intptr_t)slot, node);
    }
}

clusterNode *getImportingSlotSource(int slot) {
    dictEntry *de = dictFind(server.cluster->importing_slots_from, (void *)(intptr_t)slot);
    return de ? dictGetVal(de) : NULL;
}

static void setImportingSlotSource(int slot, clusterNode *node) {
    dictEntry *de = dictFind(server.cluster->importing_slots_from, (void *)(intptr_t)slot);
    if (node == NULL) {
        if (de) dictDelete(server.cluster->importing_slots_from, (void *)(intptr_t)slot);
        return;
    }
    if (de) {
        dictSetVal(server.cluster->importing_slots_from, de, node);
    } else {
        dictAdd(server.cluster->importing_slots_from, (void *)(intptr_t)slot, node);
    }
}

/* Aux fields were introduced in Redis OSS 7.2 to support the persistence
 * of various important node properties, such as shard id, in nodes.conf.
 * Aux fields take an explicit format of name=value pairs and have no