Skip to content

Commit 49d3861

Browse files
committed
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.
1 parent dbf8f59 commit 49d3861

File tree

7 files changed

+155
-2
lines changed

7 files changed

+155
-2
lines changed

Diff for: packages/client/lib/client/index.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,28 @@ 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+
}, GLOBAL.SERVERS.OPEN);
812+
791813
testUtils.testWithClient('sScanIterator', async client => {
792814
const members = new Set<string>();
793815
for (let i = 0; i < 100; i++) {

Diff for: 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 {

Diff for: packages/client/lib/cluster/commands.ts

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import * as HRANDFIELD_COUNT_WITHVALUES from '../commands/HRANDFIELD_COUNT_WITHV
6464
import * as HRANDFIELD_COUNT from '../commands/HRANDFIELD_COUNT';
6565
import * as HRANDFIELD from '../commands/HRANDFIELD';
6666
import * as HSCAN from '../commands/HSCAN';
67+
import * as HSCAN_NOVALUES from '../commands/HSCAN_NOVALUES';
6768
import * as HSET from '../commands/HSET';
6869
import * as HSETNX from '../commands/HSETNX';
6970
import * as HSTRLEN from '../commands/HSTRLEN';
@@ -343,6 +344,8 @@ export default {
343344
hRandField: HRANDFIELD,
344345
HSCAN,
345346
hScan: HSCAN,
347+
HSCAN_NOVALUES,
348+
hScanNoValues: HSCAN_NOVALUES,
346349
HSET,
347350
hSet: HSET,
348351
HSETNX,

Diff for: 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
});

Diff for: 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;

Diff for: packages/client/lib/commands/HSCAN_NOVALUES.spec.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
describe('transformArguments', () => {
7+
it('cusror only', () => {
8+
assert.deepEqual(
9+
transformArguments('key', 0),
10+
['HSCAN', 'key', '0', 'NOVALUES']
11+
);
12+
});
13+
14+
it('with MATCH', () => {
15+
assert.deepEqual(
16+
transformArguments('key', 0, {
17+
MATCH: 'pattern'
18+
}),
19+
['HSCAN', 'key', '0', 'MATCH', 'pattern', 'NOVALUES']
20+
);
21+
});
22+
23+
it('with COUNT', () => {
24+
assert.deepEqual(
25+
transformArguments('key', 0, {
26+
COUNT: 1
27+
}),
28+
['HSCAN', 'key', '0', 'COUNT', '1', 'NOVALUES']
29+
);
30+
});
31+
});
32+
33+
describe('transformReply', () => {
34+
it('without keys', () => {
35+
assert.deepEqual(
36+
transformReply(['0', []]),
37+
{
38+
cursor: 0,
39+
keys: []
40+
}
41+
);
42+
});
43+
44+
it('with keys', () => {
45+
assert.deepEqual(
46+
transformReply(['0', ['key1', 'key2']]),
47+
{
48+
cursor: 0,
49+
keys: ['key1', 'key2']
50+
}
51+
);
52+
});
53+
});
54+
55+
testUtils.testWithClient('client.hScanNoValues', async client => {
56+
assert.deepEqual(
57+
await client.hScanNoValues('key', 0),
58+
{
59+
cursor: 0,
60+
keys: []
61+
}
62+
);
63+
64+
await Promise.all([
65+
client.hSet('key', 'a', '1'),
66+
client.hSet('key', 'b', '2')
67+
]);
68+
69+
assert.deepEqual(
70+
await client.hScanNoValues('key', 0),
71+
{
72+
cursor: 0,
73+
keys: ['a', 'b']
74+
}
75+
);
76+
}, GLOBAL.SERVERS.OPEN);
77+
});

Diff for: packages/client/lib/commands/HSCAN_NOVALUES.ts

+27
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)