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

Node: Add XREVRANGE command #2148

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* Node: Added ZINCRBY command ([#2009](https://github.com/valkey-io/valkey-glide/pull/2009))
* Node: Added BZMPOP command ([#2018](https://github.com/valkey-io/valkey-glide/pull/2018))
* Node: Added XRANGE command ([#2069](https://github.com/valkey-io/valkey-glide/pull/2069))
* Node: Added XREVRANGE command ([#2148](https://github.com/valkey-io/valkey-glide/pull/2148))
* Node: Added PFMERGE command ([#2053](https://github.com/valkey-io/valkey-glide/pull/2053))
* Node: Added WATCH and UNWATCH commands ([#2076](https://github.com/valkey-io/valkey-glide/pull/2076))
* Node: Added WAIT command ([#2113](https://github.com/valkey-io/valkey-glide/pull/2113))
Expand Down
43 changes: 42 additions & 1 deletion node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ import {
createXPending,
createXRange,
createXRead,
createXRevRange,
createXTrim,
createZAdd,
createZCard,
Expand Down Expand Up @@ -3081,7 +3082,7 @@ export class BaseClient {
* - Use `InfBoundary.PositiveInfinity` to end with the maximum available ID.
* @param count - An optional argument specifying the maximum count of stream entries to return.
* If `count` is not provided, all stream entries in the range will be returned.
* @returns A map of stream entry ids, to an array of entries, or `null` if `count` is negative.
* @returns A map of stream entry ids, to an array of entries, or `null` if `count` is non-positive.
*
* @example
* ```typescript
Expand All @@ -3104,6 +3105,46 @@ export class BaseClient {
return this.createWritePromise(createXRange(key, start, end, count));
}

/**
* Returns stream entries matching a given range of entry IDs in reverse order. Equivalent to {@link xrange} but returns the
* entries in reverse order.
*
* @see {@link https://valkey.io/commands/xrevrange/|valkey.io} for more details.
*
* @param key - The key of the stream.
* @param end - The ending stream entry ID bound for the range.
* - Use `value` to specify a stream entry ID.
* - Use `isInclusive: false` to specify an exclusive bounded stream entry ID. This is only available starting with Valkey version 6.2.0.
* - Use `InfBoundary.PositiveInfinity` to end with the maximum available ID.
* @param start - The ending stream ID bound for the range.
* - Use `value` to specify a stream entry ID.
* - Use `isInclusive: false` to specify an exclusive bounded stream entry ID. This is only available starting with Valkey version 6.2.0.
* - Use `InfBoundary.NegativeInfinity` to start with the minimum available ID.
* @param count - An optional argument specifying the maximum count of stream entries to return.
* If `count` is not provided, all stream entries in the range will be returned.
* @returns A map of stream entry ids, to an array of entries, or `null` if `count` is non-positive.
*
* @example
* ```typescript
* await client.xadd("mystream", [["field1", "value1"]], {id: "0-1"});
* await client.xadd("mystream", [["field2", "value2"], ["field2", "value3"]], {id: "0-2"});
* console.log(await client.xrevrange("mystream", InfBoundary.PositiveInfinity, InfBoundary.NegativeInfinity));
* // Output:
* // {
* // "0-2": [["field2", "value2"], ["field2", "value3"]],
* // "0-1": [["field1", "value1"]],
* // } // Indicates the stream entry IDs and their associated field-value pairs for all stream entries in "mystream".
* ```
*/
public async xrevrange(
key: string,
end: Boundary<string>,
start: Boundary<string>,
count?: number,
): Promise<Record<string, [string, string][]> | null> {
return this.createWritePromise(createXRevRange(key, end, start, count));
}

/** 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.
*
Expand Down
19 changes: 19 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,25 @@ export function createXRange(
return createCommand(RequestType.XRange, args);
}

/**
* @internal
*/
export function createXRevRange(
key: string,
start: Boundary<string>,
end: Boundary<string>,
count?: number,
): command_request.Command {
const args = [key, getStreamBoundaryArg(start), getStreamBoundaryArg(end)];

if (count !== undefined) {
args.push("COUNT");
args.push(count.toString());
}

return createCommand(RequestType.XRevRange, args);
}

/**
* @internal
*/
Expand Down
32 changes: 31 additions & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ import {
createXPending,
createXRange,
createXRead,
createXRevRange,
createXTrim,
createZAdd,
createZCard,
Expand Down Expand Up @@ -2328,7 +2329,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* @param count - An optional argument specifying the maximum count of stream entries to return.
* If `count` is not provided, all stream entries in the range will be returned.
*
* Command Response - A map of stream entry ids, to an array of entries, or `null` if `count` is negative.
* Command Response - A map of stream entry ids, to an array of entries, or `null` if `count` is non-positive.
*/
public xrange(
key: string,
Expand All @@ -2339,6 +2340,35 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createXRange(key, start, end, count));
}

/**
* Returns stream entries matching a given range of entry IDs in reverse order. Equivalent to {@link xrange} but returns the
* entries in reverse order.
*
* @see {@link https://valkey.io/commands/xrevrange/|valkey.io} for more details.
*
* @param key - The key of the stream.
* @param end - The ending stream entry ID bound for the range.
* - Use `value` to specify a stream entry ID.
* - Use `isInclusive: false` to specify an exclusive bounded stream entry ID. This is only available starting with Valkey version 6.2.0.
* - Use `InfBoundary.PositiveInfinity` to end with the maximum available ID.
* @param start - The ending stream ID bound for the range.
* - Use `value` to specify a stream entry ID.
* - Use `isInclusive: false` to specify an exclusive bounded stream entry ID. This is only available starting with Valkey version 6.2.0.
* - Use `InfBoundary.NegativeInfinity` to start with the minimum available ID.
* @param count - An optional argument specifying the maximum count of stream entries to return.
* If `count` is not provided, all stream entries in the range will be returned.
*
* Command Response - A map of stream entry ids, to an array of entries, or `null` if `count` is non-positive.
*/
public xrevrange(
key: string,
end: Boundary<string>,
start: Boundary<string>,
count?: number,
): T {
return this.addAndReturn(createXRevRange(key, end, start, count));
}

/**
* Reads entries from the given streams.
* @see {@link https://valkey.io/commands/xread/|valkey.io} for details.
Expand Down
96 changes: 88 additions & 8 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4988,7 +4988,7 @@ export function runBaseTests<Context>(config: {
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`xrange test_%p`,
`xrange and xrevrange test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient, cluster) => {
const key = uuidv4();
Expand Down Expand Up @@ -5018,6 +5018,17 @@ export function runBaseTests<Context>(config: {
[streamId2]: [["f2", "v2"]],
});

expect(
await client.xrevrange(
key,
InfBoundary.PositiveInfinity,
InfBoundary.NegativeInfinity,
),
).toEqual({
[streamId2]: [["f2", "v2"]],
[streamId1]: [["f1", "v1"]],
});

// returns empty mapping if + before -
expect(
await client.xrange(
Expand All @@ -5026,6 +5037,14 @@ export function runBaseTests<Context>(config: {
InfBoundary.NegativeInfinity,
),
).toEqual({});
// rev search returns empty mapping if - before +
expect(
await client.xrevrange(
key,
InfBoundary.NegativeInfinity,
InfBoundary.PositiveInfinity,
),
).toEqual({});

expect(
await client.xadd(key, [["f3", "v3"]], { id: streamId3 }),
Expand All @@ -5041,9 +5060,18 @@ export function runBaseTests<Context>(config: {
1,
),
).toEqual({ [streamId3]: [["f3", "v3"]] });

expect(
await client.xrevrange(
key,
{ value: "5" },
{ isInclusive: false, value: streamId2 },
1,
),
).toEqual({ [streamId3]: [["f3", "v3"]] });
}

// xrange against an emptied stream
// xrange/xrevrange against an emptied stream
expect(
await client.xdel(key, [streamId1, streamId2, streamId3]),
).toEqual(3);
Expand All @@ -5055,6 +5083,14 @@ export function runBaseTests<Context>(config: {
10,
),
).toEqual({});
expect(
await client.xrevrange(
key,
InfBoundary.PositiveInfinity,
InfBoundary.NegativeInfinity,
10,
),
).toEqual({});

expect(
await client.xrange(
Expand All @@ -5063,6 +5099,13 @@ export function runBaseTests<Context>(config: {
InfBoundary.PositiveInfinity,
),
).toEqual({});
expect(
await client.xrevrange(
nonExistingKey,
InfBoundary.PositiveInfinity,
InfBoundary.NegativeInfinity,
),
).toEqual({});

// count value < 1 returns null
expect(
Expand All @@ -5081,32 +5124,69 @@ export function runBaseTests<Context>(config: {
-1,
),
).toEqual(null);
expect(
await client.xrevrange(
key,
InfBoundary.PositiveInfinity,
InfBoundary.NegativeInfinity,
0,
),
).toEqual(null);
expect(
await client.xrevrange(
key,
InfBoundary.PositiveInfinity,
InfBoundary.NegativeInfinity,
-1,
),
).toEqual(null);

// key exists, but it is not a stream
expect(await client.set(stringKey, "foo"));
await expect(
client.xrange(
expect(
await client.xrange(
stringKey,
InfBoundary.NegativeInfinity,
InfBoundary.PositiveInfinity,
),
).rejects.toThrow(RequestError);
expect(
await client.xrevrange(
stringKey,
InfBoundary.PositiveInfinity,
InfBoundary.NegativeInfinity,
),
).rejects.toThrow(RequestError);

// invalid start bound
await expect(
client.xrange(
expect(
await client.xrange(
key,
{ value: "not_a_stream_id" },
InfBoundary.PositiveInfinity,
),
).rejects.toThrow(RequestError);
expect(
await client.xrevrange(key, InfBoundary.PositiveInfinity, {
value: "not_a_stream_id",
}),
).rejects.toThrow(RequestError);

// invalid end bound
await expect(
client.xrange(key, InfBoundary.NegativeInfinity, {
expect(
await client.xrange(key, InfBoundary.NegativeInfinity, {
value: "not_a_stream_id",
}),
).rejects.toThrow(RequestError);
expect(
await client.xrevrange(
key,
{
value: "not_a_stream_id",
},
InfBoundary.NegativeInfinity,
),
).rejects.toThrow(RequestError);
}, protocol);
},
config.timeout,
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,8 @@ export async function transactionTest(
responseData.push(["xlen(key9)", 3]);
baseTransaction.xrange(key9, { value: "0-1" }, { value: "0-1" });
responseData.push(["xrange(key9)", { "0-1": [["field", "value1"]] }]);
baseTransaction.xrevrange(key9, { value: "0-1" }, { value: "0-1" });
responseData.push(["xrevrange(key9)", { "0-1": [["field", "value1"]] }]);
baseTransaction.xread({ [key9]: "0-1" });
responseData.push([
'xread({ [key9]: "0-1" })',
Expand Down
Loading