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

Code before:
     * This optimization is only used when current_client flag `CLIENT_EXECUTING_COMMAND` is set.
     * It only gets set during the execution of command under `call` method. Other flows requesting
     * the key slot would fallback to calculateKeySlot.
     */
    if (server.current_client && server.current_client->slot >= 0 &&
        server.current_client->flags & CLIENT_EXECUTING_COMMAND) {
        debugServerAssertWithInfo(server.current_client, NULL, calculateKeySlot(key) == server.current_client->slot);
        return server.current_client->slot;
    }
    return calculateKeySlot(key);
}

/* This is a special version of dbAdd() that is used only when loading
 * keys from the RDB file: the key is passed as an SDS string that is
 * copied by the function and freed by the caller.
 *
 * Moreover this function will not abort if the key is already busy, to
 * give more control to the caller, nor will signal the key as ready
 * since it is not useful in this context.
 *
 * The function returns 1 if the key was added to the database, otherwise 0 is returned.
 *
 * The kvstore handles `key` based on `dictType` during initialization:
 * - If `dictType.embedded-entry` is 1, it clones the `key`.
 * - Otherwise, it assumes ownership of the `key`.
 * In this case a copy of `key` is made in kvstore, the caller must ensure the `key` is properly freed.
 */
int dbAddRDBLoad(serverDb *db, sds key, robj *val) {
    int slot = getKeySlot(key);
    dictEntry *de = kvstoreDictAddRaw(db->keys, slot, key, NULL);
    if (de == NULL) return 0;
    initObjectLRUOrLFU(val);
    kvstoreDictSetVal(db->keys, slot, de, val);
    return 1;
}

/* Overwrite an existing key with a new value. Incrementing the reference
 * count of the new value is up to the caller.
 * This function does not modify the expire time of the existing key.
 *
 * The 'overwrite' flag is an indication whether this is done as part of a
 * complete replacement of their key, which can be thought as a deletion and
 * replacement (in which case we need to emit deletion signals), or just an
 * update of a value of an existing key (when false).
 *
 * The dictEntry input is optional, can be used if we already have one.
 *
 * The program is aborted if the key was not already present. */
static void dbSetValue(serverDb *db, robj *key, robj *val, int overwrite, dictEntry *de) {
    int slot = getKeySlot(key->ptr);
    if (!de) de = kvstoreDictFind(db->keys, slot, key->ptr);

Review comment (suggestion):
```suggestion
 * In this case a copy of `key` is copied in kvstore, the caller must ensure the `key` is properly freed.
```


Code after:
     */
    if (server.current_client && server.current_client->slot >= 0 &&
        server.current_client->flags & CLIENT_EXECUTING_COMMAND) {
        debugServerAssertWithInfo(server.current_client, NULL, calculateKeySlot(key) == server.current_client->slot);
        return server.current_client->slot;
    }
    return calculateKeySlot(key);
}

/* This is a special version of dbAdd() that is used only when loading
 * keys from the RDB file: the key is passed as an SDS string that is
 * copied by the function and freed by the caller.
 *
 * Moreover this function will not abort if the key is already busy, to
 * give more control to the caller, nor will signal the key as ready
 * since it is not useful in this context.
 *
 * The function returns 1 if the key was added to the database, otherwise 0 is returned.
 *
 * In this case a copy of `key` is copied in kvstore, the caller must ensure the `key` is properly freed.
 */
int dbAddRDBLoad(serverDb *db, sds key, robj *val) {
    int slot = getKeySlot(key);
    dictEntry *de = kvstoreDictAddRaw(db->keys, slot, key, NULL);
    if (de == NULL) return 0;
    initObjectLRUOrLFU(val);
    kvstoreDictSetVal(db->keys, slot, de, val);
    return 1;
}

/* Overwrite an existing key with a new value. Incrementing the reference
 * count of the new value is up to the caller.
 * This function does not modify the expire time of the existing key.
 *
 * The 'overwrite' flag is an indication whether this is done as part of a
 * complete replacement of their key, which can be thought as a deletion and
 * replacement (in which case we need to emit deletion signals), or just an
 * update of a value of an existing key (when false).
 *
 * The dictEntry input is optional, can be used if we already have one.
 *
 * The program is aborted if the key was not already present. */
static void dbSetValue(serverDb *db, robj *key, robj *val, int overwrite, dictEntry *de) {
    int slot = getKeySlot(key->ptr);
    if (!de) de = kvstoreDictFind(db->keys, slot, key->ptr);
    serverAssertWithInfo(NULL, key, de != NULL);
    robj *old = dictGetVal(de);

    val->lru = old->lru;

    if (overwrite) {