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

Code before:
}

/* Load an RDB file from the rio stream 'rdb'. On success C_OK is returned,
 * otherwise C_ERR is returned and 'errno' is set accordingly. */
int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
    functionsLibCtx *functions_lib_ctx = functionsLibCtxGetCurrent();
    rdbLoadingCtx loading_ctx = {.dbarray = server.db, .functions_lib_ctx = functions_lib_ctx};
    int retval = rdbLoadRioWithLoadingCtxScopedRdb(rdb, rdbflags, rsi, &loading_ctx);
    return retval;
}

/* Wrapper for rdbLoadRioWithLoadingCtx that manages a scoped RDB context.
 * This method wraps the rdbLoadRioWithLoadingCtx function, providing temporary
 * RDB context management. It sets a new current loading RDB, calls the wrapped
 * function, and then restores the previous loading RDB context. */
int rdbLoadRioWithLoadingCtxScopedRdb(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadingCtx *rdb_loading_ctx) {
    rio *prev_rio = server.loading_rio;
    server.loading_rio = rdb;
    int retval = rdbLoadRioWithLoadingCtx(rdb, rdbflags, rsi, rdb_loading_ctx);
    server.loading_rio = prev_rio;
    return retval;
}

/* Load an RDB file from the rio stream 'rdb'. We return one of the following:
 * - RDB_OK On success
 * - RDB_INCOMPATIBLE If the RDB is has an invalid signature or version
 * - RDB_FAILED
 * if the RDB is has an invalid signature or version we return 
 * The rdb_loading_ctx argument holds objects to which the rdb will be loaded to,
 * currently it only allow to set db object and functionLibCtx to which the data
 * will be loaded (in the future it might contains more such objects). */
int rdbLoadRioWithLoadingCtx(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadingCtx *rdb_loading_ctx) {
    uint64_t dbid = 0;
    int type, rdbver;
    uint64_t db_size = 0, expires_size = 0;
    int should_expand_db = 0;
    char buf[1024];
    int error;
    long long empty_keys_skipped = 0;
    bool is_valkey_magic = false, is_redis_magic = false;

    rdb->update_cksum = rdbLoadProgressCallback;
    rdb->max_processing_chunk = server.loading_process_events_interval_bytes;
    if (rioRead(rdb, buf, 9) == 0) goto eoferr;
    buf[9] = '\0';
    if (memcmp(buf, "REDIS0", 6) == 0) {
        is_redis_magic = true;
    } else if (memcmp(buf, "VALKEY", 6) == 0) {
        is_valkey_magic = true;
    } else {
        serverLog(LL_WARNING, "Wrong signature trying to load DB from file");

Review comment (suggestion):
```suggestion
 * - RDB_INCOMPATIBLE If the RDB has an invalid signature or version
```

Code after:
            *err = error;
        } else {
            serverLog(LL_WARNING, "Failed creating function, %s", error);
            sdsfree(error);
        }
    }
    return res;
}

/* Load an RDB file from the rio stream 'rdb'. On success C_OK is returned,
 * otherwise C_ERR is returned and 'errno' is set accordingly. */
int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {
    functionsLibCtx *functions_lib_ctx = functionsLibCtxGetCurrent();
    rdbLoadingCtx loading_ctx = {.dbarray = server.db, .functions_lib_ctx = functions_lib_ctx};
    int retval = rdbLoadRioWithLoadingCtxScopedRdb(rdb, rdbflags, rsi, &loading_ctx);
    return retval;
}

/* Wrapper for rdbLoadRioWithLoadingCtx that manages a scoped RDB context.
 * This method wraps the rdbLoadRioWithLoadingCtx function, providing temporary
 * RDB context management. It sets a new current loading RDB, calls the wrapped
 * function, and then restores the previous loading RDB context. */
int rdbLoadRioWithLoadingCtxScopedRdb(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadingCtx *rdb_loading_ctx) {
    rio *prev_rio = server.loading_rio;
    server.loading_rio = rdb;
    int retval = rdbLoadRioWithLoadingCtx(rdb, rdbflags, rsi, rdb_loading_ctx);
    server.loading_rio = prev_rio;
    return retval;
}

/* Load an RDB file from the rio stream 'rdb'. We return one of the following:
 * - RDB_OK On success
 * - RDB_INCOMPATIBLE If the RDB has an invalid signature or version
 * - RDB_FAILED in all other failure cases
 * The rdb_loading_ctx argument holds objects to which the rdb will be loaded to,
 * currently it only allow to set db object and functionLibCtx to which the data
 * will be loaded (in the future it might contains more such objects). */
int rdbLoadRioWithLoadingCtx(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadingCtx *rdb_loading_ctx) {
    uint64_t dbid = 0;
    int type, rdbver;
    uint64_t db_size = 0, expires_size = 0;
    int should_expand_db = 0;
    char buf[1024];
    int error;
    long long empty_keys_skipped = 0;
    bool is_valkey_magic = false, is_redis_magic = false;

    rdb->update_cksum = rdbLoadProgressCallback;
    rdb->max_processing_chunk = server.loading_process_events_interval_bytes;
    if (rioRead(rdb, buf, 9) == 0) goto eoferr;
    buf[9] = '\0';