Skip to content

Commit

Permalink
added brpop command to NODE
Browse files Browse the repository at this point in the history
  • Loading branch information
avifenesh committed Mar 19, 2024
1 parent 96f78c2 commit 041df4c
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Node: Added Xadd, Xtrim commands. ([#1057](https://github.com/aws/glide-for-redis/pull/1057))
* Python: Added json module and JSON.SET JSON.GET commands ([#1056](https://github.com/aws/glide-for-redis/pull/1056))
* Node: Added Time command. ([#1114](https://github.com/aws/glide-for-redis/pull/1114))
* Node: Added BRPOP command. ([#1113](https://github.com/aws/glide-for-redis/pull/1113))
* Python, Node: Added LINDEX command ([#1058](https://github.com/aws/glide-for-redis/pull/1058), [#999](https://github.com/aws/glide-for-redis/pull/999))
* Python: Added ZRANK command ([#1065](https://github.com/aws/glide-for-redis/pull/1065))
* Core: Enabled Cluster Mode periodic checks by default ([#1089](https://github.com/aws/glide-for-redis/pull/1089))
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 @@ -133,6 +133,7 @@ enum RequestType {
Time = 89;
Zrank = 90;
Rename = 91;
Brpop = 92;
}

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 @@ -363,6 +363,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::Time => Some(cmd("TIME")),
RequestType::Zrank => Some(cmd("ZRANK")),
RequestType::Rename => Some(cmd("RENAME")),
RequestType::Brpop => Some(cmd("BRPOP")),
}
}

Expand Down
23 changes: 23 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
StreamReadOptions,
StreamTrimOptions,
ZaddOptions,
createBrpop,
createDecr,
createDecrBy,
createDel,
Expand Down Expand Up @@ -1342,6 +1343,28 @@ export class BaseClient {
return this.createWritePromise(createRename(key, newKey));
}

/** Blocking list pop primitive.
* Pop an element from the tail of the first list that is non-empty,
* with the given keys being checked in the order that they are given.
* Blocks the connection when there are no elements to pop from any of the given lists.
* See https://redis.io/commands/brpop/ for more details.
*
* @param keys - The `keys` of the lists to pop from.
* @param timeout - The `timeout` in seconds.
* @returns - An `array` containing the `key` from which the element was popped and the value of the popped element,
* formatted as [key, value]. If no element could be popped and the timeout expired, returns Null.
*
* @example
* await client.brpop(["list1", "list2"], 5);
* ["list1", "element"]
*/
public brpop(
keys: string[],
timeout: number,
): Promise<[string, string] | null> {
return this.createWritePromise(createBrpop(keys, timeout));
}

/**
* @internal
*/
Expand Down
11 changes: 11 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,17 @@ export function createTime(): redis_request.Command {
return createCommand(RequestType.Time, []);
}

/**
* @internal
*/
export function createBrpop(
keys: string[],
timeout: number,
): redis_request.Command {
const args = [...keys, timeout.toString()];
return createCommand(RequestType.Brpop, args);
}

export type StreamReadOptions = {
/**
* If set, the read request will block for the set amount of milliseconds or until the server has the required number of entries.
Expand Down
16 changes: 16 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
StreamReadOptions,
StreamTrimOptions,
ZaddOptions,
createBrpop,
createClientGetName,
createClientId,
createConfigGet,
Expand Down Expand Up @@ -1126,6 +1127,21 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
public rename(key: string, newKey: string): T {
return this.addAndReturn(createRename(key, newKey));
}

/** Blocking list pop primitive.
* Pop an element from the tail of the first list that is non-empty,
* with the given keys being checked in the order that they are given.
* Blocks the connection when there are no elements to pop from any of the given lists.
* See https://redis.io/commands/brpop/ for more details.
*
* @param keys - The `keys` of the lists to pop from.
* @param timeout - The `timeout` in seconds.
* Command Response - An `array` containing the `key` from which the element was popped and the value of the popped element,
* formatted as [key, value]. If no element could be popped and the timeout expired, returns Null.
*/
public brpop(keys: string[], timeout: number): T {
return this.addAndReturn(createBrpop(keys, timeout));
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,27 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`test brpop test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
expect(
await client.rpush("brpop-test", ["foo", "bar", "baz"]),
).toEqual(3);
// Test basic usage
expect(await client.brpop(["brpop-test"], 0.1)).toEqual([
"brpop-test",
"baz",
]);
// Delete all values from list
expect(await client.del(["brpop-test"])).toEqual(1);
// Test null return when key doesn't exist
expect(await client.brpop(["brpop-test"], 0.1)).toEqual(null);
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`persist test_%p`,
async (protocol) => {
Expand Down
4 changes: 4 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export function transactionTest(
args.push("OK");
baseTransaction.exists([key10]);
args.push(1);
baseTransaction.rpush(key6, [field + "1", field + "2", field + "3"]);
args.push(3);
baseTransaction.brpop([key6], 0.1);
args.push([key6, field + "3"]);
return args;
}

Expand Down

0 comments on commit 041df4c

Please sign in to comment.