From d4e51b40599bac2d3fe97f46c8beb8ec1046f8c6 Mon Sep 17 00:00:00 2001 From: Avi Fenesh <55848801+avifenesh@users.noreply.github.com> Date: Tue, 20 Feb 2024 18:38:41 +0200 Subject: [PATCH] Added Strlen command in node (#993) --- CHANGELOG.md | 1 + glide-core/src/protobuf/redis_request.proto | 1 + glide-core/src/socket_listener.rs | 1 + node/src/BaseClient.ts | 18 +++++++++++--- node/src/Commands.ts | 7 ++++++ node/src/Transaction.ts | 12 +++++++++ node/tests/SharedTests.ts | 27 ++++++++++++++++++++- node/tests/TestUtilities.ts | 2 ++ 8 files changed, 65 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54fd099a94..5409203239 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Node: Added ZCOUNT command ([#909](https://github.com/aws/glide-for-redis/pull/909)) * Python: Added ECHO command ([#953](https://github.com/aws/glide-for-redis/pull/953)) * Python: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975)) +* Node: Added STRLEN command ([#993](https://github.com/aws/glide-for-redis/pull/993)) #### Features * Python, Node: Added support in Lua Scripts ([#775](https://github.com/aws/glide-for-redis/pull/775), [#860](https://github.com/aws/glide-for-redis/pull/860)) diff --git a/glide-core/src/protobuf/redis_request.proto b/glide-core/src/protobuf/redis_request.proto index 30f8e3c046..0a659eee8a 100644 --- a/glide-core/src/protobuf/redis_request.proto +++ b/glide-core/src/protobuf/redis_request.proto @@ -107,6 +107,7 @@ enum RequestType { HLen = 69; Echo = 70; ZPopMin = 71; + Strlen = 72; } message Command { diff --git a/glide-core/src/socket_listener.rs b/glide-core/src/socket_listener.rs index 0090a1e20e..23329b7962 100644 --- a/glide-core/src/socket_listener.rs +++ b/glide-core/src/socket_listener.rs @@ -350,6 +350,7 @@ fn get_command(request: &Command) -> Option { RequestType::HLen => Some(cmd("HLEN")), RequestType::Echo => Some(cmd("ECHO")), RequestType::ZPopMin => Some(cmd("ZPOPMIN")), + RequestType::Strlen => Some(cmd("STRLEN")), } } diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 742e646363..ce32220015 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -50,6 +50,7 @@ import { createSMembers, createSRem, createSet, + createStrlen, createTTL, createType, createUnlink, @@ -1066,11 +1067,22 @@ export class BaseClient { return this.createWritePromise(createZcount(key, minScore, maxScore)); } + /** Returns the length of the string value stored at `key`. + * See https://redis.io/commands/strlen/ for more details. + * + * @param key - The key to check its length. + * @returns - The length of the string value stored at key + * If `key` does not exist, it is treated as an empty string, and the command returns 0. + */ + public strlen(key: string): Promise { + return this.createWritePromise(createStrlen(key)); + } + /** Returns the string representation of the type of the value stored at `key`. * See https://redis.io/commands/type/ for more details. - * - * @param key - The key to check its data type. - * @returns If the key exists, the type of the stored value is returned. Otherwise, a "none" string is returned. + * + * @param key - The `key` to check its data type. + * @returns If the `key` exists, the type of the stored value is returned. Otherwise, a "none" string is returned. */ public type(key: string): Promise { return this.createWritePromise(createType(key)); diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 9f6996312d..0da92cc018 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -823,3 +823,10 @@ export function createZcount( export function createType(key: string): redis_request.Command { return createCommand(RequestType.Type, [key]); } + +/** + * @internal + */ +export function createStrlen(key: string): redis_request.Command { + return createCommand(RequestType.Strlen, [key]); +} diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 691dc7b8e6..5e5c509b27 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -53,6 +53,7 @@ import { createSRem, createSelect, createSet, + createStrlen, createTTL, createType, createUnlink, @@ -840,6 +841,17 @@ export class BaseTransaction> { return this.addAndReturn(createType(key)); } + /** Returns the length of the string value stored at `key`. + * See https://redis.io/commands/strlen/ for more details. + * + * @param key - The `key` to check its length. + * Command Response - The length of the string value stored at `key` + * If `key` does not exist, it is treated as an empty string, and the command returns 0. + */ + public strlen(key: string): T { + return this.addAndReturn(createStrlen(key)); + } + /** Executes a single command, without checking inputs. Every part of the command, including subcommands, * should be added as a separate value in args. * diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 6e6b047afc..4025da31f7 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -1390,7 +1390,6 @@ export function runBaseTests(config: { config.timeout ); - it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `zscore test_%p`, async (protocol) => { @@ -1510,6 +1509,32 @@ export function runBaseTests(config: { }, config.timeout ); + + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( + `strlen test_%p`, + async (protocol) => { + await runTest(async (client: BaseClient) => { + const key1 = uuidv4(); + const key1Value = uuidv4(); + const key1ValueLength = key1Value.length; + expect(await client.set(key1, key1Value)).toEqual("OK"); + expect(await client.strlen(key1)).toEqual(key1ValueLength); + + expect(await client.strlen("nonExistKey")).toEqual(0); + + const listName = "myList"; + const listKey1Value = uuidv4(); + const listKey2Value = uuidv4(); + + expect( + await client.lpush(listName, [listKey1Value, listKey2Value]) + ).toEqual(2); + // An error is returned when key holds a non-string value + await expect(client.strlen(listName)).rejects.toThrow(); + }, protocol); + }, + config.timeout + ); } export function runCommonTests(config: { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index a4ff1df332..3e99f6fa56 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -78,6 +78,8 @@ export function transactionTest( args.push("OK"); baseTransaction.mget([key1, key2]); args.push(["bar", "baz"]); + baseTransaction.strlen(key1); + args.push(3); baseTransaction.del([key1]); args.push(1); baseTransaction.hset(key4, { [field]: value });