All files / src/resp/command/list rpush-command.ts

36.66% Statements 11/30
0% Branches 0/2
50% Functions 1/2
39.28% Lines 11/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 681x   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 { RedisToken } from "../../protocol/redis-token";
import { IRespCommand } from "../resp-command";
 
/**
 * ### Available since 1.0.0.
 * ### RPUSH key element [element ...]
 * Insert all the specified values at the tail of the list stored at key. If key does not exist,
 * it is created as empty list before performing the push operation. When key holds a value that
 * is not a list, an error is returned.
 *
 * It is possible to push multiple elements using a single command call just specifying multiple
 * arguments at the end of the command. Elements are inserted one after the other to the tail of
 * the list, from the leftmost element to the rightmost element. So for instance the command RPUSH
 * mylist a b c will result into a list containing a as first element, b as second element and c
 * as third element.
 *
 * ### Return value
 * Integer reply: the length of the list after the push operation.
 */
export class RPushCommand extends IRespCommand {
    public DbDataType = DataType.LIST
 
    public maxParams = -1
 
    public minParams = 2
 
    public name = "rpush"
 
    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);
        let list: DatabaseValue = db.get(key);
        this.logger.debug(`Getting list "${key}"`);
        if (!list) {
            this.logger.debug(`Creating new list: "${key}"`);
            list = new DatabaseValue(
                DataType.LIST,
                []
            );
        }
        for (let index = 1; index < request.getParams().length; index++) {
            const element = request.getParam(index);
            this.logger.debug(`PUSHING element "${element}" to list "${key}"`);
            list.getList().push(element);
        }
        const size: number = list.getList().length;
        db.put(
            key,
            list
        );
        for (let index = 1; index < request.getParams().length; index++) {
            request.getServerContext().emit(`__keyevent@${request.getSession().getCurrentDb()}__:rpush ${key}`);
        }
        this.logger.debug(`Returning list ${key} size ${size}`);
        return RedisToken.integer(size);
    }
}