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

42.85% Statements 12/28
0% Branches 0/4
50% Functions 1/2
42.85% Lines 12/28

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 851x   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.
 *
 * ZINCRBY key increment member
 *
 * Increments the score of member in the sorted set stored at key by increment. If member
 * does not exist in the sorted set, it is added with increment as its score (as if its
 * previous score was 0.0). If key does not exist, a new sorted set with the specified
 * member as its sole member is created.
 *
 * An error is returned when key exists but does not hold a sorted set.
 *
 * The score value should be the string representation of a numeric value, and accepts double
 * precision floating point numbers. It is possible to provide a negative value to decrement
 * the score.
 *
 * **There is no compliment ZDECRBY.  Us a negative increment for that**
 *
 * **Return value**<br>
 * Bulk string reply: the new score of member (a double precision floating point number),
 * represented as string.
 *
 */
export class ZIncrByCommand extends IRespCommand {
    public DbDataType = DataType.ZSET
 
    public maxParams = 3
 
    public minParams = 3
 
    public name = "zincrby"
 
    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),
            zIncr: string = request.getParam(1),
            zmember: string = request.getParam(2);
        if (isNaN(Number(zIncr))) {
            return RedisToken.error("ERR value is not a valid float");
        }
 
        let score: number = 0;
        const dbKey: DatabaseValue = db.getOrDefault(
            zkey,
            new DatabaseValue(
                DataType.ZSET,
                new SortedSet()
            )
        );
        if (!dbKey.getSortedSet().has(zmember)) {
            this.logger.debug(`Creating new ZSET ${zkey} member ${zmember}`);
            dbKey.getSortedSet().add(
                zmember,
                0
            );
        }
        this.logger.debug(`Incrementing existing ZSET ${zkey} member ${zmember}`);
        dbKey.getSortedSet().incrBy(
            Number(zIncr),
            zmember
        );
 
        score = dbKey.getSortedSet().score(zmember);
        db.put(
            zkey,
            dbKey
        );
        return RedisToken.string(String(score));
    }
}