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

Code before:
/* 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;
    }
}

void clusterNodeIterReset(ClusterNodeIterator *iter) {
    if (iter->type == ITER_DICT) {
        dictResetIterator(&iter->di);
    }

Review comment (suggestion):
Let's do the actual "my shard" logic here.

```suggestion
void clusterNodeIterInitMyShard(ClusterNodeIterator *iter) {
    list *nodes = clusterGetNodesInMyShard(server.cluster->myself);
    serverAssert(nodes != NULL);
    iter->type = ITER_LIST;
    listRewind(nodes, &iter->li);
```

Code after:
    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 */
        return ln ? listNodeValue(ln) : NULL;
    }
    /* This line is unreachable but added to avoid compiler warnings */
    default: {
        serverPanic("bad type");