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

31.25% Statements 10/32
0% Branches 0/8
50% Functions 1/2
32.25% Lines 10/31

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 88 89 90 91 92 931x   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.
 * ### SREM key member [member ...]
 *
 * Remove the specified members from the set stored at key. Specified members that are not a
 * member of this set are ignored. If key does not exist, it is treated as an empty set and
 * this command returns 0.
 *
 * An error is returned when the value stored at key is not a set.
 * ### Return value
 * Integer reply: the number of members that were removed from the set, not including non
 * existing members.
 * ### History
 * >= 2.4: Accepts multiple member arguments. Redis versions older than 2.4 can only remove a
 * set member per call.
 * ### Examples
 * ```
 * redis> SADD myset "one"
 * (integer) 1
 * redis> SADD myset "two"
 * (integer) 1
 * redis> SADD myset "three"
 * (integer) 1
 * redis> SREM myset "one"
 * (integer) 1
 * redis> SREM myset "four"
 * (integer) 0
 * redis> SMEMBERS myset
 * 1) "three"
 * 2) "two"
 * redis>
 * ```
 */
export class SRemCommand extends IRespCommand {
    public DbDataType = DataType.SET
 
    public maxParams = -1
 
    public minParams = 2
 
    public name = "srem"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const key: string = request.getParam(0),
            dbKey: DatabaseValue = db.get(key);
        if (!dbKey) {
            this.logger.debug(`Key ${key} does not exist.  Returning ZERO`);
            return RedisToken.integer(0);
        }
 
        let results: number = 0;
        for (let index = 1; index < request.getParams().length; index++) {
            const member = request.getParam(index);
            this.logger.debug(`Checking key ${key} for member ${member}`);
            if (dbKey.getSet().has(member)) {
                this.logger.debug(`Removing member ${member} from key ${key}`);
                dbKey.getSet().delete(member);
                ++results;
            }
        }
        if (results > 0) {
            if (dbKey.getSet().size > 0) {
                this.logger.debug(
                    `Saving updated set ${key} as %s`,
                    ...dbKey.getSet()
                );
                db.put(
                    key,
                    dbKey
                );
            } else {
                this.logger.debug(`Removing empty set ${key}`);
                db.remove(key);
            }
        }
        return RedisToken.integer(results);
    }
}