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

37.03% Statements 10/27
0% Branches 0/10
25% Functions 1/4
38.46% Lines 10/26

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 861x   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.
 * ### SDIFF key [key ...]
 *
 * Returns the members of the set resulting from the difference between the first set and all
 * the successive sets.
 *
 * ### For example:
 * ```
 * key1 = {a,b,c,d}
 * key2 = {c}
 * key3 = {a,c,e}
 * SDIFF key1 key2 key3 = {b,d}
 * ```
 * Keys that do not exist are considered to be empty sets.
 * ### Return value
 * Array reply: list with members of the resulting set.
 * ### Examples
 * ```
 * redis> SADD key1 "a"
 * (integer) 1
 * redis> SADD key1 "b"
 * (integer) 1
 * redis> SADD key1 "c"
 * (integer) 1
 * redis> SADD key2 "c"
 * (integer) 1
 * redis> SADD key2 "d"
 * (integer) 1
 * redis> SADD key2 "e"
 * (integer) 1
 * redis> SDIFF key1 key2
 * 1) "a"
 * 2) "b"
 * redis>
 * ```
 */
export class SDiffCommand extends IRespCommand {
    public DbDataType = DataType.SET
 
    public maxParams = -1
 
    public minParams = 1
 
    public name = "sdiff"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const result: RedisToken[] = [],
            skey: string = request.getParam(0);
        if (db.exists(skey)) {
            const dbKey: DatabaseValue = db.get(skey),
                diffset: Set<any> = new Set();
            for (let index = 1; index < request.getParams().length; index++) {
                const dbDiff: DatabaseValue = db.get(request.getParam(index));
                if (dbDiff && dbDiff.getType() === DataType.SET) {
                    dbDiff.getSet().forEach((element) => {
                        if (!diffset.has(element)) {
                            diffset.add(element);
                        }
                    });
                }
            }
            dbKey.getSet().forEach((element) => {
                if (!diffset.has(element)) {
                    result.push(RedisToken.string(element));
                }
            });
        }
        return RedisToken.array(result);
    }
}