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

43.47% Statements 10/23
0% Branches 0/4
50% Functions 1/2
43.47% Lines 10/23

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 731x   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 2.0.0.
 * ### ZRANK key member
 * Returns the rank of member in the sorted set stored at key, with the scores ordered
 * from low to high. The rank (or index) is 0-based, which means that the member with
 * the lowest score has rank 0.
 *
 * Use (not implemented ZRevRankCommand | ZREVRANK) to get the rank of an element with the scores
 * ordered from high to low.
 *
 * ### Return value
 * If member exists in the sorted set, Integer reply: the rank of member.
 *
 * If member does not exist in the sorted set or key does not exist, Bulk string reply: nil.
 *
 * ### Examples
 * ```
 * redis> ZADD myzset 1 "one"
 * (integer) 1
 * redis> ZADD myzset 2 "two"
 * (integer) 1
 * redis> ZADD myzset 3 "three"
 * (integer) 1
 * redis> ZRANK myzset "three"
 * (integer) 2
 * redis> ZRANK myzset "four"
 * (nil)
 * redis>
 * ```
 */
export class ZRankCommand extends IRespCommand {
    public DbDataType = DataType.ZSET
 
    public maxParams = 2
 
    public minParams = 2
 
    public name = "zrank"
 
    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),
            member: string = request.getParam(1);
        this.logger.debug(`Getting zrank for member ${member} of key ${key}`);
        const dbValue: DatabaseValue = db.get(key);
        if (!dbValue) {
            this.logger.debug(`Key ${key} not found`);
            return RedisToken.nullString();
        }
 
        if (dbValue.getSortedSet().has(member)) {
            const result: number = dbValue.getSortedSet().rank(member);
            return RedisToken.integer(result);
        }
 
        this.logger.debug(`Member ${member} not found in key ${key}`);
        return RedisToken.nullString();
    }
}