Skip to content

Commit

Permalink
Node: add ZINTERSTORE command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 5, 2024
1 parent ff29207 commit 0e963c9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
13 changes: 12 additions & 1 deletion node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1896,10 +1896,21 @@ export class BaseClient {
* KeyWeight[] - for weighted keys with score multipliers.
* @param aggregationType - Specifies the aggregation strategy to apply when combining the scores of elements.
* @returns The number of elements in the resulting sorted set stored at `destination`.
*
* @example
* ```typescript
* // Example usage of zinterstore command with an existing key
* await client.zadd("key1", {"member1": 10.5, "member2": 8.2})
* await client.zadd("key2", {"member1": 9.5})
* await client.zinterstore("my_sorted_set", ["key1", "key2"]) // Output: 1 - Indicates that the sorted set "my_sorted_set" contains one element.
* await client.zrange_withscores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 20} - "member1" is now stored in "my_sorted_set" with score of 20.
* await client.zinterstore("my_sorted_set", ["key1", "key2"] , AggregationType.MAX ) // Output: 1 - Indicates that the sorted set "my_sorted_set" contains one element, and it's score is the maximum score between the sets.
* await client.zrange_withscores("my_sorted_set", RangeByIndex(0, -1)) // Output: {'member1': 10.5} - "member1" is now stored in "my_sorted_set" with score of 10.5.
* ```
*/
public zinterstore(
destination: string,
keys: (string | KeyWeight)[],
keys: string[] | KeyWeight[],
aggregationType?: AggregationType,
): Promise<number> {
return this.createWritePromise(
Expand Down
18 changes: 13 additions & 5 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,24 +827,30 @@ export function createZAdd(
return createCommand(RequestType.ZAdd, args);
}

/**
* `KeyWeight` - pair of variables represents a weighted key for the `ZINTERSTORE` and `ZUNIONSTORE` sorted sets commands.
*/
export type KeyWeight = [string, number];
/**
* `AggregationType` - representing aggregation types for `ZINTERSTORE` and `ZUNIONSTORE` sorted set commands.
*/
export type AggregationType = "SUM" | "MIN" | "MAX";

/**
* @internal
*/
export function createZInterstore(
destination: string,
keys: (string | KeyWeight)[],
keys: string[] | KeyWeight[],
aggregationType?: AggregationType,
): redis_request.Command {
const args = createZInterstoreArgs(destination, keys, aggregationType);
const args = createZCmdStoreArgs(destination, keys, aggregationType);
return createCommand(RequestType.ZInterStore, args);
}

function createZInterstoreArgs(
function createZCmdStoreArgs(
destination: string,
keys: (string | KeyWeight)[],
keys: string[] | KeyWeight[],
aggregationType?: AggregationType,
): string[] {
const args: string[] = [destination, keys.length.toString()];
Expand All @@ -853,9 +859,10 @@ function createZInterstoreArgs(
typeof key === "string" ? [key, 1] : key,
);

for (const [key, weight] of keyWeightPairs) {
for (const [key] of keyWeightPairs) {
args.push(key.toString());
}

const weights = keyWeightPairs.map(([, weight]) => weight.toString());

if (weights.some((weight) => weight !== "1")) {
Expand All @@ -868,6 +875,7 @@ function createZInterstoreArgs(

return args;
}

/**
* @internal
*/
Expand Down
2 changes: 1 addition & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*/
public zinterstore(
destination: string,
keys: (string | KeyWeight)[],
keys: string[] | KeyWeight[],
aggregationType?: AggregationType,
): T {
return this.addAndReturn(
Expand Down
1 change: 1 addition & 0 deletions node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ describe("RedisClusterClient", () => {
client.smove("abc", "zxy", "value"),
client.renamenx("abc", "zxy"),
client.sinter(["abc", "zxy", "lkn"]),
client.zinterstore("abc", ["zxy", "lkn"]),
// TODO all rest multi-key commands except ones tested below
];

Expand Down

0 comments on commit 0e963c9

Please sign in to comment.