Skip to content

Commit

Permalink
Merge branch 'main' into node/integ_acongo_linsert
Browse files Browse the repository at this point in the history
  • Loading branch information
acarbonetto authored Jun 10, 2024
2 parents 5f369ee + 80614d1 commit 525eb38
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
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))
* Node: Added LINSERT command ([#1544](https://github.com/aws/glide-for-redis/pull/1544))

### Breaking Changes
Expand Down
20 changes: 20 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
createPTTL,
createPersist,
createPfAdd,
createPfCount,
createRPop,
createRPush,
createRename,
Expand Down Expand Up @@ -2476,6 +2477,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 @@ -1449,6 +1449,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 @@ -65,6 +65,7 @@ import {
createPTTL,
createPersist,
createPfAdd,
createPfCount,
createPing,
createRPop,
createRPush,
Expand Down Expand Up @@ -1423,6 +1424,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 @@ -2733,6 +2733,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);

// 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 @@ -416,5 +416,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;
}

0 comments on commit 525eb38

Please sign in to comment.