All files / src/resp/command/key renamenx-command.ts

38.09% Statements 8/21
0% Branches 0/4
50% Functions 1/2
38.09% Lines 8/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 701x       1x 1x                                                               1x 4x   4x   4x   4x                                                  
import { Logger } from "../../../logger";
import { IRequest } from "../../../server/request";
import { Database } from "../../data/database";
import { DatabaseValue } from "../../data/database-value";
import { RedisToken } from "../../protocol/redis-token";
import { IRespCommand } from "../resp-command";
 
/**
 * ### Available since 1.0.0.
 * ### RENAMENX key newkey
 *
 * Renames key to newkey if newkey does not yet exist. It returns an error when key does not exist.
 *
 * In Cluster mode, both key and newkey must be in the same hash slot, meaning that in practice only
 * keys that have the same hash tag can be reliably renamed in cluster.
 *
 * ### History
 * <= 3.2.0: Before Redis 3.2.0, an error is returned if source and destination names are the same.
 * ### Return value
 * Integer reply, specifically:
 *
 * 1 if key was renamed to newkey.
 * 0 if newkey already exists.
 * ### Examples
 * ```
 * redis> SET mykey "Hello"
 * "OK"
 * redis> SET myotherkey "World"
 * "OK"
 * redis> RENAMENX mykey myotherkey
 * (integer) 0
 * redis> GET myotherkey
 * "World"
 * redis>
 * ```
 */
 
export class RenameNxCommand extends IRespCommand {
    public maxParams = 2
 
    public minParams = 2
 
    public name = "renamenx"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const oldKey: string = request.getParam(0),
            newKey: string = request.getParam(1),
            dbValue: DatabaseValue = db.get(oldKey);
        if (!dbValue) {
            this.logger.debug(`Key ${oldKey} does not exist`);
            return RedisToken.error("ERR no such key");
        } else if (db.exists(newKey)) {
            this.logger.debug(`Key ${newKey} already exists`);
            return RedisToken.integer(0);
        }
        db.rename(
            oldKey,
            newKey
        );
        this.logger.debug(`Renamed ${oldKey} to ${newKey}`);
        return RedisToken.integer(1);
    }
}