You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
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 <spotter@gmail.com>
This commit is contained in:
@@ -788,6 +788,31 @@ describe('Client', () => {
|
|||||||
assert.deepEqual(hash, results);
|
assert.deepEqual(hash, results);
|
||||||
}, GLOBAL.SERVERS.OPEN);
|
}, GLOBAL.SERVERS.OPEN);
|
||||||
|
|
||||||
|
testUtils.testWithClient('hScanNoValuesIterator', async client => {
|
||||||
|
const hash: Record<string, string> = {};
|
||||||
|
const expectedKeys: Array<string> = [];
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
hash[i.toString()] = i.toString();
|
||||||
|
expectedKeys.push(i.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.hSet('key', hash);
|
||||||
|
|
||||||
|
const keys: Array<string> = [];
|
||||||
|
for await (const key of client.hScanNoValuesIterator('key')) {
|
||||||
|
keys.push(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sort(a: string, b: string) {
|
||||||
|
return Number(a) - Number(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.deepEqual(keys.sort(sort), expectedKeys);
|
||||||
|
}, {
|
||||||
|
...GLOBAL.SERVERS.OPEN,
|
||||||
|
minimumDockerVersion: [7, 4]
|
||||||
|
});
|
||||||
|
|
||||||
testUtils.testWithClient('sScanIterator', async client => {
|
testUtils.testWithClient('sScanIterator', async client => {
|
||||||
const members = new Set<string>();
|
const members = new Set<string>();
|
||||||
for (let i = 0; i < 100; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import COMMANDS from './commands';
|
import COMMANDS from './commands';
|
||||||
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisCommandSignature, ConvertArgumentType, RedisFunction, ExcludeMappedString, RedisCommands } from '../commands';
|
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisFunctions, RedisModules, RedisExtensions, RedisScript, RedisScripts, RedisCommandSignature, ConvertArgumentType, RedisFunction, ExcludeMappedString, RedisCommands } from '../commands';
|
||||||
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
|
import RedisSocket, { RedisSocketOptions, RedisTlsSocketOptions } from './socket';
|
||||||
import RedisCommandsQueue, { QueueCommandOptions } from './commands-queue';
|
import RedisCommandsQueue, { QueueCommandOptions } from './commands-queue';
|
||||||
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
|
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
|
||||||
@@ -820,6 +820,17 @@ export default class RedisClient<
|
|||||||
} while (cursor !== 0);
|
} while (cursor !== 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async* hScanNoValuesIterator(key: string, options?: ScanOptions): AsyncIterable<ConvertArgumentType<RedisCommandArgument, string>> {
|
||||||
|
let cursor = 0;
|
||||||
|
do {
|
||||||
|
const reply = await (this as any).hScanNoValues(key, cursor, options);
|
||||||
|
cursor = reply.cursor;
|
||||||
|
for (const k of reply.keys) {
|
||||||
|
yield k;
|
||||||
|
}
|
||||||
|
} while (cursor !== 0);
|
||||||
|
}
|
||||||
|
|
||||||
async* sScanIterator(key: string, options?: ScanOptions): AsyncIterable<string> {
|
async* sScanIterator(key: string, options?: ScanOptions): AsyncIterable<string> {
|
||||||
let cursor = 0;
|
let cursor = 0;
|
||||||
do {
|
do {
|
||||||
|
@@ -72,6 +72,7 @@ import * as HRANDFIELD_COUNT_WITHVALUES from '../commands/HRANDFIELD_COUNT_WITHV
|
|||||||
import * as HRANDFIELD_COUNT from '../commands/HRANDFIELD_COUNT';
|
import * as HRANDFIELD_COUNT from '../commands/HRANDFIELD_COUNT';
|
||||||
import * as HRANDFIELD from '../commands/HRANDFIELD';
|
import * as HRANDFIELD from '../commands/HRANDFIELD';
|
||||||
import * as HSCAN from '../commands/HSCAN';
|
import * as HSCAN from '../commands/HSCAN';
|
||||||
|
import * as HSCAN_NOVALUES from '../commands/HSCAN_NOVALUES';
|
||||||
import * as HSET from '../commands/HSET';
|
import * as HSET from '../commands/HSET';
|
||||||
import * as HSETNX from '../commands/HSETNX';
|
import * as HSETNX from '../commands/HSETNX';
|
||||||
import * as HSTRLEN from '../commands/HSTRLEN';
|
import * as HSTRLEN from '../commands/HSTRLEN';
|
||||||
@@ -368,6 +369,8 @@ export default {
|
|||||||
hRandField: HRANDFIELD,
|
hRandField: HRANDFIELD,
|
||||||
HSCAN,
|
HSCAN,
|
||||||
hScan: HSCAN,
|
hScan: HSCAN,
|
||||||
|
HSCAN_NOVALUES,
|
||||||
|
hScanNoValues: HSCAN_NOVALUES,
|
||||||
HSET,
|
HSET,
|
||||||
hSet: HSET,
|
hSet: HSET,
|
||||||
HSETNX,
|
HSETNX,
|
||||||
|
@@ -73,5 +73,18 @@ describe('HSCAN', () => {
|
|||||||
tuples: []
|
tuples: []
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
client.hSet('key', 'a', '1'),
|
||||||
|
client.hSet('key', 'b', '2')
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
await client.hScan('key', 0),
|
||||||
|
{
|
||||||
|
cursor: 0,
|
||||||
|
tuples: [{field: 'a', value: '1'}, {field: 'b', value: '2'}]
|
||||||
|
}
|
||||||
|
);
|
||||||
}, GLOBAL.SERVERS.OPEN);
|
}, GLOBAL.SERVERS.OPEN);
|
||||||
});
|
});
|
||||||
|
@@ -16,7 +16,7 @@ export function transformArguments(
|
|||||||
], cursor, options);
|
], cursor, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
type HScanRawReply = [RedisCommandArgument, Array<RedisCommandArgument>];
|
export type HScanRawReply = [RedisCommandArgument, Array<RedisCommandArgument>];
|
||||||
|
|
||||||
export interface HScanTuple {
|
export interface HScanTuple {
|
||||||
field: RedisCommandArgument;
|
field: RedisCommandArgument;
|
||||||
|
79
packages/client/lib/commands/HSCAN_NOVALUES.spec.ts
Normal file
79
packages/client/lib/commands/HSCAN_NOVALUES.spec.ts
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import { strict as assert } from 'assert';
|
||||||
|
import testUtils, { GLOBAL } from '../test-utils';
|
||||||
|
import { transformArguments, transformReply } from './HSCAN_NOVALUES';
|
||||||
|
|
||||||
|
describe('HSCAN_NOVALUES', () => {
|
||||||
|
testUtils.isVersionGreaterThanHook([7, 4]);
|
||||||
|
|
||||||
|
describe('transformArguments', () => {
|
||||||
|
it('cusror only', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
transformArguments('key', 0),
|
||||||
|
['HSCAN', 'key', '0', 'NOVALUES']
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with MATCH', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
transformArguments('key', 0, {
|
||||||
|
MATCH: 'pattern'
|
||||||
|
}),
|
||||||
|
['HSCAN', 'key', '0', 'MATCH', 'pattern', 'NOVALUES']
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with COUNT', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
transformArguments('key', 0, {
|
||||||
|
COUNT: 1
|
||||||
|
}),
|
||||||
|
['HSCAN', 'key', '0', 'COUNT', '1', 'NOVALUES']
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('transformReply', () => {
|
||||||
|
it('without keys', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
transformReply(['0', []]),
|
||||||
|
{
|
||||||
|
cursor: 0,
|
||||||
|
keys: []
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with keys', () => {
|
||||||
|
assert.deepEqual(
|
||||||
|
transformReply(['0', ['key1', 'key2']]),
|
||||||
|
{
|
||||||
|
cursor: 0,
|
||||||
|
keys: ['key1', 'key2']
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
testUtils.testWithClient('client.hScanNoValues', async client => {
|
||||||
|
assert.deepEqual(
|
||||||
|
await client.hScanNoValues('key', 0),
|
||||||
|
{
|
||||||
|
cursor: 0,
|
||||||
|
keys: []
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
client.hSet('key', 'a', '1'),
|
||||||
|
client.hSet('key', 'b', '2')
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
await client.hScanNoValues('key', 0),
|
||||||
|
{
|
||||||
|
cursor: 0,
|
||||||
|
keys: ['a', 'b']
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}, GLOBAL.SERVERS.OPEN);
|
||||||
|
});
|
27
packages/client/lib/commands/HSCAN_NOVALUES.ts
Normal file
27
packages/client/lib/commands/HSCAN_NOVALUES.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { RedisCommandArgument, RedisCommandArguments } from '.';
|
||||||
|
import { ScanOptions } from './generic-transformers';
|
||||||
|
import { HScanRawReply, transformArguments as transformHScanArguments } from './HSCAN';
|
||||||
|
|
||||||
|
export { FIRST_KEY_INDEX, IS_READ_ONLY } from './HSCAN';
|
||||||
|
|
||||||
|
export function transformArguments(
|
||||||
|
key: RedisCommandArgument,
|
||||||
|
cursor: number,
|
||||||
|
options?: ScanOptions
|
||||||
|
): RedisCommandArguments {
|
||||||
|
const args = transformHScanArguments(key, cursor, options);
|
||||||
|
args.push('NOVALUES');
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HScanNoValuesReply {
|
||||||
|
cursor: number;
|
||||||
|
keys: Array<RedisCommandArgument>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function transformReply([cursor, rawData]: HScanRawReply): HScanNoValuesReply {
|
||||||
|
return {
|
||||||
|
cursor: Number(cursor),
|
||||||
|
keys: [...rawData]
|
||||||
|
};
|
||||||
|
}
|
Reference in New Issue
Block a user