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

56.52% Statements 13/23
0% Branches 0/4
50% Functions 1/2
56.52% Lines 13/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 681x   1x   1x 1x 1x 1x                                       1x 4x   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 2.0.0.
 *
 * ZCOUNT key min max
 *
 * Returns the number of elements in the sorted set at key with a score between min and max.
 *
 * The min and max arguments have the same semantic as described for ZRANGEBYSCORE.
 *
 * If the key does not exist then ZERO is returned.
 *
 * Note: the command has a complexity of just O(log(N)) because it uses elements ranks (see ZRANK)
 * to get an idea of the range. Because of this there is no need to do a work proportional to the
 * size of the range. **This may not be true for the unit-redis-ness implementation**
 *
 * **Return value**<br>
 * Integer reply: the number of elements in the specified score range.
 */
export class ZCountCommand extends IRespCommand {
    public DbDataType = DataType.ZSET
 
    public maxParams = 3
 
    public minParams = 3
 
    public name = "zcount"
 
    private DEFAULT_ERROR = "ERR min or max is not a float";
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const skey: string = request.getParam(0),
            sMin: string = request.getParam(1),
            sMax: string = request.getParam(2);
        if (isNaN(Number(sMin)) || isNaN(Number(sMax))) {
            return RedisToken.error(this.DEFAULT_ERROR);
        }
 
        let result: number = 0;
        const dbKey: DatabaseValue = db.getOrDefault(
            skey,
            new DatabaseValue(
                DataType.ZSET,
                new SortedSet()
            )
        );
        result = dbKey.getSortedSet().count(
            Number(sMin),
            Number(sMax)
        );
        return RedisToken.integer(result);
    }
}