<commit_before>const Redis = require('ioredis');

class RedisClient {
    /**
    * @constructor
    * @param {Object} config - config
    * @param {string} config.host - Redis host
    * @param {number} config.port - Redis port
    * @param {string} config.password - Redis password
    * @param {werelogs.Logger} logger - logger instance
    */
    constructor(config, logger) {
        this._client = new Redis(config);
        this._client.on('error', err =>
            logger.trace('error from redis', {
                error: err,
                method: 'RedisClient.constructor',
                redisHost: config.host,
                redisPort: config.port,
            })
        );
        return this;
    }

    /**
    * increment value of a key by 1 and set a ttl
    * @param {string} key - key holding the value
    * @param {number} expiry - expiry in seconds
    * @param {callback} cb - callback
    * @return {undefined}
    */
    incrEx(key, expiry, cb) {
        return this._client
            .multi([['incr', key], ['expire', key, expiry]])
            .exec(cb);
    }

    /**
     * increment value of a key by a given amount and set a ttl
     * @param {string} key - key holding the value
     * @param {number} amount - amount to increase by
     * @param {number} expiry - expiry in seconds
     * @param {callback} cb - callback
     * @return {undefined}
     */
    incrbyEx(key, amount, expiry, cb) {
        return this._client
            .multi([['incrby', key, amount], ['expire', key, expiry]])
            .exec(cb);
    }

    /**
    * execute a batch of commands
    * @param {string[]} cmds - list of commands
    * @param {callback} cb - callback
    * @return {undefined}
    */
    batch(cmds, cb) {
        return this._client.pipeline(cmds).exec(cb);
    }

    /**
     * Checks if a key exists
     * @param {string} key - name of key
     * @param {function} cb - callback
     *   If cb response returns 0, key does not exist.
     *   If cb response returns 1, key exists.
     * @return {undefined}
     */
    exists(key, cb) {
        return this._client.exists(key, cb);
    }

    /**
     * Add a value and its score to a sorted set. If no sorted set exists, this
     * will create a new one for the given key.
     * @param {string} key - name of key
     * @param {integer} score - score used to order set
     * @param {string} value - value to store
     * @param {callback} cb - callback
     * @return {undefined}
     */
    zadd(key, score, value, cb) {
        return this._client.zadd(key, score, value, cb);
    }

    /**
     * Get number of elements in a sorted set.
     * Note: using this on a key that does not exist will return 0.
     * Note: using this on an existing key that isn't a sorted set will
     * return an error WRONGTYPE.
     * @param {string} key - name of key
     * @param {function} cb - callback
     * @return {undefined}
     */
    zcard(key, cb) {
        return this._client.zcard(key, cb);
    }

    /**
     * Get the score for given value in a sorted set
     * Note: using this on a key that does not exist will return nil.
     * Note: using this on a value that does not exist in a valid sorted set key
     *       will return nil.
     * @param {string} key - name of key
     * @param {string} value - value within sorted set
     * @param {function} cb - callback
     * @return {undefined}
     */
    zscore(key, value, cb) {
        return this._client.zscore(key, value, cb);
    }

    /**
     * Remove a value from a sorted set
     * @param {string} key - name of key
     * @param {string|array} value - value within sorted set. Can specify
     *   multiple values within an array
     * @param {function} cb - callback
     *   The cb response returns number of values removed
     * @return {undefined}
     */
    zrem(key, value, cb) {
        return this._client.zrem(key, value, cb);
    }

    /**
     * Get specified range of elements in a sorted set
     * @param {string} key - name of key
     * @param {integer} start - start index (inclusive)
     * @param {integer} end - end index (inclusive) (can use -1)
     * @param {function} cb - callback
     * @return {undefined}
     */
    zrange(key, start, end, cb) {
        return this._client.zrange(key, start, end, cb);
    }

    /**
     * Get range of elements in a sorted set based off score
     * @param {string} key - name of key
     * @param {integer|string} min - min score value (inclusive)
     *   (can use "-inf")
     * @param {integer|string} max - max score value (inclusive)
     *   (can use "+inf")
     * @param {function} cb - callback
     * @return {undefined}
     */
    zrangebyscore(key, min, max, cb) {
        return this._client.zrangebyscore(key, min, max, cb);
    }

