Skip to content

Commit

Permalink
added Lindex command to node
Browse files Browse the repository at this point in the history
  • Loading branch information
avifenesh committed Feb 19, 2024
1 parent d580c0c commit 69240a2
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 @@ -14,6 +14,7 @@
* Node: Added ZCOUNT command ([#909](https:/aws/glide-for-redis/pull/909))
* Python: Added ECHO command ([#953](https:/aws/glide-for-redis/pull/953))
* Python: Added ZPOPMIN command ([#975](https:/aws/glide-for-redis/pull/975))
* Node: Added LINDEX command ([#998](https:/aws/glide-for-redis/pull/998))

#### Features
* Python, Node: Added support in Lua Scripts ([#775](https:/aws/glide-for-redis/pull/775), [#860](https:/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 @@ -107,6 +107,7 @@ enum RequestType {
HLen = 69;
Echo = 70;
ZPopMin = 71;
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 @@ -350,6 +350,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::HLen => Some(cmd("HLEN")),
RequestType::Echo => Some(cmd("ECHO")),
RequestType::ZPopMin => Some(cmd("ZPOPMIN")),
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 @@ -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 sorted list.
* @param index - The index of the element in the list.
* @returns - The element at index in the list stored at key
* If `key` does not exist, null is returned.
*/
public lindex(key: string, index: number): Promise<number> {
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 @@ -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()]);
}
16 changes: 16 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
createZcount,
createZrem,
createZscore,
createLindex,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -854,6 +855,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 sorted list.
* @param index - The index of the element in the list.
* @returns - The element at index in the list stored at key
* 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 @@ -1510,6 +1510,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);
}, 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 @@ -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);
Expand Down

0 comments on commit 69240a2

Please sign in to comment.