Skip to content

Commit 64fca37

Browse files
gerzsesjpotter
andauthored
Support the NOVALUES option of HSCAN (#2711)
* Support the NOVALUES option of HSCAN Issue #2705 The NOVALUES option instructs HSCAN to only return keys, without their values. This is materialized as a new command, `hScanNoValues`, given that the return type is different from the usual return type of `hScan`. Also a new iterator is provided, `hScanNoValuesIterator`, for the same reason. * skip hscan novalues test if redis < 7.4 * Also don't test hscan no values iterator < 7.4 --------- Co-authored-by: Shaya Potter <[email protected]>
1 parent 5576a0d commit 64fca37

File tree

7 files changed

+160
-2
lines changed

7 files changed

+160
-2
lines changed

packages/client/lib/client/index.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,31 @@ describe('Client', () => {
788788
assert.deepEqual(hash, results);
789789
}, GLOBAL.SERVERS.OPEN);
790790

791+
testUtils.testWithClient('hScanNoValuesIterator', async client => {
792+
const hash: Record<string, string> = {};
793+
const expectedKeys: Array<string> = [];
794+
for (let i = 0; i < 100; i++) {
795+
hash[i.toString()] = i.toString();
796+
expectedKeys.push(i.toString());
797+
}
798+
799+
await client.hSet('key', hash);
800+
801+
const keys: Array<string> = [];
802+
for await (const key of client.hScanNoValuesIterator('key')) {
803+
keys.push(key);
804+
}
805+
806+
function sort(a: string, b: string) {
807+
return Number(a) - Number(b);
808+
}
809+
810+
assert.deepEqual(keys.sort(sort), expectedKeys);
811+
}, {
812+
...GLOBAL.SERVERS.OPEN,
813+
minimumDockerVersion: [7, 4]
814+
});
815+
791816
testUtils.testWithClient('sScanIterator', async client => {
792817
const members = new Set<string>();
793818
for (let i = 0; i < 100; i++) {

packages/client/lib/client/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import COMMANDS from './commands';
2-
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisCommandSignature, ConvertArgumentType, RedisFunction, ExcludeMappedString, RedisCommands } from '../commands';
2+
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisCommandSignature, ConvertArgumentType, RedisFunction, ExcludeMappedString, RedisCommands } from '../commands';
33
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
44
import RedisCommandsQueue, { QueueCommandOptions } from './commands-queue';
55
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
@@ -820,6 +820,17 @@ export default class RedisClient<
820820
} while (cursor !== 0);
821821
}
822822

823+
async* hScanNoValuesIterator(key: string, options?: ScanOptions): AsyncIterable<ConvertArgumentType<RedisCommandArgument, string>> {
824+
let cursor = 0;
825+
do {
826+
const reply = await (this as any).hScanNoValues(key, cursor, options);
827+
cursor = reply.cursor;
828+
for (const k of reply.keys) {
829+
yield k;
830+
}
831+
} while (cursor !== 0);
832+
}
833+
823834
async* sScanIterator(key: string, options?: ScanOptions): AsyncIterable<string> {
824835
let cursor = 0;
825836
do {

packages/client/lib/cluster/commands.ts

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import * as HRANDFIELD_COUNT_WITHVALUES from '../commands/HRANDFIELD_COUNT_WITHV
7272
import * as HRANDFIELD_COUNT from '../commands/HRANDFIELD_COUNT';
7373
import * as HRANDFIELD from '../commands/HRANDFIELD';
7474
import * as HSCAN from '../commands/HSCAN';
75+
import * as HSCAN_NOVALUES from '../commands/HSCAN_NOVALUES';
7576
import * as HSET from '../commands/HSET';
7677
import * as HSETNX from '../commands/HSETNX';
7778
import * as HSTRLEN from '../commands/HSTRLEN';
@@ -368,6 +369,8 @@ export default {
368369
hRandField: HRANDFIELD,
369370
HSCAN,
370371
hScan: HSCAN,
372+
HSCAN_NOVALUES,
373+
hScanNoValues: HSCAN_NOVALUES,
371374
HSET,
372375
hSet: HSET,
373376
HSETNX,

packages/client/lib/commands/HSCAN.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,18 @@ describe('HSCAN', () => {
7373
tuples: []
7474
}
7575
);
76+
77+
await Promise.all([
78+
client.hSet('key', 'a', '1'),
79+
client.hSet('key', 'b', '2')
80+
]);
81+
82+
assert.deepEqual(
83+
await client.hScan('key', 0),
84+
{
85+
cursor: 0,
86+
tuples: [{field: 'a', value: '1'}, {field: 'b', value: '2'}]
87+
}
88+
);
7689
}, GLOBAL.SERVERS.OPEN);
7790
});

