Skip to content

Commit

Permalink
Node: added zinterstore command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 4, 2024
1 parent e23886a commit 7611d05
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Node: Added ZINTERSTORE command ([#1513](https://github.com/aws/glide-for-redis/pull/1513))
* Node: Added HKEYS command ([#1499](https://github.com/aws/glide-for-redis/pull/1499))
* Node: Added SMOVE command ([#1476](https://github.com/aws/glide-for-redis/pull/1476))
* Python: Added OBJECT ENCODING command ([#1471](https://github.com/aws/glide-for-redis/pull/1471))
Expand Down
3 changes: 2 additions & 1 deletion node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import * as net from "net";
import { Buffer, BufferWriter, Reader, Writer } from "protobufjs";
import {
AggregationType,
ExpireOptions,
KeyWeight,
RangeByIndex,
Expand Down Expand Up @@ -1918,7 +1919,7 @@ export class BaseClient {
public zinterstore(
destination: string,
keys: (string | KeyWeight)[],
aggregationType?: "SUM" | "MIN" | "MAX",
aggregationType?: AggregationType,
): Promise<number> {
return this.createWritePromise(
createZInterstore(destination, keys, aggregationType),
Expand Down
20 changes: 10 additions & 10 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,15 @@ export function createZAdd(
}

export type KeyWeight = [string, number];
export type AggregationType = "SUM" | "MIN" | "MAX";

/**
* @internal
*/
export function createZInterstore(
destination: string,
keys: (string | KeyWeight)[],
aggregationType?: "SUM" | "MIN" | "MAX",
aggregationType?: AggregationType,
): redis_request.Command {
const args = createZInterstoreArgs(destination, keys, aggregationType);
return createCommand(RequestType.ZInterStore, args);
Expand All @@ -851,22 +852,21 @@ export function createZInterstore(
function createZInterstoreArgs(
destination: string,
keys: (string | KeyWeight)[],
aggregationType?: "SUM" | "MIN" | "MAX",
aggregationType?: AggregationType,
): string[] {
const args: string[] = [destination, `${keys.length}`];
const args: string[] = [destination, keys.length.toString()];

const keyWeightPairs = keys.map((key) =>
typeof key === "string" ? [key, 1] : key,
);

for (const [key, weight] of keyWeightPairs) {
args.push(`${key}`);
args.push(key.toString());
}
// keyWeightPairs.forEach((key, weight) => {
// args.push(`${key}`);
// });
const weights = keyWeightPairs.map(([_, weight]) => weight);
if (weights.some((weight) => weight !== 1)) {
args.push("WEIGHTS", ...(weights as unknown as string));
const weights = keyWeightPairs.map(([, weight]) => weight.toString());

if (weights.some((weight) => weight !== "1")) {
args.push("WEIGHTS", ...weights);
}

if (aggregationType) {
Expand Down
3 changes: 2 additions & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import {
AggregationType,
ExpireOptions,
InfoOptions,
KeyWeight,
Expand Down Expand Up @@ -1066,7 +1067,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
public zinterstore(
destination: string,
keys: (string | KeyWeight)[],
aggregationType?: "SUM" | "MIN" | "MAX",
aggregationType?: AggregationType,
): T {
return this.addAndReturn(
createZInterstore(destination, keys, aggregationType),
Expand Down
22 changes: 11 additions & 11 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ export function runBaseTests<Context>(config: {
stop: -1,
};
const membersScores1 = { one: 1.0, two: 2.0 };
const membersScores2 = { one: 1.5, two: 2.5, three: 3.5 };
const membersScores2 = { one: 2.0, two: 3.0, three: 4.0 };

expect(await client.zadd(key1, membersScores1)).toEqual(2);
expect(await client.zadd(key2, membersScores2)).toEqual(3);
Expand All @@ -1930,8 +1930,8 @@ export function runBaseTests<Context>(config: {
range,
);
const expectedMap = {
one: 2.5,
two: 4.5,
one: 3,
two: 5,
};
expect(compareMaps(zinterstoreMap, expectedMap)).toBe(true);

Expand All @@ -1944,8 +1944,8 @@ export function runBaseTests<Context>(config: {
range,
);
const expectedMapMax = {
one: 1.5,
two: 2.5,
one: 2,
two: 3,
};
expect(compareMaps(zinterstoreMapMax, expectedMapMax)).toBe(
true,
Expand All @@ -1960,8 +1960,8 @@ export function runBaseTests<Context>(config: {
range,
);
const expectedMapMin = {
one: 1.0,
two: 2.0,
one: 1,
two: 2,
};
expect(compareMaps(zinterstoreMapMin, expectedMapMin)).toBe(
true,
Expand All @@ -1976,8 +1976,8 @@ export function runBaseTests<Context>(config: {
range,
);
const expectedMapSum = {
one: 2.5,
two: 4.5,
one: 3,
two: 5,
};
expect(compareMaps(zinterstoreMapSum, expectedMapSum)).toBe(
true,
Expand All @@ -1999,8 +1999,8 @@ export function runBaseTests<Context>(config: {
range,
);
const expectedMapMultiplied = {
one: 5.0,
two: 9.0,
one: 6,
two: 10,
};
expect(
compareMaps(
Expand Down

0 comments on commit 7611d05

Please sign in to comment.