All files / src/resp/command/sset zrem-command.ts

46.15% Statements 12/26
0% Branches 0/4
50% Functions 1/2
48% Lines 12/25

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 681x   1x   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 { SortedSet } from "../../data/sorted-set";
import { RedisToken } from "../../protocol/redis-token";
import { IRespCommand } from "../resp-command";
 
/**
 * Available since 1.2.0.
 *
 * ZREM key member [member ...]
 *
 * Removes the specified members from the sorted set stored at key. Non existing members are ignored.
 *
 * An error is returned when key exists and does not hold a sorted set.
 *
 * **Return value**<br>
 * Integer reply, specifically:
 *
 * The number of members removed from the sorted set, not including non existing members.
 *
 */
export class ZRemCommand extends IRespCommand {
    public DbDataType = DataType.ZSET
 
    public maxParams = -1
 
    public minParams = 2
 
    public name = "zrem"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const zkey: string = request.getParam(0);
        let result: number = 0;
        const dbKey: DatabaseValue = db.getOrDefault(
            zkey,
            new DatabaseValue(
                DataType.ZSET,
                new SortedSet()
            )
        );
        for (let index = 1; index < request.getParams().length; index++) {
            if (dbKey.getSortedSet().has(request.getParam(index))) {
                ++result;
                dbKey.getSortedSet().del(request.getParam(index));
            }
        }
        if (dbKey.getSortedSet().length > 0) {
            db.put(
                zkey,
                dbKey
            );
        } else {
            this.logger.debug(`Removing empty zset ${zkey}`);
            db.remove(zkey);
        }
        return RedisToken.integer(result);
    }
}