Skip to content

Commit

Permalink
Node: added zpopmin command. (valkey-io#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan Wattad authored Feb 21, 2024
1 parent 89f9970 commit fd54e7c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* 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))
* 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))
* 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))
* 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))
* Python: Added ZPOPMAX command ([#996](https://github.com/aws/glide-for-redis/pull/996))
Expand Down
19 changes: 19 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
createZadd,
createZcard,
createZcount,
createZpopmin,
createZrem,
createZscore,
} from "./Commands";
Expand Down Expand Up @@ -1100,6 +1101,24 @@ export class BaseClient {
return this.createWritePromise(createType(key));
}

/** Removes and returns the members with the lowest scores from the sorted set stored at `key`.
* If `count` is provided, up to `count` members with the lowest scores are removed and returned.
* Otherwise, only one member with the lowest score is removed and returned.
* See https://redis.io/commands/zpopmin for more details.
*
* @param key - The key of the sorted set.
* @param count - Specifies the quantity of members to pop. If not specified, pops one member.
* If `count` is higher than the sorted set's cardinality, returns all members and their scores.
* @returns A map of the removed members and their scores, ordered from the one with the lowest score to the one with the highest.
* If `key` doesn't exist, it will be treated as an empty sorted set and the command returns an empty map.
*/
public zpopmin(
key: string,
count?: number
): Promise<Record<string, number>> {
return this.createWritePromise(createZpopmin(key, count));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
8 changes: 8 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,11 @@ export function createLindex(
): redis_request.Command {
return createCommand(RequestType.Lindex, [key, index.toString()]);
}

/**
* @internal
*/
export function createZpopmin(key: string, count?: number): redis_request.Command {
const args: string[] = count == undefined ? [key] : [key, count.toString()];
return createCommand(RequestType.ZPopMin, args);
}
20 changes: 19 additions & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
createLRange,
createLRem,
createLTrim,
createLindex,
createMGet,
createMSet,
createPExpire,
Expand All @@ -61,9 +62,9 @@ import {
createZadd,
createZcard,
createZcount,
createZpopmin,
createZrem,
createZscore,
createLindex,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

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

/** Removes and returns the members with the lowest scores from the sorted set stored at `key`.
* If `count` is provided, up to `count` members with the lowest scores are removed and returned.
* Otherwise, only one member with the lowest score is removed and returned.
* See https://redis.io/commands/zpopmin for more details.
*
* @param key - The key of the sorted set.
* @param count - Specifies the quantity of members to pop. If not specified, pops one member.
* If `count` is higher than the sorted set's cardinality, returns all members and their scores.
*
* 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.
* If `key` doesn't exist, it will be treated as an empty sorted set and the command returns an empty map.
*/
public zpopmin(key: string, count?: number): T {
return this.addAndReturn(createZpopmin(key, count));
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
22 changes: 22 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,7 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`lindex test_%p`,
async (protocol) => {
Expand All @@ -1575,6 +1576,27 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zpopmin test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const membersScores = { a: 1, b: 2, c: 3 };
expect(await client.zadd(key, membersScores)).toEqual(3);
expect(await client.zpopmin(key)).toEqual({ a: 1.0 });
expect(await client.zpopmin(key, 3)).toEqual({
b: 2.0,
c: 3.0,
});
expect(await client.zpopmin(key)).toEqual({});
expect(await client.set(key, "value")).toEqual("OK");
await expect(client.zpopmin(key)).rejects.toThrow();
expect(await client.zpopmin("notExsitingKey")).toEqual({});
}, 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 @@ -143,6 +143,8 @@ export function transactionTest(
args.push(3.0);
baseTransaction.zcount(key8, { bound: 2 }, "positiveInfinity");
args.push(1);
baseTransaction.zpopmin(key8)
args.push({"member2": 3.0})
return args;
}

Expand Down

0 comments on commit fd54e7c

Please sign in to comment.