Skip to content

Commit

Permalink
Added zremRangeByScore command in Node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Feb 16, 2024
1 parent 1b09b1c commit c547a42
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
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 @@ -105,6 +105,7 @@ enum RequestType {
ZScore = 67;
Type = 68;
HLen = 69;
ZRemRangeByScore = 70;
}

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 @@ -348,6 +348,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::ZScore => Some(cmd("ZSCORE")),
RequestType::Type => Some(cmd("TYPE")),
RequestType::HLen => Some(cmd("HLEN")),
RequestType::ZRemRangeByScore => Some(cmd("ZREMRANGEBYSCORE")),
}
}

Expand Down
19 changes: 19 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
createZcard,
createZcount,
createZrem,
createZremRangeByScore,
createZscore,
} from "./Commands";
import {
Expand Down Expand Up @@ -1065,6 +1066,24 @@ export class BaseClient {
return this.createWritePromise(createZcount(key, minScore, maxScore));
}

/** Removes all elements in the sorted set stored at `key` with a score between `minScore` and `maxScore` (inclusive).
* See https://redis.io/commands/zremrangebyscore/ for more details.
*
* @param key - The key of the sorted set.
* @param minScore - The minimum score to remove from. Can be positive/negative infinity, or specific score and inclusivity.
* @param maxScore - The maximum score to remove to. Can be positive/negative infinity, or specific score and inclusivity.
* @returns the number of members removed.
* If `key` does not exist, it is treated as an empty sorted set, and the command returns 0.
* If `minScore` is greater than `maxScore`, 0 is returned.
*/
public zremRangeByScore(
key: string,
minScore: ScoreLimit,
maxScore: ScoreLimit
): Promise<number> {
return this.createWritePromise(createZremRangeByScore(key, minScore, maxScore));
}

private readonly MAP_READ_FROM_STRATEGY: Record<
ReadFrom,
connection_request.ReadFrom
Expand Down
37 changes: 37 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,40 @@ export function createZcount(

return createCommand(RequestType.Zcount, args);
}

/**
* @internal
*/
export function createZremRangeByScore(
key: string,
minScore: ScoreLimit,
maxScore: ScoreLimit
): redis_request.Command {
const args = [key];

if (minScore == "positiveInfinity") {
args.push(positiveInfinityArg);
} else if (minScore == "negativeInfinity") {
args.push(negativeInfinityArg);
} else {
const value =
minScore.isInclusive == false
? isInclusiveArg + minScore.bound.toString()
: minScore.bound.toString();
args.push(value);
}

if (maxScore == "positiveInfinity") {
args.push(positiveInfinityArg);
} else if (maxScore == "negativeInfinity") {
args.push(negativeInfinityArg);
} else {
const value =
maxScore.isInclusive == false
? isInclusiveArg + maxScore.bound.toString()
: maxScore.bound.toString();
args.push(value);
}

return createCommand(RequestType.ZRemRangeByScore, args);
}
22 changes: 21 additions & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ import {
createZcard,
createZcount,
createZrem,
createZscore,
createZremRangeByScore,
createZscore
} from "./Commands";
import { redis_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -828,6 +829,25 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createZcount(key, minScore, maxScore));
}

/** Removes all elements in the sorted set stored at `key` with a score between `minScore` and `maxScore` (inclusive).
* See https://redis.io/commands/zremrangebyscore/ for more details.
*
* @param key - The key of the sorted set.
* @param minScore - The minimum score to remove from. Can be positive/negative infinity, or specific score and inclusivity.
* @param maxScore - The maximum score to remove to. Can be positive/negative infinity, or specific score and inclusivity.
*
* Command Response - the number of members removed.
* If `key` does not exist, it is treated as an empty sorted set, and the command returns 0.
* If `minScore` is greater than `maxScore`, 0 is returned.
*/
public zremRangeByScore(
key: string,
minScore: ScoreLimit,
maxScore: ScoreLimit
): T {
return this.addAndReturn(createZremRangeByScore(key, minScore, maxScore));
}

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

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zremRangeByScore 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.zremRangeByScore(
key,
{ bound: 1, isInclusive: false },
{ bound: 2 }
)
).toEqual(1);

expect(
await client.zremRangeByScore(
key,
{ bound: 1 },
"negativeInfinity"
)
).toEqual(0);

expect(
await client.zremRangeByScore(
"nonExistingKey",
"negativeInfinity",
"positiveInfinity"
)
).toEqual(0);
}, 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 @@ -135,6 +135,8 @@ export function transactionTest(
args.push(3.0);
baseTransaction.zcount(key8, { bound: 2 }, "positiveInfinity");
args.push(1);
baseTransaction.zremRangeByScore(key8, "negativeInfinity", "positiveInfinity");
args.push(1);
return args;
}

Expand Down

0 comments on commit c547a42

Please sign in to comment.