Skip to content

Commit

Permalink
Node: add binary variant to hyperloglog commands (#2176)
Browse files Browse the repository at this point in the history
* Node: add binary variant to hyperloglog commands

---------

Signed-off-by: Yi-Pin Chen <[email protected]>
  • Loading branch information
yipin-chen authored Aug 22, 2024
1 parent 99f9e88 commit 3307cd7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 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 binary variant to HYPERLOGLOG commands ([#2176](https://github.com/valkey-io/valkey-glide/pull/2176))
* Node: Added FUNCTION DUMP and FUNCTION RESTORE commands (transaction) ([#2173](https://github.com/valkey-io/valkey-glide/pull/2173))
* Node: Added FUNCTION DUMP and FUNCTION RESTORE commands ([#2129](https://github.com/valkey-io/valkey-glide/pull/2129), [#2173](https://github.com/valkey-io/valkey-glide/pull/2173))
* Node: Added ZUNIONSTORE command ([#2145](https://github.com/valkey-io/valkey-glide/pull/2145))
Expand Down
15 changes: 10 additions & 5 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5493,7 +5493,10 @@ export class BaseClient {
* console.log(result); // Output: 1 - Indicates that a new empty data structure was created
* ```
*/
public async pfadd(key: string, elements: string[]): Promise<number> {
public async pfadd(
key: GlideString,
elements: GlideString[],
): Promise<number> {
return this.createWritePromise(createPfAdd(key, elements));
}

Expand All @@ -5512,7 +5515,7 @@ export class BaseClient {
* console.log(result); // Output: 4 - The approximated cardinality of the union of "hll_1" and "hll_2"
* ```
*/
public async pfcount(keys: string[]): Promise<number> {
public async pfcount(keys: GlideString[]): Promise<number> {
return this.createWritePromise(createPfCount(keys));
}

Expand All @@ -5538,10 +5541,12 @@ export class BaseClient {
* ```
*/
public async pfmerge(
destination: string,
sourceKeys: string[],
destination: GlideString,
sourceKeys: GlideString[],
): Promise<"OK"> {
return this.createWritePromise(createPfMerge(destination, sourceKeys));
return this.createWritePromise(createPfMerge(destination, sourceKeys), {
decoder: Decoder.String,
});
}

/** Returns the internal encoding for the Valkey object stored at `key`.
Expand Down
10 changes: 5 additions & 5 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2826,8 +2826,8 @@ export function createRenameNX(
* @internal
*/
export function createPfAdd(
key: string,
elements: string[],
key: GlideString,
elements: GlideString[],
): command_request.Command {
const args = [key, ...elements];
return createCommand(RequestType.PfAdd, args);
Expand All @@ -2836,16 +2836,16 @@ export function createPfAdd(
/**
* @internal
*/
export function createPfCount(keys: string[]): command_request.Command {
export function createPfCount(keys: GlideString[]): command_request.Command {
return createCommand(RequestType.PfCount, keys);
}

/**
* @internal
*/
export function createPfMerge(
destination: string,
sourceKey: string[],
destination: GlideString,
sourceKey: GlideString[],
): command_request.Command {
return createCommand(RequestType.PfMerge, [destination, ...sourceKey]);
}
Expand Down
8 changes: 4 additions & 4 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ import {
createXGroupCreateConsumer,
createXGroupDelConsumer,
createXGroupDestroy,
createXGroupSetid,
createXInfoConsumers,
createXInfoGroups,
createXInfoStream,
Expand Down Expand Up @@ -259,7 +260,6 @@ import {
createZScore,
createZUnion,
createZUnionStore,
createXGroupSetid,
} from "./Commands";
import { command_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -3017,7 +3017,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* Command Response - If the HyperLogLog is newly created, or if the HyperLogLog approximated cardinality is
* altered, then returns `1`. Otherwise, returns `0`.
*/
public pfadd(key: string, elements: string[]): T {
public pfadd(key: GlideString, elements: GlideString[]): T {
return this.addAndReturn(createPfAdd(key, elements));
}

Expand All @@ -3030,7 +3030,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* 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 {
public pfcount(keys: GlideString[]): T {
return this.addAndReturn(createPfCount(keys));
}

Expand All @@ -3044,7 +3044,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* @param sourceKeys - The keys of the HyperLogLog structures to be merged.
* Command Response - A simple "OK" response.
*/
public pfmerge(destination: string, sourceKeys: string[]): T {
public pfmerge(destination: GlideString, sourceKeys: GlideString[]): T {
return this.addAndReturn(createPfMerge(destination, sourceKeys));
}

Expand Down
14 changes: 10 additions & 4 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6976,7 +6976,9 @@ export function runBaseTests(config: {
const key = uuidv4();
expect(await client.pfadd(key, [])).toEqual(1);
expect(await client.pfadd(key, ["one", "two"])).toEqual(1);
expect(await client.pfadd(key, ["two"])).toEqual(0);
expect(
await client.pfadd(Buffer.from(key), [Buffer.from("two")]),
).toEqual(0);
expect(await client.pfadd(key, [])).toEqual(0);

// key exists, but it is not a HyperLogLog
Expand All @@ -7000,7 +7002,7 @@ export function runBaseTests(config: {
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([Buffer.from(key2)])).toEqual(3);
expect(await client.pfcount([key1, key2])).toEqual(4);
expect(
await client.pfcount([key1, key2, nonExistingKey]),
Expand Down Expand Up @@ -7041,11 +7043,15 @@ export function runBaseTests(config: {
expect(await client.pfadd(key2, ["b", "c", "d"])).toEqual(1);

// merge into new HyperLogLog data set
expect(await client.pfmerge(key3, [key1, key2])).toEqual("OK");
expect(
await client.pfmerge(Buffer.from(key3), [key1, key2]),
).toEqual("OK");
expect(await client.pfcount([key3])).toEqual(4);

// merge into existing HyperLogLog data set
expect(await client.pfmerge(key1, [key2])).toEqual("OK");
expect(await client.pfmerge(key1, [Buffer.from(key2)])).toEqual(
"OK",
);
expect(await client.pfcount([key1])).toEqual(4);

// non-existing source key
Expand Down

0 comments on commit 3307cd7

Please sign in to comment.