packages/client/lib/commands/HSCAN.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function transformArguments(
1616
], cursor, options);
1717
}
1818

19-
type HScanRawReply = [RedisCommandArgument, Array<RedisCommandArgument>];
19+
export type HScanRawReply = [RedisCommandArgument, Array<RedisCommandArgument>];
2020

2121
export interface HScanTuple {
2222
field: RedisCommandArgument;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments, transformReply } from './HSCAN_NOVALUES';
4+
5+
describe('HSCAN_NOVALUES', () => {
6+
testUtils.isVersionGreaterThanHook([7, 4]);
7+
8+
describe('transformArguments', () => {
9+
it('cusror only', () => {
10+
assert.deepEqual(
11+
transformArguments('key', 0),
12+
['HSCAN', 'key', '0', 'NOVALUES']
13+
);
14+
});
15+
16+
it('with MATCH', () => {
17+
assert.deepEqual(
18+
transformArguments('key', 0, {
19+
MATCH: 'pattern'
20+
}),
21+
['HSCAN', 'key', '0', 'MATCH', 'pattern', 'NOVALUES']
22+
);
23+
});
24+
25+
it('with COUNT', () => {
26+
assert.deepEqual(
27+
transformArguments('key', 0, {
28+
COUNT: 1
29+
}),
30+
['HSCAN', 'key', '0', 'COUNT', '1', 'NOVALUES']
31+
);
32+
});
33+
});
34+
35+
describe('transformReply', () => {
36+
it('without keys', () => {
37+
assert.deepEqual(
38+
transformReply(['0', []]),
39+
{
40+
cursor: 0,
41+
keys: []
42+
}
43+
);
44+
});
45+
46+
it('with keys', () => {
47+
assert.deepEqual(
48+
transformReply(['0', ['key1', 'key2']]),
49+
{
50+
cursor: 0,
51+
keys: ['key1', 'key2']
52+
}
53+
);
54+
});
55+
});
56+
57+
testUtils.testWithClient('client.hScanNoValues', async client => {
58+
assert.deepEqual(
59+
await client.hScanNoValues('key', 0),
60+
{
61+
cursor: 0,
62+
keys: []
63+
}
64+
);
65+
66+
await Promise.all([
67+
client.hSet('key', 'a', '1'),
68+
client.hSet('key', 'b', '2')
69+
]);
70+
71+
assert.deepEqual(
72+
await client.hScanNoValues('key', 0),
73+
{
74+
cursor: 0,
75+
keys: ['a', 'b']
76+
}
77+
);
78+
}, GLOBAL.SERVERS.OPEN);
79+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
import { ScanOptions } from './generic-transformers';
3+
import { HScanRawReply, transformArguments as transformHScanArguments } from './HSCAN';
4+
5+
export { FIRST_KEY_INDEX, IS_READ_ONLY } from './HSCAN';
6+
7+
export function transformArguments(
8+
key: RedisCommandArgument,
9+
cursor: number,
10+
options?: ScanOptions
11+
): RedisCommandArguments {
12+
const args = transformHScanArguments(key, cursor, options);
13+
args.push('NOVALUES');
14+
return args;
15+
}
16+
17+
interface HScanNoValuesReply {
18+
cursor: number;
19+
keys: Array<RedisCommandArgument>;
20+
}
21+
22+
export function transformReply([cursor, rawData]: HScanRawReply): HScanNoValuesReply {
23+
return {
24+
cursor: Number(cursor),
25+
keys: [...rawData]
26+
};
27+
}

0 commit comments

Comments
 (0)