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: added PFCOUNT command #1545

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -18,6 +18,7 @@
* Node: Added OBJECT ENCODING command ([#1518](https://github.com/aws/glide-for-redis/pull/1518))
* Python: Added LMOVE and BLMOVE commands ([#1536](https://github.com/aws/glide-for-redis/pull/1536))
* Node: Added SUNIONSTORE command ([#1549](https://github.com/aws/glide-for-redis/pull/1549))
* Node: Added PFCOUNT command ([#1545](https://github.com/aws/glide-for-redis/pull/1545))

### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/aws/glide-for-redis/pull/1494))
Expand Down
20 changes: 20 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
createPTTL,
createPersist,
createPfAdd,
createPfCount,
createRPop,
createRPush,
createRename,
Expand Down Expand Up @@ -2443,6 +2444,25 @@ export class BaseClient {
return this.createWritePromise(createPfAdd(key, elements));
}

/** Estimates the cardinality of the data stored in a HyperLogLog structure for a single key or
* calculates the combined cardinality of multiple keys by merging their HyperLogLogs temporarily.
*
* See https://valkey.io/commands/pfcount/ for more details.
*
* @remarks When in cluster mode, all `keys` must map to the same hash slot.
* @param keys - The keys of the HyperLogLog data structures to be analyzed.
* @returns - The approximated cardinality of given HyperLogLog data structures.
* The cardinality of a key that does not exist is `0`.
* @example
* ```typescript
* const result = await client.pfcount(["hll_1", "hll_2"]);
* console.log(result); // Output: 4 - The approximated cardinality of the union of "hll_1" and "hll_2"
* ```
*/
public pfcount(keys: string[]): Promise<number> {
return this.createWritePromise(createPfCount(keys));
}

/** Returns the internal encoding for the Redis object stored at `key`.
*
* See https://valkey.io/commands/object-encoding for more details.
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,13 @@ export function createPfAdd(
return createCommand(RequestType.PfAdd, args);
}

/**
* @internal
*/
export function createPfCount(keys: string[]): redis_request.Command {
return createCommand(RequestType.PfCount, keys);
}

/**
* @internal
*/
Expand Down
14 changes: 14 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
createPTTL,
createPersist,
createPfAdd,
createPfCount,
createPing,
createRPop,
createRPush,
Expand Down Expand Up @@ -1397,6 +1398,19 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createPfAdd(key, elements));
}

/** Estimates the cardinality of the data stored in a HyperLogLog structure for a single key or
* calculates the combined cardinality of multiple keys by merging their HyperLogLogs temporarily.
*
* See https://valkey.io/commands/pfcount/ for more details.
*
* @param keys - The keys of the HyperLogLog data structures to be analyzed.
* Command Response - The approximated cardinality of given HyperLogLog data structures.
* The cardinality of a key that does not exist is `0`.
*/
public pfcount(keys: string[]): T {
return this.addAndReturn(createPfCount(keys));
}

/** Returns the internal encoding for the Redis object stored at `key`.
*
* See https://valkey.io/commands/object-encoding for more details.
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 @@ -290,6 +290,7 @@ describe("RedisClusterClient", () => {
client.sinter(["abc", "zxy", "lkn"]),
client.zinterstore("abc", ["zxy", "lkn"]),
client.sunionstore("abc", ["zxy", "lkn"]),
client.pfcount(["abc", "zxy", "lkn"]),
// TODO all rest multi-key commands except ones tested below
];

Expand Down
40 changes: 40 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,46 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"pfcount test_%p",
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = `{key}-1-${uuidv4()}`;
const key2 = `{key}-2-${uuidv4()}`;
const key3 = `{key}-3-${uuidv4()}`;
const stringKey = `{key}-4-${uuidv4()}`;
const nonExistingKey = `{key}-5-${uuidv4()}`;

expect(await client.pfadd(key1, ["a", "b", "c"])).toEqual(1);
expect(await client.pfadd(key2, ["b", "c", "d"])).toEqual(1);
expect(await client.pfcount([key1])).toEqual(3);
expect(await client.pfcount([key2])).toEqual(3);
expect(await client.pfcount([key1, key2])).toEqual(4);
expect(
await client.pfcount([key1, key2, nonExistingKey]),
).toEqual(4);

// empty HyperLogLog data set
expect(await client.pfadd(key3, [])).toEqual(1);
expect(await client.pfcount([key3])).toEqual(0);

yipin-chen marked this conversation as resolved.
Show resolved Hide resolved
// invalid argument - key list must not be empty
try {
expect(await client.pfcount([])).toThrow();
} catch (e) {
expect((e as Error).message).toMatch(
"ResponseError: wrong number of arguments",
);
}

// key exists, but it is not a HyperLogLog
expect(await client.set(stringKey, "value")).toEqual("OK");
await expect(client.pfcount([stringKey])).rejects.toThrow();
}, protocol);
},
config.timeout,
);

// Set command tests

async function setWithExpiryOptions(client: BaseClient) {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,7 @@ export async function transactionTest(
args.push([key6, field + "1"]);
baseTransaction.pfadd(key11, ["a", "b", "c"]);
args.push(1);
baseTransaction.pfcount([key11]);
args.push(3);
return args;
}
Loading