Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added zrem command in node and added zadd and zaddIncr to transactions. #834

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
createTTL,
createUnlink,
createZadd,
createZrem,
} from "./Commands";
import {
ClosingError,
Expand Down Expand Up @@ -1038,6 +1039,20 @@ export class BaseClient {
);
}

/** Removes the specified members from the sorted set stored at `key`.
* Specified members that are not a member of this set are ignored.
* See https://redis.io/commands/zrem/ for more details.
*
* @param key - The key of the sorted set.
* @param members - A list of members to remove from the sorted set.
* @returns The number of members that were removed from the sorted set, not including non-existing members.
* If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zrem(key: string, members: string[]): Promise<number> {
return this.createWritePromise(createZrem(key, members));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
10 changes: 10 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,13 @@ export function createZadd(
);
return createCommand(RequestType.Zadd, args);
}

/**
* @internal
*/
export function createZrem(
key: string,
members: string[]
): redis_request.Command {
return createCommand(RequestType.Zrem, [key].concat(members));
}
70 changes: 70 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ExpireOptions,
InfoOptions,
SetOptions,
ZaddOptions,
createClientGetName,
createClientId,
createConfigGet,
Expand Down Expand Up @@ -49,6 +50,8 @@ import {
createSet,
createTTL,
createUnlink,
createZadd,
createZrem,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -696,6 +699,73 @@ export class BaseTransaction {
this.commands.push(createTTL(key));
}

/** Adds members with their scores to the sorted set stored at `key`.
* If a member is already a part of the sorted set, its score is updated.
* See https://redis.io/commands/zadd/ for more details.
*
* @param key - The key of the sorted set.
* @param membersScoresMap - A mapping of members to their corresponding scores.
* @param options - The Zadd options.
* @param changed - Modify the return value from the number of new elements added, to the total number of elements changed.
*
* Command Response - The number of elements added to the sorted set.
* If `changed` is set, returns the number of elements updated in the sorted set.
*/
public zadd(
key: string,
membersScoresMap: Record<string, number>,
options?: ZaddOptions,
changed?: boolean
) {
this.commands.push(
createZadd(
key,
membersScoresMap,
options,
changed ? "CH" : undefined
)
);
}

/** Increments the score of member in the sorted set stored at `key` by `increment`.
* If `member` does not exist in the sorted set, it is added with `increment` as its score (as if its previous score was 0.0).
* If `key` does not exist, a new sorted set with the specified member as its sole member is created.
* See https://redis.io/commands/zadd/ for more details.
*
* @param key - The key of the sorted set.
* @param member - A member in the sorted set to increment.
* @param increment - The score to increment the member.
* @param options - The Zadd options.
*
* Command Response - The score of the member.
* If there was a conflict with the options, the operation aborts and null is returned.
*/
public zaddIncr(
key: string,
member: string,
increment: number,
options?: ZaddOptions
) {
this.commands.push(
createZadd(key, { [member]: increment }, options, "INCR")
);
}

/** Removes the specified members from the sorted set stored at `key`.
* Specified members that are not a member of this set are ignored.
* See https://redis.io/commands/zrem/ for more details.
*
* @param key - The key of the sorted set.
* @param members - A list of members to remove from the sorted set.
*
* Command Response - The number of members that were removed from the sorted set, not including non-existing members.
* If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
* If `key` holds a value that is not a sorted set, an error is returned.
*/
public zrem(key: string, members: string[]) {
this.commands.push(createZrem(key, members));
}

/** 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
19 changes: 19 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,25 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it(
"zrem test",
async () => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const membersScores = { one: 1, two: 2, three: 3 };
expect(await client.zadd(key, membersScores)).toEqual(3);
expect(await client.zrem(key, ["one"])).toEqual(1);
expect(await client.zrem(key, ["one", "two", "three"])).toEqual(
2
);
expect(
await client.zrem("non_existing_set", ["member"])
).toEqual(0);
});
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
7 changes: 7 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function transactionTest(
const key5 = "{key}" + uuidv4();
const key6 = "{key}" + uuidv4();
const key7 = "{key}" + uuidv4();
const key8 = "{key}" + uuidv4();
const field = uuidv4();
const value = uuidv4();
baseTransaction.set(key1, "bar");
Expand Down Expand Up @@ -84,6 +85,9 @@ export function transactionTest(
baseTransaction.srem(key7, ["foo"]);
baseTransaction.scard(key7);
baseTransaction.smembers(key7);
baseTransaction.zadd(key8, { member1: 1, member2: 2 });
baseTransaction.zaddIncr(key8, "member2", 1);
baseTransaction.zrem(key8, ["member1"]);
return [
"OK",
null,
Expand All @@ -108,6 +112,9 @@ export function transactionTest(
1,
1,
["bar"],
2,
3,
1,
];
}

Expand Down