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

Code before:
    /* Write the SELECT DB opcode */
    if ((res = rdbSaveType(rdb, RDB_OPCODE_SELECTDB)) < 0) goto werr;
    written += res;
    if ((res = rdbSaveLen(rdb, dbid)) < 0) goto werr;
    written += res;

    /* Write the RESIZE DB opcode. */
    unsigned long long expires_size = kvstoreSize(db->expires) + kvstoreImportingSize(db->expires);
    if ((res = rdbSaveType(rdb, RDB_OPCODE_RESIZEDB)) < 0) goto werr;
    written += res;
    if ((res = rdbSaveLen(rdb, db_size)) < 0) goto werr;
    written += res;
    if ((res = rdbSaveLen(rdb, expires_size)) < 0) goto werr;
    written += res;

    kvs_it = kvstoreIteratorInit(db->keys, HASHTABLE_ITER_SAFE | HASHTABLE_ITER_PREFETCH_VALUES | HASHTABLE_ITER_INCLUDE_IMPORTING);
    int last_slot = -1;
    /* Iterate this DB writing every entry */
    void *next;
    while (kvstoreIteratorNext(kvs_it, &next)) {
        robj *o = next;
        int curr_slot = kvstoreIteratorGetCurrentHashtableIndex(kvs_it);
        /* Save slot info. */
        if (server.cluster_enabled && curr_slot != last_slot) {
            sds slot_info = sdscatprintf(sdsempty(), "%i,%lu,%lu,%lu", curr_slot, kvstoreHashtableSize(db->keys, curr_slot),
                                         kvstoreHashtableSize(db->expires, curr_slot), kvstoreHashtableSize(db->keys_with_volatile_items, curr_slot));
            if ((res = rdbSaveAuxFieldStrStr(rdb, "slot-info", slot_info)) < 0) {
                sdsfree(slot_info);
                goto werr;
            }
            written += res;
            last_slot = curr_slot;
            sdsfree(slot_info);
        }
        sds keystr = objectGetKey(o);
        robj key;
        long long expire;
        size_t rdb_bytes_before_key = rdb->processed_bytes;

        initStaticStringObject(key, keystr);
        expire = objectGetExpire(o);
        if ((res = rdbSaveKeyValuePair(rdb, &key, o, expire, dbid)) < 0) goto werr;
        written += res;

        /* In fork child process, we can try to release memory back to the
         * OS and possibly avoid or decrease COW. We give the dismiss
         * mechanism a hint about an estimated size of the object we stored. */
        size_t dump_size = rdb->processed_bytes - rdb_bytes_before_key;
        if (server.in_fork_child) dismissObject(o, dump_size);

        /* Update child info every 1 second (approximately).

Review comment (suggestion):
Can I have some linebreaks here, please?

```suggestion
            sds slot_info = sdscatprintf(sdsempty(), "%i,%lu,%lu,%lu", curr_slot,
                                         kvstoreHashtableSize(db->keys, curr_slot),
                                         kvstoreHashtableSize(db->expires, curr_slot),
                                         kvstoreHashtableSize(db->keys_with_volatile_items, curr_slot));
```

Code after:
    /* Write the SELECT DB opcode */
    if ((res = rdbSaveType(rdb, RDB_OPCODE_SELECTDB)) < 0) goto werr;
    written += res;
    if ((res = rdbSaveLen(rdb, dbid)) < 0) goto werr;
    written += res;

    /* Write the RESIZE DB opcode. */
    unsigned long long expires_size = kvstoreSize(db->expires) + kvstoreImportingSize(db->expires);
    if ((res = rdbSaveType(rdb, RDB_OPCODE_RESIZEDB)) < 0) goto werr;
    written += res;
    if ((res = rdbSaveLen(rdb, db_size)) < 0) goto werr;
    written += res;
    if ((res = rdbSaveLen(rdb, expires_size)) < 0) goto werr;
    written += res;

    kvs_it = kvstoreIteratorInit(db->keys, HASHTABLE_ITER_SAFE | HASHTABLE_ITER_PREFETCH_VALUES | HASHTABLE_ITER_INCLUDE_IMPORTING);
    int last_slot = -1;
    /* Iterate this DB writing every entry */
    void *next;
    while (kvstoreIteratorNext(kvs_it, &next)) {
        robj *o = next;
        int curr_slot = kvstoreIteratorGetCurrentHashtableIndex(kvs_it);
        /* Save slot info. */
        if (server.cluster_enabled && curr_slot != last_slot) {
            sds slot_info = sdscatprintf(sdsempty(), "%i,%lu,%lu,%lu", curr_slot,
                                         kvstoreHashtableSize(db->keys, curr_slot),
                                         kvstoreHashtableSize(db->expires, curr_slot),
                                         kvstoreHashtableSize(db->keys_with_volatile_items, curr_slot));
            if ((res = rdbSaveAuxFieldStrStr(rdb, "slot-info", slot_info)) < 0) {
                sdsfree(slot_info);
                goto werr;
            }
            written += res;
            last_slot = curr_slot;
            sdsfree(slot_info);
        }
        sds keystr = objectGetKey(o);
        robj key;
        long long expire;
        size_t rdb_bytes_before_key = rdb->processed_bytes;

        initStaticStringObject(key, keystr);
        expire = objectGetExpire(o);
        if ((res = rdbSaveKeyValuePair(rdb, &key, o, expire, dbid)) < 0) goto werr;
        written += res;

        /* In fork child process, we can try to release memory back to the
         * OS and possibly avoid or decrease COW. We give the dismiss
         * mechanism a hint about an estimated size of the object we stored. */
        size_t dump_size = rdb->processed_bytes - rdb_bytes_before_key;
        if (server.in_fork_child) dismissObject(o, dump_size);