All files / src/resp/command/string mget-command.ts

41.66% Statements 10/24
0% Branches 0/4
50% Functions 1/2
41.66% Lines 10/24

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 661x   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.
 * ### MGET key [key ...]
 * Returns the values of all specified keys. For every key that does not hold a string value or
 * does not exist, the special value nil is returned. Because of this, the operation never fails.
 *
 * ### Return value
 * Array reply: list of values at the specified keys.
 * ### Examples
 * ```
 * redis> SET key1 "Hello"
 * "OK"
 * redis> SET key2 "World"
 * "OK"
 * redis> MGET key1 key2 nonexisting
 * 1) "Hello"
 * 2) "World"
 * 3) (nil)
 * redis>
 * ```
 */
export class MGetCommand extends IRespCommand {
    dbDataType = DataType.STRING
 
    maxParams = -1
 
    minParams = 1
 
    name = "mget"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        const results: RedisToken[] = [];
        for (const key of request.getParams()) {
            this.logger.debug(`Getting key ${key}`);
            const dbValue: DatabaseValue = db.get(key);
            if (dbValue) {
                if (dbValue.getType() !== DataType.STRING) {
                    this.logger.debug(`Ignoring type ${dbValue.getType} for key ${key}`);
                    results.push(RedisToken.nullString());
                } else {
                    this.logger.debug(`Saving ${key} value ${dbValue.getString()}`);
                    results.push(RedisToken.string(dbValue.getString()));
                }
            } else {
                this.logger.debug(`Key ${key} does not exist`);
                results.push(RedisToken.nullString());
            }
        }
        return RedisToken.array(results);
    }
}