    clear(cb) {
        return this._client.flushdb(cb);
    }

    disconnect() {
        this._client.disconnect();
    }
}

module.exports = RedisClient;<commit_msg>ARSN-147 Type RedisClient<commit_after>import Redis from 'ioredis';
import { Logger } from 'werelogs';

export type Config = { host: string; port: number; password: string };
export type Callback = (error: Error | null, value?: any) => void;

export default class RedisClient {
    _client: Redis.Redis;

    constructor(config: Config, logger: Logger) {
        this._client = new Redis(config);
        this._client.on('error', err =>
            logger.trace('error from redis', {
                error: err,
                method: 'RedisClient.constructor',
                redisHost: config.host,
                redisPort: config.port,
            })
        );
        return this;
    }

    /** increment value of a key by 1 and set a ttl */
    incrEx(key: string, expiry: number, cb: Callback) {
        const exp = expiry.toString();
        return this._client
            .multi([['incr', key], ['expire', key, exp]])
            .exec(cb);
    }

    /** increment value of a key by a given amount and set a ttl */
    incrbyEx(key: string, amount: number, expiry: number, cb: Callback) {
        const am = amount.toString();
        const exp = expiry.toString();
        return this._client
            .multi([['incrby', key, am], ['expire', key, exp]])
            .exec(cb);
    }

    /** execute a batch of commands */
    batch(cmds: string[][], cb: Callback) {
        return this._client.pipeline(cmds).exec(cb);
    }

    /**
     * Checks if a key exists
     * @param cb - callback
     *   If cb response returns 0, key does not exist.
     *   If cb response returns 1, key exists.
     */
    exists(key: string, cb: Callback) {
        return this._client.exists(key, cb);
    }

    /**
     * Add a value and its score to a sorted set. If no sorted set exists, this
     * will create a new one for the given key.
     * @param score - score used to order set
     */
    zadd(key: string, score: number, value: string, cb: Callback) {
        return this._client.zadd(key, score, value, cb);
    }

    /**
     * Get number of elements in a sorted set.
     * Note: using this on a key that does not exist will return 0.
     * Note: using this on an existing key that isn't a sorted set will
     * return an error WRONGTYPE.
     */
    zcard(key: string, cb: Callback) {
        return this._client.zcard(key, cb);
    }

    /**
     * Get the score for given value in a sorted set
     * Note: using this on a key that does not exist will return nil.
     * Note: using this on a value that does not exist in a valid sorted set key
     *       will return nil.
     */
     zscore(key: string, value: string, cb: Callback) {
        return this._client.zscore(key, value, cb);
    }

    /**
     * Remove a value from a sorted set
     * @param value - value within sorted set. Can specify multiple values within an array
     * @param {function} cb - callback
     *   The cb response returns number of values removed
     */
    zrem(key: string, value: string | string[], cb: Callback) {
        return this._client.zrem(key, value, cb);
    }

    /**
     * Get specified range of elements in a sorted set
     * @param start - start index (inclusive)
     * @param end - end index (inclusive) (can use -1)
     */
    zrange(key: string, start: number, end: number, cb: Callback) {
        return this._client.zrange(key, start, end, cb);
    }

    /**
     * Get range of elements in a sorted set based off score
     * @param min - min score value (inclusive)
     *   (can use "-inf")
     * @param max - max score value (inclusive)
     *   (can use "+inf")
     */
    zrangebyscore(
        key: string,
        min: number | string,
        max: number | string,
        cb: Callback,
    ) {
        return this._client.zrangebyscore(key, min, max, cb);
    }

    clear(cb: Callback) {
        return this._client.flushdb(cb);
    }

    disconnect() {
        this._client.disconnect();
    }
}
{'fraction_non_alphanumeric': 0.1165266106442577, 'fraction_numerical': 0.0014565826330532213, 'mean_word_length': 2.0484972677595628, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6058483', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2658, 'n_words': 969}