All files / src/resp/command/set smove-command.ts

37.93% Statements 11/29
0% Branches 0/4
50% Functions 1/2
37.93% Lines 11/29

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 881x   1x   1x 1x 1x                                               1x 4x   4x   4x   4x   4x                                                                                                
import { Logger } from "../../../logger";
import { IRequest } from "../../../server/request";
import { DataType } from "../../data/data-type";
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.
 *
 * SMOVE source destination member
 *
 * Move member from the set at source to the set at destination.
 * **Unlike the original implementation, this operation is **NOT** atomic. The key will be removed**
 * **from the source set before it is added to the destination set.**
 *
 * If the source set does not exist or does not contain the specified element, no operation
 * is performed and 0 is returned. Otherwise, the element is removed from the source set and
 * added to the destination set. When the specified element already exists in the destination
 * set, it is only removed from the source set.
 *
 * An error is returned if source or destination does not hold a set value.
 *
 * **Return value**<br>
 * Integer reply, specifically:
 *
 * 1 if the element is moved.
 * 0 if the element is not a member of source and no operation was performed.
 */
export class SMoveCommand extends IRespCommand {
    public DbDataType = DataType.SET
 
    public maxParams = 3
 
    public minParams = 3
 
    public name = "smove"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const skeyFrom: string = request.getParam(0),
            skeyTo: string = request.getParam(1),
            skeyName: string = request.getParam(2),
            dbFrom: DatabaseValue = db.getOrDefault(
                skeyFrom,
                new DatabaseValue(
                    DataType.SET,
                    new Set()
                )
            ),
            dbTo: DatabaseValue = db.getOrDefault(
                skeyTo,
                new DatabaseValue(
                    DataType.SET,
                    new Set()
                )
            );
        let result = 0;
        if (dbFrom.getSet().has(skeyName)) {
            dbFrom.getSet().delete(skeyName);
            if (!dbTo.getSet().has(skeyName)) {
                dbTo.getSet().add(skeyName);
                db.put(
                    skeyTo,
                    DatabaseValue.set(dbTo.getSet())
                );
                result = 1;
            } else {
                this.logger.debug(`${skeyTo}.${skeyName} already exists`);
                result = 1;
            }
            db.put(
                skeyFrom,
                DatabaseValue.set(dbFrom.getSet())
            );
        } else {
            this.logger.debug(`Did not find ${skeyFrom}.${skeyName}`);
        }
        return RedisToken.integer(result);
    }
}