Skip to content

Commit

Permalink
Node: add binary support to string commands (valkey-io#2183)
Browse files Browse the repository at this point in the history
* Node: add binary support to string commands

Signed-off-by: TJ Zhang <[email protected]>
  • Loading branch information
tjzhang-BQ authored Aug 28, 2024
1 parent e48133f commit 015d5a9
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
* Node: Added ZINTER and ZUNION commands ([#2146](https://github.com/aws/glide-for-redis/pull/2146))
* Node: Added XACK commands ([#2112](https://github.com/valkey-io/valkey-glide/pull/2112))
* Node: Added XGROUP SETID command ([#2135]((https://github.com/valkey-io/valkey-glide/pull/2135))
* Node: Added binary variant to string commands ([#2183](https://github.com/valkey-io/valkey-glide/pull/2183))

#### Breaking Changes
* Node: (Refactor) Convert classes to types ([#2005](https://github.com/valkey-io/valkey-glide/pull/2005))
Expand Down
83 changes: 56 additions & 27 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,11 @@ export class BaseClient {
* @remarks Since Valkey version 6.2.0.
*
* @param key - The key to retrieve from the database.
* @param options - (Optional) Set expiriation to the given key.
* "persist" will retain the time to live associated with the key. Equivalent to `PERSIST` in the VALKEY API.
* Otherwise, a {@link TimeUnit} and duration of the expire time should be specified.
* @param options - (Optional) Additional Parameters:
* - (Optional) `expiry`: expiriation to the given key:
* `"persist"` will retain the time to live associated with the key. Equivalent to `PERSIST` in the VALKEY API.
* Otherwise, a {@link TimeUnit} and duration of the expire time should be specified.
* - (Optional) `decoder`: see {@link DecoderOption}.
* @returns If `key` exists, returns the value of `key` as a `string`. Otherwise, return `null`.
*
* @example
Expand All @@ -999,10 +1001,14 @@ export class BaseClient {
* ```
*/
public async getex(
key: string,
options?: "persist" | { type: TimeUnit; duration: number },
): Promise<string | null> {
return this.createWritePromise(createGetEx(key, options));
key: GlideString,
options?: {
expiry: "persist" | { type: TimeUnit; duration: number };
} & DecoderOption,
): Promise<GlideString | null> {
return this.createWritePromise(createGetEx(key, options?.expiry), {
decoder: options?.decoder,
});
}

/**
Expand Down Expand Up @@ -1076,7 +1082,7 @@ export class BaseClient {
*
* @param key - The key to store.
* @param value - The value to store with the given key.
* @param options - The set options.
* @param options - (Optional) See {@link SetOptions} and {@link DecoderOption}.
* @returns - If the value is successfully set, return OK.
* If value isn't set because of `onlyIfExists` or `onlyIfDoesNotExist` conditions, return null.
* If `returnOldValue` is set, return the old value as a string.
Expand All @@ -1103,9 +1109,11 @@ export class BaseClient {
public async set(
key: GlideString,
value: GlideString,
options?: SetOptions,
): Promise<"OK" | string | null> {
return this.createWritePromise(createSet(key, value, options));
options?: SetOptions & DecoderOption,
): Promise<"OK" | GlideString | null> {
return this.createWritePromise(createSet(key, value, options), {
decoder: options?.decoder,
});
}

/**
Expand Down Expand Up @@ -1294,7 +1302,7 @@ export class BaseClient {
* console.log(result); // Output: 11
* ```
*/
public async incr(key: string): Promise<number> {
public async incr(key: GlideString): Promise<number> {
return this.createWritePromise(createIncr(key));
}

Expand All @@ -1314,7 +1322,7 @@ export class BaseClient {
* console.log(result); // Output: 15
* ```
*/
public async incrBy(key: string, amount: number): Promise<number> {
public async incrBy(key: GlideString, amount: number): Promise<number> {
return this.createWritePromise(createIncrBy(key, amount));
}

Expand All @@ -1336,7 +1344,10 @@ export class BaseClient {
* console.log(result); // Output: 13.0
* ```
*/
public async incrByFloat(key: string, amount: number): Promise<number> {
public async incrByFloat(
key: GlideString,
amount: number,
): Promise<number> {
return this.createWritePromise(createIncrByFloat(key, amount));
}

Expand All @@ -1355,7 +1366,7 @@ export class BaseClient {
* console.log(result); // Output: 9
* ```
*/
public async decr(key: string): Promise<number> {
public async decr(key: GlideString): Promise<number> {
return this.createWritePromise(createDecr(key));
}

Expand All @@ -1375,7 +1386,7 @@ export class BaseClient {
* console.log(result); // Output: 5
* ```
*/
public async decrBy(key: string, amount: number): Promise<number> {
public async decrBy(key: GlideString, amount: number): Promise<number> {
return this.createWritePromise(createDecrBy(key, amount));
}

Expand Down Expand Up @@ -6319,6 +6330,7 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param options - (Optional) See {@link DecoderOption}.
* @returns A `String` containing all the longest common subsequence combined between the 2 strings.
* An empty `String` is returned if the keys do not exist or have no common subsequences.
*
Expand All @@ -6329,8 +6341,12 @@ export class BaseClient {
* console.log(result); // Output: 'acd'
* ```
*/
public async lcs(key1: string, key2: string): Promise<string> {
return this.createWritePromise(createLCS(key1, key2));
public async lcs(
key1: GlideString,
key2: GlideString,
options?: DecoderOption,
): Promise<string> {
return this.createWritePromise(createLCS(key1, key2), options);
}

/**
Expand All @@ -6342,6 +6358,7 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param options - (Optional) See {@link DecoderOption}.
* @returns The total length of all the longest common subsequences between the 2 strings.
*
* @example
Expand All @@ -6351,8 +6368,15 @@ export class BaseClient {
* console.log(result); // Output: 3
* ```
*/
public async lcsLen(key1: string, key2: string): Promise<number> {
return this.createWritePromise(createLCS(key1, key2, { len: true }));
public async lcsLen(
key1: GlideString,
key2: GlideString,
options?: DecoderOption,
): Promise<number> {
return this.createWritePromise(
createLCS(key1, key2, { len: true }),
options,
);
}

/**
Expand All @@ -6365,8 +6389,9 @@ export class BaseClient {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param withMatchLen - (Optional) If `true`, include the length of the substring matched for the each match.
* @param minMatchLen - (Optional) The minimum length of matches to include in the result.
* @param options - (Optional) Additional parameters:
* - (Optional) `withMatchLen`: if `true`, include the length of the substring matched for the each match.
* - (Optional) `minMatchLen`: the minimum length of matches to include in the result.
* @returns A `Record` containing the indices of the longest common subsequences between the
* 2 strings and the lengths of the longest common subsequences. The resulting map contains two
* keys, "matches" and "len":
Expand Down Expand Up @@ -6402,12 +6427,16 @@ export class BaseClient {
* ```
*/
public async lcsIdx(
key1: string,
key2: string,
options?: { withMatchLen?: boolean; minMatchLen?: number },
key1: GlideString,
key2: GlideString,
options?: {
withMatchLen?: boolean;
minMatchLen?: number;
},
): Promise<Record<string, (number | [number, number])[][] | number>> {
return this.createWritePromise(
createLCS(key1, key2, { idx: options ?? {} }),
{ decoder: Decoder.String },
);
}

Expand Down Expand Up @@ -6509,9 +6538,9 @@ export class BaseClient {
* ```
*/
public async setrange(
key: string,
key: GlideString,
offset: number,
value: string,
value: GlideString,
): Promise<number> {
return this.createWritePromise(createSetRange(key, offset, value));
}
Expand Down
20 changes: 10 additions & 10 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,15 @@ export function createMSetNX(
/**
* @internal
*/
export function createIncr(key: string): command_request.Command {
export function createIncr(key: GlideString): command_request.Command {
return createCommand(RequestType.Incr, [key]);
}

/**
* @internal
*/
export function createIncrBy(
key: string,
key: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.IncrBy, [key, amount.toString()]);
Expand All @@ -366,7 +366,7 @@ export function createIncrBy(
* @internal
*/
export function createIncrByFloat(
key: string,
key: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.IncrByFloat, [key, amount.toString()]);
Expand Down Expand Up @@ -442,15 +442,15 @@ export function createHSetNX(
/**
* @internal
*/
export function createDecr(key: string): command_request.Command {
export function createDecr(key: GlideString): command_request.Command {
return createCommand(RequestType.Decr, [key]);
}

/**
* @internal
*/
export function createDecrBy(
key: string,
key: GlideString,
amount: number,
): command_request.Command {
return createCommand(RequestType.DecrBy, [key, amount.toString()]);
Expand Down Expand Up @@ -3681,8 +3681,8 @@ export function createLastSave(): command_request.Command {

/** @internal */
export function createLCS(
key1: string,
key2: string,
key1: GlideString,
key2: GlideString,
options?: {
len?: boolean;
idx?: { withMatchLen?: boolean; minMatchLen?: number };
Expand Down Expand Up @@ -3794,9 +3794,9 @@ export function createZScan(

/** @internal */
export function createSetRange(
key: string,
key: GlideString,
offset: number,
value: string,
value: GlideString,
): command_request.Command {
return createCommand(RequestType.SetRange, [key, offset.toString(), value]);
}
Expand Down Expand Up @@ -3944,7 +3944,7 @@ export enum TimeUnit {
* @internal
*/
export function createGetEx(
key: string,
key: GlideString,
options?: "persist" | { type: TimeUnit; duration: number },
): command_request.Command {
const args = [key];
Expand Down
29 changes: 15 additions & 14 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* Command Response - If `key` exists, returns the value of `key` as a `string`. Otherwise, return `null`.
*/
public getex(
key: string,
key: GlideString,
options?: "persist" | { type: TimeUnit; duration: number },
): T {
return this.addAndReturn(createGetEx(key, options));
Expand Down Expand Up @@ -386,7 +386,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* If `value` isn't set because of `onlyIfExists` or `onlyIfDoesNotExist` conditions, return null.
* If `returnOldValue` is set, return the old value as a string.
*/
public set(key: string, value: string, options?: SetOptions): T {
public set(key: GlideString, value: GlideString, options?: SetOptions): T {
return this.addAndReturn(createSet(key, value, options));
}

Expand Down Expand Up @@ -545,7 +545,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* Command Response - the value of `key` after the increment.
*/
public incr(key: string): T {
public incr(key: GlideString): T {
return this.addAndReturn(createIncr(key));
}

Expand All @@ -557,7 +557,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* Command Response - the value of `key` after the increment.
*/
public incrBy(key: string, amount: number): T {
public incrBy(key: GlideString, amount: number): T {
return this.addAndReturn(createIncrBy(key, amount));
}

Expand All @@ -572,7 +572,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* Command Response - the value of `key` after the increment.
*
*/
public incrByFloat(key: string, amount: number): T {
public incrByFloat(key: GlideString, amount: number): T {
return this.addAndReturn(createIncrByFloat(key, amount));
}

Expand All @@ -594,7 +594,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* Command Response - the value of `key` after the decrement.
*/
public decr(key: string): T {
public decr(key: GlideString): T {
return this.addAndReturn(createDecr(key));
}

Expand All @@ -606,7 +606,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* Command Response - the value of `key` after the decrement.
*/
public decrBy(key: string, amount: number): T {
public decrBy(key: GlideString, amount: number): T {
return this.addAndReturn(createDecrBy(key, amount));
}

Expand Down Expand Up @@ -3679,7 +3679,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* Command Response - A `String` containing all the longest common subsequence combined between the 2 strings.
* An empty `String` is returned if the keys do not exist or have no common subsequences.
*/
public lcs(key1: string, key2: string): T {
public lcs(key1: GlideString, key2: GlideString): T {
return this.addAndReturn(createLCS(key1, key2));
}

Expand All @@ -3694,7 +3694,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* Command Response - The total length of all the longest common subsequences between the 2 strings.
*/
public lcsLen(key1: string, key2: string): T {
public lcsLen(key1: GlideString, key2: GlideString): T {
return this.addAndReturn(createLCS(key1, key2, { len: true }));
}

Expand All @@ -3707,8 +3707,9 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* @param key1 - The key that stores the first string.
* @param key2 - The key that stores the second string.
* @param withMatchLen - (Optional) If `true`, include the length of the substring matched for the each match.
* @param minMatchLen - (Optional) The minimum length of matches to include in the result.
* @param options - (Optional) Additional parameters:
* - (Optional) `withMatchLen`: if `true`, include the length of the substring matched for the each match.
* - (Optional) `minMatchLen`: the minimum length of matches to include in the result.
*
* Command Response - A `Record` containing the indices of the longest common subsequences between the
* 2 strings and the lengths of the longest common subsequences. The resulting map contains two
Expand All @@ -3722,8 +3723,8 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* See example of {@link BaseClient.lcsIdx|lcsIdx} for more details.
*/
public lcsIdx(
key1: string,
key2: string,
key1: GlideString,
key2: GlideString,
options?: { withMatchLen?: boolean; minMatchLen?: number },
): T {
return this.addAndReturn(createLCS(key1, key2, { idx: options ?? {} }));
Expand Down Expand Up @@ -3766,7 +3767,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* Command Response - The length of the string stored at `key` after it was modified.
*/
public setrange(key: string, offset: number, value: string): T {
public setrange(key: GlideString, offset: number, value: GlideString): T {
return this.addAndReturn(createSetRange(key, offset, value));
}

Expand Down
Loading

0 comments on commit 015d5a9

Please sign in to comment.