Skip to content

Commit

Permalink
Node: Add binary support for HSCAN (#2240)
Browse files Browse the repository at this point in the history
* Add binary support for HSCAN

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

* Update CHANGELOG

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

* Run Prettier

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

* Change hscan test resultValues to GlideStrings

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

* Use convert results to strings for hscan tests

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

* Run Prettier

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

* Convert a few more hscan test results to strings

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

* Run Prettier

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>

---------

Signed-off-by: Jonathan Louie <jonathanl@bitquilltech.com>
  • Loading branch information
jonathanl-bq authored Sep 6, 2024
1 parent d6ee9b7 commit 79fdec5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 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 HSCAN command ([#2240](https://github.com/valkey-io/valkey-glide/pull/2240))
* Node: Added binary variant to sorted set commands ([#2190](https://github.com/valkey-io/valkey-glide/pull/2190), [#2210](https://github.com/valkey-io/valkey-glide/pull/2210))
* Node: Added binary variant to HASH commands ([#2194](https://github.com/valkey-io/valkey-glide/pull/2194))
* Node: Added binary variant to server management commands ([#2179](https://github.com/valkey-io/valkey-glide/pull/2179))
Expand Down
11 changes: 7 additions & 4 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2106,11 +2106,14 @@ export class BaseClient {
* ```
*/
public async hscan(
key: string,
key: GlideString,
cursor: string,
options?: HScanOptions,
): Promise<[string, string[]]> {
return this.createWritePromise(createHScan(key, cursor, options));
options?: HScanOptions & DecoderOption,
): Promise<[string, GlideString[]]> {
return this.createWritePromise<[GlideString, GlideString[]]>(
createHScan(key, cursor, options),
options,
).then((res) => [res[0].toString(), res[1]]); // convert cursor back to string
}

/**
Expand Down
2 changes: 1 addition & 1 deletion node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3667,7 +3667,7 @@ export function createHRandField(
* @internal
*/
export function createHScan(
key: string,
key: GlideString,
cursor: string,
options?: HScanOptions,
): command_request.Command {
Expand Down
2 changes: 1 addition & 1 deletion node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* where the value is at even indices and the value is at odd indices.
* If `options.noValues` is set to `true`, the second element will only contain the fields without the values.
*/
public hscan(key: string, cursor: string, options?: HScanOptions): T {
public hscan(key: GlideString, cursor: string, options?: HScanOptions): T {
return this.addAndReturn(createHScan(key, cursor, options));
}

Expand Down
25 changes: 17 additions & 8 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1451,8 +1451,8 @@ export function runBaseTests(config: {
const resultValues: string[] = [];

for (let i = 0; i < resultArray.length; i += 2) {
resultKeys.push(resultArray[i]);
resultValues.push(resultArray[i + 1]);
resultKeys.push(resultArray[i].toString());
resultValues.push(resultArray[i + 1].toString());
}

// Verify if all keys from charMap are in resultKeys
Expand All @@ -1469,13 +1469,16 @@ export function runBaseTests(config: {
// Test hscan with match
result = await client.hscan(key1, initialCursor, {
match: "a",
decoder: Decoder.Bytes,
});

expect(result[resultCursorIndex]).toEqual(initialCursor);
expect(result[resultCollectionIndex]).toEqual(["a", "0"]);
expect(result[resultCollectionIndex]).toEqual(
["a", "0"].map(Buffer.from),
);

// Set up testing data with the numberMap set to be used for the next set test keys and test results.
expect(await client.hset(key1, numberMap)).toEqual(
expect(await client.hset(Buffer.from(key1), numberMap)).toEqual(
Object.keys(numberMap).length,
);

Expand All @@ -1490,8 +1493,10 @@ export function runBaseTests(config: {
const resultEntry = result[resultCollectionIndex];

for (let i = 0; i < resultEntry.length; i += 2) {
secondResultAllKeys.push(resultEntry[i]);
secondResultAllValues.push(resultEntry[i + 1]);
secondResultAllKeys.push(resultEntry[i].toString());
secondResultAllValues.push(
resultEntry[i + 1].toString(),
);
}

if (isFirstLoop) {
Expand All @@ -1515,8 +1520,12 @@ export function runBaseTests(config: {
);

for (let i = 0; i < secondResultEntry.length; i += 2) {
secondResultAllKeys.push(secondResultEntry[i]);
secondResultAllValues.push(secondResultEntry[i + 1]);
secondResultAllKeys.push(
secondResultEntry[i].toString(),
);
secondResultAllValues.push(
secondResultEntry[i + 1].toString(),
);
}
} while (resultCursor != initialCursor); // 0 is returned for the cursor of the last iteration.

Expand Down

0 comments on commit 79fdec5

Please sign in to comment.