Skip to content

Commit

Permalink
Node: Add XRANGE command (valkey-io#2069)
Browse files Browse the repository at this point in the history
* Update changelog

Signed-off-by: Jonathan Louie <[email protected]>

* Implement XRANGE command

Signed-off-by: Jonathan Louie <[email protected]>

* Update CHANGELOG.md

Signed-off-by: Jonathan Louie <[email protected]>

* Apply prettier

Signed-off-by: Jonathan Louie <[email protected]>

* Fix style issue

Signed-off-by: Jonathan Louie <[email protected]>

* Fix failing test

Signed-off-by: Jonathan Louie <[email protected]>

* Fix another failing test

Signed-off-by: Jonathan Louie <[email protected]>

* Address PR comments

Signed-off-by: Jonathan Louie <[email protected]>

* Change return type for xrange

Signed-off-by: Jonathan Louie <[email protected]>

* Try to address remaining PR comments

Signed-off-by: Jonathan Louie <[email protected]>

* Switch to ScoreBoundary

Signed-off-by: Jonathan Louie <[email protected]>

* Remove merge conflict marker

Signed-off-by: Jonathan Louie <[email protected]>

* Apply prettier for SharedTests

Signed-off-by: Jonathan Louie <[email protected]>

* Replace remaining instances of StreamRangeBound with ScoreBoundary

Signed-off-by: Jonathan Louie <[email protected]>

* Update test to use string instead of number

Signed-off-by: Jonathan Louie <[email protected]>

* Change ScoreBoundary to generic Boundary and add check for Redis version to xrange tests

Signed-off-by: Jonathan Louie <[email protected]>

* Adjust documentation and fix linting and tests

Signed-off-by: Jonathan Louie <[email protected]>

* Adjust documentation

Signed-off-by: Jonathan Louie <[email protected]>

---------

Signed-off-by: Jonathan Louie <[email protected]>
  • Loading branch information
jonathanl-bq authored Aug 14, 2024
1 parent 27342ab commit edcb15a
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 163 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* Node: Added ZMPOP command ([#1994](https://github.com/valkey-io/valkey-glide/pull/1994))
* 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 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
8 changes: 4 additions & 4 deletions node/npm/glide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ function initialize() {
InsertPosition,
SetOptions,
ZaddOptions,
InfScoreBoundary,
ScoreBoundary,
InfBoundary,
Boundary,
UpdateOptions,
ProtocolVersion,
RangeByIndex,
Expand Down Expand Up @@ -220,8 +220,8 @@ function initialize() {
InsertPosition,
SetOptions,
ZaddOptions,
InfScoreBoundary,
ScoreBoundary,
InfBoundary,
Boundary,
UpdateOptions,
ProtocolVersion,
RangeByIndex,
Expand Down
74 changes: 57 additions & 17 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
BitOffsetOptions,
BitmapIndexType,
BitwiseOperation,
Boundary,
CoordOrigin, // eslint-disable-line @typescript-eslint/no-unused-vars
ExpireOptions,
GeoAddOptions,
Expand All @@ -41,7 +42,6 @@ import {
RangeByLex,
RangeByScore,
ReturnTypeXinfoStream,
ScoreBoundary,
ScoreFilter,
SearchOrigin,
SetOptions,
Expand Down Expand Up @@ -176,6 +176,7 @@ import {
createXInfoStream,
createXLen,
createXPending,
createXRange,
createXRead,
createXTrim,
createZAdd,
Expand Down Expand Up @@ -2980,6 +2981,45 @@ export class BaseClient {
return this.createWritePromise(scriptInvocation);
}

/**
* Returns stream entries matching a given range of entry IDs.
*
* See https://valkey.io/commands/xrange for more details.
*
* @param key - The key of the stream.
* @param start - The starting 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.NegativeInfinity` to start with the minimum available ID.
* @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 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.
*
* @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.xrange("mystream", InfBoundary.NegativeInfinity, InfBoundary.PositiveInfinity));
* // Output:
* // {
* // "0-1": [["field1", "value1"]],
* // "0-2": [["field2", "value2"], ["field2", "value3"]],
* // } // Indicates the stream entry IDs and their associated field-value pairs for all stream entries in "mystream".
* ```
*/
public async xrange(
key: string,
start: Boundary<string>,
end: Boundary<string>,
count?: number,
): Promise<Record<string, [string, string][]> | null> {
return this.createWritePromise(createXRange(key, start, end, 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.
* See https://valkey.io/commands/zadd/ for more details.
Expand Down Expand Up @@ -3277,7 +3317,7 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of the zcount method to count members in a sorted set within a score range
* const result = await client.zcount("my_sorted_set", { value: 5.0, isInclusive: true }, InfScoreBoundary.PositiveInfinity);
* const result = await client.zcount("my_sorted_set", { value: 5.0, isInclusive: true }, InfBoundary.PositiveInfinity);
* console.log(result); // Output: 2 - Indicates that there are 2 members with scores between 5.0 (inclusive) and +inf in the sorted set "my_sorted_set".
* ```
*
Expand All @@ -3290,8 +3330,8 @@ export class BaseClient {
*/
public zcount(
key: string,
minScore: ScoreBoundary<number>,
maxScore: ScoreBoundary<number>,
minScore: Boundary<number>,
maxScore: Boundary<number>,
): Promise<number> {
return this.createWritePromise(createZCount(key, minScore, maxScore));
}
Expand Down Expand Up @@ -3321,7 +3361,7 @@ export class BaseClient {
* ```typescript
* // Example usage of zrange method to retrieve members within a score range in ascending order
* const result = await client.zrange("my_sorted_set", {
* start: InfScoreBoundary.NegativeInfinity,
* start: InfBoundary.NegativeInfinity,
* stop: { value: 3, isInclusive: false },
* type: "byScore",
* });
Expand Down Expand Up @@ -3363,7 +3403,7 @@ export class BaseClient {
* ```typescript
* // Example usage of zrangeWithScores method to retrieve members within a score range with their scores
* const result = await client.zrangeWithScores("my_sorted_set", {
* start: InfScoreBoundary.NegativeInfinity,
* start: InfBoundary.NegativeInfinity,
* stop: { value: 3, isInclusive: false },
* type: "byScore",
* });
Expand Down Expand Up @@ -3409,7 +3449,7 @@ export class BaseClient {
* ```typescript
* // Example usage of zrangeStore method to retrieve members within a score range in ascending order and store in "destination_key"
* const result = await client.zrangeStore("destination_key", "my_sorted_set", {
* start: InfScoreBoundary.NegativeInfinity,
* start: InfBoundary.NegativeInfinity,
* stop: { value: 3, isInclusive: false },
* type: "byScore",
* });
Expand Down Expand Up @@ -3805,14 +3845,14 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of zremRangeByLex method when the sorted set does not exist
* const result = await client.zremRangeByLex("non_existing_sorted_set", InfScoreBoundary.NegativeInfinity, { value: "e" });
* const result = await client.zremRangeByLex("non_existing_sorted_set", InfBoundary.NegativeInfinity, { value: "e" });
* console.log(result); // Output: 0 - Indicates that no elements were removed.
* ```
*/
public zremRangeByLex(
key: string,
minLex: ScoreBoundary<string>,
maxLex: ScoreBoundary<string>,
minLex: Boundary<string>,
maxLex: Boundary<string>,
): Promise<number> {
return this.createWritePromise(
createZRemRangeByLex(key, minLex, maxLex),
Expand All @@ -3832,7 +3872,7 @@ export class BaseClient {
* @example
* ```typescript
* // Example usage of zremRangeByScore method to remove members from a sorted set based on score range
* const result = await client.zremRangeByScore("my_sorted_set", { value: 5.0, isInclusive: true }, InfScoreBoundary.PositiveInfinity);
* const result = await client.zremRangeByScore("my_sorted_set", { value: 5.0, isInclusive: true }, InfBoundary.PositiveInfinity);
* console.log(result); // Output: 2 - Indicates that 2 members with scores between 5.0 (inclusive) and +inf have been removed from the sorted set "my_sorted_set".
* ```
*
Expand All @@ -3845,8 +3885,8 @@ export class BaseClient {
*/
public zremRangeByScore(
key: string,
minScore: ScoreBoundary<number>,
maxScore: ScoreBoundary<number>,
minScore: Boundary<number>,
maxScore: Boundary<number>,
): Promise<number> {
return this.createWritePromise(
createZRemRangeByScore(key, minScore, maxScore),
Expand All @@ -3867,7 +3907,7 @@ export class BaseClient {
*
* @example
* ```typescript
* const result = await client.zlexcount("my_sorted_set", {value: "c"}, InfScoreBoundary.PositiveInfinity);
* const result = await client.zlexcount("my_sorted_set", {value: "c"}, InfBoundary.PositiveInfinity);
* console.log(result); // Output: 2 - Indicates that there are 2 members with lex scores between "c" (inclusive) and positive infinity in the sorted set "my_sorted_set".
* ```
*
Expand All @@ -3879,8 +3919,8 @@ export class BaseClient {
*/
public async zlexcount(
key: string,
minLex: ScoreBoundary<string>,
maxLex: ScoreBoundary<string>,
minLex: Boundary<string>,
maxLex: Boundary<string>,
): Promise<number> {
return this.createWritePromise(createZLexCount(key, minLex, maxLex));
}
Expand Down Expand Up @@ -4132,7 +4172,7 @@ export class BaseClient {
* ```typescript
* console.log(await client.xpending("my_stream", "my_group"), {
* start: { value: "0-1", isInclusive: true },
* end: InfScoreBoundary.PositiveInfinity,
* end: InfBoundary.PositiveInfinity,
* count: 2,
* consumer: "consumer1"
* }); // Output:
Expand Down
Loading

0 comments on commit edcb15a

Please sign in to comment.