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

Code before:
    dictSdsDestructor,     /* key destructor */
    NULL,                  /* val destructor */
    NULL                   /* allow to expand */
};

/* Cluster shards hash table, mapping shard id to list of nodes */
dictType clusterSdsToListType = {
    dictSdsHash,        /* hash function */
    NULL,               /* key dup */
    dictSdsKeyCompare,  /* key compare */
    dictSdsDestructor,  /* key destructor */
    dictListDestructor, /* val destructor */
    NULL                /* allow to expand */
};

typedef struct {
    enum { ITER_DICT, ITER_LIST } type;
    union {
        dictIterator di;
        listIter li;
    };
} ClusterNodeIterator;

void clusterNodeIterInitAllNodes(ClusterNodeIterator *iter, dict *d) {
    iter->type = ITER_DICT;
    dictInitSafeIterator(&iter->di, d);
}

void clusterNodeIterInitMyShard(ClusterNodeIterator *iter, list *l) {
    iter->type = ITER_LIST;
    listRewind(l, &iter->li);
}

clusterNode *clusterNodeIterNext(ClusterNodeIterator *iter) {
    switch (iter->type) {
    case ITER_DICT: {
        /* Get the next entry in the dictionary */
        dictEntry *de = dictNext(&iter->di);
        /* Return the value associated with the entry, or NULL if no more entries */
        return de ? dictGetVal(de) : NULL;
    }
    case ITER_LIST: {
        /* Get the next node in the list */
        listNode *ln = listNext(&iter->li);
        /* Return the value associated with the node, or NULL if no more nodes */
        return ln ? listNodeValue(ln) : NULL;
    }
    /* This line is unreachable but added to avoid compiler warnings */
    default: serverPanic("bad type"); return NULL;
    }
}

Review comment (suggestion):
```suggestion
void clusterNodeIterInitAllNodes(ClusterNodeIterator *iter) {
    iter->type = ITER_DICT;
    dictInitSafeIterator(&iter->di, server.cluster->nodes);
```

Code after:
dictType clusterNodesBlackListDictType = {
    dictSdsCaseHash,       /* hash function */
    NULL,                  /* key dup */
    dictSdsKeyCaseCompare, /* key compare */
    dictSdsDestructor,     /* key destructor */
    NULL,                  /* val destructor */
    NULL                   /* allow to expand */
};

/* Cluster shards hash table, mapping shard id to list of nodes */
dictType clusterSdsToListType = {
    dictSdsHash,        /* hash function */
    NULL,               /* key dup */
    dictSdsKeyCompare,  /* key compare */
    dictSdsDestructor,  /* key destructor */
    dictListDestructor, /* val destructor */
    NULL                /* allow to expand */
};

typedef struct {
    enum { ITER_DICT, ITER_LIST } type;
    union {
        dictIterator di;
        listIter li;
    };
} ClusterNodeIterator;

static void clusterNodeIterInitAllNodes(ClusterNodeIterator *iter) {
    iter->type = ITER_DICT;
    dictInitSafeIterator(&iter->di, server.cluster->nodes);
}

static void clusterNodeIterInitMyShard(ClusterNodeIterator *iter) {
    list *nodes = clusterGetNodesInMyShard(server.cluster->myself);
    serverAssert(nodes != NULL);
    iter->type = ITER_LIST;
    listRewind(nodes, &iter->li);
}

static clusterNode *clusterNodeIterNext(ClusterNodeIterator *iter) {
    switch (iter->type) {
    case ITER_DICT: {
        /* Get the next entry in the dictionary */
        dictEntry *de = dictNext(&iter->di);
        /* Return the value associated with the entry, or NULL if no more entries */
        return de ? dictGetVal(de) : NULL;
    }
    case ITER_LIST: {
        /* Get the next node in the list */
        listNode *ln = listNext(&iter->li);
        /* Return the value associated with the node, or NULL if no more nodes */