Skip to content

Commit

Permalink
added Lindex command to node (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
avifenesh authored Feb 20, 2024
1 parent d4e51b4 commit 66793f2
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* 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))
* 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))
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ enum RequestType {
Echo = 70;
ZPopMin = 71;
Strlen = 72;
Lindex = 73;
}

message Command {
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::Echo => Some(cmd("ECHO")),
RequestType::ZPopMin => Some(cmd("ZPOPMIN")),
RequestType::Strlen => Some(cmd("STRLEN")),
RequestType::Lindex => Some(cmd("LINDEX")),
}
}

Expand Down
16 changes: 16 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
createLRange,
createLRem,
createLTrim,
createLindex,
createMGet,
createMSet,
createPExpire,
Expand Down Expand Up @@ -1096,6 +1097,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<string | null> {
return this.createWritePromise(createLindex(key, index));
}

/**
* @internal
*/
Expand Down
10 changes: 10 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,13 @@ export function createType(key: string): redis_request.Command {
export function createStrlen(key: string): redis_request.Command {
return createCommand(RequestType.Strlen, [key]);
}

/**
* @internal
*/
export function createLindex(
key: string,
index: number
): redis_request.Command {
return createCommand(RequestType.Lindex, [key, index.toString()]);
}
16 changes: 16 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
createZcount,
createZrem,
createZscore,
createLindex,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -866,6 +867,21 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
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));
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,24 @@ export function runBaseTests<Context>(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<Context>(config: {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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);
Expand Down

0 comments on commit 66793f2

Please sign in to comment.