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: add binary variant to hyperloglog commands #2176

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -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 ([#2129](https://github.com/valkey-io/valkey-glide/pull/2129))
* Node: Added ZUNIONSTORE command ([#2145](https://github.com/valkey-io/valkey-glide/pull/2145))
* Node: Added XREADGROUP command ([#2124](https://github.com/valkey-io/valkey-glide/pull/2124))
Expand Down
25 changes: 17 additions & 8 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ import {
createXGroupCreateConsumer,
createXGroupDelConsumer,
createXGroupDestroy,
createXGroupSetid,
createXInfoConsumers,
createXInfoGroups,
createXInfoStream,
Expand Down Expand Up @@ -220,7 +221,6 @@ import {
createZScore,
createZUnion,
createZUnionStore,
createXGroupSetid,
} from "./Commands";
import {
ClosingError,
Expand Down Expand Up @@ -5448,8 +5448,13 @@ 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> {
return this.createWritePromise(createPfAdd(key, elements));
public async pfadd(
key: GlideString,
elements: GlideString[],
): Promise<number> {
return this.createWritePromise(createPfAdd(key, elements), {
decoder: Decoder.String,
});
}

/** Estimates the cardinality of the data stored in a HyperLogLog structure for a single key or
Expand All @@ -5467,8 +5472,10 @@ 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> {
return this.createWritePromise(createPfCount(keys));
public async pfcount(keys: GlideString[]): Promise<number> {
return this.createWritePromise(createPfCount(keys), {
decoder: Decoder.String,
});
}

/**
Expand All @@ -5493,10 +5500,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 @@ -2824,8 +2824,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 @@ -2834,16 +2834,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 @@ -216,6 +216,7 @@ import {
createXGroupCreateConsumer,
createXGroupDelConsumer,
createXGroupDestroy,
createXGroupSetid,
createXInfoConsumers,
createXInfoGroups,
createXInfoStream,
Expand Down Expand Up @@ -256,7 +257,6 @@ import {
createZScore,
createZUnion,
createZUnionStore,
createXGroupSetid,
} from "./Commands";
import { command_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -3014,7 +3014,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 @@ -3027,7 +3027,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 @@ -3041,7 +3041,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 @@ -6850,7 +6850,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 @@ -6874,7 +6876,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 @@ -6915,11 +6917,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
Loading