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

Code before:
        return ln ? listNodeValue(ln) : NULL;
    }

    case ITER_NODE: {
        if (iter->node) {
            clusterNode *node = iter->node;
            iter->node = NULL;
            return node;
        }
        return NULL;
    }
    }
    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 *)(long)slot);
    return de ? dictGetVal(de) : NULL;
}

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

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

static void setImportingSlotSource(int slot, clusterNode *node) {
    dictEntry *de = dictFind(server.cluster->importing_slots_from, (void *)(long)slot);
    if (node == NULL) {
        if (de) dictDelete(server.cluster->importing_slots_from, (void *)(long)slot);

Review comment (suggestion):
Nit: For maximum portability, i guess we should ideally use `intptr_t` instead of `long` in these casts. There is no guarantee in the C standard that long is pointer-sized but `intptr_t` always is.

Code after:
        return ln ? listNodeValue(ln) : NULL;
    }

    case ITER_NODE: {
        if (iter->node) {
            clusterNode *node = iter->node;
            iter->node = NULL;
            return node;
        }
        return NULL;
    }
    }
    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);