Skip to content

Commit fd54e7c

Browse files
author
Adan Wattad
authored
Node: added zpopmin command. (#1008)
1 parent 89f9970 commit fd54e7c

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Python, Node: Added HLEN command ([#944](https://github.com/aws/glide-for-redis/pull/944), [#981](https://github.com/aws/glide-for-redis/pull/981))
1414
* Node: Added ZCOUNT command ([#909](https://github.com/aws/glide-for-redis/pull/909))
1515
* Python: Added ECHO command ([#953](https://github.com/aws/glide-for-redis/pull/953))
16-
* Python: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975))
16+
* Python, Node: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975), [#1008](https://github.com/aws/glide-for-redis/pull/1008))
1717
* Node: Added STRLEN command ([#993](https://github.com/aws/glide-for-redis/pull/993))
1818
* Node: Added LINDEX command ([#999](https://github.com/aws/glide-for-redis/pull/999))
1919
* Python: Added ZPOPMAX command ([#996](https://github.com/aws/glide-for-redis/pull/996))

node/src/BaseClient.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
createZadd,
6060
createZcard,
6161
createZcount,
62+
createZpopmin,
6263
createZrem,
6364
createZscore,
6465
} from "./Commands";
@@ -1100,6 +1101,24 @@ export class BaseClient {
11001101
return this.createWritePromise(createType(key));
11011102
}
11021103

1104+
/** Removes and returns the members with the lowest scores from the sorted set stored at `key`.
1105+
* If `count` is provided, up to `count` members with the lowest scores are removed and returned.
1106+
* Otherwise, only one member with the lowest score is removed and returned.
1107+
* See https://redis.io/commands/zpopmin for more details.
1108+
*
1109+
* @param key - The key of the sorted set.
1110+
* @param count - Specifies the quantity of members to pop. If not specified, pops one member.
1111+
* If `count` is higher than the sorted set's cardinality, returns all members and their scores.
1112+
* @returns A map of the removed members and their scores, ordered from the one with the lowest score to the one with the highest.
1113+
* If `key` doesn't exist, it will be treated as an empty sorted set and the command returns an empty map.
1114+
*/
1115+
public zpopmin(
1116+
key: string,
1117+
count?: number
1118+
): Promise<Record<string, number>> {
1119+
return this.createWritePromise(createZpopmin(key, count));
1120+
}
1121+
11031122
private readonly MAP_READ_FROM_STRATEGY: Record<
11041123
ReadFrom,
11051124
connection_request.ReadFrom

node/src/Commands.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,11 @@ export function createLindex(
847847
): redis_request.Command {
848848
return createCommand(RequestType.Lindex, [key, index.toString()]);
849849
}
850+
851+
/**
852+
* @internal
853+
*/
854+
export function createZpopmin(key: string, count?: number): redis_request.Command {
855+
const args: string[] = count == undefined ? [key] : [key, count.toString()];
856+
return createCommand(RequestType.ZPopMin, args);
857+
}

node/src/Transaction.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
createLRange,
4242
createLRem,
4343
createLTrim,
44+
createLindex,
4445
createMGet,
4546
createMSet,
4647
createPExpire,
@@ -61,9 +62,9 @@ import {
6162
createZadd,
6263
createZcard,
6364
createZcount,
65+
createZpopmin,
6466
createZrem,
6567
createZscore,
66-
createLindex,
6768
} from "./Commands";
6869
import { redis_request } from "./ProtobufMessage";
6970

@@ -858,13 +859,30 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
858859
* See https://redis.io/commands/strlen/ for more details.
859860
*
860861
* @param key - The `key` to check its length.
862+
*
861863
* Command Response - The length of the string value stored at `key`
862864
* If `key` does not exist, it is treated as an empty string, and the command returns 0.
863865
*/
864866
public strlen(key: string): T {
865867
return this.addAndReturn(createStrlen(key));
866868
}
867869

870+
/** Removes and returns the members with the lowest scores from the sorted set stored at `key`.
871+
* If `count` is provided, up to `count` members with the lowest scores are removed and returned.
872+
* Otherwise, only one member with the lowest score is removed and returned.
873+
* See https://redis.io/commands/zpopmin for more details.
874+
*
875+
* @param key - The key of the sorted set.
876+
* @param count - Specifies the quantity of members to pop. If not specified, pops one member.
877+
* If `count` is higher than the sorted set's cardinality, returns all members and their scores.
878+
*
879+
* Command Response - A map of the removed members and their scores, ordered from the one with the lowest score to the one with the highest.
880+
* If `key` doesn't exist, it will be treated as an empty sorted set and the command returns an empty map.
881+
*/
882+
public zpopmin(key: string, count?: number): T {
883+
return this.addAndReturn(createZpopmin(key, count));
884+
}
885+
868886
/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
869887
* should be added as a separate value in args.
870888
*

node/tests/SharedTests.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,7 @@ export function runBaseTests<Context>(config: {
15571557
},
15581558
config.timeout
15591559
);
1560+
15601561
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
15611562
`lindex test_%p`,
15621563
async (protocol) => {
@@ -1575,6 +1576,27 @@ export function runBaseTests<Context>(config: {
15751576
},
15761577
config.timeout
15771578
);
1579+
1580+
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
1581+
`zpopmin test_%p`,
1582+
async (protocol) => {
1583+
await runTest(async (client: BaseClient) => {
1584+
const key = uuidv4();
1585+
const membersScores = { a: 1, b: 2, c: 3 };
1586+
expect(await client.zadd(key, membersScores)).toEqual(3);
1587+
expect(await client.zpopmin(key)).toEqual({ a: 1.0 });
1588+
expect(await client.zpopmin(key, 3)).toEqual({
1589+
b: 2.0,
1590+
c: 3.0,
1591+
});
1592+
expect(await client.zpopmin(key)).toEqual({});
1593+
expect(await client.set(key, "value")).toEqual("OK");
1594+
await expect(client.zpopmin(key)).rejects.toThrow();
1595+
expect(await client.zpopmin("notExsitingKey")).toEqual({});
1596+
}, protocol);
1597+
},
1598+
config.timeout
1599+
);
15781600
}
15791601

15801602
export function runCommonTests<Context>(config: {

node/tests/TestUtilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ export function transactionTest(
143143
args.push(3.0);
144144
baseTransaction.zcount(key8, { bound: 2 }, "positiveInfinity");
145145
args.push(1);
146+
baseTransaction.zpopmin(key8)
147+
args.push({"member2": 3.0})
146148
return args;
147149
}
148150

0 commit comments

Comments
 (0)