Skip to content

Commit

Permalink
Add zremRangeByRank command in Node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Mar 6, 2024
1 parent b4fded9 commit e67ed72
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 5 deletions.
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 @@ -125,6 +125,7 @@ enum RequestType {
XGroupDestroy = 81;
HSetNX = 82;
SIsMember = 83;
ZRemRangeByRank = 84;
}

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 @@ -362,6 +362,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::XTrim => Some(cmd("XTRIM")),
RequestType::HSetNX => Some(cmd("HSETNX")),
RequestType::SIsMember => Some(cmd("SISMEMBER")),
RequestType::ZRemRangeByRank => Some(cmd("ZREMRANGEBYRANK")),
}
}

Expand Down
18 changes: 18 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
createZpopmax,
createZpopmin,
createZrem,
createZremRangeByRank,
createZscore,
} from "./Commands";
import {
Expand Down Expand Up @@ -1149,6 +1150,23 @@ export class BaseClient {
return this.createWritePromise(createEcho(message));
}

/** Removes all elements in the sorted set stored at `key` with rank between `start` and `end`.
* Both `start` and `end` are zero-based indexes with 0 being the element with the lowest score.
* These indexes can be negative numbers, where they indicate offsets starting at the element with the highest score.
* See https://redis.io/commands/zremrangebyrank/ for more details.
*
* @param key - The key of the sorted set.
* @param start - The starting point of the range.
* @param end - The end of the range.
* @returns The number of members removed.
* If `start` exceeds the end of the sorted set, or if `start` is greater than `end`, 0 returned.
* If `end` exceeds the actual end of the sorted set, the range will stop at the actual end of the sorted set.
* If `key` does not exist 0 will be returned.
*/
public zremRangeByRank(key: string, start: number, end: number): Promise<number> {
return this.createWritePromise(createZremRangeByRank(key, start, end));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
15 changes: 15 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,18 @@ export function createZpopmax(key: string, count?: number): redis_request.Comman
export function createEcho(message: string): redis_request.Command {
return createCommand(RequestType.Echo, [message]);
}

/**
* @internal
*/
export function createZremRangeByRank(
key: string,
start: number,
stop: number
): redis_request.Command {
return createCommand(RequestType.ZRemRangeByRank, [
key,
start.toString(),
stop.toString(),
]);
}
19 changes: 19 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
createZpopmax,
createZpopmin,
createZrem,
createZremRangeByRank,
createZscore,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";
Expand Down Expand Up @@ -912,6 +913,24 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createEcho(message));
}

/** Removes all elements in the sorted set stored at `key` with rank between `start` and `end`.
* Both `start` and `end` are zero-based indexes with 0 being the element with the lowest score.
* These indexes can be negative numbers, where they indicate offsets starting at the element with the highest score.
* See https://redis.io/commands/zremrangebyrank/ for more details.
*
* @param key - The key of the sorted set.
* @param start - The starting point of the range.
* @param end - The end of the range.
*
* Command Response - The number of members removed.
* If `start` exceeds the end of the sorted set, or if `start` is greater than `end`, 0 returned.
* If `end` exceeds the actual end of the sorted set, the range will stop at the actual end of the sorted set.
* If `key` does not exist 0 will be returned.
*/
public zremRangeByRank(key: string, start: number, end: number): T {
return this.addAndReturn(createZremRangeByRank(key, start, end));
}

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

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zremRangeByRank test_%p`,
async (protocol) => {
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.zremRangeByRank(key, 2, 1)).toEqual(0);
expect(await client.zremRangeByRank(key, 0, 1)).toEqual(2);
expect(await client.zremRangeByRank(key, 0, 10)).toEqual(1);
expect(
await client.zremRangeByRank("nonExistingKey", 0, -1)
).toEqual(0);
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
12 changes: 7 additions & 5 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,24 @@ export function transactionTest(
args.push(1);
baseTransaction.smembers(key7);
args.push(["bar"]);
baseTransaction.zadd(key8, { member1: 1, member2: 2, member3: 3.5 });
args.push(3);
baseTransaction.zadd(key8, { member1: 1, member2: 2, member3: 3.5, member4: 4 });
args.push(4);
baseTransaction.zaddIncr(key8, "member2", 1);
args.push(3);
baseTransaction.zrem(key8, ["member1"]);
args.push(1);
baseTransaction.zcard(key8);
args.push(2);
args.push(3);
baseTransaction.zscore(key8, "member2");
args.push(3.0);
baseTransaction.zcount(key8, { bound: 2 }, "positiveInfinity");
args.push(2);
args.push(3);
baseTransaction.zpopmin(key8);
args.push({ member2: 3.0 });
baseTransaction.zpopmax(key8);
args.push({ member3: 3.5 });
args.push({ member4: 4 });
baseTransaction.zremRangeByRank(key8, 0, 1);
args.push(1);
return args;
}

Expand Down

0 comments on commit e67ed72

Please sign in to comment.