From c23558f8e2f154dc447e2e2f1f9d1ce4d368c9d0 Mon Sep 17 00:00:00 2001 From: avifenesh Date: Mon, 19 Feb 2024 17:51:00 +0000 Subject: [PATCH] added Lindex command to node --- CHANGELOG.md | 1 + glide-core/src/protobuf/redis_request.proto | 1 + glide-core/src/socket_listener.rs | 1 + node/src/BaseClient.ts | 16 ++++++++++++++++ node/src/Commands.ts | 10 ++++++++++ node/src/Transaction.ts | 16 ++++++++++++++++ node/tests/SharedTests.ts | 19 +++++++++++++++++++ node/tests/TestUtilities.ts | 2 ++ 8 files changed, 66 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54fd099a94..5f66248c45 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 LINDEX command ([#999](https://github.com/aws/glide-for-redis/pull/999)) #### 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..bf98c2d241 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; + Lindex = 73; } message Command { diff --git a/glide-core/src/socket_listener.rs b/glide-core/src/socket_listener.rs index 0090a1e20e..3ffcd11a2e 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::Lindex => Some(cmd("LINDEX")), } } diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index 742e646363..ffdc9a3b92 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -39,6 +39,7 @@ import { createLRange, createLRem, createLTrim, + createLindex, createMGet, createMSet, createPExpire, @@ -1084,6 +1085,21 @@ export class BaseClient { preferReplica: connection_request.ReadFrom.PreferReplica, }; + /** Returns the element at index 'index' in the list stored at 'key'. + * The index is zero-based, so 0 means the first element, 1 the second element and so on. + * Negative indices can be used to designate elements starting at the tail of the list. + * Here, -1 means the last element, -2 means the penultimate and so forth. + * See https://redis.io/commands/lindex/ for more details. + * + * @param key - The key of the list. + * @param index - The index of the element in the list to retrieve. + * @returns - The element at index in the list stored at 'key' + * If 'index' is out of range or if 'key' does not exist, null is returned. + */ + public lindex(key: string, index: number): Promise { + return this.createWritePromise(createLindex(key, index)); + } + /** * @internal */ diff --git a/node/src/Commands.ts b/node/src/Commands.ts index 9f6996312d..e726e1d961 100644 --- a/node/src/Commands.ts +++ b/node/src/Commands.ts @@ -823,3 +823,13 @@ export function createZcount( export function createType(key: string): redis_request.Command { return createCommand(RequestType.Type, [key]); } + +/** + * @internal + */ +export function createLindex( + key: string, + index: number +): redis_request.Command { + return createCommand(RequestType.Lindex, [key, index.toString()]); +} diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 691dc7b8e6..b174d9534b 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -61,6 +61,7 @@ import { createZcount, createZrem, createZscore, + createLindex, } from "./Commands"; import { redis_request } from "./ProtobufMessage"; @@ -854,6 +855,21 @@ export class BaseTransaction> { public customCommand(args: string[]): T { return this.addAndReturn(createCustomCommand(args)); } + + /** Returns the element at index 'index' in the list stored at 'key'. + * The index is zero-based, so 0 means the first element, 1 the second element and so on. + * Negative indices can be used to designate elements starting at the tail of the list. + * Here, -1 means the last element, -2 means the penultimate and so forth. + * See https://redis.io/commands/lindex/ for more details. + * + * @param key - The key of the list. + * @param index - The index of the element in the list to retrieve. + * Command Response - The element at index in the list stored at 'key' + * If 'index' is out of range or if 'key' does not exist, null is returned. + */ + public lindex(key: string, index: number): T { + return this.addAndReturn(createLindex(key, index)); + } } /** diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index 6e6b047afc..ca274a5640 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -1510,6 +1510,25 @@ export function runBaseTests(config: { }, config.timeout ); + + it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( + `lindex test_%p`, + async (protocol) => { + await runTest(async (client: BaseClient) => { + const listName = uuidv4(); + const listKey1Value = uuidv4(); + const listKey2Value = uuidv4(); + expect( + await client.lpush(listName, [listKey1Value, listKey2Value]) + ).toEqual(2); + expect(await client.lindex(listName, 0)).toEqual(listKey2Value); + expect(await client.lindex(listName, 1)).toEqual(listKey1Value); + expect(await client.lindex("notExsitingList", 1)).toEqual(null); + expect(await client.lindex(listName, 3)).toEqual(null); + }, protocol); + }, + config.timeout + ); } export function runCommonTests(config: { diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index a4ff1df332..1b7f8ae503 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -113,6 +113,8 @@ export function transactionTest( args.push([field + "3", field + "2"]); baseTransaction.rpush(key6, [field + "1", field + "2", field + "3"]); args.push(3); + baseTransaction.lindex(key6, 0); + args.push(field + "1"); baseTransaction.rpop(key6); args.push(field + "3"); baseTransaction.rpopCount(key6, 2);