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: implement BITPOS change #2225

Closed
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
65 changes: 16 additions & 49 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
BitOffset, // eslint-disable-line @typescript-eslint/no-unused-vars
BitOffsetMultiplier, // eslint-disable-line @typescript-eslint/no-unused-vars
BitOffsetOptions,
BitmapIndexType,
BitwiseOperation,
Boundary,
CoordOrigin, // eslint-disable-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -1510,12 +1509,18 @@ export class BaseClient {
* The offset can also be a negative number indicating an offset starting at the end of the list, with `-1` being
* the last byte of the list, `-2` being the penultimate, and so on.
*
* @see {@link https://valkey.io/commands/bitpos/|valkey.io} for more details.
* If you are using Valkey 7.0.0 or above, the optional `indexType` can also be provided to specify whether the
* `start` and `end` offsets specify BIT or BYTE offsets. If `indexType` is not provided, BYTE offsets
* are assumed. If BIT is specified, `start=0` and `end=2` means to look at the first three bits. If BYTE is
* specified, `start=0` and `end=2` means to look at the first three bytes.
*
* @see {@link https://valkey.io/commands/bitpos/|valkey.io} for details.
*
* @param key - The key of the string.
* @param bit - The bit value to match. Must be `0` or `1`.
* @param start - (Optional) The starting offset. If not supplied, the search will start at the beginning of the string.
* @returns The position of the first occurrence of `bit` in the binary value of the string held at `key`.
* @param options - (Optional) The {@link BitOffsetOptions}.
*
* @returns The position of the first occurrence of `bit` in the binary value of the string held at `key`.
* If `start` was provided, the search begins at the offset indicated by `start`.
*
* @example
Expand All @@ -1526,59 +1531,21 @@ export class BaseClient {
*
* const result2 = await client.bitpos("key1", 1, -1);
* console.log(result2); // Output: 10 - The first occurrence of bit value 1, starting at the last byte in the string stored at "key1", is at the eleventh position.
* ```
*/
public async bitpos(
key: GlideString,
bit: number,
start?: number,
): Promise<number> {
return this.createWritePromise(createBitPos(key, bit, start));
}

/**
* Returns the position of the first bit matching the given `bit` value. The offsets are zero-based indexes, with
* `0` being the first element of the list, `1` being the next, and so on. These offsets can also be negative
* numbers indicating offsets starting at the end of the list, with `-1` being the last element of the list, `-2`
* being the penultimate, and so on.
*
* If you are using Valkey 7.0.0 or above, the optional `indexType` can also be provided to specify whether the
* `start` and `end` offsets specify BIT or BYTE offsets. If `indexType` is not provided, BYTE offsets
* are assumed. If BIT is specified, `start=0` and `end=2` means to look at the first three bits. If BYTE is
* specified, `start=0` and `end=2` means to look at the first three bytes.
*
* @see {@link https://valkey.io/commands/bitpos/|valkey.io} for more details.
*
* @param key - The key of the string.
* @param bit - The bit value to match. Must be `0` or `1`.
* @param start - The starting offset.
* @param end - The ending offset.
* @param indexType - (Optional) The index offset type. This option can only be specified if you are using Valkey
* version 7.0.0 or above. Could be either {@link BitmapIndexType.BYTE} or {@link BitmapIndexType.BIT}. If no
* index type is provided, the indexes will be assumed to be byte indexes.
* @returns The position of the first occurrence from the `start` to the `end` offsets of the `bit` in the binary
* value of the string held at `key`.
*
* @example
* ```typescript
* await client.set("key1", "A12"); // "A12" has binary value 01000001 00110001 00110010
* const result1 = await client.bitposInterval("key1", 1, 1, -1);
* console.log(result1); // Output: 10 - The first occurrence of bit value 1 in the second byte to the last byte of the string stored at "key1" is at the eleventh position.
* const result3 = await client.bitpos("key1", 1, {start: 1, end: -1});
* console.log(result3); // Output: 10 - The first occurrence of bit value 1 in the second byte to the last byte of the string stored at "key1" is at the eleventh position.
*
* const result2 = await client.bitposInterval("key1", 1, 2, 9, BitmapIndexType.BIT);
* console.log(result2); // Output: 7 - The first occurrence of bit value 1 in the third to tenth bits of the string stored at "key1" is at the eighth position.
* const result4 = await client.bitpos("key1", 1, {start: 2, end: 9, indexType: BitmapIndexType.BIT});
* console.log(result4); // Output: 7 - The first occurrence of bit value 1 in the third to tenth bits of the string stored at "key1" is at the eighth position.
* ```
*/
public async bitposInterval(
public async bitpos(
key: GlideString,
bit: number,
start: number,
end: number,
indexType?: BitmapIndexType,
options?: BitOffsetOptions,
): Promise<number> {
return this.createWritePromise(
createBitPos(key, bit, start, end, indexType),
);
return this.createWritePromise(createBitPos(key, bit, options));
}

/**
Expand Down
28 changes: 10 additions & 18 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,7 @@ export function createFunctionRestore(
}

/**
* Represents offsets specifying a string interval to analyze in the {@link BaseClient.bitcount|bitcount} command. The offsets are
* Represents offsets specifying a string interval to analyze in the {@link BaseClient.bitcount|bitcount and @link BaseClient.bitpos} command. The offsets are
* zero-based indexes, with `0` being the first index of the string, `1` being the next index and so on.
* The offsets can also be negative numbers indicating offsets starting at the end of the string, with `-1` being
* the last index of the string, `-2` being the penultimate, and so on.
Expand All @@ -2425,9 +2425,9 @@ export function createFunctionRestore(
*/
export type BitOffsetOptions = {
/** The starting offset index. */
start: number;
start?: number;
/** The ending offset index. */
end: number;
end?: number;
/**
* The index offset type. This option can only be specified if you are using server version 7.0.0 or above.
* Could be either {@link BitmapIndexType.BYTE} or {@link BitmapIndexType.BIT}.
Expand All @@ -2446,8 +2446,8 @@ export function createBitCount(
const args = [key];

if (options) {
args.push(options.start.toString());
args.push(options.end.toString());
if (options.start) args.push(options.start.toString());
if (options.end) args.push(options.end.toString());
if (options.indexType) args.push(options.indexType);
}

Expand All @@ -2474,22 +2474,14 @@ export enum BitmapIndexType {
export function createBitPos(
key: GlideString,
bit: number,
start?: number,
end?: number,
indexType?: BitmapIndexType,
options?: BitOffsetOptions,
): command_request.Command {
const args: GlideString[] = [key, bit.toString()];

if (start !== undefined) {
args.push(start.toString());
}

if (end !== undefined) {
args.push(end.toString());
}

if (indexType) {
args.push(indexType);
if (options) {
if (options.start != undefined) args.push(options.start.toString());
if (options.end != undefined) args.push(options.end.toString());
if (options.indexType) args.push(options.indexType);
}

return createCommand(RequestType.BitPos, args);
Expand Down
38 changes: 6 additions & 32 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
BitOffset, // eslint-disable-line @typescript-eslint/no-unused-vars
BitOffsetMultiplier, // eslint-disable-line @typescript-eslint/no-unused-vars
BitOffsetOptions,
BitmapIndexType,
BitwiseOperation,
Boundary,
CoordOrigin, // eslint-disable-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -671,25 +670,6 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* The offset can also be a negative number indicating an offset starting at the end of the list, with `-1` being
* the last byte of the list, `-2` being the penultimate, and so on.
*
* @see {@link https://valkey.io/commands/bitpos/|valkey.io} for details.
*
* @param key - The key of the string.
* @param bit - The bit value to match. Must be `0` or `1`.
* @param start - (Optional) The starting offset. If not supplied, the search will start at the beginning of the string.
*
* Command Response - The position of the first occurrence of `bit` in the binary value of the string held at `key`.
* If `start` was provided, the search begins at the offset indicated by `start`.
*/
public bitpos(key: GlideString, bit: number, start?: number): T {
return this.addAndReturn(createBitPos(key, bit, start));
}

/**
* Returns the position of the first bit matching the given `bit` value. The offsets are zero-based indexes, with
* `0` being the first element of the list, `1` being the next, and so on. These offsets can also be negative
* numbers indicating offsets starting at the end of the list, with `-1` being the last element of the list, `-2`
* being the penultimate, and so on.
*
* If you are using Valkey 7.0.0 or above, the optional `indexType` can also be provided to specify whether the
* `start` and `end` offsets specify BIT or BYTE offsets. If `indexType` is not provided, BYTE offsets
* are assumed. If BIT is specified, `start=0` and `end=2` means to look at the first three bits. If BYTE is
Expand All @@ -699,23 +679,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* @param key - The key of the string.
* @param bit - The bit value to match. Must be `0` or `1`.
* @param start - The starting offset.
* @param end - The ending offset.
* @param indexType - (Optional) The index offset type. This option can only be specified if you are using Valkey
* version 7.0.0 or above. Could be either {@link BitmapIndexType.BYTE} or {@link BitmapIndexType.BIT}. If no
* index type is provided, the indexes will be assumed to be byte indexes.
* @param options - (Optional) The {@link BitOffsetOptions}.
*
* Command Response - The position of the first occurrence from the `start` to the `end` offsets of the `bit` in the
* binary value of the string held at `key`.
* Command Response - The position of the first occurrence of `bit` in the binary value of the string held at `key`.
* If `start` was provided, the search begins at the offset indicated by `start`.
*/
public bitposInterval(
public bitpos(
key: GlideString,
bit: number,
start: number,
end: number,
indexType?: BitmapIndexType,
options?: BitOffsetOptions,
): T {
return this.addAndReturn(createBitPos(key, bit, start, end, indexType));
return this.addAndReturn(createBitPos(key, bit, options));
}

/**
Expand Down
Loading
Loading