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 Mar 6, 2024
1 parent 712c4ac commit c5bffdf
Show file tree
Hide file tree
Showing 7 changed files with 121 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 @@ -127,6 +127,7 @@ enum RequestType {
SIsMember = 83;
Hvals = 84;
PTTL = 85;
ZRemRangeByScore = 86;
}

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 @@ -364,6 +364,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::SIsMember => Some(cmd("SISMEMBER")),
RequestType::Hvals => Some(cmd("HVALS")),
RequestType::PTTL => Some(cmd("PTTL")),
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 @@ -65,6 +65,7 @@ import {
createZpopmax,
createZpopmin,
createZrem,
createZremRangeByScore,
createZscore,
} from "./Commands";
import {
Expand Down Expand Up @@ -1171,6 +1172,24 @@ export class BaseClient {
return this.createWritePromise(createPttl(key));
}

/** 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 @@ -884,3 +884,40 @@ export function createEcho(message: string): redis_request.Command {
export function createPttl(key: string): redis_request.Command {
return createCommand(RequestType.PTTL, [key]);
}

/**
* @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);
}
20 changes: 20 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
createZpopmax,
createZpopmin,
createZrem,
createZremRangeByScore,
createZscore,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";
Expand Down Expand Up @@ -936,6 +937,25 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createPttl(key));
}

/** 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 @@ -1680,6 +1680,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
12 changes: 7 additions & 5 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,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.zremRangeByScore(key8, "negativeInfinity", "positiveInfinity");
args.push(1);
return args;
}

Expand Down

0 comments on commit c5bffdf

Please sign in to comment.